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

     1  env: {
     2  	type: "trait"
     3  	annotations: {}
     4  	description: "Add env on K8s pod for your workload which follows the pod spec in path 'spec.template'"
     5  	attributes: {
     6  		appliesToWorkloads: ["deployments.apps", "statefulsets.apps", "daemonsets.apps", "jobs.batch"]
     7  	}
     8  }
     9  template: {
    10  	#PatchParams: {
    11  		// +usage=Specify the name of the target container, if not set, use the component name
    12  		containerName: *"" | string
    13  		// +usage=Specify if replacing the whole environment settings for the container
    14  		replace: *false | bool
    15  		// +usage=Specify the  environment variables to merge, if key already existing, override its value
    16  		env: [string]: string
    17  		// +usage=Specify which existing environment variables to unset
    18  		unset: *[] | [...string]
    19  	}
    20  	PatchContainer: {
    21  		_params: #PatchParams
    22  		name:    _params.containerName
    23  		_delKeys: {for k in _params.unset {(k): ""}}
    24  		_baseContainers: context.output.spec.template.spec.containers
    25  		_matchContainers_: [ for _container_ in _baseContainers if _container_.name == name {_container_}]
    26  		_baseContainer: *_|_ | {...}
    27  		if len(_matchContainers_) == 0 {
    28  			err: "container \(name) not found"
    29  		}
    30  		if len(_matchContainers_) > 0 {
    31  			_baseContainer: _matchContainers_[0]
    32  			_baseEnv:       _baseContainer.env
    33  			if _baseEnv == _|_ {
    34  				// +patchStrategy=replace
    35  				env: [ for k, v in _params.env if _delKeys[k] == _|_ {
    36  					name:  k
    37  					value: v
    38  				}]
    39  			}
    40  			if _baseEnv != _|_ {
    41  				_baseEnvMap: {for envVar in _baseEnv {(envVar.name): envVar}}
    42  				// +patchStrategy=replace
    43  				env: [ for envVar in _baseEnv if _delKeys[envVar.name] == _|_ && !_params.replace {
    44  					name: envVar.name
    45  					if _params.env[envVar.name] != _|_ {
    46  						value: _params.env[envVar.name]
    47  					}
    48  					if _params.env[envVar.name] == _|_ {
    49  						if envVar.value != _|_ {
    50  							value: envVar.value
    51  						}
    52  						if envVar.valueFrom != _|_ {
    53  							valueFrom: envVar.valueFrom
    54  						}
    55  					}
    56  				}] + [ for k, v in _params.env if _delKeys[k] == _|_ && (_params.replace || _baseEnvMap[k] == _|_) {
    57  					name:  k
    58  					value: v
    59  				}]
    60  			}
    61  		}
    62  	}
    63  	patch: spec: template: spec: {
    64  		if parameter.containers == _|_ {
    65  			// +patchKey=name
    66  			containers: [{
    67  				PatchContainer & {_params: {
    68  					if parameter.containerName == "" {
    69  						containerName: context.name
    70  					}
    71  					if parameter.containerName != "" {
    72  						containerName: parameter.containerName
    73  					}
    74  					replace: parameter.replace
    75  					env:     parameter.env
    76  					unset:   parameter.unset
    77  				}}
    78  			}]
    79  		}
    80  		if parameter.containers != _|_ {
    81  			// +patchKey=name
    82  			containers: [ for c in parameter.containers {
    83  				if c.containerName == "" {
    84  					err: "containerName must be set for containers"
    85  				}
    86  				if c.containerName != "" {
    87  					PatchContainer & {_params: c}
    88  				}
    89  			}]
    90  		}
    91  	}
    92  
    93  	parameter: *#PatchParams | close({
    94  		// +usage=Specify the environment variables for multiple containers
    95  		containers: [...#PatchParams]
    96  	})
    97  
    98  	errs: [ for c in patch.spec.template.spec.containers if c.err != _|_ {c.err}]
    99  }