From 63ab2a366ae3fa6976d7628c12e0dd5118fe2470 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Wed, 11 Oct 2023 10:42:46 +0300 Subject: [PATCH 01/20] add MeshsyncConfig used to receive config from meshync CRD Signed-off-by: Daniel Kiptoon --- internal/config/types.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/config/types.go b/internal/config/types.go index 53b611ba..b9967ca0 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -32,3 +32,9 @@ type ListenerConfig struct { PublishTo string `json:"publish-to" yaml:"publish-to"` SubscribeTo string `json:"subscribe-to" yaml:"subscribe-to"` } + +// Meshsync configuration controls the resources meshsync produces and consumes +type MeshsyncConfig struct { + PipelineConfigs map[string]PipelineConfigs `json:"pipeline-configs,omitempty" yaml:"pipeline-configs,omitempty"` + ListenerConfigs map[string]ListenerConfig `json:"listener-config,omitempty" yaml:"listener-config,omitempty"` +} From 8fecc42b08b2b2f88f6d6ec7174fbd3d0475eda9 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Wed, 11 Oct 2023 10:43:55 +0300 Subject: [PATCH 02/20] add functionality to process config from meshsync crd Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 internal/config/crd_config.go diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go new file mode 100644 index 00000000..898a5722 --- /dev/null +++ b/internal/config/crd_config.go @@ -0,0 +1,57 @@ +package config + +import ( + "context" + "errors" + + "github.com/layer5io/meshkit/utils" + mesherykube "github.com/layer5io/meshkit/utils/kubernetes" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var ( + namespace = "meshery" // Namespace for the Custom Resource + crName = "meshery-meshsync" // Name of the custom resource + version = "v1alpha1" // Version of the Custom Resource + group = "meshery.layer5.io" //Group for the Custom Resource + resource = "meshsyncs" //Name of the Resource +) + +func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { + // Initialize kubeclient + kubeClient, err := mesherykube.New(nil) + if err != nil { + return nil, ErrInitConfig(err) + } + // initialize the dynamic kube client + dyClient := kubeClient.DynamicKubeClient + + // initialize the group version resource to access the custom resource + gvr := schema.GroupVersionResource{Version: version, Group: group, Resource: resource} + + // make a call to get the custom resource + crd, err := dyClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), crName, metav1.GetOptions{}) + + if err != nil { + return nil, ErrInitConfig(err) + } + // get the spec section of the unstructured object + spec := crd.Object["spec"] + configObj := spec.(map[string]interface{})["config"] + + if configObj == nil { + return nil, ErrInitConfig(errors.New("Custom Resource does not have Meshsync Configs")) + } + configStr, err := utils.Marshal(configObj) + if err != nil { + return nil, ErrInitConfig(err) + } + meshsyncConfig := MeshsyncConfig{} + err = utils.Unmarshal(string(configStr), &meshsyncConfig) + + if err != nil { + return nil, ErrInitConfig(err) + } + return &meshsyncConfig, nil +} From c2f557e0f6b58ca42025800d24c570617fba97f9 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Wed, 11 Oct 2023 10:44:57 +0300 Subject: [PATCH 03/20] refactor to obtain incoporate configs from meshsync CRD if available Signed-off-by: Daniel Kiptoon --- main.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main.go b/main.go index 068fbe50..f21b2e08 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,20 @@ func main() { os.Exit(1) } + // get configs from meshsync crd if available + crdConfigs, err := config.GetMeshsyncCRDConfigs() + + if err != nil { + // no configs found from meshsync CRD log warning + log.Warn(err) + } + + // pass configs from crd to default configs + if crdConfigs != nil { + config.Pipelines = crdConfigs.PipelineConfigs + config.Listeners = crdConfigs.ListenerConfigs + } + // Config init and seed cfg, err := config.New(provider) if err != nil { From 439496a3f7e387dec8fd1528da4b771872c801ca Mon Sep 17 00:00:00 2001 From: Kiptoon Kipkurui <50325211+KiptoonKipkurui@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:52:05 +0300 Subject: [PATCH 04/20] ensure the cr is present before using it Co-authored-by: Aisuko Signed-off-by: Kiptoon Kipkurui <50325211+KiptoonKipkurui@users.noreply.github.com> --- internal/config/crd_config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index 898a5722..b85357ff 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -36,7 +36,11 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { if err != nil { return nil, ErrInitConfig(err) } - // get the spec section of the unstructured object + + if crd == nil { + return nil, ErrInitConfig(errors.New("Custom Resource is nil")) + } + spec := crd.Object["spec"] configObj := spec.(map[string]interface{})["config"] From 2ee712fcb12f12fda7c61a80d42612fdf96b8132 Mon Sep 17 00:00:00 2001 From: Kiptoon Kipkurui <50325211+KiptoonKipkurui@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:53:22 +0300 Subject: [PATCH 05/20] check conversion of spec for success Co-authored-by: Aisuko Signed-off-by: Kiptoon Kipkurui <50325211+KiptoonKipkurui@users.noreply.github.com> --- internal/config/crd_config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index b85357ff..e5af5c1e 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -43,7 +43,11 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { spec := crd.Object["spec"] configObj := spec.(map[string]interface{})["config"] - + specMap, ok := spec.(map[string]interface{}) + if !ok { + return nil, ErrInitConfig(errors.New("Unable to convert spec to map")) + } + configObj := specMap["config"] if configObj == nil { return nil, ErrInitConfig(errors.New("Custom Resource does not have Meshsync Configs")) } From 55d3cc30ad3e10d2b19323fa3ae9a5a75dae0f9e Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Mon, 23 Oct 2023 22:53:35 +0300 Subject: [PATCH 06/20] refactor to add checks before using spec map Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index e5af5c1e..66217020 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -42,7 +42,6 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { } spec := crd.Object["spec"] - configObj := spec.(map[string]interface{})["config"] specMap, ok := spec.(map[string]interface{}) if !ok { return nil, ErrInitConfig(errors.New("Unable to convert spec to map")) From 3dcd825d5ccd77aec7fd39bde923d2c80bc700f3 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Thu, 26 Oct 2023 19:30:22 +0300 Subject: [PATCH 07/20] update variable namings for better understandability Signed-off-by: Daniel Kiptoon --- internal/config/types.go | 4 ++-- main.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/config/types.go b/internal/config/types.go index b9967ca0..0f2150c1 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -35,6 +35,6 @@ type ListenerConfig struct { // Meshsync configuration controls the resources meshsync produces and consumes type MeshsyncConfig struct { - PipelineConfigs map[string]PipelineConfigs `json:"pipeline-configs,omitempty" yaml:"pipeline-configs,omitempty"` - ListenerConfigs map[string]ListenerConfig `json:"listener-config,omitempty" yaml:"listener-config,omitempty"` + Pipelines map[string]PipelineConfigs `json:"pipeline-configs,omitempty" yaml:"pipeline-configs,omitempty"` + Listeners map[string]ListenerConfig `json:"listener-config,omitempty" yaml:"listener-config,omitempty"` } diff --git a/main.go b/main.go index f21b2e08..d288f07e 100644 --- a/main.go +++ b/main.go @@ -49,8 +49,8 @@ func main() { // pass configs from crd to default configs if crdConfigs != nil { - config.Pipelines = crdConfigs.PipelineConfigs - config.Listeners = crdConfigs.ListenerConfigs + config.Pipelines = crdConfigs.Pipelines + config.Listeners = crdConfigs.Listeners } // Config init and seed From 042eb801450f213891fbacde7556daa5d31d6be7 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 22:22:15 +0300 Subject: [PATCH 08/20] add slice package Signed-off-by: Daniel Kiptoon --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index ee284889..ebae8fb1 100644 --- a/go.mod +++ b/go.mod @@ -144,6 +144,7 @@ require ( go.opentelemetry.io/otel/trace v1.16.0 // indirect go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect golang.org/x/crypto v0.14.0 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d golang.org/x/oauth2 v0.10.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.13.0 // indirect diff --git a/go.sum b/go.sum index cb1d4113..23a1447d 100644 --- a/go.sum +++ b/go.sum @@ -750,6 +750,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= From 46256a96152aaaa8be7a62fc381630f319cd9b02 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 22:41:21 +0300 Subject: [PATCH 09/20] add watched resource model Signed-off-by: Daniel Kiptoon --- internal/config/types.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/config/types.go b/internal/config/types.go index 0f2150c1..d7971395 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -20,21 +20,31 @@ const ( type PipelineConfigs []PipelineConfig type PipelineConfig struct { - Name string `json:"name" yaml:"name"` - PublishTo string `json:"publish-to" yaml:"publish-to"` + Name string `json:"name" yaml:"name"` + PublishTo string `json:"publish-to" yaml:"publish-to"` + Events []string `json:"events" yaml:"events"` } type ListenerConfigs []ListenerConfig type ListenerConfig struct { - Name string `json:"name" yaml:"name"` - ConnectionName string `json:"connection-name" yaml:"connection-name"` - PublishTo string `json:"publish-to" yaml:"publish-to"` - SubscribeTo string `json:"subscribe-to" yaml:"subscribe-to"` + Name string `json:"name" yaml:"name"` + ConnectionName string `json:"connection-name" yaml:"connection-name"` + PublishTo string `json:"publish-to" yaml:"publish-to"` + SubscribeTo string `json:"subscribe-to" yaml:"subscribe-to"` + Events []string `json:"events" yaml:"events"` } // Meshsync configuration controls the resources meshsync produces and consumes type MeshsyncConfig struct { + BlackList []string `json:"blacklist" yaml:"blacklist"` Pipelines map[string]PipelineConfigs `json:"pipeline-configs,omitempty" yaml:"pipeline-configs,omitempty"` Listeners map[string]ListenerConfig `json:"listener-config,omitempty" yaml:"listener-config,omitempty"` + WhiteList []ResourceConfig `json:"resource-configs" yaml:"resource-configs"` +} + +// Watched Resource configuration +type ResourceConfig struct { + Resource string + Events []string } From c82b97c89e8772f6378ac96a2d3fa27485c650d0 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 22:42:12 +0300 Subject: [PATCH 10/20] refactor to allow configuration of resources Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 66 +++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index 66217020..a4517f70 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -6,6 +6,8 @@ import ( "github.com/layer5io/meshkit/utils" mesherykube "github.com/layer5io/meshkit/utils/kubernetes" + "golang.org/x/exp/slices" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -46,7 +48,7 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { if !ok { return nil, ErrInitConfig(errors.New("Unable to convert spec to map")) } - configObj := specMap["config"] + configObj := specMap["watch-list"] if configObj == nil { return nil, ErrInitConfig(errors.New("Custom Resource does not have Meshsync Configs")) } @@ -55,10 +57,70 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { return nil, ErrInitConfig(err) } meshsyncConfig := MeshsyncConfig{} - err = utils.Unmarshal(string(configStr), &meshsyncConfig) + configMap := corev1.ConfigMap{} + err = utils.Unmarshal(string(configStr), &configMap) if err != nil { return nil, ErrInitConfig(err) } + + if _, ok := configMap.Data["blacklist"]; ok { + err = utils.Unmarshal(configMap.Data["blacklist"], &meshsyncConfig.BlackList) + if err != nil { + return nil, ErrInitConfig(err) + } + } + + if _, ok := configMap.Data["whitelist"]; ok { + err = utils.Unmarshal(configMap.Data["whitelist"], &meshsyncConfig.WhiteList) + if err != nil { + return nil, ErrInitConfig(err) + } + } + + // Handle global resources + globalPipelines := make(PipelineConfigs, 0) + for _, v := range Pipelines[GlobalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + globalPipelines = append(globalPipelines, v) + } + } + if len(globalPipelines) > 0 { + meshsyncConfig.Pipelines = map[string]PipelineConfigs{} + meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines + } + + // Handle local resources + localPipelines := make(PipelineConfigs, 0) + for _, v := range Pipelines[LocalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + localPipelines = append(localPipelines, v) + } + } + + if len(localPipelines) > 0 { + meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines + } + + // Handle listeners + listerners := make(ListenerConfigs, 0) + for _, v := range Listeners { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + listerners = append(listerners, v) + } + } + + if len(listerners) > 0 { + meshsyncConfig.Listeners = make(map[string]ListenerConfig) + meshsyncConfig.Listeners = Listeners + } + return &meshsyncConfig, nil } From 483799283e1cee244da8161bfa092f883af1c613 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 22:56:32 +0300 Subject: [PATCH 11/20] check whether an event type is supported before publishing item Signed-off-by: Daniel Kiptoon --- internal/pipeline/handlers.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/pipeline/handlers.go b/internal/pipeline/handlers.go index fce4cace..0c7b02e1 100644 --- a/internal/pipeline/handlers.go +++ b/internal/pipeline/handlers.go @@ -7,6 +7,7 @@ import ( "github.com/layer5io/meshkit/broker" internalconfig "github.com/layer5io/meshsync/internal/config" "github.com/layer5io/meshsync/pkg/model" + "golang.org/x/exp/slices" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/tools/cache" ) @@ -69,6 +70,11 @@ func (ri *RegisterInformer) registerHandlers(s cache.SharedIndexInformer) { } func (ri *RegisterInformer) publishItem(obj *unstructured.Unstructured, evtype broker.EventType, config internalconfig.PipelineConfig) error { + + // if the event is not supported skip + if !slices.Contains(ri.config.Events, string(evtype)) { + return nil + } err := ri.broker.Publish(config.PublishTo, &broker.Message{ ObjectType: broker.MeshSync, EventType: evtype, From 95c9d15dfb3072d47aaab8c1c81580e4a4b6c3eb Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 23:16:25 +0300 Subject: [PATCH 12/20] check for presence of configs from piplines and listeners before using them Signed-off-by: Daniel Kiptoon --- main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d288f07e..969ca5fd 100644 --- a/main.go +++ b/main.go @@ -49,8 +49,13 @@ func main() { // pass configs from crd to default configs if crdConfigs != nil { - config.Pipelines = crdConfigs.Pipelines - config.Listeners = crdConfigs.Listeners + if len(crdConfigs.Pipelines) > 0 { + config.Pipelines = crdConfigs.Pipelines + } + + if len(crdConfigs.Listeners) > 0 { + config.Listeners = crdConfigs.Listeners + } } // Config init and seed From 0e99363f165de90cc10fb2da10ee356b9482da62 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sun, 29 Oct 2023 23:16:59 +0300 Subject: [PATCH 13/20] safe initialize pipeline Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index a4517f70..56501e00 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -103,7 +103,9 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { } if len(localPipelines) > 0 { - meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + if meshsyncConfig.Pipelines == nil { + meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + } meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines } From d1b0c7657cb3623f85a9bb241a7e5fc870db2a15 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sat, 4 Nov 2023 10:07:48 +0300 Subject: [PATCH 14/20] go mod tidy package management Signed-off-by: Daniel Kiptoon --- go.mod | 14 +++++++------- go.sum | 36 ++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index ebae8fb1..15a01a5d 100644 --- a/go.mod +++ b/go.mod @@ -17,9 +17,9 @@ require ( github.com/spf13/viper v1.15.0 golang.org/x/net v0.17.0 gorm.io/gorm v1.25.4 - k8s.io/api v0.28.2 - k8s.io/apimachinery v0.28.2 - k8s.io/client-go v0.28.2 + k8s.io/api v0.28.3 + k8s.io/apimachinery v0.28.3 + k8s.io/client-go v0.28.3 k8s.io/kubectl v0.28.2 ) @@ -49,7 +49,7 @@ require ( github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-metrics v0.0.1 // indirect github.com/docker/go-units v0.5.0 // indirect - github.com/emicklei/go-restful/v3 v3.10.1 // indirect + github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch v5.6.0+incompatible // indirect github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect github.com/fatih/color v1.13.0 // indirect @@ -162,10 +162,10 @@ require ( gorm.io/driver/postgres v1.4.6 // indirect gorm.io/driver/sqlite v1.4.4 // indirect helm.sh/helm/v3 v3.12.3 // indirect - k8s.io/apiextensions-apiserver v0.27.3 // indirect - k8s.io/apiserver v0.27.3 // indirect + k8s.io/apiextensions-apiserver v0.28.3 // indirect + k8s.io/apiserver v0.28.3 // indirect k8s.io/cli-runtime v0.28.2 // indirect - k8s.io/component-base v0.28.2 // indirect + k8s.io/component-base v0.28.3 // indirect k8s.io/klog/v2 v2.100.1 // indirect k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect diff --git a/go.sum b/go.sum index 23a1447d..8f533b55 100644 --- a/go.sum +++ b/go.sum @@ -162,8 +162,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 h1:ZClxb8laGDf5arXfYcAtECDFgAgHklGI8CxgjHnXKJ4= github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE= -github.com/emicklei/go-restful/v3 v3.10.1 h1:rc42Y5YTp7Am7CS630D7JmhRjq4UlEUuEKfrDac4bSQ= -github.com/emicklei/go-restful/v3 v3.10.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= +github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw= github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -778,8 +778,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1009,8 +1009,8 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= -golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= +golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1171,20 +1171,20 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -k8s.io/api v0.28.2 h1:9mpl5mOb6vXZvqbQmankOfPIGiudghwCoLl1EYfUZbw= -k8s.io/api v0.28.2/go.mod h1:RVnJBsjU8tcMq7C3iaRSGMeaKt2TWEUXcpIt/90fjEg= -k8s.io/apiextensions-apiserver v0.27.3 h1:xAwC1iYabi+TDfpRhxh4Eapl14Hs2OftM2DN5MpgKX4= -k8s.io/apiextensions-apiserver v0.27.3/go.mod h1:BH3wJ5NsB9XE1w+R6SSVpKmYNyIiyIz9xAmBl8Mb+84= -k8s.io/apimachinery v0.28.2 h1:KCOJLrc6gu+wV1BYgwik4AF4vXOlVJPdiqn0yAWWwXQ= -k8s.io/apimachinery v0.28.2/go.mod h1:RdzF87y/ngqk9H4z3EL2Rppv5jj95vGS/HaFXrLDApU= -k8s.io/apiserver v0.27.3 h1:AxLvq9JYtveYWK+D/Dz/uoPCfz8JC9asR5z7+I/bbQ4= -k8s.io/apiserver v0.27.3/go.mod h1:Y61+EaBMVWUBJtxD5//cZ48cHZbQD+yIyV/4iEBhhNA= +k8s.io/api v0.28.3 h1:Gj1HtbSdB4P08C8rs9AR94MfSGpRhJgsS+GF9V26xMM= +k8s.io/api v0.28.3/go.mod h1:MRCV/jr1dW87/qJnZ57U5Pak65LGmQVkKTzf3AtKFHc= +k8s.io/apiextensions-apiserver v0.28.3 h1:Od7DEnhXHnHPZG+W9I97/fSQkVpVPQx2diy+2EtmY08= +k8s.io/apiextensions-apiserver v0.28.3/go.mod h1:NE1XJZ4On0hS11aWWJUTNkmVB03j9LM7gJSisbRt8Lc= +k8s.io/apimachinery v0.28.3 h1:B1wYx8txOaCQG0HmYF6nbpU8dg6HvA06x5tEffvOe7A= +k8s.io/apimachinery v0.28.3/go.mod h1:uQTKmIqs+rAYaq+DFaoD2X7pcjLOqbQX2AOiO0nIpb8= +k8s.io/apiserver v0.28.3 h1:8Ov47O1cMyeDzTXz0rwcfIIGAP/dP7L8rWbEljRcg5w= +k8s.io/apiserver v0.28.3/go.mod h1:YIpM+9wngNAv8Ctt0rHG4vQuX/I5rvkEMtZtsxW2rNM= k8s.io/cli-runtime v0.28.2 h1:64meB2fDj10/ThIMEJLO29a1oujSm0GQmKzh1RtA/uk= k8s.io/cli-runtime v0.28.2/go.mod h1:bTpGOvpdsPtDKoyfG4EG041WIyFZLV9qq4rPlkyYfDA= -k8s.io/client-go v0.28.2 h1:DNoYI1vGq0slMBN/SWKMZMw0Rq+0EQW6/AK4v9+3VeY= -k8s.io/client-go v0.28.2/go.mod h1:sMkApowspLuc7omj1FOSUxSoqjr+d5Q0Yc0LOFnYFJY= -k8s.io/component-base v0.28.2 h1:Yc1yU+6AQSlpJZyvehm/NkJBII72rzlEsd6MkBQ+G0E= -k8s.io/component-base v0.28.2/go.mod h1:4IuQPQviQCg3du4si8GpMrhAIegxpsgPngPRR/zWpzc= +k8s.io/client-go v0.28.3 h1:2OqNb72ZuTZPKCl+4gTKvqao0AMOl9f3o2ijbAj3LI4= +k8s.io/client-go v0.28.3/go.mod h1:LTykbBp9gsA7SwqirlCXBWtK0guzfhpoW4qSm7i9dxo= +k8s.io/component-base v0.28.3 h1:rDy68eHKxq/80RiMb2Ld/tbH8uAE75JdCqJyi6lXMzI= +k8s.io/component-base v0.28.3/go.mod h1:fDJ6vpVNSk6cRo5wmDa6eKIG7UlIQkaFmZN2fYgIUD8= k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ= From a01a14a4cda8a117c7672defc5eb68b06fc0fc0c Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sat, 4 Nov 2023 10:08:26 +0300 Subject: [PATCH 15/20] refactor to supply dynamic kubernetes client from main to enable smoother unit testing Signed-off-by: Daniel Kiptoon --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 969ca5fd..f73bfd99 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/layer5io/meshkit/broker/nats" configprovider "github.com/layer5io/meshkit/config/provider" "github.com/layer5io/meshkit/logger" + mesherykube "github.com/layer5io/meshkit/utils/kubernetes" "github.com/layer5io/meshsync/internal/channels" "github.com/layer5io/meshsync/internal/config" "github.com/layer5io/meshsync/meshsync" @@ -39,8 +40,15 @@ func main() { os.Exit(1) } + // Initialize kubeclient + kubeClient, err := mesherykube.New(nil) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + // get configs from meshsync crd if available - crdConfigs, err := config.GetMeshsyncCRDConfigs() + crdConfigs, err := config.GetMeshsyncCRDConfigs(kubeClient.DynamicKubeClient) if err != nil { // no configs found from meshsync CRD log warning From 6d6870285be852dc90ab98c4f8121e302e6f0016 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sat, 4 Nov 2023 10:08:52 +0300 Subject: [PATCH 16/20] add unit tests for crd configuration Signed-off-by: Daniel Kiptoon --- internal/config/crd_config_test.go | 116 +++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 internal/config/crd_config_test.go diff --git a/internal/config/crd_config_test.go b/internal/config/crd_config_test.go new file mode 100644 index 00000000..57609b7d --- /dev/null +++ b/internal/config/crd_config_test.go @@ -0,0 +1,116 @@ +package config + +// test for empty blacklist/whitelist +import ( + "context" + "reflect" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/dynamic/fake" +) + +var ( + Kind string = "MeshSync" + APIVersion string = "meshery.layer5.io/v1alpha1" + URL string = "https://layer5.io" + fakeDyClient *fake.FakeDynamicClient + ctx = context.Background() +) + +func TestWhiteListResources(t *testing.T) { + + // Create an instance of the custom resource. + watchList := corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1apha1", + Kind: "ConfigMap", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "watch-list", + Namespace: "default", + }, + Data: map[string]string{ + "blacklist": "", + "whitelist": "[{\"Resource\":\"namespaces.v1.\",\"Events\":[\"ADDED\",\"DELETE\"]},{\"Resource\":\"replicasets.v1.apps\",\"Events\":[\"ADDED\",\"DELETE\"]},{\"Resource\":\"pods.v1.\",\"Events\":[\"MODIFIED\"]}]", + }, + } + + meshsyncConfig, err := PopulateConfigs(watchList) + + if err != nil { + t.Errorf("Meshsync config not well deserialized got %s", err.Error()) + } + + if len(meshsyncConfig.WhiteList) == 0 { + t.Errorf("WhiteListed resources not correctly deserialized") + } + expectedWhiteList := []ResourceConfig{ + {Resource: "namespaces.v1.", Events: []string{"ADDED", "DELETE"}}, + {Resource: "replicasets.v1.apps", Events: []string{"ADDED", "DELETE"}}, + {Resource: "pods.v1.", Events: []string{"MODIFIED"}}, + } + + if !reflect.DeepEqual(meshsyncConfig.WhiteList, expectedWhiteList) { + t.Error("WhiteListed resources not equal") + } + + // now we assertain the global and local pipelines have been correctly configured + // global pipelines: namespaces + // local pipelines: pods, replicasets + + if len(meshsyncConfig.Pipelines["global"]) != 1 { + t.Error("global pipelines not well configured expected 1") + } + + if len(meshsyncConfig.Pipelines["local"]) != 2 { + t.Error("global pipelines not well configured expected 2") + } +} + +func TestBlackListResources(t *testing.T) { + + // Create an instance of the custom resource. + watchList := corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1apha1", + Kind: "ConfigMap", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "watch-list", + Namespace: "default", + }, + Data: map[string]string{ + "blacklist": "[\"namespaces.v1.\",\"pods.v1.\"]", + "whitelist": "", + }, + } + + meshsyncConfig, err := PopulateConfigs(watchList) + + if err != nil { + t.Errorf("Meshsync config not well deserialized got %s", err.Error()) + } + + if len(meshsyncConfig.BlackList) == 0 { + t.Errorf("WhiteListed resources") + } + + expectedBlackList := []string{"namespaces.v1.", "pods.v1."} + if !reflect.DeepEqual(meshsyncConfig.BlackList, expectedBlackList) { + t.Error("WhiteListed resources not equal") + } + + // now we assertain the global and local pipelines have been correctly configured + // excempted global pipelines: namespaces + // excempted local pipelines: pods, replicasets + + if len(meshsyncConfig.Pipelines["global"]) != 5 { + t.Error("global pipelines not well configured expected 5") + } + + if len(meshsyncConfig.Pipelines["local"]) != 14 { + t.Error("global pipelines not well configured expected 15") + } +} From be975d803101e8ae445ced7d22caf6cf894ad5d0 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sat, 4 Nov 2023 10:09:16 +0300 Subject: [PATCH 17/20] add default events to be tracked Signed-off-by: Daniel Kiptoon --- internal/config/default_config.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/config/default_config.go b/internal/config/default_config.go index d1712a33..df162e0a 100644 --- a/internal/config/default_config.go +++ b/internal/config/default_config.go @@ -144,4 +144,6 @@ var ( SubscribeTo: "meshery.meshsync.request", }, } + + DefaultEvents = []string{"ADD", "UPDATE", "DELETE"} ) From 077384f406ba75224006ecaf133d1b911a8057a8 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Sat, 4 Nov 2023 10:09:41 +0300 Subject: [PATCH 18/20] refactor for easier testing Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 162 +++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 52 deletions(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index 56501e00..4cffcce0 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -5,11 +5,11 @@ import ( "errors" "github.com/layer5io/meshkit/utils" - mesherykube "github.com/layer5io/meshkit/utils/kubernetes" "golang.org/x/exp/slices" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" ) var ( @@ -20,15 +20,7 @@ var ( resource = "meshsyncs" //Name of the Resource ) -func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { - // Initialize kubeclient - kubeClient, err := mesherykube.New(nil) - if err != nil { - return nil, ErrInitConfig(err) - } - // initialize the dynamic kube client - dyClient := kubeClient.DynamicKubeClient - +func GetMeshsyncCRDConfigs(dyClient dynamic.Interface) (*MeshsyncConfig, error) { // initialize the group version resource to access the custom resource gvr := schema.GroupVersionResource{Version: version, Group: group, Resource: resource} @@ -56,7 +48,7 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { if err != nil { return nil, ErrInitConfig(err) } - meshsyncConfig := MeshsyncConfig{} + configMap := corev1.ConfigMap{} err = utils.Unmarshal(string(configStr), &configMap) @@ -64,65 +56,131 @@ func GetMeshsyncCRDConfigs() (*MeshsyncConfig, error) { return nil, ErrInitConfig(err) } + // populate the required configs + meshsyncConfig, err := PopulateConfigs(configMap) + + if err != nil { + return nil, ErrInitConfig(err) + } + return meshsyncConfig, nil +} + +// PopulateConfigs compares the default configs and the whitelist and blacklist +func PopulateConfigs(configMap corev1.ConfigMap) (*MeshsyncConfig, error) { + meshsyncConfig := &MeshsyncConfig{} + if _, ok := configMap.Data["blacklist"]; ok { - err = utils.Unmarshal(configMap.Data["blacklist"], &meshsyncConfig.BlackList) - if err != nil { - return nil, ErrInitConfig(err) + if len(configMap.Data["blacklist"]) > 0 { + err := utils.Unmarshal(configMap.Data["blacklist"], &meshsyncConfig.BlackList) + if err != nil { + return nil, ErrInitConfig(err) + } } } if _, ok := configMap.Data["whitelist"]; ok { - err = utils.Unmarshal(configMap.Data["whitelist"], &meshsyncConfig.WhiteList) - if err != nil { - return nil, ErrInitConfig(err) + if len(configMap.Data["whitelist"]) > 0 { + err := utils.Unmarshal(configMap.Data["whitelist"], &meshsyncConfig.WhiteList) + if err != nil { + return nil, ErrInitConfig(err) + } } } + // ensure that atleast one of whitelist or blacklist has been supplied + if len(meshsyncConfig.BlackList) == 0 && len(meshsyncConfig.WhiteList) == 0 { + return nil, ErrInitConfig(errors.New("Both whitelisted and blacklisted resources missing")) + } + // Handle global resources globalPipelines := make(PipelineConfigs, 0) - for _, v := range Pipelines[GlobalResourceKey] { - if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { - config := meshsyncConfig.WhiteList[idx] - v.Events = config.Events - globalPipelines = append(globalPipelines, v) + localPipelines := make(PipelineConfigs, 0) + + if len(meshsyncConfig.WhiteList) != 0 { + for _, v := range Pipelines[GlobalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + globalPipelines = append(globalPipelines, v) + } + } + if len(globalPipelines) > 0 { + meshsyncConfig.Pipelines = map[string]PipelineConfigs{} + meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines } - } - if len(globalPipelines) > 0 { - meshsyncConfig.Pipelines = map[string]PipelineConfigs{} - meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines - } - // Handle local resources - localPipelines := make(PipelineConfigs, 0) - for _, v := range Pipelines[LocalResourceKey] { - if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { - config := meshsyncConfig.WhiteList[idx] - v.Events = config.Events - localPipelines = append(localPipelines, v) + // Handle local resources + for _, v := range Pipelines[LocalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + localPipelines = append(localPipelines, v) + } } - } - if len(localPipelines) > 0 { - if meshsyncConfig.Pipelines == nil { - meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + if len(localPipelines) > 0 { + if meshsyncConfig.Pipelines == nil { + meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + } + meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines } - meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines - } - // Handle listeners - listerners := make(ListenerConfigs, 0) - for _, v := range Listeners { - if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { - config := meshsyncConfig.WhiteList[idx] - v.Events = config.Events - listerners = append(listerners, v) + // Handle listeners + listerners := make(ListenerConfigs, 0) + for _, v := range Listeners { + if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { + config := meshsyncConfig.WhiteList[idx] + v.Events = config.Events + listerners = append(listerners, v) + } } - } - if len(listerners) > 0 { - meshsyncConfig.Listeners = make(map[string]ListenerConfig) - meshsyncConfig.Listeners = Listeners + if len(listerners) > 0 { + meshsyncConfig.Listeners = make(map[string]ListenerConfig) + meshsyncConfig.Listeners = Listeners + } + } else if len(meshsyncConfig.BlackList) != 0 { + + for _, v := range Pipelines[GlobalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx == -1 { + v.Events = DefaultEvents + globalPipelines = append(globalPipelines, v) + } + } + if len(globalPipelines) > 0 { + meshsyncConfig.Pipelines = map[string]PipelineConfigs{} + meshsyncConfig.Pipelines[GlobalResourceKey] = globalPipelines + } + + // Handle local resources + for _, v := range Pipelines[LocalResourceKey] { + if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx == -1 { + v.Events = DefaultEvents + localPipelines = append(localPipelines, v) + } + } + + if len(localPipelines) > 0 { + if meshsyncConfig.Pipelines == nil { + meshsyncConfig.Pipelines = make(map[string]PipelineConfigs) + } + meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines + } + + // Handle listeners + listerners := make(ListenerConfigs, 0) + for _, v := range Listeners { + if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx != -1 { + v.Events = DefaultEvents + listerners = append(listerners, v) + } + } + + if len(listerners) > 0 { + meshsyncConfig.Listeners = make(map[string]ListenerConfig) + meshsyncConfig.Listeners = Listeners + } } - return &meshsyncConfig, nil + return meshsyncConfig, nil } From 99d89b297b1c5ff12e41d085ea3aa5faffe17296 Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Tue, 7 Nov 2023 22:13:13 +0300 Subject: [PATCH 19/20] ensure either whitelist or blacklisted resources allowed and remove listener configuration from whiteliest and blacklist resources Signed-off-by: Daniel Kiptoon --- internal/config/crd_config.go | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/internal/config/crd_config.go b/internal/config/crd_config.go index 4cffcce0..076957f7 100644 --- a/internal/config/crd_config.go +++ b/internal/config/crd_config.go @@ -92,6 +92,11 @@ func PopulateConfigs(configMap corev1.ConfigMap) (*MeshsyncConfig, error) { return nil, ErrInitConfig(errors.New("Both whitelisted and blacklisted resources missing")) } + // ensure that only one of whitelist or blacklist has been supplied + if len(meshsyncConfig.BlackList) != 0 && len(meshsyncConfig.WhiteList) != 0 { + return nil, ErrInitConfig(errors.New("Both whitelisted and blacklisted resources not currently supported")) + } + // Handle global resources globalPipelines := make(PipelineConfigs, 0) localPipelines := make(PipelineConfigs, 0) @@ -125,21 +130,7 @@ func PopulateConfigs(configMap corev1.ConfigMap) (*MeshsyncConfig, error) { meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines } - // Handle listeners - listerners := make(ListenerConfigs, 0) - for _, v := range Listeners { - if idx := slices.IndexFunc(meshsyncConfig.WhiteList, func(c ResourceConfig) bool { return c.Resource == v.Name }); idx != -1 { - config := meshsyncConfig.WhiteList[idx] - v.Events = config.Events - listerners = append(listerners, v) - } - } - - if len(listerners) > 0 { - meshsyncConfig.Listeners = make(map[string]ListenerConfig) - meshsyncConfig.Listeners = Listeners - } - } else if len(meshsyncConfig.BlackList) != 0 { + } else { for _, v := range Pipelines[GlobalResourceKey] { if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx == -1 { @@ -166,20 +157,6 @@ func PopulateConfigs(configMap corev1.ConfigMap) (*MeshsyncConfig, error) { } meshsyncConfig.Pipelines[LocalResourceKey] = localPipelines } - - // Handle listeners - listerners := make(ListenerConfigs, 0) - for _, v := range Listeners { - if idx := slices.IndexFunc(meshsyncConfig.BlackList, func(c string) bool { return c == v.Name }); idx != -1 { - v.Events = DefaultEvents - listerners = append(listerners, v) - } - } - - if len(listerners) > 0 { - meshsyncConfig.Listeners = make(map[string]ListenerConfig) - meshsyncConfig.Listeners = Listeners - } } return meshsyncConfig, nil From 988de5bd7357d0912f2f5c70956a0f3ee874f25c Mon Sep 17 00:00:00 2001 From: Daniel Kiptoon Date: Tue, 7 Nov 2023 22:28:17 +0300 Subject: [PATCH 20/20] update packages to get dynamic kubernetes go client Signed-off-by: Daniel Kiptoon --- go.mod | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index ccce62d2..12418e85 100644 --- a/go.mod +++ b/go.mod @@ -15,11 +15,12 @@ require ( github.com/layer5io/meshkit v0.6.75 github.com/myntra/pipeline v0.0.0-20180618182531-2babf4864ce8 github.com/spf13/viper v1.17.0 + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/net v0.17.0 gorm.io/gorm v1.25.5 - k8s.io/api v0.28.2 - k8s.io/apimachinery v0.28.2 - k8s.io/client-go v0.28.2 + k8s.io/api v0.28.3 + k8s.io/apimachinery v0.28.3 + k8s.io/client-go v0.28.3 k8s.io/kubectl v0.28.2 ) @@ -145,7 +146,6 @@ require ( go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.14.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/oauth2 v0.12.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.13.0 // indirect