Skip to content

Commit

Permalink
Feat: add create event action for event listener watcher (kubevela#26)
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
Signed-off-by: Amit Singh <singhamitch@outlook.com>
  • Loading branch information
FogDong authored and semmet95 committed May 10, 2023
1 parent b688741 commit 902166f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 60 deletions.
13 changes: 4 additions & 9 deletions controllers/triggerservice/triggerservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/conf-create-event-listener.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions examples/conf-record-event.yaml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 0 additions & 15 deletions examples/sampleconf-record-event.yaml

This file was deleted.

6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
25 changes: 5 additions & 20 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ package action

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/kubevela/pkg/cue/cuex"
"github.com/mitchellh/hashstructure/v2"
Expand All @@ -35,8 +32,8 @@ import (
type Job struct {
sourceType string
id string
context string
properties string
context any
properties any
template string
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package filter

import (
"context"
"encoding/json"
"fmt"

"cuelang.org/go/cue"
Expand All @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/templates/static/action/create-event-listener.cue
Original file line number Diff line number Diff line change
@@ -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
},
]
}
}
}
}
13 changes: 12 additions & 1 deletion pkg/templates/static/action/record-event.cue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -52,3 +57,9 @@ if len(events) > 10 {
}
}
}

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

0 comments on commit 902166f

Please sign in to comment.