From 902166f5c58db359cb51e82aa10cdb6261b6fcf7 Mon Sep 17 00:00:00 2001 From: Tianxin Dong Date: Mon, 6 Feb 2023 17:48:52 +0800 Subject: [PATCH] Feat: add create event action for event listener watcher (#26) Signed-off-by: FogDong Signed-off-by: Amit Singh --- .../triggerservice_controller.go | 13 +++------ ...econf-bump-app.yaml => conf-bump-app.yaml} | 0 examples/conf-create-event-listener.yaml | 12 ++++++++ examples/conf-record-event.yaml | 14 ++++++++++ examples/sampleconf-record-event.yaml | 15 ---------- go.mod | 6 ++-- go.sum | 6 ++-- pkg/action/action.go | 25 ++++------------- pkg/filter/filter.go | 8 +----- .../static/action/create-event-listener.cue | 28 +++++++++++++++++++ pkg/templates/static/action/record-event.cue | 13 ++++++++- 11 files changed, 80 insertions(+), 60 deletions(-) rename examples/{sampleconf-bump-app.yaml => conf-bump-app.yaml} (100%) create mode 100644 examples/conf-create-event-listener.yaml create mode 100644 examples/conf-record-event.yaml delete mode 100644 examples/sampleconf-record-event.yaml create mode 100644 pkg/templates/static/action/create-event-listener.cue diff --git a/controllers/triggerservice/triggerservice_controller.go b/controllers/triggerservice/triggerservice_controller.go index 118685d..f9f0ef2 100644 --- a/controllers/triggerservice/triggerservice_controller.go +++ b/controllers/triggerservice/triggerservice_controller.go @@ -111,24 +111,19 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu func (r *Reconciler) createWorker(ctx context.Context, ts *standardv1alpha1.TriggerService) error { templateName := "default" - opts := make([]cuex.CompileOption, 0) - opts = append(opts, cuex.WithExtraData("triggerService", map[string]string{ - "name": ts.Name, - "namespace": ts.Namespace, - })) if ts.Spec.Worker != nil { if ts.Spec.Worker.Template != "" { templateName = ts.Spec.Worker.Template } - if ts.Spec.Worker.Properties != nil { - opts = append(opts, cuex.WithExtraData("parameter", ts.Spec.Worker.Properties)) - } } template, err := templates.NewLoader("worker").LoadTemplate(ctx, templateName) if err != nil { return err } - v, err := cuex.DefaultCompiler.Get().CompileStringWithOptions(ctx, template, opts...) + v, err := cuex.CompileStringWithOptions(ctx, template, cuex.WithExtraData("triggerService", map[string]string{ + "name": ts.Name, + "namespace": ts.Namespace, + }), cuex.WithExtraData("parameter", ts.Spec.Worker.Properties)) if err != nil { return err } diff --git a/examples/sampleconf-bump-app.yaml b/examples/conf-bump-app.yaml similarity index 100% rename from examples/sampleconf-bump-app.yaml rename to examples/conf-bump-app.yaml diff --git a/examples/conf-create-event-listener.yaml b/examples/conf-create-event-listener.yaml new file mode 100644 index 0000000..410688a --- /dev/null +++ b/examples/conf-create-event-listener.yaml @@ -0,0 +1,12 @@ + triggers: + - source: + type: resource-watcher + properties: + apiVersion: core.oam.dev/v1alpha1 + kind: WorkflowRun + events: + - create + matchingLabels: + trigger.oam.dev/watch: "true" + action: + type: create-event-listener diff --git a/examples/conf-record-event.yaml b/examples/conf-record-event.yaml new file mode 100644 index 0000000..1c36ee1 --- /dev/null +++ b/examples/conf-record-event.yaml @@ -0,0 +1,14 @@ + triggers: + - source: + type: resource-watcher + properties: + apiVersion: apps/v1 + kind: Deployment + events: + - update + filter: context.data.status.readyReplicas == context.data.status.replicas + action: + type: record-event + properties: + name: + fromLabel: "workflowrun.oam.dev/name" diff --git a/examples/sampleconf-record-event.yaml b/examples/sampleconf-record-event.yaml deleted file mode 100644 index c60b474..0000000 --- a/examples/sampleconf-record-event.yaml +++ /dev/null @@ -1,15 +0,0 @@ - triggers: - - source: - type: resource-watcher - properties: - # We are interested in ConfigMap events. - apiVersion: "v1" - kind: ConfigMap - namespace: default - # Only watch update event. - events: - - update - filter: | - context: data: metadata: name: =~"this-will-trigger-update-.*" - action: - type: record-event diff --git a/go.mod b/go.mod index 4243538..9849789 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,10 @@ require ( cloud.google.com/go v0.102.0 cuelang.org/go v0.5.0-beta.2.0.20230130095913-d573e0c2f041 github.com/crossplane/crossplane-runtime v0.14.1-0.20210722005935-0b469fcc77cd - github.com/kubevela/pkg v0.0.0-20230201074644-eab57c8c8982 + github.com/kubevela/pkg v0.0.0-20230206030316-ac89a0850bc3 github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/mitchellh/mapstructure v1.5.0 github.com/oam-dev/kubevela-core-api v1.6.0-alpha.1 - github.com/onsi/ginkgo/v2 v2.1.6 - github.com/onsi/gomega v1.20.2 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.0 @@ -24,7 +22,6 @@ require ( k8s.io/client-go v0.25.3 k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed sigs.k8s.io/controller-runtime v0.12.3 - sigs.k8s.io/yaml v1.3.0 ) require ( @@ -128,6 +125,7 @@ require ( sigs.k8s.io/apiserver-runtime v1.1.2-0.20221102045245-fb656940062f // indirect sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect + sigs.k8s.io/yaml v1.3.0 // indirect ) replace sigs.k8s.io/apiserver-network-proxy/konnectivity-client => sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.24 diff --git a/go.sum b/go.sum index 7282733..38a0812 100644 --- a/go.sum +++ b/go.sum @@ -897,8 +897,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kubevela/pkg v0.0.0-20230201074644-eab57c8c8982 h1:ARqqrnHmS6CKjEwG4Vn9IcyhvhcsscVJlnbHVg45/co= -github.com/kubevela/pkg v0.0.0-20230201074644-eab57c8c8982/go.mod h1:0TUvCW4+l+p1sHNihtIK1o8ih/FcM4Pt1HqXldKf0Mc= +github.com/kubevela/pkg v0.0.0-20230206030316-ac89a0850bc3 h1:THCicuuPTUG2S9hSfUgaGcGoYI9OJm2m1SbGgBgJJ9k= +github.com/kubevela/pkg v0.0.0-20230206030316-ac89a0850bc3/go.mod h1:0TUvCW4+l+p1sHNihtIK1o8ih/FcM4Pt1HqXldKf0Mc= github.com/kulti/thelper v0.4.0/go.mod h1:vMu2Cizjy/grP+jmsvOFDx1kYP6+PD1lqg4Yu5exl2U= github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= @@ -1065,7 +1065,6 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.1.6 h1:Fx2POJZfKRQcM1pH49qSZiYeu319wji004qX+GDovrU= -github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.3.0/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= @@ -1081,7 +1080,6 @@ github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je4 github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0= github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= -github.com/onsi/gomega v1.20.2/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc= github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= diff --git a/pkg/action/action.go b/pkg/action/action.go index aecdcd1..606017e 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -18,10 +18,7 @@ package action import ( "context" - "encoding/json" - "fmt" "strconv" - "strings" "github.com/kubevela/pkg/cue/cuex" "github.com/mitchellh/hashstructure/v2" @@ -35,8 +32,8 @@ import ( type Job struct { sourceType string id string - context string - properties string + context any + properties any template string } @@ -51,17 +48,6 @@ func New(meta v1alpha1.ActionMeta, contextData map[string]interface{}) (*Job, er if err != nil { return nil, err } - contextByte, err := json.Marshal(contextData) - if err != nil { - return nil, err - } - propByte := []byte("{}") - if meta.Properties != nil { - propByte, err = meta.Properties.MarshalJSON() - if err != nil { - return nil, err - } - } id, err := computeHash(meta) if err != nil { return nil, err @@ -70,8 +56,8 @@ func New(meta v1alpha1.ActionMeta, contextData map[string]interface{}) (*Job, er id: id, template: template, sourceType: meta.Type, - context: fmt.Sprintf("context: %s", string(contextByte)), - properties: fmt.Sprintf("parameter: %s", string(propByte)), + context: contextData, + properties: meta.Properties, } return &ret, nil @@ -99,8 +85,7 @@ func (j *Job) ID() string { // Run execute action func (j *Job) Run(ctx context.Context) error { - str := strings.Join([]string{j.template, j.properties, j.context}, "\n") - v, err := cuex.CompileString(ctx, str) + v, err := cuex.CompileStringWithOptions(ctx, j.template, cuex.WithExtraData("parameter", j.properties), cuex.WithExtraData("context", j.context)) if err != nil { return err } diff --git a/pkg/filter/filter.go b/pkg/filter/filter.go index 088df33..012ac6b 100644 --- a/pkg/filter/filter.go +++ b/pkg/filter/filter.go @@ -18,7 +18,6 @@ package filter import ( "context" - "encoding/json" "fmt" "cuelang.org/go/cue" @@ -27,12 +26,7 @@ import ( // ApplyFilter applies the given filter to an object. func ApplyFilter(ctx context.Context, contextData map[string]interface{}, filter string) (bool, error) { - contextByte, err := json.Marshal(contextData) - if err != nil { - return false, err - } - contextTemplate := fmt.Sprintf("context: %s", string(contextByte)) - filterVal, err := cuex.CompileString(ctx, fmt.Sprintf("filter: {\n%s\n%s\n}", filter, contextTemplate)) + filterVal, err := cuex.CompileStringWithOptions(ctx, fmt.Sprintf("filter: %s", filter), cuex.WithExtraData("context", contextData)) if err != nil { return false, err } diff --git a/pkg/templates/static/action/create-event-listener.cue b/pkg/templates/static/action/create-event-listener.cue new file mode 100644 index 0000000..fa493ce --- /dev/null +++ b/pkg/templates/static/action/create-event-listener.cue @@ -0,0 +1,28 @@ +import ( + "vela/kube" +) + +apply: kube.#Apply & { + $params: { + resource: { + apiVersion: "standard.oam.dev/v1alpha1" + kind: "EventListener" + metadata: { + name: context.data.metadata.name + namespace: context.data.metadata.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 + }, + ] + } + } + } +} diff --git a/pkg/templates/static/action/record-event.cue b/pkg/templates/static/action/record-event.cue index 2767fe2..b3520fa 100644 --- a/pkg/templates/static/action/record-event.cue +++ b/pkg/templates/static/action/record-event.cue @@ -7,7 +7,12 @@ meta: { apiVersion: "standard.oam.dev/v1alpha1" kind: "EventListener" metadata: { - name: context.data.metadata.name + if parameter.name != _|_ { + name: context.data.metadata.labels[parameter.name.fromLabel] + } + if parameter.name == _|_ { + name: context.data.metadata.name + } namespace: context.data.metadata.namespace } } @@ -52,3 +57,9 @@ if len(events) > 10 { } } } + +parameter: { + name?: { + fromLabel: string + } +}