From bf878b22854f314d5cf249cd5d05379800954af1 Mon Sep 17 00:00:00 2001 From: yangs Date: Thu, 13 Apr 2023 10:00:41 +0800 Subject: [PATCH] Feat: add cluster info to action context (#43) Feat: add cluster info in action context Signed-off-by: yangsoon Co-authored-by: yangsoon --- go.mod | 12 ++++----- go.sum | 24 ++++++++--------- .../controller/controller.go | 26 ++++++++++++++++++- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index fc51ea2..cc8651a 100644 --- a/go.mod +++ b/go.mod @@ -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/robfig/cron/v3 v3.0.1 @@ -39,7 +39,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 @@ -63,12 +63,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 @@ -118,7 +118,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 diff --git a/go.sum b/go.sum index d2c8fe7..758b074 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,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= @@ -79,8 +79,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= @@ -196,8 +196,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= @@ -225,16 +225,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= @@ -430,8 +430,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= diff --git a/pkg/source/builtin/k8sresourcewatcher/controller/controller.go b/pkg/source/builtin/k8sresourcewatcher/controller/controller.go index 75b2c96..3e1632c 100644 --- a/pkg/source/builtin/k8sresourcewatcher/controller/controller.go +++ b/pkg/source/builtin/k8sresourcewatcher/controller/controller.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + "github.com/kubevela/pkg/multicluster" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta" @@ -59,6 +60,7 @@ type Controller struct { sourceConf types.Config listenEvents map[types.EventType]bool controllerType string + cluster string } // Setup prepares controllers @@ -100,6 +102,7 @@ func Setup(ctx context.Context, cli dynamic.Interface, mapper meta.RESTMapper, c cache.Indexers{}, ) + c := newResourceController(ctx, logger, informer, ctrlConf.Kind) c := newResourceController(ctx, logger, informer, ctrlConf.Kind) // precheck -> c.sourceConf = ctrlConf @@ -116,14 +119,18 @@ func Setup(ctx context.Context, cli dynamic.Interface, mapper meta.RESTMapper, c return c } +func newResourceController(ctx context.Context, 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) - //nolint:errcheck // no need to check err here informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { + newEvent.Event = types.Event{ + Type: types.EventTypeCreate, + Cluster: cluster, + } newEvent.Event = types.Event{ Type: types.EventTypeCreate, Cluster: cluster, @@ -137,6 +144,10 @@ func newResourceController(ctx context.Context, logger *logrus.Entry, informer c } }, UpdateFunc: func(old, new interface{}) { + newEvent.Event = types.Event{ + Type: types.EventTypeUpdate, + Cluster: cluster, + } newEvent.Event = types.Event{ Type: types.EventTypeUpdate, Cluster: cluster, @@ -150,6 +161,10 @@ func newResourceController(ctx context.Context, logger *logrus.Entry, informer c } }, DeleteFunc: func(obj interface{}) { + newEvent.Event = types.Event{ + Type: types.EventTypeDelete, + Cluster: cluster, + } newEvent.Event = types.Event{ Type: types.EventTypeDelete, Cluster: cluster, @@ -169,6 +184,7 @@ func newResourceController(ctx context.Context, logger *logrus.Entry, informer c informer: informer, queue: queue, cluster: cluster, + cluster: cluster, } } @@ -182,6 +198,12 @@ func (c *Controller) Run(stopCh <-chan struct{}) { "cluster": c.cluster, }) c.logger.Info("starting watch k8s resources...") + 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) @@ -190,6 +212,7 @@ func (c *Controller) Run(stopCh <-chan struct{}) { return } c.logger.Info("resource watcher synced resources and ready for work") + c.logger.Info("resource watcher synced resources and ready for work") wait.Until(c.runWorker, time.Second, stopCh) } @@ -264,6 +287,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/%s happened, calling event handlers", e.Type, e.Cluster, obj.GetNamespace(), obj.GetName()) 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)