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

     1  command: {
     2  	type: "trait"
     3  	annotations: {}
     4  	description: "Add command 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 the command to use in the target container, if not set, it will not be changed
    14  		command: *null | [...string]
    15  		// +usage=Specify the args to use in the target container, if set, it will override existing args
    16  		args: *null | [...string]
    17  		// +usage=Specify the args to add in the target container, existing args will be kept, cannot be used with `args`
    18  		addArgs: *null | [...string]
    19  		// +usage=Specify the existing args to delete in the target container, cannot be used with `args`
    20  		delArgs: *null | [...string]
    21  	}
    22  	PatchContainer: {
    23  		_params:         #PatchParams
    24  		name:            _params.containerName
    25  		_baseContainers: context.output.spec.template.spec.containers
    26  		_matchContainers_: [ for _container_ in _baseContainers if _container_.name == name {_container_}]
    27  		_baseContainer: *_|_ | {...}
    28  		if len(_matchContainers_) == 0 {
    29  			err: "container \(name) not found"
    30  		}
    31  		if len(_matchContainers_) > 0 {
    32  			_baseContainer: _matchContainers_[0]
    33  			if _params.command != null {
    34  				// +patchStrategy=replace
    35  				command: _params.command
    36  			}
    37  			if (_params.addArgs != null || _params.delArgs != null) && _params.args != null {
    38  				err: "cannot set addArgs/delArgs and args at the same time"
    39  			}
    40  			_delArgs: {...}
    41  			if _params.delArgs != null {
    42  				_delArgs: {for k in _params.delArgs {(k): ""}}
    43  			}
    44  			if _params.delArgs == null {
    45  				_delArgs: {}
    46  			}
    47  			_args: [...string]
    48  			if _params.args != null {
    49  				_args: _params.args
    50  			}
    51  			if _params.args == null && _baseContainer.args != _|_ {
    52  				_args: _baseContainer.args
    53  			}
    54  			if _params.args == null && _baseContainer.args == _|_ {
    55  				_args: []
    56  			}
    57  			_argsMap: {for a in _args {(a): ""}}
    58  			_addArgs: [...string]
    59  			if _params.addArgs != null {
    60  				_addArgs: _params.addArgs
    61  			}
    62  			if _params.addArgs == null {
    63  				_addArgs: []
    64  			}
    65  
    66  			// +patchStrategy=replace
    67  			args: [ for a in _args if _delArgs[a] == _|_ {a}] + [ for a in _addArgs if _delArgs[a] == _|_ && _argsMap[a] == _|_ {a}]
    68  		}
    69  	}
    70  	// +patchStrategy=open
    71  	patch: spec: template: spec: {
    72  		if parameter.containers == _|_ {
    73  			// +patchKey=name
    74  			containers: [{
    75  				PatchContainer & {_params: {
    76  					if parameter.containerName == "" {
    77  						containerName: context.name
    78  					}
    79  					if parameter.containerName != "" {
    80  						containerName: parameter.containerName
    81  					}
    82  					command: parameter.command
    83  					args:    parameter.args
    84  					addArgs: parameter.addArgs
    85  					delArgs: parameter.delArgs
    86  				}}
    87  			}]
    88  		}
    89  		if parameter.containers != _|_ {
    90  			// +patchKey=name
    91  			containers: [ for c in parameter.containers {
    92  				if c.containerName == "" {
    93  					err: "container name must be set for containers"
    94  				}
    95  				if c.containerName != "" {
    96  					PatchContainer & {_params: c}
    97  				}
    98  			}]
    99  		}
   100  	}
   101  
   102  	parameter: *#PatchParams | close({
   103  		// +usage=Specify the commands for multiple containers
   104  		containers: [...#PatchParams]
   105  	})
   106  
   107  	errs: [ for c in patch.spec.template.spec.containers if c.err != _|_ {c.err}]
   108  }