Skip to content

Commit

Permalink
Feat: add cluster info to action context (kubevela#43)
Browse files Browse the repository at this point in the history
Feat: add cluster info in action context

Signed-off-by: yangsoon <songyang.song@alibaba-inc.com>
Co-authored-by: yangsoon <songyang.song@alibaba-inc.com>
Signed-off-by: Amit Singh <singhamitch@outlook.com>
  • Loading branch information
2 people authored and semmet95 committed May 10, 2023
1 parent feaff53 commit fe14961
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 33 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/kubevela/kube-trigger
go 1.19

require (
cuelang.org/go v0.5.0-beta.2.0.20230130095913-d573e0c2f041
cuelang.org/go v0.5.0-beta.5
github.com/crossplane/crossplane-runtime v0.19.2
github.com/google/go-cmp v0.5.9
github.com/kubevela/pkg v1.8.1-0.20230403024929-46ddc1466157
github.com/kubevela/pkg v1.8.1-0.20230411071527-ac5fa22727f7
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
Expand Down Expand Up @@ -35,7 +35,7 @@ require (
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -55,12 +55,12 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
Expand Down Expand Up @@ -90,7 +90,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
25 changes: 13 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.100.2 h1:t9Iw5QH5v4XtlEQaCtUY7x6sCABps8sW0acw7e2WQ6Y=
cloud.google.com/go v0.100.2/go.mod h1:4Xra9TjzAeYHrl5+oeLlzbM2k3mjVhZh4UqTZ//w99A=
cloud.google.com/go/compute v0.1.0/go.mod h1:GAesmwr110a34z04OlxYkATPBEfVhkymfTBXtfbBFow=
cloud.google.com/go/compute v1.7.0 h1:v/k9Eueb8aAJ0vZuxKMrgm6kPhCLZU9HxFU+AFDs9Uk=
cuelang.org/go v0.5.0-beta.2.0.20230130095913-d573e0c2f041 h1:00Lo6MPqzIiVHi37+p8o+GwzpB9t83YsUGlkXQ+bC7Q=
cuelang.org/go v0.5.0-beta.2.0.20230130095913-d573e0c2f041/go.mod h1:okjJBHFQFer+a41sAe2SaGm1glWS8oEb6CmJvn5Zdws=
cuelang.org/go v0.5.0-beta.5 h1:TAV4ZjXw2M6xf6jI8XyAAXCqWJ82Y0oxhlf9w3l544A=
cuelang.org/go v0.5.0-beta.5/go.mod h1:okjJBHFQFer+a41sAe2SaGm1glWS8oEb6CmJvn5Zdws=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
Expand Down Expand Up @@ -70,8 +70,8 @@ github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
Expand Down Expand Up @@ -173,8 +173,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand All @@ -201,16 +201,16 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
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 v1.8.1-0.20230403024929-46ddc1466157 h1:01SXHJZjS2CYlPY1HpcmIkFCNko59sttqC7u/PYFt7s=
github.com/kubevela/pkg v1.8.1-0.20230403024929-46ddc1466157/go.mod h1:A6f7lQwCOt0KRPnQz511QLB4JgzPpDXYUG7bY8LJkEE=
github.com/kubevela/pkg v1.8.1-0.20230411071527-ac5fa22727f7 h1:Mz6KLtuqfIdoeIsbStI2M8PYShlxAf18S36ZeK4sCmU=
github.com/kubevela/pkg v1.8.1-0.20230411071527-ac5fa22727f7/go.mod h1:dGT23SGBw16frh5ReEYMFraxjppkX6jPCltBgTeLUhE=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2 h1:hAHbPm5IJGijwng3PWk09JkG9WeqChjprR5s9bBZ+OM=
github.com/matttproud/golang_protobuf_extensions v1.0.2/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
Expand Down Expand Up @@ -354,8 +354,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down Expand Up @@ -551,6 +551,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
55 changes: 54 additions & 1 deletion pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ package filter
import (
"context"
"fmt"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
"cuelang.org/go/tools/fix"
"github.com/kubevela/pkg/cue/cuex"
)

// ApplyFilter applies the given filter to an object.
func ApplyFilter(ctx context.Context, contextData map[string]interface{}, filter string) (bool, error) {
filterVal, err := cuex.CompileStringWithOptions(ctx, fmt.Sprintf("filter: %s", filter), cuex.WithExtraData("context", contextData))
template, err := BuildFilterTemplate(filter)
if err != nil {
return false, err
}
filterVal, err := cuex.CompileStringWithOptions(ctx, template, cuex.WithExtraData("context", contextData))
if err != nil {
return false, err
}
Expand All @@ -47,3 +56,47 @@ func ApplyFilter(ctx context.Context, contextData map[string]interface{}, filter
}
return resultBool, nil
}

// BuildFilterTemplate build filter template
func BuildFilterTemplate(filter string) (string, error) {
f, err := parser.ParseFile("-", filter)
if err != nil {
return "", err
}
n := fix.File(f)
if n.Imports == nil {
return fmt.Sprintf("filter: %s", filter), nil
}
var importDecls, contentDecls []ast.Decl
for _, decl := range n.Decls {
if importDecl, ok := decl.(*ast.ImportDecl); ok {
importDecls = append(importDecls, importDecl)
} else {
contentDecls = append(contentDecls, decl)
}
}
importString, err := encodeDeclsToString(importDecls)
if err != nil {
return "", err
}
contentString, err := encodeDeclsToString(contentDecls)
if err != nil {
return "", err
}
return fmt.Sprintf(filterTemplate, importString, contentString), nil
}

func encodeDeclsToString(decls []ast.Decl) (string, error) {
bs, err := format.Node(&ast.File{Decls: decls}, format.Simplify())
if err != nil {
return "", fmt.Errorf("failed to encode cue: %w", err)
}
return strings.TrimSpace(string(bs)), nil
}

var filterTemplate = `
%s
filter: {
%s
}
`
57 changes: 57 additions & 0 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2022 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package filter

import (
"testing"

"github.com/kubevela/pkg/util/stringtools"
"github.com/stretchr/testify/assert"
)

func TestBuildFilterTemplate(t *testing.T) {
testcases := map[string]struct {
src string
result string
}{
"build filter with a single expression": {
src: "a == 1",
result: "filter: a == 1",
},
"build filter with import declarations": {
src: `
import "strings"
strings.Contains("abc", "a")`,
result: `
import "strings"
filter: {
strings.Contains("abc", "a")
}`,
},
}

for name, testcase := range testcases {
t.Run(name, func(t *testing.T) {
result, err := BuildFilterTemplate(testcase.src)
assert.NoError(t, err)
assert.Equal(t,
stringtools.TrimLeadingIndent(testcase.result),
stringtools.TrimLeadingIndent(result),
)
})
}
}
38 changes: 26 additions & 12 deletions pkg/source/builtin/k8sresourcewatcher/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"github.com/kubevela/pkg/multicluster"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -55,6 +56,7 @@ type Controller struct {
sourceConf types.Config
listenEvents map[types.EventType]bool
controllerType string
cluster string
}

// Setup prepares controllers
Expand Down Expand Up @@ -91,7 +93,7 @@ func Setup(ctx context.Context, cli dynamic.Interface, mapper meta.RESTMapper, c
cache.Indexers{},
)

c := newResourceController(logger, informer, ctrlConf.Kind)
c := newResourceController(ctx, logger, informer, ctrlConf.Kind)
// precheck ->
c.sourceConf = ctrlConf
c.eventHandlers = eh
Expand All @@ -107,13 +109,17 @@ func Setup(ctx context.Context, cli dynamic.Interface, mapper meta.RESTMapper, c
return c
}

func newResourceController(logger *logrus.Entry, informer cache.SharedIndexInformer, kind string) *Controller {
func newResourceController(ctx context.Context, logger *logrus.Entry, informer cache.SharedIndexInformer, kind string) *Controller {
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
var newEvent types.InformerEvent
var err error
cluster, _ := multicluster.ClusterFrom(ctx)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
newEvent.Type = types.EventTypeCreate
newEvent.Event = types.Event{
Type: types.EventTypeCreate,
Cluster: cluster,
}
newEvent.EventObj = obj
meta := utils.GetObjectMetaData(obj)
logger.Tracef("received add event: %v %s/%s", kind, meta.GetName(), meta.GetNamespace())
Expand All @@ -122,7 +128,10 @@ func newResourceController(logger *logrus.Entry, informer cache.SharedIndexInfor
}
},
UpdateFunc: func(old, new interface{}) {
newEvent.Type = types.EventTypeUpdate
newEvent.Event = types.Event{
Type: types.EventTypeUpdate,
Cluster: cluster,
}
newEvent.EventObj = new
meta := utils.GetObjectMetaData(new)
logger.Tracef("received update event: %v %s/%s", kind, meta.GetName(), meta.GetNamespace())
Expand All @@ -131,7 +140,10 @@ func newResourceController(logger *logrus.Entry, informer cache.SharedIndexInfor
}
},
DeleteFunc: func(obj interface{}) {
newEvent.Type = types.EventTypeDelete
newEvent.Event = types.Event{
Type: types.EventTypeDelete,
Cluster: cluster,
}
newEvent.EventObj = obj
meta := utils.GetObjectMetaData(obj)
logger.Tracef("received delete event: %v %s/%s", kind, meta.GetName(), meta.GetNamespace())
Expand All @@ -145,26 +157,28 @@ func newResourceController(logger *logrus.Entry, informer cache.SharedIndexInfor
logger: logger,
informer: informer,
queue: queue,
cluster: cluster,
}
}

// Run starts the kube-trigger controller
func (c *Controller) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()

c.logger.Info("starting...")
c.logger = c.logger.WithFields(logrus.Fields{
"apiVersion": c.sourceConf.APIVersion,
"kind": c.sourceConf.Kind,
"cluster": c.cluster,
})
c.logger.Info("starting watch k8s resources...")
serverStartTime = time.Now().Local()

go c.informer.Run(stopCh)

if !cache.WaitForCacheSync(stopCh, c.HasSynced) {
utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
return
}

c.logger.Info("synced and ready")

c.logger.Info("resource watcher synced resources and ready for work")
wait.Until(c.runWorker, time.Second, stopCh)
}

Expand Down Expand Up @@ -238,7 +252,7 @@ func (c *Controller) processItem(newEvent types.InformerEvent) error {
}

func (c *Controller) callEventHandler(obj metav1.Object, e types.Event) {
c.logger.Infof("%s event %s/%s happened, calling event handlers", e.Type, obj.GetName(), obj.GetNamespace())
c.logger.Infof("%s event %s/%s/%s happened, calling event handlers", e.Type, e.Cluster, obj.GetNamespace(), obj.GetName())
for _, fn := range c.eventHandlers {
err := fn(c.controllerType, e, obj)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (w *K8sResourceWatcher) Init(properties *runtime.RawExtension, eh eventhand
}
w.eventHandlers[ctrlConf.Key()] = append(w.eventHandlers[ctrlConf.Key()], eh)

w.logger = logrus.WithField("source", v1alpha1.SourceTypeResourceWatcher)
w.logger = logrus.WithField("source-type", v1alpha1.SourceTypeResourceWatcher)

w.logger.Debugf("initialized")
return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/source/builtin/k8sresourcewatcher/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ const (

// Event represent an event got from k8s api server
type Event struct {
Type EventType `json:"type"`
Type EventType `json:"type"`
Cluster string `json:"cluster"`
}

// InformerEvent indicate the informerEvent
Expand Down

0 comments on commit fe14961

Please sign in to comment.