apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: cloneset
  namespace: vela-system
  annotations:
    definition.oam.dev/description: "Describes long-running, scalable, containerized services that have a stable network endpoint to receive external network traffic from customers.
    If workload type is skipped for any service defined in Appfile, it will be defaulted to `webservice` type."
spec:
  workload:
    definition:
      apiVersion: apps.kruise.io/v1alpha1
      kind: CloneSet
  schematic:
    cue:
      template: |
        output: {
        	apiVersion: "apps.kruise.io/v1alpha1"
        	kind:       "CloneSet"
        	metadata: labels: {
        		"app.oam.dev/component": context.name
        	}
        	spec: {
        		selector: matchLabels: {
        			"app.oam.dev/component": context.name
        		}

        		template: {
        			metadata: labels: {
        				"app.oam.dev/component": context.name
        			}

        			spec: {
        				containers: [{
        					name:  context.name
        					image: parameter.image

        					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["imagePullPolicy"] != _|_ {
        						imagePullPolicy: parameter.imagePullPolicy
        					}

        					ports: [{
        						containerPort: parameter.port
        					}]

        					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["livenessProbe"] != _|_ {
        						livenessProbe: parameter.livenessProbe
        					}

        					if parameter["readinessProbe"] != _|_ {
        						readinessProbe: parameter.readinessProbe
        					}

        					lifecycle: {
        						if parameter.postStart != _|_ {
        							postStart: exec: command: parameter.postStart
        						}
        						if parameter.preStop != _|_ {
        							preStop: exec: command: parameter.preStop
        						}
        					}
        				}]
        				if parameter.hostAliases != _|_ {
        					hostAliases: parameter.hostAliases
        				}
        				if parameter["imagePullSecrets"] != _|_ {
        					imagePullSecrets: [ for v in parameter.imagePullSecrets {name: v}]
        				}
        			}
        		}
        		if parameter.updateStrategyType != _|_ {
        			updateStrategy: {
        				type: parameter.updateStrategyType
        			}
        		}
        	}
        }
        parameter: {
        	// +usage=Which image would you like to use for your service
        	// +short=i
        	image: string

        	// +usage=Specify image pull policy for your service
        	imagePullPolicy?: string

        	// +usage=Specify image pull secrets for your service
        	imagePullSecrets?: [...string]

        	// +usage=Number of CPU units for the service, like `0.5` (0.5 CPU core), `1` (1 CPU core)
        	cpu?: string

        	// +usage=Specify the amount of memory to limit
        	memory?: *"2048Mi" | =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"

        	// +usage=Commands to run in the container
        	cmd?: [...string]

        	// +usage=Arguments to the command.
        	args?: [...string]

        	// +usage=postStart commands will be called immediately after a container is created.
        	postStart?: [...string]

        	// +usage=PreStop is called immediately before a container is terminated due to an API request or management event such as liveness/startup probe failure, preemption, resource contention, etc. The handler is not called if the container crashes or exits. The reason for termination is passed to the handler. The Pod's termination grace period countdown begins before the PreStop hooked is executed. Regardless of the outcome of the handler, the container will eventually terminate within the Pod's termination grace period. Other management of the container blocks until the hook completes or until the termination grace period is reached. More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
        	preStop?: [...string]

        	// +usage=Which port do you want customer traffic sent to
        	// +short=p
        	port: *80 | int

        	// +usage=Define arguments by using environment variables
        	env?: [...#ENV]

        	// +usage=Cloneset updateStrategy, candidates are `ReCreate`/`InPlaceIfPossible`/`InPlaceOnly`
        	updateStrategyType?: string

        	// +usage=HostAliases is a list of hosts and IPs that will be write into the pod's hosts file
        	hostAliases?: [...{
        		hostnames: [...string]
        		ip: string
        	}]

        	// +usage=Instructions for assessing whether the container is alive.
        	livenessProbe?: #HealthProbe

        	// +usage=Instructions for assessing whether the container is in a suitable state to serve traffic.
        	readinessProbe?: #HealthProbe
        }

        #ENV: {
        	// +usage=Environment variable name
        	name: string
        	// +usage=The value of the environment variable
        	value?: string
        	// +usage=Specifies a source the value of this var should come from
        	valueFrom?: {
        		// +usage=Selects a key of a secret in the pod's namespace
        		secretKeyRef: {
        			// +usage=The name of the secret in the pod's namespace to select from
        			name: string
        			// +usage=The key of the secret to select from. Must be a valid secret key
        			key: string
        		}
        	}
        }

        #HealthProbe: {

        	// +usage=Instructions for assessing container health by executing a command. Either this attribute or the httpGet attribute or the tcpSocket attribute MUST be specified. This attribute is mutually exclusive with both the httpGet attribute and the tcpSocket attribute.
        	exec?: {
        		// +usage=A command to be executed inside the container to assess its health. Each space delimited token of the command is a separate array element. Commands exiting 0 are considered to be successful probes, whilst all other exit codes are considered failures.
        		command: [...string]
        	}

        	// +usage=Instructions for assessing container health by executing an HTTP GET request. Either this attribute or the exec attribute or the tcpSocket attribute MUST be specified. This attribute is mutually exclusive with both the exec attribute and the tcpSocket attribute.
        	httpGet?: {
        		// +usage=The endpoint, relative to the port, to which the HTTP GET request should be directed.
        		path: string
        		// +usage=The TCP socket within the container to which the HTTP GET request should be directed.
        		port: int
        		httpHeaders?: [...{
        			name:  string
        			value: string
        		}]
        	}

        	// +usage=Instructions for assessing container health by probing a TCP socket. Either this attribute or the exec attribute or the httpGet attribute MUST be specified. This attribute is mutually exclusive with both the exec attribute and the httpGet attribute.
        	tcpSocket?: {
        		// +usage=The TCP socket within the container that should be probed to assess container health.
        		port: int
        	}

        	// +usage=Number of seconds after the container is started before the first probe is initiated.
        	initialDelaySeconds: *0 | int

        	// +usage=How often, in seconds, to execute the probe.
        	periodSeconds: *10 | int

        	// +usage=Number of seconds after which the probe times out.
        	timeoutSeconds: *1 | int

        	// +usage=Minimum consecutive successes for the probe to be considered successful after having failed.
        	successThreshold: *1 | int

        	// +usage=Number of consecutive failures required to determine the container is not alive (liveness probe) or not ready (readiness probe).
        	failureThreshold: *3 | int
        }