diff --git a/config/definition/task.yaml b/config/definition/task.yaml new file mode 100644 index 0000000..afc48ab --- /dev/null +++ b/config/definition/task.yaml @@ -0,0 +1,103 @@ +apiVersion: core.oam.dev/v1alpha1 +kind: Definition +metadata: + name: task + namespace: vela-system +spec: + type: trigger-action + templates: + # create a Job resource as an action in the same namespace as the source (by default) + main.cue: | + import ( + "vela/kube" + ) + + apply: kube.#Apply & { + $params: { + resource: { + apiVersion: "batch/v1" + kind: "Job" + metadata: { + name: parameter.name + namespace: parameter.namespace + if context.data.metadata.labels != _|_ { + labels: context.data.metadata.labels + } + ownerReferences: [ + { + apiVersion: context.data.apiVersion + kind: context.data.kind + name: context.data.metadata.name + uid: context.data.metadata.uid + controller: true + }, + ] + } + + spec: { + if parameter.ttlSecondsAfterFinished != _|_ { + ttlSecondsAfterFinished: parameter.ttlSecondsAfterFinished + } + + template: { + spec: { + restartPolicy: parameter.restart + containers: [{ + name: parameter.name + image: parameter.image + command: parameter.cmd + + if parameter.env == _|_ { + env: [{ + name: "SOURCE_NAME" + value: context.data.metadata.name + },{ + name: "SOURCE_NAMESPACE" + value: context.data.metadata.namespace + }] + } + + if parameter.env != _|_ { + env: [{ + name: "SOURCE_NAME" + value: context.data.metadata.name + },{ + name: "SOURCE_NAMESPACE" + value: context.data.metadata.namespace + }] + parameter.env + } + }] + } + } + } + } + } + } + + parameter: { + // +usage=The image to run the job container on + image: string + + // +usage=Name of the cron job + name: *context.data.metadata.name | string + + // +usage=The namespace to create the Job in + namespace: *context.data.metadata.namespace | string + + // +usage=Define the job restart policy, the value can only be Never or OnFailure. By default, it's Never. + restart: *"Never" | string + + // +usage=Number of seconds to wait before a successfully completed job is cleaned up + ttlSecondsAfterFinished?: uint + + // +usage=Commands to run in the container + cmd: [...string] + + // +usage=Define evironment variables for the Job container + env?: [...{ + // +usage=Name of the environment variable + name: string + // +usage=Value of the environment variable + value: string + }] + } \ No newline at end of file diff --git a/examples/triggerservice-image-update.yaml b/examples/triggerservice-image-update.yaml new file mode 100644 index 0000000..f9857ee --- /dev/null +++ b/examples/triggerservice-image-update.yaml @@ -0,0 +1,28 @@ +apiVersion: standard.oam.dev/v1alpha1 +kind: TriggerService +metadata: + name: image-rebase-trigger + namespace: default +spec: + triggers: + - source: + # source is all the kpack Image resources in all the namespaces + type: resource-watcher + properties: + apiVersion: kpack.io/v1alpha2 + # kpack needs to be installed on the cluster to have this resource type + kind: Image + events: + - update + + # only trigger action when an Image is successfully rebased + filter: > + context.data.status.latestBuildReason == "STACK" && context.data.status.conditions[0].status == "True" + + action: + type: task + properties: + cmd: [/bin/sh, -c, "echo Image: ${SOURCE_NAME} in namespace: ${SOURCE_NAMESPACE} has been successfully rebased at $(date)"] + image: busybox + name: image-update-task + ttlSecondsAfterFinished: 600