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

     1  import (
     2  	"vela/op"
     3  )
     4  
     5  "vela-cli": {
     6  	type: "workflow-step"
     7  	annotations: {
     8  		"category": "Scripts & Commands"
     9  	}
    10  	labels: {}
    11  	description: "Run a vela command"
    12  }
    13  template: {
    14  	mountsArray: [
    15  		if parameter.storage != _|_ && parameter.storage.secret != _|_ for v in parameter.storage.secret {
    16  			{
    17  				name:      "secret-" + v.name
    18  				mountPath: v.mountPath
    19  				if v.subPath != _|_ {
    20  					subPath: v.subPath
    21  				}
    22  			}
    23  		},
    24  		if parameter.storage != _|_ && parameter.storage.hostPath != _|_ for v in parameter.storage.hostPath {
    25  			{
    26  				name:      "hostpath-" + v.name
    27  				mountPath: v.mountPath
    28  			}
    29  		},
    30  	]
    31  
    32  	volumesList: [
    33  		if parameter.storage != _|_ && parameter.storage.secret != _|_ for v in parameter.storage.secret {
    34  			{
    35  				name: "secret-" + v.name
    36  				secret: {
    37  					defaultMode: v.defaultMode
    38  					secretName:  v.secretName
    39  					if v.items != _|_ {
    40  						items: v.items
    41  					}
    42  				}
    43  			}
    44  			if parameter.storage != _|_ && parameter.storage.hostPath != _|_ for v in parameter.storage.hostPath {
    45  				{
    46  					name: "hostpath-" + v.name
    47  					path: v.path
    48  				}
    49  			}
    50  		},
    51  	]
    52  
    53  	deDupVolumesArray: [
    54  		for val in [
    55  			for i, vi in volumesList {
    56  				for j, vj in volumesList if j < i && vi.name == vj.name {
    57  					_ignore: true
    58  				}
    59  				vi
    60  			},
    61  		] if val._ignore == _|_ {
    62  			val
    63  		},
    64  	]
    65  
    66  	job: op.#Apply & {
    67  		value: {
    68  			apiVersion: "batch/v1"
    69  			kind:       "Job"
    70  			metadata: {
    71  				name: "\(context.name)-\(context.stepName)-\(context.stepSessionID)"
    72  				if parameter.serviceAccountName == "kubevela-vela-core" {
    73  					namespace: "vela-system"
    74  				}
    75  				if parameter.serviceAccountName != "kubevela-vela-core" {
    76  					namespace: context.namespace
    77  				}
    78  			}
    79  			spec: {
    80  				backoffLimit: 3
    81  				template: {
    82  					metadata: {
    83  						labels: {
    84  							"workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
    85  						}
    86  					}
    87  					spec: {
    88  						containers: [
    89  							{
    90  								name:         "\(context.name)-\(context.stepName)-\(context.stepSessionID)-job"
    91  								image:        parameter.image
    92  								command:      parameter.command
    93  								volumeMounts: mountsArray
    94  							},
    95  						]
    96  						restartPolicy:  "Never"
    97  						serviceAccount: parameter.serviceAccountName
    98  						volumes:        deDupVolumesArray
    99  					}
   100  				}
   101  			}
   102  		}
   103  	}
   104  
   105  	log: op.#Log & {
   106  		source: {
   107  			resources: [{labelSelector: {
   108  				"workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
   109  			}}]
   110  		}
   111  	}
   112  
   113  	fail: op.#Steps & {
   114  		if job.value.status.failed != _|_ {
   115  			if job.value.status.failed > 2 {
   116  				breakWorkflow: op.#Fail & {
   117  					message: "failed to execute vela command"
   118  				}
   119  			}
   120  		}
   121  	}
   122  
   123  	wait: op.#ConditionalWait & {
   124  		continue: job.value.status.succeeded != _|_ && job.value.status.succeeded > 0
   125  	}
   126  
   127  	parameter: {
   128  		// +usage=Specify the name of the addon.
   129  		addonName: string
   130  		// +usage=Specify the vela command
   131  		command: [...string]
   132  		// +usage=Specify the image
   133  		image: *"oamdev/vela-cli:v1.6.4" | string
   134  		// +usage=specify serviceAccountName want to use
   135  		serviceAccountName: *"kubevela-vela-core" | string
   136  		storage?: {
   137  			// +usage=Mount Secret type storage
   138  			secret?: [...{
   139  				name:        string
   140  				mountPath:   string
   141  				subPath?:    string
   142  				defaultMode: *420 | int
   143  				secretName:  string
   144  				items?: [...{
   145  					key:  string
   146  					path: string
   147  					mode: *511 | int
   148  				}]
   149  			}]
   150  			// +usage=Declare host path type storage
   151  			hostPath?: [...{
   152  				name:      string
   153  				path:      string
   154  				mountPath: string
   155  				type:      *"Directory" | "DirectoryOrCreate" | "FileOrCreate" | "File" | "Socket" | "CharDevice" | "BlockDevice"
   156  			}]
   157  		}
   158  	}
   159  }