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

     1  resource: {
     2  	type: "trait"
     3  	annotations: {}
     4  	description: "Add resource requests and limits on K8s pod for your workload which follows the pod spec in path 'spec.template.'"
     5  	attributes: {
     6  		podDisruptive: true
     7  		appliesToWorkloads: ["deployments.apps", "statefulsets.apps", "daemonsets.apps", "jobs.batch", "cronjobs.batch"]
     8  	}
     9  }
    10  template: {
    11  
    12  	let resourceContent = {
    13  		resources: {
    14  			if parameter.cpu != _|_ if parameter.memory != _|_ if parameter.requests == _|_ if parameter.limits == _|_ {
    15  				// +patchStrategy=retainKeys
    16  				requests: {
    17  					cpu:    parameter.cpu
    18  					memory: parameter.memory
    19  				}
    20  				// +patchStrategy=retainKeys
    21  				limits: {
    22  					cpu:    parameter.cpu
    23  					memory: parameter.memory
    24  				}
    25  			}
    26  
    27  			if parameter.requests != _|_ {
    28  				// +patchStrategy=retainKeys
    29  				requests: {
    30  					cpu:    parameter.requests.cpu
    31  					memory: parameter.requests.memory
    32  				}
    33  			}
    34  			if parameter.limits != _|_ {
    35  				// +patchStrategy=retainKeys
    36  				limits: {
    37  					cpu:    parameter.limits.cpu
    38  					memory: parameter.limits.memory
    39  				}
    40  			}
    41  		}
    42  	}
    43  
    44  	if context.output.spec != _|_ if context.output.spec.template != _|_ {
    45  		patch: spec: template: spec: {
    46  			// +patchKey=name
    47  			containers: [resourceContent]
    48  		}
    49  	}
    50  	if context.output.spec != _|_ if context.output.spec.jobTemplate != _|_ {
    51  		patch: spec: jobTemplate: spec: template: spec: {
    52  			// +patchKey=name
    53  			containers: [resourceContent]
    54  		}
    55  	}
    56  
    57  	parameter: {
    58  		// +usage=Specify the amount of cpu for requests and limits
    59  		cpu?: *1 | number | string
    60  		// +usage=Specify the amount of memory for requests and limits
    61  		memory?: *"2048Mi" | =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"
    62  		// +usage=Specify the resources in requests
    63  		requests?: {
    64  			// +usage=Specify the amount of cpu for requests
    65  			cpu: *1 | number | string
    66  			// +usage=Specify the amount of memory for requests
    67  			memory: *"2048Mi" | =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"
    68  		}
    69  		// +usage=Specify the resources in limits
    70  		limits?: {
    71  			// +usage=Specify the amount of cpu for limits
    72  			cpu: *1 | number | string
    73  			// +usage=Specify the amount of memory for limits
    74  			memory: *"2048Mi" | =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"
    75  		}
    76  	}
    77  }