-
Notifications
You must be signed in to change notification settings - Fork 17
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
FogDong
merged 3 commits into
kubevela:main
from
semmet95:feature/image-state-update-action
Jun 21, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"] | ||
image: busybox | ||
name: image-update-task | ||
ttlSecondsAfterFinished: 600 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 fromimage-update-task
totask
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)