Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image state update action #49

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions config/definition/task.yaml
Original file line number Diff line number Diff line change
@@ -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
}]
}
28 changes: 28 additions & 0 deletions examples/triggerservice-image-update.yaml
Original file line number Diff line number Diff line change
@@ -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)"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!
But I don't quite get the usage of this image-update-task job. It seems like in your example, this action will only echo a message in a created job, am I understanding it correctly?

Copy link
Contributor Author

@semmet95 semmet95 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just added added a simple echo command as an example 😬
The image-update-task definition is the more important part.
I could update the example if you have any suggestions though.
The intention is to have some sort of action taken when an image is rebased, since the change in the Image resource itself can be triggered by change in another resource like ClusterStack

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! In general, the image-update-task will just use job to handle some tasks. Why not we change the name from image-update-task to task?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes sense. I was so caught up in using it to react to Image resources that I didn't see its actually a generic action. I'll update the name.

Copy link
Contributor Author

@semmet95 semmet95 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FogDong Updated the file and the definition name. Thanks for the suggestion :)

image: busybox
name: image-update-task
ttlSecondsAfterFinished: 600