github.com/oam-dev/kubevela@v1.9.11/vela-templates/definitions/internal/component/daemon.cue (about)

     1  import (
     2  	"strconv"
     3  )
     4  
     5  daemon: {
     6  	type: "component"
     7  	annotations: {}
     8  	labels: {}
     9  	description: "Describes daemonset services in Kubernetes."
    10  	attributes: {
    11  		workload: {
    12  			definition: {
    13  				apiVersion: "apps/v1"
    14  				kind:       "DaemonSet"
    15  			}
    16  			type: "daemonsets.apps"
    17  		}
    18  		status: {
    19  			customStatus: #"""
    20  				ready: {
    21  					replicas: *0 | int
    22  				} & {
    23  					if context.output.status.numberReady != _|_ {
    24  						replicas: context.output.status.numberReady
    25  					}
    26  				}
    27  				desired: {
    28  					replicas: *0 | int
    29  				} & {
    30  					if context.output.status.desiredNumberScheduled != _|_ {
    31  						replicas: context.output.status.desiredNumberScheduled
    32  					}
    33  				}
    34  				message: "Ready:\(ready.replicas)/\(desired.replicas)"
    35  				"""#
    36  			healthPolicy: #"""
    37  				ready: {
    38  					replicas: *0 | int
    39  				} & {
    40  					if context.output.status.numberReady != _|_ {
    41  						replicas: context.output.status.numberReady
    42  					}
    43  				}
    44  				desired: {
    45  					replicas: *0 | int
    46  				} & {
    47  					if context.output.status.desiredNumberScheduled != _|_ {
    48  						replicas: context.output.status.desiredNumberScheduled
    49  					}
    50  				}
    51  				current: {
    52  					replicas: *0 | int
    53  				} & {
    54  					if context.output.status.currentNumberScheduled != _|_ {
    55  						replicas: context.output.status.currentNumberScheduled
    56  					}
    57  				}
    58  				updated: {
    59  					replicas: *0 | int
    60  				} & {
    61  					if context.output.status.updatedNumberScheduled != _|_ {
    62  						replicas: context.output.status.updatedNumberScheduled
    63  					}
    64  				}
    65  				generation: {
    66  					metadata: context.output.metadata.generation
    67  					observed: *0 | int
    68  				} & {
    69  					if context.output.status.observedGeneration != _|_ {
    70  						observed: context.output.status.observedGeneration
    71  					}
    72  				}
    73  				isHealth: (desired.replicas == ready.replicas) && (desired.replicas == updated.replicas) && (desired.replicas == current.replicas) && (generation.observed == generation.metadata || generation.observed > generation.metadata)
    74  				"""#
    75  		}
    76  	}
    77  }
    78  template: {
    79  	mountsArray: [
    80  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.pvc != _|_ for v in parameter.volumeMounts.pvc {
    81  			{
    82  				mountPath: v.mountPath
    83  				if v.subPath != _|_ {
    84  					subPath: v.subPath
    85  				}
    86  				name: v.name
    87  			}
    88  		},
    89  
    90  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.configMap != _|_ for v in parameter.volumeMounts.configMap {
    91  			{
    92  				mountPath: v.mountPath
    93  				if v.subPath != _|_ {
    94  					subPath: v.subPath
    95  				}
    96  				name: v.name
    97  			}
    98  		},
    99  
   100  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.secret != _|_ for v in parameter.volumeMounts.secret {
   101  			{
   102  				mountPath: v.mountPath
   103  				if v.subPath != _|_ {
   104  					subPath: v.subPath
   105  				}
   106  				name: v.name
   107  			}
   108  		},
   109  
   110  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.emptyDir != _|_ for v in parameter.volumeMounts.emptyDir {
   111  			{
   112  				mountPath: v.mountPath
   113  				if v.subPath != _|_ {
   114  					subPath: v.subPath
   115  				}
   116  				name: v.name
   117  			}
   118  		},
   119  
   120  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.hostPath != _|_ for v in parameter.volumeMounts.hostPath {
   121  			{
   122  				mountPath: v.mountPath
   123  				if v.subPath != _|_ {
   124  					subPath: v.subPath
   125  				}
   126  				name: v.name
   127  			}
   128  		},
   129  	]
   130  
   131  	volumesList: [
   132  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.pvc != _|_ for v in parameter.volumeMounts.pvc {
   133  			{
   134  				name: v.name
   135  				persistentVolumeClaim: claimName: v.claimName
   136  			}
   137  		},
   138  
   139  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.configMap != _|_ for v in parameter.volumeMounts.configMap {
   140  			{
   141  				name: v.name
   142  				configMap: {
   143  					defaultMode: v.defaultMode
   144  					name:        v.cmName
   145  					if v.items != _|_ {
   146  						items: v.items
   147  					}
   148  				}
   149  			}
   150  		},
   151  
   152  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.secret != _|_ for v in parameter.volumeMounts.secret {
   153  			{
   154  				name: v.name
   155  				secret: {
   156  					defaultMode: v.defaultMode
   157  					secretName:  v.secretName
   158  					if v.items != _|_ {
   159  						items: v.items
   160  					}
   161  				}
   162  			}
   163  		},
   164  
   165  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.emptyDir != _|_ for v in parameter.volumeMounts.emptyDir {
   166  			{
   167  				name: v.name
   168  				emptyDir: medium: v.medium
   169  			}
   170  		},
   171  
   172  		if parameter.volumeMounts != _|_ && parameter.volumeMounts.hostPath != _|_ for v in parameter.volumeMounts.hostPath {
   173  			{
   174  				name: v.name
   175  				hostPath: {
   176  					path: v.path
   177  				}
   178  			}
   179  		},
   180  	]
   181  
   182  	deDupVolumesArray: [
   183  		for val in [
   184  			for i, vi in volumesList {
   185  				for j, vj in volumesList if j < i && vi.name == vj.name {
   186  					_ignore: true
   187  				}
   188  				vi
   189  			},
   190  		] if val._ignore == _|_ {
   191  			val
   192  		},
   193  	]
   194  
   195  	output: {
   196  		apiVersion: "apps/v1"
   197  		kind:       "DaemonSet"
   198  		spec: {
   199  			selector: matchLabels: {
   200  				"app.oam.dev/component": context.name
   201  			}
   202  
   203  			template: {
   204  				metadata: {
   205  					labels: {
   206  						if parameter.labels != _|_ {
   207  							parameter.labels
   208  						}
   209  						if parameter.addRevisionLabel {
   210  							"app.oam.dev/revision": context.revision
   211  						}
   212  						"app.oam.dev/name":      context.appName
   213  						"app.oam.dev/component": context.name
   214  					}
   215  					if parameter.annotations != _|_ {
   216  						annotations: parameter.annotations
   217  					}
   218  				}
   219  
   220  				spec: {
   221  					containers: [{
   222  						name:  context.name
   223  						image: parameter.image
   224  						if parameter["port"] != _|_ && parameter["ports"] == _|_ {
   225  							ports: [{
   226  								containerPort: parameter.port
   227  							}]
   228  						}
   229  						if parameter["ports"] != _|_ {
   230  							ports: [ for v in parameter.ports {
   231  								{
   232  									containerPort: v.port
   233  									protocol:      v.protocol
   234  									if v.name != _|_ {
   235  										name: v.name
   236  									}
   237  									if v.name == _|_ {
   238  										name: "port-" + strconv.FormatInt(v.port, 10)
   239  									}
   240  								}}]
   241  						}
   242  
   243  						if parameter["imagePullPolicy"] != _|_ {
   244  							imagePullPolicy: parameter.imagePullPolicy
   245  						}
   246  
   247  						if parameter["cmd"] != _|_ {
   248  							command: parameter.cmd
   249  						}
   250  
   251  						if parameter["env"] != _|_ {
   252  							env: parameter.env
   253  						}
   254  
   255  						if context["config"] != _|_ {
   256  							env: context.config
   257  						}
   258  
   259  						if parameter["cpu"] != _|_ {
   260  							resources: {
   261  								limits: cpu:   parameter.cpu
   262  								requests: cpu: parameter.cpu
   263  							}
   264  						}
   265  
   266  						if parameter["memory"] != _|_ {
   267  							resources: {
   268  								limits: memory:   parameter.memory
   269  								requests: memory: parameter.memory
   270  							}
   271  						}
   272  
   273  						if parameter["volumes"] != _|_ && parameter["volumeMounts"] == _|_ {
   274  							volumeMounts: [ for v in parameter.volumes {
   275  								{
   276  									mountPath: v.mountPath
   277  									name:      v.name
   278  								}}]
   279  						}
   280  
   281  						if parameter["volumeMounts"] != _|_ {
   282  							volumeMounts: mountsArray
   283  						}
   284  
   285  						if parameter["livenessProbe"] != _|_ {
   286  							livenessProbe: parameter.livenessProbe
   287  						}
   288  
   289  						if parameter["readinessProbe"] != _|_ {
   290  							readinessProbe: parameter.readinessProbe
   291  						}
   292  
   293  					}]
   294  
   295  					if parameter["hostAliases"] != _|_ {
   296  						// +patchKey=ip
   297  						hostAliases: parameter.hostAliases
   298  					}
   299  
   300  					if parameter["imagePullSecrets"] != _|_ {
   301  						imagePullSecrets: [ for v in parameter.imagePullSecrets {
   302  							name: v
   303  						},
   304  						]
   305  					}
   306  
   307  					if parameter["volumes"] != _|_ && parameter["volumeMounts"] == _|_ {
   308  						volumes: [ for v in parameter.volumes {
   309  							{
   310  								name: v.name
   311  								if v.type == "pvc" {
   312  									persistentVolumeClaim: claimName: v.claimName
   313  								}
   314  								if v.type == "configMap" {
   315  									configMap: {
   316  										defaultMode: v.defaultMode
   317  										name:        v.cmName
   318  										if v.items != _|_ {
   319  											items: v.items
   320  										}
   321  									}
   322  								}
   323  								if v.type == "secret" {
   324  									secret: {
   325  										defaultMode: v.defaultMode
   326  										secretName:  v.secretName
   327  										if v.items != _|_ {
   328  											items: v.items
   329  										}
   330  									}
   331  								}
   332  								if v.type == "emptyDir" {
   333  									emptyDir: medium: v.medium
   334  								}
   335  							}
   336  						}]
   337  					}
   338  
   339  					if parameter["volumeMounts"] != _|_ {
   340  						volumes: deDupVolumesArray
   341  					}
   342  				}
   343  			}
   344  		}
   345  	}
   346  
   347  	exposePorts: [
   348  		if parameter.ports != _|_ for v in parameter.ports if v.expose == true {
   349  			port:       v.port
   350  			targetPort: v.port
   351  			if v.name != _|_ {
   352  				name: v.name
   353  			}
   354  			if v.name == _|_ {
   355  				name: "port-" + strconv.FormatInt(v.port, 10)
   356  			}
   357  		},
   358  	]
   359  
   360  	outputs: {
   361  		if len(exposePorts) != 0 {
   362  			webserviceExpose: {
   363  				apiVersion: "v1"
   364  				kind:       "Service"
   365  				metadata: name: context.name
   366  				spec: {
   367  					selector: "app.oam.dev/component": context.name
   368  					ports: exposePorts
   369  					type:  parameter.exposeType
   370  				}
   371  			}
   372  		}
   373  	}
   374  
   375  	parameter: {
   376  		// +usage=Specify the labels in the workload
   377  		labels?: [string]: string
   378  
   379  		// +usage=Specify the annotations in the workload
   380  		annotations?: [string]: string
   381  
   382  		// +usage=Which image would you like to use for your service
   383  		// +short=i
   384  		image: string
   385  
   386  		// +usage=Specify image pull policy for your service
   387  		imagePullPolicy?: "Always" | "Never" | "IfNotPresent"
   388  
   389  		// +usage=Specify image pull secrets for your service
   390  		imagePullSecrets?: [...string]
   391  
   392  		// +ignore
   393  		// +usage=Deprecated field, please use ports instead
   394  		// +short=p
   395  		port?: int
   396  
   397  		// +usage=Which ports do you want customer traffic sent to, defaults to 80
   398  		ports?: [...{
   399  			// +usage=Number of port to expose on the pod's IP address
   400  			port: int
   401  			// +usage=Name of the port
   402  			name?: string
   403  			// +usage=Protocol for port. Must be UDP, TCP, or SCTP
   404  			protocol: *"TCP" | "UDP" | "SCTP"
   405  			// +usage=Specify if the port should be exposed
   406  			expose: *false | bool
   407  		}]
   408  
   409  		// +ignore
   410  		// +usage=Specify what kind of Service you want. options: "ClusterIP", "NodePort", "LoadBalancer", "ExternalName"
   411  		exposeType: *"ClusterIP" | "NodePort" | "LoadBalancer" | "ExternalName"
   412  
   413  		// +ignore
   414  		// +usage=If addRevisionLabel is true, the revision label will be added to the underlying pods
   415  		addRevisionLabel: *false | bool
   416  
   417  		// +usage=Commands to run in the container
   418  		cmd?: [...string]
   419  
   420  		// +usage=Define arguments by using environment variables
   421  		env?: [...{
   422  			// +usage=Environment variable name
   423  			name: string
   424  			// +usage=The value of the environment variable
   425  			value?: string
   426  			// +usage=Specifies a source the value of this var should come from
   427  			valueFrom?: {
   428  				// +usage=Selects a key of a secret in the pod's namespace
   429  				secretKeyRef?: {
   430  					// +usage=The name of the secret in the pod's namespace to select from
   431  					name: string
   432  					// +usage=The key of the secret to select from. Must be a valid secret key
   433  					key: string
   434  				}
   435  				// +usage=Selects a key of a config map in the pod's namespace
   436  				configMapKeyRef?: {
   437  					// +usage=The name of the config map in the pod's namespace to select from
   438  					name: string
   439  					// +usage=The key of the config map to select from. Must be a valid secret key
   440  					key: string
   441  				}
   442  			}
   443  		}]
   444  
   445  		// +usage=Number of CPU units for the service, like `0.5` (0.5 CPU core), `1` (1 CPU core)
   446  		cpu?: string
   447  
   448  		// +usage=Specifies the attributes of the memory resource required for the container.
   449  		memory?: string
   450  
   451  		volumeMounts?: {
   452  			// +usage=Mount PVC type volume
   453  			pvc?: [...{
   454  				name:      string
   455  				mountPath: string
   456  				// +usage=The name of the PVC
   457  				claimName: string
   458  			}]
   459  			// +usage=Mount ConfigMap type volume
   460  			configMap?: [...{
   461  				name:        string
   462  				mountPath:   string
   463  				defaultMode: *420 | int
   464  				cmName:      string
   465  				items?: [...{
   466  					key:  string
   467  					path: string
   468  					mode: *511 | int
   469  				}]
   470  			}]
   471  			// +usage=Mount Secret type volume
   472  			secret?: [...{
   473  				name:        string
   474  				mountPath:   string
   475  				defaultMode: *420 | int
   476  				secretName:  string
   477  				items?: [...{
   478  					key:  string
   479  					path: string
   480  					mode: *511 | int
   481  				}]
   482  			}]
   483  			// +usage=Mount EmptyDir type volume
   484  			emptyDir?: [...{
   485  				name:      string
   486  				mountPath: string
   487  				medium:    *"" | "Memory"
   488  			}]
   489  			// +usage=Mount HostPath type volume
   490  			hostPath?: [...{
   491  				name:              string
   492  				mountPath:         string
   493  				mountPropagation?: "None" | "HostToContainer" | "Bidirectional"
   494  				path:              string
   495  				readOnly?:         bool
   496  			}]
   497  		}
   498  
   499  		// +usage=Deprecated field, use volumeMounts instead.
   500  		volumes?: [...{
   501  			name:      string
   502  			mountPath: string
   503  			// +usage=Specify volume type, options: "pvc","configMap","secret","emptyDir", default to emptyDir
   504  			type: *"emptyDir" | "pvc" | "configMap" | "secret"
   505  			if type == "pvc" {
   506  				claimName: string
   507  			}
   508  			if type == "configMap" {
   509  				defaultMode: *420 | int
   510  				cmName:      string
   511  				items?: [...{
   512  					key:  string
   513  					path: string
   514  					mode: *511 | int
   515  				}]
   516  			}
   517  			if type == "secret" {
   518  				defaultMode: *420 | int
   519  				secretName:  string
   520  				items?: [...{
   521  					key:  string
   522  					path: string
   523  					mode: *511 | int
   524  				}]
   525  			}
   526  			if type == "emptyDir" {
   527  				medium: *"" | "Memory"
   528  			}
   529  		}]
   530  
   531  		// +usage=Instructions for assessing whether the container is alive.
   532  		livenessProbe?: #HealthProbe
   533  
   534  		// +usage=Instructions for assessing whether the container is in a suitable state to serve traffic.
   535  		readinessProbe?: #HealthProbe
   536  
   537  		// +usage=Specify the hostAliases to add
   538  		hostAliases?: [...{
   539  			ip: string
   540  			hostnames: [...string]
   541  		}]
   542  	}
   543  
   544  	#HealthProbe: {
   545  
   546  		// +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.
   547  		exec?: {
   548  			// +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.
   549  			command: [...string]
   550  		}
   551  
   552  		// +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.
   553  		httpGet?: {
   554  			// +usage=The endpoint, relative to the port, to which the HTTP GET request should be directed.
   555  			path: string
   556  			// +usage=The TCP socket within the container to which the HTTP GET request should be directed.
   557  			port:    int
   558  			host?:   string
   559  			scheme?: *"HTTP" | string
   560  			httpHeaders?: [...{
   561  				name:  string
   562  				value: string
   563  			}]
   564  		}
   565  
   566  		// +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.
   567  		tcpSocket?: {
   568  			// +usage=The TCP socket within the container that should be probed to assess container health.
   569  			port: int
   570  		}
   571  
   572  		// +usage=Number of seconds after the container is started before the first probe is initiated.
   573  		initialDelaySeconds: *0 | int
   574  
   575  		// +usage=How often, in seconds, to execute the probe.
   576  		periodSeconds: *10 | int
   577  
   578  		// +usage=Number of seconds after which the probe times out.
   579  		timeoutSeconds: *1 | int
   580  
   581  		// +usage=Minimum consecutive successes for the probe to be considered successful after having failed.
   582  		successThreshold: *1 | int
   583  
   584  		// +usage=Number of consecutive failures required to determine the container is not alive (liveness probe) or not ready (readiness probe).
   585  		failureThreshold: *3 | int
   586  	}
   587  }