Skip to content

Commit

Permalink
Fix: fix built-in actions (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
  • Loading branch information
FogDong committed Feb 14, 2023
1 parent 50e12b1 commit f3a7737
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 55 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ triggers:
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
nameSelector:
fromLabel: "watch-this"
```

See [examples](https://github.com/kubevela/kube-trigger/tree/main/examples) directory for more instructions.
Expand Down Expand Up @@ -218,8 +218,8 @@ triggers:
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
nameSelector:
fromLabel: "watch-this"
```

Let's assume your config file is `config.yaml`, to run kube-trigger:
Expand Down Expand Up @@ -256,8 +256,9 @@ spec:
type: bump-application-revision
properties:
namespace: default
matchingLabels:
my-label: my-value
# Select Applications to bump using labels.
nameSelector:
fromLabel: "watch-this"
```

## Advanced kube-trigger Configuration
Expand Down
4 changes: 2 additions & 2 deletions examples/conf-bump-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ triggers:
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
nameSelector:
fromLabel: "watch-this"
2 changes: 1 addition & 1 deletion examples/conf-record-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
action:
type: record-event
properties:
name:
nameSelector:
fromLabel: "workflowrun.oam.dev/name"
11 changes: 8 additions & 3 deletions examples/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ kind: ConfigMap
metadata:
name: this-will-trigger-update-1
namespace: "default"
labels:
"watch-this": "this-will-be-updated-1"
"workflowrun.oam.dev/name": "this-will-be-updated-1"
data:
content: EDIT_ME_AFTER_APPLY

Expand All @@ -13,6 +16,8 @@ metadata:
# Filters will be used to filter this name.
name: this-will-trigger-update-2
namespace: "default"
labels:
"watch-this": "this-will-be-updated-2"
data:
content: EDIT_ME_AFTER_APPLY

Expand All @@ -27,7 +32,7 @@ metadata:
name: this-will-be-updated-1
# Labels will be used to select which Application to bump revision.
labels:
my-label: my-value
"watch-this": "this-will-be-updated-1"
namespace: default
spec:
components: [ ]
Expand All @@ -41,7 +46,7 @@ metadata:
app.oam.dev/publishVersion: "1"
name: this-will-be-updated-2
labels:
my-label: my-value
"watch-this": "this-will-be-updated-2"
namespace: default
spec:
components: [ ]
Expand All @@ -50,5 +55,5 @@ spec:
apiVersion: standard.oam.dev/v1alpha1
kind: EventListener
metadata:
name: this-will-trigger-update-1
name: this-will-be-updated-1
namespace: default
4 changes: 2 additions & 2 deletions examples/triggerservice-bump-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ spec:
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
nameSelector:
fromLabel: "watch-this"
1 change: 0 additions & 1 deletion examples/triggerservice-create-event-listener.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ spec:
- create
matchingLabels:
trigger.oam.dev/watch: "true"
filter: ""
action:
type: create-event-listener
2 changes: 1 addition & 1 deletion examples/triggerservice-record-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ spec:
action:
type: record-event
properties:
name:
nameSelector:
fromLabel: "workflowrun.oam.dev/name"
112 changes: 77 additions & 35 deletions pkg/templates/static/action/bump-application-revision.cue
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,95 @@ import (
"strconv"
)

list: kube.#List & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
items: *[] | [{...}]

if parameter.nameSelector != _|_ if parameter.nameSelector.matchingLabels != _|_ {
list: kube.#List & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
}
filter: {
namespace: parameter.namespace
if parameter.nameSelector.matchingLabels != _|_ {
matchingLabels: parameter.nameSelector.matchingLabels
}
}
}
filter: {
namespace: parameter.namespace
if parameter.matchingLabels != _|_ {
matchingLabels: parameter.matchingLabels
}
items: list.$returns.items
}

if parameter.nameSelector != _|_ if parameter.nameSelector.fromLabel != _|_ {
get: kube.#Get & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
metadata: {
name: context.data.metadata.labels[parameter.nameSelector.fromLabel]
namespace: parameter.namespace
}
}
}
}
items: [{get.$returns}]
}

for index, item in list.$returns.items {
if item.metadata.annotations["app.oam.dev/publishVersion"] != _|_ {
if strconv.ParseInt(item.metadata.annotations["app.oam.dev/publishVersion"], 10, 64) != _|_ {
"patch-\(index)": kube.#Patch & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
metadata: {
name: item.metadata.name
namespace: item.metadata.namespace
}
}
patch: {
type: "merge"
data: {
metadata: {
annotations: {
"app.oam.dev/publishVersion": strconv.FormatInt(strconv.ParseInt(item.metadata.annotations["app.oam.dev/publishVersion"], 10, 64)+1, 10)
}
}
}
}
if parameter.nameSelector == _|_ {
get: kube.#Get & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
metadata: {
name: context.data.metadata.name
namespace: parameter.namespace
}
}
}
}
items: [{get.$returns}]
}

for index, item in items {
if item.metadata.annotations["app.oam.dev/publishVersion"] != _|_ {
if strconv.ParseInt(item.metadata.annotations["app.oam.dev/publishVersion"], 10, 64) != _|_ {
"patch-\(index)": kube.#Patch & {
$params: {
resource: {
apiVersion: "core.oam.dev/v1beta1"
kind: "Application"
metadata: {
name: item.metadata.name
namespace: item.metadata.namespace
}
}
patch: {
type: "merge"
data: {
metadata: {
annotations: {
"app.oam.dev/publishVersion": strconv.FormatInt(strconv.ParseInt(item.metadata.annotations["app.oam.dev/publishVersion"], 10, 64)+1, 10)
}
}
}
}
}
}
}
}
}

parameter: {
// +usage=The namespace to list the resources
namespace?: *"" | string
// +usage=The label selector to filter the resources
matchingLabels?: {...}
namespace: *context.data.metadata.namespace | string
// +usage=The name selector to select the app to bump revision
nameSelector?: close({
fromLabel?: string
}) | close({
// +usage=The label selector to filter the resources
matchingLabels?: {...}
})
}
8 changes: 4 additions & 4 deletions pkg/templates/static/action/record-event.cue
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ meta: {
apiVersion: "standard.oam.dev/v1alpha1"
kind: "EventListener"
metadata: {
if parameter.name != _|_ {
name: context.data.metadata.labels[parameter.name.fromLabel]
if parameter.nameSelector != _|_ {
name: context.data.metadata.labels[parameter.nameSelector.fromLabel]
}
if parameter.name == _|_ {
if parameter.nameSelector == _|_ {
name: context.data.metadata.name
}
namespace: context.data.metadata.namespace
Expand Down Expand Up @@ -68,7 +68,7 @@ if len(events) > 10 {
}

parameter: {
name?: {
nameSelector?: {
fromLabel: string
}
}

0 comments on commit f3a7737

Please sign in to comment.