import (
"strconv"
"strings"
)
statefulset: {
type: "component"
annotations: {}
labels: {
"componentdefinition.spectrocloud.com/type": "application"
"definition.spectrocloud.com/category": "Application-Workload"
}
description: "Describes long-running, scalable, containerized services used to manage stateful application, like a database."
attributes: {
status: {
customStatus: {
ready: {
readyReplicas: *0 | int
} & {
if context.output.status.readyReplicas != _|_ {
readyReplicas: context.output.status.readyReplicas
}
}
message: "Ready:\(ready.readyReplicas)/\(context.output.spec.replicas)"
}
healthPolicy: {
ready: {
updatedReplicas: *0 | int
readyReplicas: *0 | int
replicas: *0 | int
observedGeneration: *0 | int
} & {
if context.output.status.updatedReplicas != _|_ {
updatedReplicas: context.output.status.updatedReplicas
}
if context.output.status.readyReplicas != _|_ {
readyReplicas: context.output.status.readyReplicas
}
if context.output.status.replicas != _|_ {
replicas: context.output.status.replicas
}
if context.output.status.observedGeneration != _|_ {
observedGeneration: context.output.status.observedGeneration
}
}
_isHealth: (context.output.spec.replicas == ready.readyReplicas) && (context.output.spec.replicas == ready.updatedReplicas) && (context.output.spec.replicas == ready.replicas) && (ready.observedGeneration == context.output.metadata.generation || ready.observedGeneration > context.output.metadata.generation)
isHealth: *_isHealth | bool
if context.output.metadata.annotations != _|_ {
if context.output.metadata.annotations["app.spectrocloud.com/disable-health-check"] != _|_ {
isHealth: true
}
}
}
}
}
}
template: {
mountsArray: [
if parameter.volumeMounts != _|_ if parameter.volumeMounts.pvc != _|_ for v in parameter.volumeMounts.pvc {
{
mountPath: v.mountPath
if v.subPath != _|_ {
subPath: v.subPath
}
name: v.name
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.configMap != _|_ for v in parameter.volumeMounts.configMap {
{
mountPath: v.mountPath
if v.subPath != _|_ {
subPath: v.subPath
}
name: v.name
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.secret != _|_ for v in parameter.volumeMounts.secret {
{
mountPath: v.mountPath
if v.subPath != _|_ {
subPath: v.subPath
}
name: v.name
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.emptyDir != _|_ for v in parameter.volumeMounts.emptyDir {
{
mountPath: v.mountPath
if v.subPath != _|_ {
subPath: v.subPath
}
name: v.name
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.hostPath != _|_ for v in parameter.volumeMounts.hostPath {
{
mountPath: v.mountPath
if v.subPath != _|_ {
subPath: v.subPath
}
name: v.name
}
},
]
volumesList: [
if parameter.volumeMounts != _|_ if parameter.volumeMounts.pvc != _|_ for v in parameter.volumeMounts.pvc {
{
name: v.name
persistentVolumeClaim: claimName: v.claimName
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.configMap != _|_ for v in parameter.volumeMounts.configMap {
{
name: v.name
configMap: {
defaultMode: v.defaultMode
name: v.cmName
if v.items != _|_ {
items: v.items
}
}
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.secret != _|_ for v in parameter.volumeMounts.secret {
{
name: v.name
secret: {
defaultMode: v.defaultMode
secretName: v.secretName
if v.items != _|_ {
items: v.items
}
}
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.emptyDir != _|_ for v in parameter.volumeMounts.emptyDir {
{
name: v.name
emptyDir: medium: v.medium
}
},
if parameter.volumeMounts != _|_ if parameter.volumeMounts.hostPath != _|_ for v in parameter.volumeMounts.hostPath {
{
name: v.name
hostPath: {
path: v.path
}
}
},
]
deDupVolumesArray: [
for val in [
for i, vi in volumesList {
for j, vj in volumesList if j < i && vi.name == vj.name {
_ignore: true
}
vi
},
] if val._ignore == _|_ {
val
},
]
output: {
apiVersion: "apps/v1"
kind: "StatefulSet"
spec: {
selector: matchLabels: {
"app.spectrocloud.com/component": context.name
}
template: {
metadata: {
labels: {
if parameter.labels != _|_ {
parameter.labels
}
if parameter.addRevisionLabel {
"app.spectrocloud.com/revision": context.revision
}
"app.spectrocloud.com/name": context.workloadName
"app.spectrocloud.com/component": context.name
}
if parameter.annotations != _|_ {
annotations: parameter.annotations
}
}
spec: {
containers: [{
name: context.name
image: parameter.image
if parameter["port"] != _|_ if parameter["ports"] == _|_ {
ports: [{
containerPort: parameter.port
}]
}
if parameter["ports"] != _|_ {
ports: [for v in parameter.ports {
{
containerPort: {
if v.containerPort != _|_ {v.containerPort}
if v.containerPort == _|_ {v.port}
}
protocol: v.protocol
if v.name != _|_ {
name: v.name
}
if v.name == _|_ {
_name: {
if v.containerPort != _|_ {"port-" + strconv.FormatInt(v.containerPort, 10)}
if v.containerPort == _|_ {"port-" + strconv.FormatInt(v.port, 10)}
}
name: *_name | string
if v.protocol != "TCP" {
name: _name + "-" + strings.ToLower(v.protocol)
}
}
}}]
}
if parameter["imagePullPolicy"] != _|_ {
imagePullPolicy: parameter.imagePullPolicy
}
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
if parameter["args"] != _|_ {
args: parameter.args
}
if parameter["env"] != _|_ {
env: parameter.env
}
if context["config"] != _|_ {
env: context.config
}
if parameter["cpu"] != _|_ {
resources: {
limits: cpu: parameter.cpu
requests: cpu: parameter.cpu
}
}
if parameter["memory"] != _|_ {
resources: {
limits: memory: parameter.memory
requests: memory: parameter.memory
}
}
if parameter["volumes"] != _|_ if parameter["volumeMounts"] == _|_ {
volumeMounts: [for v in parameter.volumes {
{
mountPath: v.mountPath
name: v.name
}}]
}
if parameter["volumeMounts"] != _|_ {
volumeMounts: mountsArray
}
if parameter["livenessProbe"] != _|_ {
livenessProbe: parameter.livenessProbe
}
if parameter["readinessProbe"] != _|_ {
readinessProbe: parameter.readinessProbe
}
}]
if parameter["hostAliases"] != _|_ {
hostAliases: parameter.hostAliases
}
if parameter["imagePullSecrets"] != _|_ {
imagePullSecrets: [for v in parameter.imagePullSecrets {
name: v
},
]
}
if parameter["volumes"] != _|_ if parameter["volumeMounts"] == _|_ {
volumes: [for v in parameter.volumes {
{
name: v.name
if v.type == "pvc" {
persistentVolumeClaim: claimName: v.claimName
}
if v.type == "configMap" {
configMap: {
defaultMode: v.defaultMode
name: v.cmName
if v.items != _|_ {
items: v.items
}
}
}
if v.type == "secret" {
secret: {
defaultMode: v.defaultMode
secretName: v.secretName
if v.items != _|_ {
items: v.items
}
}
}
if v.type == "emptyDir" {
emptyDir: medium: v.medium
}
}
}]
}
if parameter["volumeMounts"] != _|_ {
volumes: deDupVolumesArray
}
}
}
}
}
exposePorts: [
if parameter.ports != _|_ for v in parameter.ports if v.expose == true {
port: v.port
if v.containerPort != _|_ {targetPort: v.containerPort}
if v.containerPort == _|_ {targetPort: v.port}
if v.name != _|_ {name: v.name}
if v.name == _|_ {
_name: {
if v.containerPort != _|_ {
"port-" + strconv.FormatInt(v.containerPort, 10)
}
if v.containerPort == _|_ {
"port-" + strconv.FormatInt(v.port, 10)
}
}
name: *_name | string
if v.protocol != "TCP" {
name: _name + "-" + strings.ToLower(v.protocol)
}
}
if v.nodePort != _|_ if parameter.exposeType == "NodePort" {
nodePort: v.nodePort
}
if v.protocol != _|_ {
protocol: v.protocol
}
},
]
outputs: {
if len(exposePorts) != 0 {
statefulsetsExpose: {
apiVersion: "v1"
kind: "Service"
metadata: name: context.name
spec: {
selector: "app.spectrocloud.com/component": context.name
ports: exposePorts
type: parameter.exposeType
}
}
}
}
parameter: {
labels?: [string]: string
annotations?: [string]: string
image: string
imagePullPolicy?: "Always" | "Never" | "IfNotPresent"
imagePullSecrets?: [...string]
port?: int
ports?: [...{
port: int
containerPort?: int
name?: string
protocol: *"TCP" | "UDP" | "SCTP"
expose: *false | bool
nodePort?: int
}]
exposeType: *"ClusterIP" | "NodePort" | "LoadBalancer"
addRevisionLabel: *false | bool
cmd?: [...string]
args?: [...string]
env?: [...{
name: string
value?: string
valueFrom?: {
secretKeyRef?: {
name: string
key: string
}
configMapKeyRef?: {
name: string
key: string
}
}
}]
cpu?: string
memory?: string
volumeMounts?: {
pvc?: [...{
name: string
mountPath: string
subPath?: string
claimName: string
}]
configMap?: [...{
name: string
mountPath: string
subPath?: string
defaultMode: *420 | int
cmName: string
items?: [...{
key: string
path: string
mode: *511 | int
}]
}]
secret?: [...{
name: string
mountPath: string
subPath?: string
defaultMode: *420 | int
secretName: string
items?: [...{
key: string
path: string
mode: *511 | int
}]
}]
emptyDir?: [...{
name: string
mountPath: string
subPath?: string
medium: *"" | "Memory"
}]
hostPath?: [...{
name: string
mountPath: string
subPath?: string
path: string
}]
}
volumes?: [...{
name: string
mountPath: string
type: *"emptyDir" | "pvc" | "configMap" | "secret"
if type == "pvc" {
claimName: string
}
if type == "configMap" {
defaultMode: *420 | int
cmName: string
items?: [...{
key: string
path: string
mode: *511 | int
}]
}
if type == "secret" {
defaultMode: *420 | int
secretName: string
items?: [...{
key: string
path: string
mode: *511 | int
}]
}
if type == "emptyDir" {
medium: *"" | "Memory"
}
}]
livenessProbe?: #ExecHealthProbe | #HTTPHealthProbe | #TCPHealthProbe
readinessProbe?: #ExecHealthProbe | #HTTPHealthProbe | #TCPHealthProbe
hostAliases?: [...{
ip: string
hostnames: [...string]
}]
}
#ExecHealthProbe: {
exec: {
command!: [...string]
}
initialDelaySeconds: *0 | int
periodSeconds: *10 | int
timeoutSeconds: *1 | int
successThreshold: *1 | int
failureThreshold: *3 | int
}
#HTTPHealthProbe: {
httpGet: {
path: string
port: int
host?: string
scheme?: *"HTTP" | string
httpHeaders?: [...{
name: string
value: string
}]
}
initialDelaySeconds: *0 | int
periodSeconds: *10 | int
timeoutSeconds: *1 | int
successThreshold: *1 | int
failureThreshold: *3 | int
}
#TCPHealthProbe: {
tcpSocket: {
port: int
}
initialDelaySeconds: *0 | int
periodSeconds: *10 | int
timeoutSeconds: *1 | int
successThreshold: *1 | int
failureThreshold: *3 | int
}
}