From 1196aa26acd3df584809b18b2d8adc5531a734f9 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Wed, 21 May 2025 15:27:39 +0530 Subject: [PATCH 01/35] SecretFieldClusterId to CmFieldClusterId --- common-lib/informer/bean.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index 38aed85af..ec8495d40 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -21,8 +21,8 @@ const ( ClusterActionAdd = "add" ClusterActionUpdate = "update" ClusterActionDelete = "delete" - SecretFieldAction = "action" - SecretFieldClusterId = "cluster_id" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" ) const ( From 17673c372d53b068c6b81619be1917b02b4bce73 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 13:32:13 +0530 Subject: [PATCH 02/35] replace secret informer with field selector to configmap informer with label selector --- kubelink/pkg/k8sInformer/K8sInformer.go | 35 +++++++++---------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index 67ff10b3f..0e6f0e22d 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -192,21 +192,18 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { if clusterInfo.ClusterName == commonBean.DEFAULT_CLUSTER { impl.logger.Debugw("starting informer, reading new cluster request for default cluster") labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - //kubectl get secret --field-selector type==cluster.request/modify --all-namespaces - opts.FieldSelector = "type==cluster.request/modify" + opts.LabelSelector = "type==cluster.request/modify" }) informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clusterClient, 15*time.Minute, labelOptions) stopper := make(chan struct{}) - secretInformer := informerFactory.Core().V1().Secrets() - _, err = secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + cmInformer := informerFactory.Core().V1().ConfigMaps() + _, err = cmInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { startTime := time.Now() - impl.logger.Debugw("CLUSTER_ADD_INFORMER: cluster secret add event received", "obj", obj, "time", time.Now()) - if secretObject, ok := obj.(*coreV1.Secret); ok { - if secretObject.Type != informerBean.ClusterModifyEventSecretType { - return - } - data := secretObject.Data + impl.logger.Debugw("CLUSTER_ADD_INFORMER: cluster cm add event received", "obj", obj, "time", time.Now()) + if cmObject, ok := obj.(*coreV1.ConfigMap); ok { + // todo check if an extra check is req or not for checking label of cm , if equals + data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) var idInt int @@ -234,12 +231,9 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { }, UpdateFunc: func(oldObj, newObj interface{}) { startTime := time.Now() - impl.logger.Debugw("CLUSTER_UPDATE_INFORMER: cluster secret update event received", "oldObj", oldObj, "newObj", newObj, "time", time.Now()) - if secretObject, ok := newObj.(*coreV1.Secret); ok { - if secretObject.Type != informerBean.ClusterModifyEventSecretType { - return - } - data := secretObject.Data + impl.logger.Debugw("CLUSTER_UPDATE_INFORMER: cluster cm update event received", "oldObj", oldObj, "newObj", newObj, "time", time.Now()) + if cmObject, ok := newObj.(*coreV1.ConfigMap); ok { + data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) var idInt int @@ -264,12 +258,9 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { }, DeleteFunc: func(obj interface{}) { startTime := time.Now() - impl.logger.Debugw("CLUSTER_DELETE_INFORMER: secret delete event received", "obj", obj, "time", time.Now()) - if secretObject, ok := obj.(*coreV1.Secret); ok { - if secretObject.Type != informerBean.ClusterModifyEventSecretType { - return - } - data := secretObject.Data + impl.logger.Debugw("CLUSTER_DELETE_INFORMER: cm delete event received", "obj", obj, "time", time.Now()) + if cmObject, ok := obj.(*coreV1.ConfigMap); ok { + data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) idInt, err := strconv.Atoi(id) From 405943c16b875c5846b1654cbfd49b2e8286e6e0 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 14:10:40 +0530 Subject: [PATCH 03/35] CreateConfigMapObject --- common-lib/utils/k8s/K8sUtil.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index 49cdd3a3e..1b1287076 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -112,6 +112,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) @@ -406,6 +407,26 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: configMapName, + }, + } + if labels != nil && len(labels) > 0 { + configMap.ObjectMeta.Labels = labels + } + if annotations != nil && len(annotations) > 0 { + configMap.ObjectMeta.Annotations = annotations + } + if data != nil && len(data) > 0 { + configMap.Data = data + } + + return impl.CreateConfigMap(namespace, configMap, client) +} + func (impl *K8sServiceImpl) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) { cm, err := client.ConfigMaps(namespace).Update(context.Background(), cm, metav1.UpdateOptions{}) if err != nil { From d504346d3884e4d696c8d40680964cf743b10bee Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 14:30:51 +0530 Subject: [PATCH 04/35] ClusterModifyEventSecretTypeKey --- common-lib/informer/bean.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index ec8495d40..11b9b785d 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -17,12 +17,13 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - CmFieldAction = "action" - CmFieldClusterId = "cluster_id" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" ) const ( From 18ca4a361f4668822f13e8eb66dd4d15f8dbc067 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 16:15:15 +0530 Subject: [PATCH 05/35] DeleteConfigMap k8s util func --- common-lib/utils/k8s/K8sUtil.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index 1b1287076..16ec0fbc5 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -113,6 +113,7 @@ type K8sService interface { UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) @@ -427,6 +428,15 @@ func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[str return impl.CreateConfigMap(namespace, configMap, client) } +func (impl *K8sServiceImpl) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error { + err := client.ConfigMaps(namespace).Delete(context.Background(), name, metav1.DeleteOptions{}) + if err != nil { + impl.logger.Errorw("error in deleting cm", "namespace", namespace, "err", err) + return err + } + return nil +} + func (impl *K8sServiceImpl) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) { cm, err := client.ConfigMaps(namespace).Update(context.Background(), cm, metav1.UpdateOptions{}) if err != nil { From 0edc8f8d7419f9fb8b11388c3a06e75f5ff11afb Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 18:46:47 +0530 Subject: [PATCH 06/35] correct label selector --- kubelink/pkg/k8sInformer/K8sInformer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index 0e6f0e22d..34cd798ee 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -192,7 +192,7 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { if clusterInfo.ClusterName == commonBean.DEFAULT_CLUSTER { impl.logger.Debugw("starting informer, reading new cluster request for default cluster") labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = "type==cluster.request/modify" + opts.LabelSelector = "type=cluster.request/modify" }) informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clusterClient, 15*time.Minute, labelOptions) stopper := make(chan struct{}) From 28fd5f0984457c24b12f3570ad6460eaef5a87f7 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 19:31:23 +0530 Subject: [PATCH 07/35] add extra validation in add , update and delete func if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { return } --- kubelink/pkg/k8sInformer/K8sInformer.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index 34cd798ee..15f7d68fd 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -202,7 +202,9 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_ADD_INFORMER: cluster cm add event received", "obj", obj, "time", time.Now()) if cmObject, ok := obj.(*coreV1.ConfigMap); ok { - // todo check if an extra check is req or not for checking label of cm , if equals + if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + return + } data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) @@ -233,6 +235,9 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_UPDATE_INFORMER: cluster cm update event received", "oldObj", oldObj, "newObj", newObj, "time", time.Now()) if cmObject, ok := newObj.(*coreV1.ConfigMap); ok { + if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + return + } data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) @@ -260,6 +265,9 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_DELETE_INFORMER: cm delete event received", "obj", obj, "time", time.Now()) if cmObject, ok := obj.(*coreV1.ConfigMap); ok { + if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + return + } data := cmObject.Data action := data["action"] id := string(data["cluster_id"]) From dce8d6878100f74c208ee6c27d2fe7847d9b21ae Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 22 May 2025 19:35:11 +0530 Subject: [PATCH 08/35] cm informer instead of secret informer for cluster --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 +- kubewatch/pkg/informer/bean/bean.go | 1 + kubewatch/pkg/informer/cluster/helper.go | 20 ++++---- kubewatch/pkg/informer/cluster/informer.go | 46 +++++++++---------- kubewatch/pkg/middleware/instrument.go | 2 +- kubewatch/pkg/resource/commonClient.go | 2 +- kubewatch/pkg/resource/informerFactory.go | 2 +- kubewatch/pkg/resource/secret/handler.go | 28 +++++------ .../devtron-labs/common-lib/informer/bean.go | 13 +++--- .../common-lib/utils/k8s/K8sUtil.go | 31 +++++++++++++ kubewatch/vendor/modules.txt | 4 +- 12 files changed, 94 insertions(+), 61 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 7d65a860c..8a25d6a28 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250519113737-b060cea3a495 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index 6692a5504..cee486a78 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250519113737-b060cea3a495 h1:aEJw3HPicUqpwBIZos5lsiNIuL1c4w5bqadkHyj+dAA= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250519113737-b060cea3a495/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 h1:vVQeIaO2OT0xXTklcXJeQFhDGhs2TimBHYnlBr7/GNY= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/pkg/informer/bean/bean.go b/kubewatch/pkg/informer/bean/bean.go index 43c5ead05..6a4e9aedc 100644 --- a/kubewatch/pkg/informer/bean/bean.go +++ b/kubewatch/pkg/informer/bean/bean.go @@ -27,6 +27,7 @@ type ClusterInfo struct { const ( ClusterModifyEventFieldSelector = "type==cluster.request/modify" + ClusterModifyEventLabelSelector = "type=cluster.request/modify" InformerAlreadyExistMessage = "INFORMER_ALREADY_EXIST" ExitCode143Error = "Error (exit code 143)" diff --git a/kubewatch/pkg/informer/cluster/helper.go b/kubewatch/pkg/informer/cluster/helper.go index 23883624e..e38dac66d 100644 --- a/kubewatch/pkg/informer/cluster/helper.go +++ b/kubewatch/pkg/informer/cluster/helper.go @@ -113,13 +113,13 @@ func (impl *InformerImpl) startInformerForCluster(clusterInfo *repository.Cluste return nil } -func (impl *InformerImpl) handleClusterChangeEvent(secretObject *coreV1.Secret) error { - if secretObject.Type != informerBean.ClusterModifyEventSecretType { +func (impl *InformerImpl) handleClusterChangeEvent(cmObject *coreV1.ConfigMap) error { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { return nil } - data := secretObject.Data - action := data[informerBean.SecretFieldAction] - id := string(data[informerBean.SecretFieldClusterId]) + data := cmObject.Data + action := data[informerBean.CmFieldAction] + id := string(data[informerBean.CmFieldClusterId]) clusterId, convErr := strconv.Atoi(id) if convErr != nil { impl.logger.Errorw("error in converting cluster id to int", "clusterId", id, "err", convErr) @@ -139,13 +139,13 @@ func (impl *InformerImpl) handleClusterChangeEvent(secretObject *coreV1.Secret) return nil } -func (impl *InformerImpl) handleClusterDeleteEvent(secretObject *coreV1.Secret) error { - if secretObject.Type != informerBean.ClusterModifyEventSecretType { +func (impl *InformerImpl) handleClusterDeleteEvent(cmObject *coreV1.ConfigMap) error { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { return nil } - data := secretObject.Data - action := data[informerBean.SecretFieldAction] - id := string(data[informerBean.SecretFieldClusterId]) + data := cmObject.Data + action := data[informerBean.CmFieldAction] + id := string(data[informerBean.CmFieldClusterId]) clusterId, err := strconv.Atoi(id) if err != nil { return err diff --git a/kubewatch/pkg/informer/cluster/informer.go b/kubewatch/pkg/informer/cluster/informer.go index 8e7f0439a..cf6c764c1 100644 --- a/kubewatch/pkg/informer/cluster/informer.go +++ b/kubewatch/pkg/informer/cluster/informer.go @@ -95,50 +95,50 @@ func (impl *InformerImpl) StartDevtronClusterWatcher() error { } impl.logger.Debug("starting informer, reading new cluster request for default cluster", "clusterId", clusterInfo.Id, "clusterName", clusterInfo.ClusterName) labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.FieldSelector = informerBean.ClusterModifyEventFieldSelector + opts.LabelSelector = informerBean.ClusterModifyEventLabelSelector }) - // addFunc is called when a new secret is created - addFunc := func(secretObject *coreV1.Secret) { - if err := impl.handleClusterChangeEvent(secretObject); err != nil { - impl.logger.Errorw("error in handling cluster add event", "err", err, "secretObject", secretObject) + // addFunc is called when a new cm is created + addFunc := func(cmObject *coreV1.ConfigMap) { + if err := impl.handleClusterChangeEvent(cmObject); err != nil { + impl.logger.Errorw("error in handling cluster add event", "cmObject", cmObject, "err", err) } } - // updateFunc is called when an existing secret is updated - updateFunc := func(oldSecretObject, newSecretObject *coreV1.Secret) { - if err := impl.handleClusterChangeEvent(newSecretObject); err != nil { - impl.logger.Errorw("error in handling cluster update event", "err", err, "newSecretObject", newSecretObject) + // updateFunc is called when an existing cm is updated + updateFunc := func(oldCmObject, newCmObject *coreV1.ConfigMap) { + if err := impl.handleClusterChangeEvent(newCmObject); err != nil { + impl.logger.Errorw("error in handling cluster update event", "newCmObject", newCmObject, "err", err) } } - // deleteFunc is called when an existing secret is deleted - deleteFunc := func(secretObject *coreV1.Secret) { - if err := impl.handleClusterDeleteEvent(secretObject); err != nil { - impl.logger.Errorw("error in handling cluster delete event", "err", err, "secretObject", secretObject) + // deleteFunc is called when an existing cm is deleted + deleteFunc := func(cmObject *coreV1.ConfigMap) { + if err := impl.handleClusterDeleteEvent(cmObject); err != nil { + impl.logger.Errorw("error in handling cluster delete event", "cmObject", cmObject, "err", err) } } - informerFactory := impl.informerClient.GetSecretInformerFactory() + informerFactory := impl.informerClient.GetConfigMapInformerFactory() restConfig := impl.k8sUtil.GetK8sConfigForCluster(clusterInfo) - eventHandler := resourceBean.NewEventHandlers[coreV1.Secret](). + eventHandler := resourceBean.NewEventHandlers[coreV1.ConfigMap](). AddFuncHandler(addFunc). UpdateFuncHandler(updateFunc). DeleteFuncHandler(deleteFunc) clusterLabels := informerBean.NewClusterLabels(clusterInfo.ClusterName, clusterInfo.Id) - secretFactory, err := informerFactory.GetSharedInformerFactory(restConfig, clusterLabels, eventHandler, labelOptions) + cmFactory, err := informerFactory.GetSharedInformerFactory(restConfig, clusterLabels, eventHandler, labelOptions) if err != nil { - impl.logger.Errorw("error in registering default cluster secret informer", "err", err) - middleware.IncUnregisteredInformers(clusterLabels, middleware.DEFAULT_CLUSTER_SECRET_INFORMER) + impl.logger.Errorw("error in registering default cluster cm informer", "err", err) + middleware.IncUnregisteredInformers(clusterLabels, middleware.DEFAULT_CLUSTER_CM_INFORMER) return err } - stopChannel, err := impl.getStopChannel(secretFactory, clusterLabels) + stopChannel, err := impl.getStopChannel(cmFactory, clusterLabels) if err != nil { return err } - secretFactory.Start(stopChannel) + cmFactory.Start(stopChannel) // waiting for cache sync - synced := secretFactory.WaitForCacheSync(stopChannel) + synced := cmFactory.WaitForCacheSync(stopChannel) for v, ok := range synced { if !ok { - impl.logger.Errorw("failed to sync secret informer for default cluster", "value", v) - return errors.New("failed to sync secret informer for default cluster") + impl.logger.Errorw("failed to sync cm informer for default cluster", "value", v) + return errors.New("failed to sync cm informer for default cluster") } } impl.logger.Infow("informer started for cluster watcher", "clusterId", clusterInfo.Id, "clusterName", clusterInfo.ClusterName) diff --git a/kubewatch/pkg/middleware/instrument.go b/kubewatch/pkg/middleware/instrument.go index 65c874c88..5c49c5ebb 100644 --- a/kubewatch/pkg/middleware/instrument.go +++ b/kubewatch/pkg/middleware/instrument.go @@ -63,7 +63,7 @@ const ( CI_STAGE_ARGO_WORKFLOW_INFORMER InformerMetrics = "CIStageArgoWorkflow" CD_STAGE_ARGO_WORLFLOW_INFORMER InformerMetrics = "CDStageArgoWorkflow" ARGO_CD_INFORMER InformerMetrics = "ArgoCD" - DEFAULT_CLUSTER_SECRET_INFORMER InformerMetrics = "DefaultClusterSecret" + DEFAULT_CLUSTER_CM_INFORMER InformerMetrics = "DefaultClusterCm" SYSTEM_EXECUTOR_INFORMER InformerMetrics = "SystemExecutor" ) diff --git a/kubewatch/pkg/resource/commonClient.go b/kubewatch/pkg/resource/commonClient.go index efb3f62fc..066cbea98 100644 --- a/kubewatch/pkg/resource/commonClient.go +++ b/kubewatch/pkg/resource/commonClient.go @@ -27,7 +27,7 @@ import ( type InformerClient interface { GetSharedInformerClient(sharedInformerType bean.SharedInformerType) SharedInformer - GetSecretInformerFactory() InformerFactory[coreV1.Secret] + GetConfigMapInformerFactory() InformerFactory[coreV1.ConfigMap] GetPodInformerFactory() InformerFactory[coreV1.Pod] } diff --git a/kubewatch/pkg/resource/informerFactory.go b/kubewatch/pkg/resource/informerFactory.go index f74b270eb..fc05810ae 100644 --- a/kubewatch/pkg/resource/informerFactory.go +++ b/kubewatch/pkg/resource/informerFactory.go @@ -31,7 +31,7 @@ type InformerFactory[T any] interface { eventHandlers *bean.EventHandlers[T], options ...kubeinformers.SharedInformerOption) (kubeinformers.SharedInformerFactory, error) } -func (impl *InformerClientImpl) GetSecretInformerFactory() InformerFactory[coreV1.Secret] { +func (impl *InformerClientImpl) GetConfigMapInformerFactory() InformerFactory[coreV1.ConfigMap] { return secret.NewInformerFactoryImpl(impl.logger, impl.k8sUtil) } diff --git a/kubewatch/pkg/resource/secret/handler.go b/kubewatch/pkg/resource/secret/handler.go index cbb070745..0332c170a 100644 --- a/kubewatch/pkg/resource/secret/handler.go +++ b/kubewatch/pkg/resource/secret/handler.go @@ -43,38 +43,38 @@ func NewInformerFactoryImpl(logger *zap.SugaredLogger, } func (impl *InformerFactoryImpl) GetSharedInformerFactory(config *rest.Config, clusterLabels *bean2.ClusterLabels, - eventHandlers *bean.EventHandlers[coreV1.Secret], options ...kubeinformers.SharedInformerOption) (kubeinformers.SharedInformerFactory, error) { + eventHandlers *bean.EventHandlers[coreV1.ConfigMap], options ...kubeinformers.SharedInformerOption) (kubeinformers.SharedInformerFactory, error) { clusterClient, k8sErr := impl.k8sUtil.GetK8sClientForConfig(config) if k8sErr != nil { middleware.IncUnreachableCluster(clusterLabels) return nil, k8sErr } informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clusterClient, 15*time.Minute, options...) - secretInformer := informerFactory.Core().V1().Secrets() - _, eventErr := secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + cmInformer := informerFactory.Core().V1().ConfigMaps() + _, eventErr := cmInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(newObj interface{}) { - impl.logger.Debugw("event received in cluster secret add informer", "time", time.Now()) - if secretObject, ok := newObj.(*coreV1.Secret); ok { - eventHandlers.AddFunc(secretObject) + impl.logger.Debugw("event received in cluster cm add informer", "time", time.Now()) + if cmObject, ok := newObj.(*coreV1.ConfigMap); ok { + eventHandlers.AddFunc(cmObject) } }, UpdateFunc: func(oldObj, newObj interface{}) { - impl.logger.Debugw("event received in cluster secret update informer", "time", time.Now()) - oldSecretObject, validOld := oldObj.(*coreV1.Secret) - newSecretObject, validNew := newObj.(*coreV1.Secret) + impl.logger.Debugw("event received in cluster cm update informer", "time", time.Now()) + oldCmObject, validOld := oldObj.(*coreV1.ConfigMap) + newCmObject, validNew := newObj.(*coreV1.ConfigMap) if validOld && validNew { - eventHandlers.UpdateFunc(oldSecretObject, newSecretObject) + eventHandlers.UpdateFunc(oldCmObject, newCmObject) } }, DeleteFunc: func(obj interface{}) { - impl.logger.Debugw("event received in secret delete informer", "time", time.Now()) - if secretObject, ok := obj.(*coreV1.Secret); ok { - eventHandlers.DeleteFunc(secretObject) + impl.logger.Debugw("event received in cm delete informer", "time", time.Now()) + if cmObject, ok := obj.(*coreV1.ConfigMap); ok { + eventHandlers.DeleteFunc(cmObject) } }, }) if eventErr != nil { - impl.logger.Errorw("error in adding event handler for cluster secret informer", "err", eventErr) + impl.logger.Errorw("error in adding event handler for cluster cm informer", "err", eventErr) return informerFactory, eventErr } return informerFactory, nil diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 38aed85af..11b9b785d 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -17,12 +17,13 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - SecretFieldAction = "action" - SecretFieldClusterId = "cluster_id" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" ) const ( diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 49cdd3a3e..16ec0fbc5 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -112,6 +112,8 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) @@ -406,6 +408,35 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: configMapName, + }, + } + if labels != nil && len(labels) > 0 { + configMap.ObjectMeta.Labels = labels + } + if annotations != nil && len(annotations) > 0 { + configMap.ObjectMeta.Annotations = annotations + } + if data != nil && len(data) > 0 { + configMap.Data = data + } + + return impl.CreateConfigMap(namespace, configMap, client) +} + +func (impl *K8sServiceImpl) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error { + err := client.ConfigMaps(namespace).Delete(context.Background(), name, metav1.DeleteOptions{}) + if err != nil { + impl.logger.Errorw("error in deleting cm", "namespace", namespace, "err", err) + return err + } + return nil +} + func (impl *K8sServiceImpl) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) { cm, err := client.ConfigMaps(namespace).Update(context.Background(), cm, metav1.UpdateOptions{}) if err != nil { diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 1a4c3f1fb..8a83dbcaa 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250519113737-b060cea3a495 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250519113737-b060cea3a495 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 From 9a252fc137f7fa7265b235ec0209c4e72323d2e2 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 15:23:49 +0530 Subject: [PATCH 09/35] bump common lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 ++-- kubewatch/vendor/modules.txt | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 8a25d6a28..49be5b108 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index cee486a78..a48d75c32 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 h1:vVQeIaO2OT0xXTklcXJeQFhDGhs2TimBHYnlBr7/GNY= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 h1:XQPsCx9IV2b/tPimCrLBha4QgOBhTs+Ad7L1DeYo72Y= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 8a83dbcaa..81e8859b4 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250522104515-18ca4a361f46 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 From 1b6fc973cbb66f34d1b81437c59369e326ce6d7c Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 15:38:52 +0530 Subject: [PATCH 10/35] bump common lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 +-- .../devtron-labs/common-lib/informer/bean.go | 13 ++++---- .../common-lib/utils/k8s/K8sUtil.go | 31 +++++++++++++++++++ kubelink/vendor/modules.txt | 4 +-- 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index cb1aeb825..7a917c1b9 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250521130159-a5d26dc2ab8d + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index b2840ee85..1d81b9e2a 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250521130159-a5d26dc2ab8d h1:q0a5FgU3HowrydPq+BjIMHCF0izHjl1oPc71JDLsv+8= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250521130159-a5d26dc2ab8d/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 h1:rEOoEOPSc8tirrJPNzGpU7f3SQ1Nmu8S5CFAxeAOQWY= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 38aed85af..11b9b785d 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -17,12 +17,13 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - SecretFieldAction = "action" - SecretFieldClusterId = "cluster_id" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" ) const ( diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 49cdd3a3e..16ec0fbc5 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -112,6 +112,8 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) @@ -406,6 +408,35 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: configMapName, + }, + } + if labels != nil && len(labels) > 0 { + configMap.ObjectMeta.Labels = labels + } + if annotations != nil && len(annotations) > 0 { + configMap.ObjectMeta.Annotations = annotations + } + if data != nil && len(data) > 0 { + configMap.Data = data + } + + return impl.CreateConfigMap(namespace, configMap, client) +} + +func (impl *K8sServiceImpl) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error { + err := client.ConfigMaps(namespace).Delete(context.Background(), name, metav1.DeleteOptions{}) + if err != nil { + impl.logger.Errorw("error in deleting cm", "namespace", namespace, "err", err) + return err + } + return nil +} + func (impl *K8sServiceImpl) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) { cm, err := client.ConfigMaps(namespace).Update(context.Background(), cm, metav1.UpdateOptions{}) if err != nil { diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index d381ea6ca..f899c9c5c 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250521130159-a5d26dc2ab8d +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1361,7 +1361,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250521130159-a5d26dc2ab8d +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From df280dc551ff3272856668b2c7d6ea054c3ffef7 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 15:54:24 +0530 Subject: [PATCH 11/35] bump common lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 +- .../common-lib/utils/k8s/K8sUtil.go | 676 +++--------------- kubewatch/vendor/modules.txt | 4 +- 4 files changed, 87 insertions(+), 599 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 49be5b108..1640cd7d3 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index a48d75c32..7aff391ce 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 h1:XQPsCx9IV2b/tPimCrLBha4QgOBhTs+Ad7L1DeYo72Y= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 h1:PtkkGF5gADOACsKilvsXyl1P3Ea6nI8s2kTd34/uEmg= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 16ec0fbc5..0f313f2c7 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -20,16 +20,11 @@ import ( "context" "encoding/json" error2 "errors" - "flag" "fmt" "github.com/devtron-labs/common-lib/utils" http2 "github.com/devtron-labs/common-lib/utils/http" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "io" - v13 "k8s.io/api/policy/v1" - v1beta12 "k8s.io/api/policy/v1beta1" - "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/dynamic" "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/metrics/pkg/apis/metrics/v1beta1" @@ -38,8 +33,6 @@ import ( "log" "net/http" "net/url" - "os/user" - "path/filepath" "strconv" "strings" "time" @@ -48,8 +41,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/version" - "go.uber.org/zap" - v14 "k8s.io/api/apps/v1" batchV1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" @@ -60,159 +51,19 @@ import ( "k8s.io/client-go/kubernetes" v12 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/yaml" ) -type K8sServiceImpl struct { - logger *zap.SugaredLogger - runTimeConfig *RuntimeConfig - httpClientConfig *CustomK8sHttpTransportConfig - kubeconfig *string -} - -type K8sService interface { - GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request - GetMetricsClientSet(restConfig *rest.Config, k8sHttpClient *http.Client) (*metrics.Clientset, error) - GetNmByName(ctx context.Context, metricsClientSet *metrics.Clientset, name string) (*v1beta1.NodeMetrics, error) - GetNmList(ctx context.Context, metricsClientSet *metrics.Clientset) (*v1beta1.NodeMetricsList, error) - GetPodsListForNamespace(ctx context.Context, k8sClientSet *kubernetes.Clientset, namespace string) (*v1.PodList, error) - GetServerVersionFromDiscoveryClient(k8sClientSet *kubernetes.Clientset) (*version.Info, error) - GetServerGroups(k8sClientSet *kubernetes.Clientset) (*metav1.APIGroupList, error) - GetNodeByName(ctx context.Context, k8sClientSet *kubernetes.Clientset, name string) (*v1.Node, error) - GetNodesList(ctx context.Context, k8sClientSet *kubernetes.Clientset) (*v1.NodeList, error) - GetCoreV1ClientByRestConfig(restConfig *rest.Config) (*v12.CoreV1Client, error) - GetCoreV1ClientInCluster() (*v12.CoreV1Client, error) - GetKubeVersion() (*version.Info, error) - ValidateResource(resourceObj map[string]interface{}, gvk schema.GroupVersionKind, validateCallback func(namespace string, group string, kind string, resourceName string) bool) bool - BuildK8sObjectListTableData(manifest *unstructured.UnstructuredList, namespaced bool, gvk schema.GroupVersionKind, includeMetadata bool, validateResourceAccess func(namespace string, group string, kind string, resourceName string) bool) (*ClusterResourceListMap, error) - ValidateForResource(namespace string, resourceRef interface{}, validateCallback func(namespace string, group string, kind string, resourceName string) bool) bool - GetPodByName(namespace string, name string, client *v12.CoreV1Client) (*v1.Pod, error) - GetK8sInClusterRestConfig() (*rest.Config, error) - GetResourceInfoByLabelSelector(ctx context.Context, namespace string, labelSelector string) (*v1.Pod, error) - GetClientByToken(serverUrl string, token map[string]string) (*v12.CoreV1Client, error) - ListNamespaces(client *v12.CoreV1Client) (*v1.NamespaceList, error) - DeleteAndCreateJob(content []byte, namespace string, clusterConfig *ClusterConfig) error - DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig) error - CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job) error - GetLiveZCall(path string, k8sClientSet *kubernetes.Clientset) ([]byte, error) - DiscoveryClientGetLiveZCall(cluster *ClusterConfig) ([]byte, error) - GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config) (*http.Client, *kubernetes.Clientset, error) - GetK8sConfigAndClients(clusterConfig *ClusterConfig) (*rest.Config, *http.Client, *kubernetes.Clientset, error) - GetK8sInClusterConfigAndDynamicClients() (*rest.Config, *http.Client, dynamic.Interface, error) - GetK8sInClusterConfigAndClients() (*rest.Config, *http.Client, *kubernetes.Clientset, error) - DeleteJob(namespace string, name string, clusterConfig *ClusterConfig) error - DeleteSecret(namespace string, name string, client *v12.CoreV1Client) error - UpdateSecret(namespace string, secret *v1.Secret, client *v12.CoreV1Client) (*v1.Secret, error) - CreateSecretData(namespace string, secret *v1.Secret, v1Client *v12.CoreV1Client) (*v1.Secret, error) - CreateSecret(namespace string, data map[string][]byte, secretName string, secretType v1.SecretType, client *v12.CoreV1Client, labels map[string]string, stringData map[string]string) (*v1.Secret, error) - GetSecret(namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) - GetSecretWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) - PatchConfigMapJsonType(namespace string, clusterConfig *ClusterConfig, name string, data interface{}, path string) (*v1.ConfigMap, error) - PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) - UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) - DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error - GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) - GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) - GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) - CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) - UpdateNSLabels(namespace *v1.Namespace, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, err error) - GetK8sDiscoveryClientInCluster() (*discovery.DiscoveryClient, error) - GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error) - GetClientForInCluster() (*v12.CoreV1Client, error) - GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error) - GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error) - GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) - UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) - DeleteResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, name string, forceDelete bool) (*ManifestResponse, error) - GetPodListByLabel(namespace, label string, clientSet *kubernetes.Clientset) ([]v1.Pod, error) - ExtractK8sServerMajorAndMinorVersion(k8sServerVersion *version.Info) (int, int, error) - GetK8sServerVersion(clientSet *kubernetes.Clientset) (*version.Info, error) - DecodeGroupKindversion(data string) (*schema.GroupVersionKind, error) - GetApiResources(restConfig *rest.Config, includeOnlyVerb string) ([]*K8sApiResource, error) - CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) - PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) - GetResourceList(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, asTable bool, listOptions *metav1.ListOptions) (*ResourceListResponse, bool, error) - GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) - GetPodLogs(ctx context.Context, restConfig *rest.Config, name string, namespace string, sinceTime *metav1.Time, tailLines int, sinceSeconds int, follow bool, containerName string, isPrevContainerLogsEnabled bool) (io.ReadCloser, error) - ListEvents(restConfig *rest.Config, namespace string, groupVersionKind schema.GroupVersionKind, ctx context.Context, name string) (*v1.EventList, error) - GetResourceIf(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) - FetchConnectionStatusForCluster(k8sClientSet *kubernetes.Clientset) error - CreateK8sClientSet(restConfig *rest.Config) (*kubernetes.Clientset, error) - CreateOrUpdateSecretByName(client *v12.CoreV1Client, namespace, uniqueSecretName string, secretLabel map[string]string, secretData map[string]string) error - //CreateK8sClientSetWithCustomHttpTransport(restConfig *rest.Config) (*kubernetes.Clientset, error) - - //below functions are exposed for K8sUtilExtended - GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) - OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) - CreateNsWithLabels(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) - CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) - GetGVRForCRD(config *rest.Config, CRDName string) (schema.GroupVersionResource, error) - GetResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string) (*unstructured.Unstructured, error) - PatchResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string, patchType types.PatchType, patchData []byte) (*unstructured.Unstructured, error) - DeleteResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string, forceDelete bool) error -} - -func NewK8sUtil(logger *zap.SugaredLogger, runTimeConfig *RuntimeConfig) (*K8sServiceImpl, error) { - var kubeconfig *string - if runTimeConfig.LocalDevMode { - usr, err := user.Current() - if err != nil { - logger.Errorw("error in NewK8sUtil, failed to get current user", "err", err) - return nil, err - } - kubeconfig = flag.String("kubeconfig-authenticator-xyz", filepath.Join(usr.HomeDir, ".kube", "config"), "(optional) absolute path to the kubeconfig file") - } - - httpClientConfig := NewCustomK8sHttpTransportConfig() - flag.Parse() - return &K8sServiceImpl{logger: logger, runTimeConfig: runTimeConfig, kubeconfig: kubeconfig, httpClientConfig: httpClientConfig}, nil -} - -func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error) { - restConfig, err := impl.GetRestConfigByClusterWithoutCustomTransport(clusterConfig) - if err != nil { - impl.logger.Errorw("error, GetRestConfigByClusterWithoutCustomTransport", "err", err) - return nil, err - } - restConfig, err = impl.OverrideRestConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err) - } - return restConfig, err -} - -func (impl *K8sServiceImpl) GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) { - bearerToken := clusterConfig.BearerToken - var restConfig *rest.Config - var err error - if clusterConfig.Host == commonBean.DefaultClusterUrl && len(bearerToken) == 0 { - restConfig, err = impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for default cluster", "err", err) - return nil, err - } - } else { - restConfig = &rest.Config{Host: clusterConfig.Host, BearerToken: bearerToken} - clusterConfig.PopulateTlsConfigurationsInto(restConfig) +func (impl *K8sServiceImpl) getOpts(opts []K8sServiceOpts) Options { + customOpts := impl.opts + for _, opt := range opts { + customOpts = opt(customOpts) } - return restConfig, nil + return customOpts } -func (impl *K8sServiceImpl) OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err) - return nil, err - } - return restConfig, nil -} - -func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error) { - cfg, err := impl.GetRestConfigByCluster(clusterConfig) +func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { + cfg, err := impl.GetRestConfigByCluster(clusterConfig, opts...) if err != nil { impl.logger.Errorw("error in getting rest config for default cluster", "err", err) return nil, err @@ -220,20 +71,14 @@ func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig) (*v12. return impl.GetCoreV1ClientByRestConfig(cfg) } -func (impl *K8sServiceImpl) GetClientForInCluster() (*v12.CoreV1Client, error) { +func (impl *K8sServiceImpl) GetClientForInCluster(opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { // creates the in-cluster config - config, err := impl.GetK8sInClusterRestConfig() + config, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Errorw("error in getting config", "err", err) return nil, err } - config, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(config) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - // creates the clientset httpClient, err := OverrideK8sHttpClientWithTracer(config) if err != nil { @@ -248,19 +93,13 @@ func (impl *K8sServiceImpl) GetClientForInCluster() (*v12.CoreV1Client, error) { return clientset, err } -func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error) { - cfg, err := impl.GetRestConfigByCluster(clusterConfig) +func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*discovery.DiscoveryClient, error) { + cfg, err := impl.GetRestConfigByCluster(clusterConfig, opts...) if err != nil { impl.logger.Errorw("error in getting rest config for default cluster", "err", err) return nil, err } - cfg, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(cfg) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(cfg) if err != nil { impl.logger.Errorw("error in getting http client for default cluster", "err", err) @@ -274,19 +113,13 @@ func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig) return discoveryClient, err } -func (impl *K8sServiceImpl) GetK8sDiscoveryClientInCluster() (*discovery.DiscoveryClient, error) { - config, err := impl.GetK8sInClusterRestConfig() +func (impl *K8sServiceImpl) GetK8sDiscoveryClientInCluster(opts ...K8sServiceOpts) (*discovery.DiscoveryClient, error) { + config, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Errorw("error in getting config", "err", err) return nil, err } - config, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(config) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(config) if err != nil { impl.logger.Errorw("error in getting http client for default cluster", "err", err) @@ -379,11 +212,6 @@ func (impl *K8sServiceImpl) CreateNsWithLabels(namespace string, labels map[stri } } -func (impl *K8sServiceImpl) deleteNs(namespace string, client *v12.CoreV1Client) error { - err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{}) - return err -} - func (impl *K8sServiceImpl) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) { return impl.GetConfigMapWithCtx(context.Background(), namespace, name, client) } @@ -499,12 +327,6 @@ func (impl *K8sServiceImpl) PatchConfigMapJsonType(namespace string, clusterConf return cm, nil } -type JsonPatchType struct { - Op string `json:"op"` - Path string `json:"path"` - Value interface{} `json:"value"` -} - func (impl *K8sServiceImpl) GetSecret(namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) { return impl.GetSecretWithCtx(context.Background(), namespace, name, client) } @@ -564,8 +386,8 @@ func (impl *K8sServiceImpl) DeleteSecret(namespace string, name string, client * return nil } -func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConfig *ClusterConfig) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConfig *ClusterConfig, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, DeleteJob", "err", err) return err @@ -589,47 +411,6 @@ func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConf return nil } -func (impl *K8sServiceImpl) GetK8sInClusterConfigAndClients() (*rest.Config, *http.Client, *kubernetes.Clientset, error) { - restConfig, err := impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for in cluster", "err", err) - return nil, nil, nil, err - } - - k8sHttpClient, k8sClientSet, err := impl.GetK8sConfigAndClientsByRestConfig(restConfig) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) GetK8sInClusterConfigAndDynamicClients() (*rest.Config, *http.Client, dynamic.Interface, error) { - restConfig, err := impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for in cluster", "err", err) - return nil, nil, nil, err - } - - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, nil, nil, err - } - - k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) - if err != nil { - impl.logger.Errorw("error in getting k8s http client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - dynamicClientSet, err := dynamic.NewForConfigAndClient(restConfig, k8sHttpClient) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, dynamicClientSet, nil -} - func (impl *K8sServiceImpl) GetK8sDynamicClient(restConfig *rest.Config, k8sHttpClient *http.Client) (dynamic.Interface, error) { dynamicClientSet, err := dynamic.NewForConfigAndClient(restConfig, k8sHttpClient) if err != nil { @@ -639,44 +420,8 @@ func (impl *K8sServiceImpl) GetK8sDynamicClient(restConfig *rest.Config, k8sHttp return dynamicClientSet, nil } -func (impl *K8sServiceImpl) GetK8sConfigAndClients(clusterConfig *ClusterConfig) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { - restConfig, err := impl.GetRestConfigByCluster(clusterConfig) - if err != nil { - impl.logger.Errorw("error in getting rest config by cluster", "err", err, "clusterName", clusterConfig.ClusterName) - return nil, nil, nil, err - } - - k8sHttpClient, k8sClientSet, err := impl.GetK8sConfigAndClientsByRestConfig(restConfig) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config", "err", err, "clusterName", clusterConfig.ClusterName) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config) (*http.Client, *kubernetes.Clientset, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, nil, err - } - - k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) - if err != nil { - impl.logger.Errorw("error in getting k8s http client set by rest config", "err", err) - return nil, nil, err - } - k8sClientSet, err := kubernetes.NewForConfigAndClient(restConfig, k8sHttpClient) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config", "err", err) - return nil, nil, err - } - return k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig) ([]byte, error) { - _, _, k8sClientSet, err := impl.GetK8sConfigAndClients(cluster) +func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig, opts ...K8sServiceOpts) ([]byte, error) { + _, _, k8sClientSet, err := impl.GetK8sConfigAndClients(cluster, opts...) if err != nil { impl.logger.Errorw("errir in getting clients and configs", "err", err, "clusterName", cluster.ClusterName) return nil, err @@ -690,6 +435,7 @@ func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig) return response, err } + func (impl *K8sServiceImpl) GetLiveZCall(path string, k8sClientSet *kubernetes.Clientset) ([]byte, error) { response, err := k8sClientSet.Discovery().RESTClient().Get().AbsPath(path).DoRaw(context.Background()) if err != nil { @@ -699,8 +445,8 @@ func (impl *K8sServiceImpl) GetLiveZCall(path string, k8sClientSet *kubernetes.C return response, err } -func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, CreateJob", "err", err) } @@ -727,8 +473,8 @@ func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConf // DeletePod delete pods with label job-name -func (impl *K8sServiceImpl) DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, DeletePod", "err", err) return err @@ -833,25 +579,6 @@ func (impl *K8sServiceImpl) GetResourceInfoByLabelSelector(ctx context.Context, } } -func (impl *K8sServiceImpl) GetK8sInClusterRestConfig() (*rest.Config, error) { - impl.logger.Debug("getting k8s rest config") - if impl.runTimeConfig.LocalDevMode { - restConfig, err := clientcmd.BuildConfigFromFlags("", *impl.kubeconfig) - if err != nil { - impl.logger.Errorw("Error while building config from flags", "error", err) - return nil, err - } - return restConfig, nil - } else { - clusterConfig, err := rest.InClusterConfig() - if err != nil { - impl.logger.Errorw("error in fetch default cluster config", "err", err) - return nil, err - } - return clusterConfig, nil - } -} - func (impl *K8sServiceImpl) GetPodByName(namespace string, name string, client *v12.CoreV1Client) (*v1.Pod, error) { pod, err := client.Pods(namespace).Get(context.Background(), name, metav1.GetOptions{}) if err != nil { @@ -1057,29 +784,6 @@ func (impl *K8sServiceImpl) ValidateForResource(namespace string, resourceRef in return false } -func (impl *K8sServiceImpl) getEventKindHeader() ([]string, map[int]string) { - headers := []string{"type", "message", "namespace", "involved object", "source", "count", "age", "last seen"} - columnIndexes := make(map[int]string) - columnIndexes[0] = "last seen" - columnIndexes[1] = "type" - columnIndexes[2] = "namespace" - columnIndexes[3] = "involved object" - columnIndexes[5] = "source" - columnIndexes[6] = "message" - columnIndexes[7] = "age" - columnIndexes[8] = "count" - return headers, columnIndexes -} - -func OverrideK8sHttpClientWithTracer(restConfig *rest.Config) (*http.Client, error) { - httpClientFor, err := rest.HTTPClientFor(restConfig) - if err != nil { - fmt.Println("error occurred while overriding k8s client", "reason", err) - return nil, err - } - httpClientFor.Transport = otelhttp.NewTransport(httpClientFor.Transport) - return httpClientFor, nil -} func (impl *K8sServiceImpl) GetKubeVersion() (*version.Info, error) { discoveryClient, err := impl.GetK8sDiscoveryClientInCluster() if err != nil { @@ -1094,9 +798,9 @@ func (impl *K8sServiceImpl) GetKubeVersion() (*version.Info, error) { return k8sServerVersion, err } -func (impl *K8sServiceImpl) GetCoreV1ClientInCluster() (*v12.CoreV1Client, error) { +func (impl *K8sServiceImpl) GetCoreV1ClientInCluster(opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { restConfig := &rest.Config{} - restConfig, err := impl.GetK8sInClusterRestConfig() + restConfig, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Error("Error in creating config for default cluster", "err", err) return nil, err @@ -1105,14 +809,6 @@ func (impl *K8sServiceImpl) GetCoreV1ClientInCluster() (*v12.CoreV1Client, error } func (impl *K8sServiceImpl) GetCoreV1ClientByRestConfig(restConfig *rest.Config) (*v12.CoreV1Client, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClientFor, err := rest.HTTPClientFor(restConfig) if err != nil { impl.logger.Error("error occurred while overriding k8s client", "reason", err) @@ -1134,6 +830,7 @@ func (impl *K8sServiceImpl) GetNodesList(ctx context.Context, k8sClientSet *kube } return nodeList, err } + func (impl *K8sServiceImpl) GetNodeByName(ctx context.Context, k8sClientSet *kubernetes.Clientset, name string) (*v1.Node, error) { node, err := k8sClientSet.CoreV1().Nodes().Get(ctx, name, metav1.GetOptions{}) if err != nil { @@ -1172,6 +869,7 @@ func (impl *K8sServiceImpl) GetPodsListForNamespace(ctx context.Context, k8sClie } return podList, err } + func (impl *K8sServiceImpl) GetNmList(ctx context.Context, metricsClientSet *metrics.Clientset) (*v1beta1.NodeMetricsList, error) { nmList, err := metricsClientSet.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{}) if err != nil { @@ -1180,6 +878,7 @@ func (impl *K8sServiceImpl) GetNmList(ctx context.Context, metricsClientSet *met } return nmList, err } + func (impl *K8sServiceImpl) GetNmByName(ctx context.Context, metricsClientSet *metrics.Clientset, name string) (*v1beta1.NodeMetrics, error) { nodeMetrics, err := metricsClientSet.MetricsV1beta1().NodeMetricses().Get(ctx, name, metav1.GetOptions{}) if err != nil { @@ -1188,6 +887,7 @@ func (impl *K8sServiceImpl) GetNmByName(ctx context.Context, metricsClientSet *m } return nodeMetrics, err } + func (impl *K8sServiceImpl) GetMetricsClientSet(restConfig *rest.Config, k8sHttpClient *http.Client) (*metrics.Clientset, error) { metricsClientSet, err := metrics.NewForConfigAndClient(restConfig, k8sHttpClient) if err != nil { @@ -1196,6 +896,7 @@ func (impl *K8sServiceImpl) GetMetricsClientSet(restConfig *rest.Config, k8sHttp } return metricsClientSet, err } + func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request { podLogOpts := &v1.PodLogOptions{ Container: container, @@ -1205,71 +906,7 @@ func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, nam return req } -// DeletePod will delete the given pod, or return an error if it couldn't -func DeletePod(pod v1.Pod, k8sClientSet *kubernetes.Clientset, deleteOptions metav1.DeleteOptions) error { - return k8sClientSet.CoreV1().Pods(pod.Namespace).Delete(context.Background(), pod.Name, deleteOptions) -} - -// EvictPod will evict the given pod, or return an error if it couldn't -func EvictPod(pod v1.Pod, k8sClientSet *kubernetes.Clientset, evictionGroupVersion schema.GroupVersion, deleteOptions metav1.DeleteOptions) error { - switch evictionGroupVersion { - case v13.SchemeGroupVersion: - // send policy/v1 if the server supports it - eviction := &v13.Eviction{ - ObjectMeta: metav1.ObjectMeta{ - Name: pod.Name, - Namespace: pod.Namespace, - }, - DeleteOptions: &deleteOptions, - } - return k8sClientSet.PolicyV1().Evictions(eviction.Namespace).Evict(context.TODO(), eviction) - - default: - // otherwise, fall back to policy/v1beta1, supported by all servers that support the eviction subresource - eviction := &v1beta12.Eviction{ - ObjectMeta: metav1.ObjectMeta{ - Name: pod.Name, - Namespace: pod.Namespace, - }, - DeleteOptions: &deleteOptions, - } - return k8sClientSet.PolicyV1beta1().Evictions(eviction.Namespace).Evict(context.TODO(), eviction) - } -} - -// CheckEvictionSupport uses Discovery API to find out if the server support -// eviction subresource If support, it will return its groupVersion; Otherwise, -// it will return an empty GroupVersion -func CheckEvictionSupport(clientset kubernetes.Interface) (schema.GroupVersion, error) { - discoveryClient := clientset.Discovery() - - // version info available in subresources since v1.8.0 in https://github.com/kubernetes/kubernetes/pull/49971 - resourceList, err := discoveryClient.ServerResourcesForGroupVersion("v1") - if err != nil { - return schema.GroupVersion{}, err - } - for _, resource := range resourceList.APIResources { - if resource.Name == commonBean.EvictionSubresource && resource.Kind == commonBean.EvictionKind && - len(resource.Group) > 0 && len(resource.Version) > 0 { - return schema.GroupVersion{Group: resource.Group, Version: resource.Version}, nil - } - } - return schema.GroupVersion{}, nil -} - -func UpdateNodeUnschedulableProperty(desiredUnschedulable bool, node *v1.Node, k8sClientSet *kubernetes.Clientset) (*v1.Node, error) { - node.Spec.Unschedulable = desiredUnschedulable - node, err := k8sClientSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}) - return node, err -} - func (impl *K8sServiceImpl) CreateK8sClientSet(restConfig *rest.Config) (*kubernetes.Clientset, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("service err, OverrideK8sHttpClientWithTracer", "err", err) @@ -1313,29 +950,7 @@ func (impl *K8sServiceImpl) FetchConnectionStatusForCluster(k8sClientSet *kubern return err } -func CheckIfValidLabel(labelKey string, labelValue string) error { - labelKey = strings.TrimSpace(labelKey) - labelValue = strings.TrimSpace(labelValue) - - errs := validation.IsQualifiedName(labelKey) - if len(labelKey) == 0 || len(errs) > 0 { - return error2.New(fmt.Sprintf("Validation error - label key - %s is not satisfying the label key criteria", labelKey)) - } - - errs = validation.IsValidLabelValue(labelValue) - if len(labelValue) == 0 || len(errs) > 0 { - return error2.New(fmt.Sprintf("Validation error - label value - %s is not satisfying the label value criteria for label key - %s", labelValue, labelKey)) - } - return nil -} - func (impl *K8sServiceImpl) GetResourceIf(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { return nil, false, err @@ -1360,12 +975,6 @@ func (impl *K8sServiceImpl) GetResourceIf(restConfig *rest.Config, groupVersionK } func (impl *K8sServiceImpl) ListEvents(restConfig *rest.Config, namespace string, groupVersionKind schema.GroupVersionKind, ctx context.Context, name string) (*v1.EventList, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } _, namespaced, err := impl.GetResourceIf(restConfig, groupVersionKind) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name) @@ -1405,13 +1014,6 @@ func (impl *K8sServiceImpl) ListEvents(restConfig *rest.Config, namespace string } func (impl *K8sServiceImpl) GetPodLogs(ctx context.Context, restConfig *rest.Config, name string, namespace string, sinceTime *metav1.Time, tailLines int, sinceSeconds int, follow bool, containerName string, isPrevContainerLogsEnabled bool) (io.ReadCloser, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("error in getting pod logs", "err", err) @@ -1449,14 +1051,8 @@ func (impl *K8sServiceImpl) GetPodLogs(ctx context.Context, restConfig *rest.Con } return stream, nil } -func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { - - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } +func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("error in getting http client", "err", err) @@ -1497,26 +1093,7 @@ func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Confi return dynamicIf.Resource(resource), apiResource.Namespaced, nil } -func ServerResourceForGroupVersionKind(discoveryClient discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (*metav1.APIResource, error) { - resources, err := discoveryClient.ServerResourcesForGroupVersion(gvk.GroupVersion().String()) - if err != nil { - return nil, err - } - for _, r := range resources.APIResources { - if r.Kind == gvk.Kind { - return &r, nil - } - } - return nil, errors.NewNotFound(schema.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, "") -} func (impl *K8sServiceImpl) GetResourceList(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, asTable bool, listOptions *metav1.ListOptions) (*ResourceListResponse, bool, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } - resourceIf, namespaced, err := impl.GetResourceIfWithAcceptHeader(restConfig, gvk, asTable) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1543,15 +1120,8 @@ func (impl *K8sServiceImpl) GetResourceList(ctx context.Context, restConfig *res return &ResourceListResponse{*resp}, namespaced, nil } -func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name, "namespace", namespace) @@ -1571,15 +1141,9 @@ func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig return &ManifestResponse{Manifest: *resp}, nil } +// GetApiResources returns the list of api resources from k8s. // if verb is supplied empty, that means - return all func (impl *K8sServiceImpl) GetApiResources(restConfig *rest.Config, includeOnlyVerb string) ([]*K8sApiResource, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig) if err != nil { impl.logger.Errorw("error in getting dynamic k8s client", "err", err) @@ -1648,15 +1212,8 @@ func (impl *K8sServiceImpl) GetApiResources(restConfig *rest.Config, includeOnly } return apiResources, nil } -func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1680,15 +1237,8 @@ func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *res } return &ManifestResponse{Manifest: *resp}, nil } -func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1706,15 +1256,8 @@ func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, n } return &ManifestResponse{Manifest: *resp}, nil } -func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1740,14 +1283,6 @@ func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest } func (impl *K8sServiceImpl) DeleteResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, name string, forceDelete bool) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name, "namespace", namespace) @@ -1822,100 +1357,6 @@ func (impl *K8sServiceImpl) GetPodListByLabel(namespace, label string, clientSet return podList.Items, nil } -//func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unstructured) (*health.HealthStatus, error) { -// return health.GetHealthCheckFunc(gvk) -//} - -func isServiceAccountTokenSecret(un *unstructured.Unstructured) (bool, metav1.OwnerReference) { - ref := metav1.OwnerReference{ - APIVersion: "v1", - Kind: commonBean.ServiceAccountKind, - } - - if typeVal, ok, err := unstructured.NestedString(un.Object, "type"); !ok || err != nil || typeVal != "kubernetes.io/service-account-token" { - return false, ref - } - - annotations := un.GetAnnotations() - if annotations == nil { - return false, ref - } - - id, okId := annotations["kubernetes.io/service-account.uid"] - name, okName := annotations["kubernetes.io/service-account.name"] - if okId && okName { - ref.Name = name - ref.UID = types.UID(id) - } - return ref.Name != "" && ref.UID != "", ref -} - -func ResolveResourceReferences(un *unstructured.Unstructured) ([]metav1.OwnerReference, func(ResourceKey) bool) { - var isInferredParentOf func(_ ResourceKey) bool - ownerRefs := un.GetOwnerReferences() - gvk := un.GroupVersionKind() - - switch { - - // Special case for endpoint. Remove after https://github.com/kubernetes/kubernetes/issues/28483 is fixed - case gvk.Group == "" && gvk.Kind == commonBean.EndpointsKind && len(un.GetOwnerReferences()) == 0: - ownerRefs = append(ownerRefs, metav1.OwnerReference{ - Name: un.GetName(), - Kind: commonBean.ServiceKind, - APIVersion: "v1", - }) - - // Special case for Operator Lifecycle Manager ClusterServiceVersion: - case un.GroupVersionKind().Group == "operators.coreos.com" && un.GetKind() == "ClusterServiceVersion": - if un.GetAnnotations()["olm.operatorGroup"] != "" { - ownerRefs = append(ownerRefs, metav1.OwnerReference{ - Name: un.GetAnnotations()["olm.operatorGroup"], - Kind: "OperatorGroup", - APIVersion: "operators.coreos.com/v1", - }) - } - - // Edge case: consider auto-created service account tokens as a child of service account objects - case un.GetKind() == commonBean.SecretKind && un.GroupVersionKind().Group == "": - if yes, ref := isServiceAccountTokenSecret(un); yes { - ownerRefs = append(ownerRefs, ref) - } - - case (un.GroupVersionKind().Group == "apps" || un.GroupVersionKind().Group == "extensions") && un.GetKind() == commonBean.StatefulSetKind: - if refs, err := isStatefulSetChild(un); err != nil { - fmt.Println("error") - } else { - isInferredParentOf = refs - } - } - - return ownerRefs, isInferredParentOf -} - -func isStatefulSetChild(un *unstructured.Unstructured) (func(ResourceKey) bool, error) { - sts := v14.StatefulSet{} - data, err := json.Marshal(un) - if err != nil { - return nil, err - } - err = json.Unmarshal(data, &sts) - if err != nil { - return nil, err - } - - templates := sts.Spec.VolumeClaimTemplates - return func(key ResourceKey) bool { - if key.Kind == commonBean.PersistentVolumeClaimKind && key.GroupKind().Group == "" { - for _, templ := range templates { - if strings.HasPrefix(key.Name, fmt.Sprintf("%s-%s-", templ.Name, un.GetName())) { - return true - } - } - } - return false - }, nil -} - func (impl *K8sServiceImpl) CreateOrUpdateSecretByName(client *v12.CoreV1Client, namespace, uniqueSecretName string, secretLabel map[string]string, secretData map[string]string) error { secret, err := impl.GetSecret(namespace, uniqueSecretName, client) @@ -2012,3 +1453,50 @@ func (impl *K8sServiceImpl) DeleteResourceByGVR(ctx context.Context, config *res } return nil } + +func (impl *K8sServiceImpl) GetK8sInClusterRestConfig(opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterRestConfig() +} + +func (impl *K8sServiceImpl) GetK8sConfigAndClients(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sConfigAndClients(clusterConfig) +} + +func (impl *K8sServiceImpl) GetK8sInClusterConfigAndDynamicClients(opts ...K8sServiceOpts) (*rest.Config, *http.Client, dynamic.Interface, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterConfigAndDynamicClients() +} + +func (impl *K8sServiceImpl) GetK8sInClusterConfigAndClients(opts ...K8sServiceOpts) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterConfigAndClients() +} + +func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetRestConfigByCluster(clusterConfig) +} + +func (impl *K8sServiceImpl) OverrideRestConfigWithCustomTransport(restConfig *rest.Config, opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).OverrideRestConfigWithCustomTransport(restConfig) +} + +func (impl *K8sServiceImpl) GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config, opts ...K8sServiceOpts) (*http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sConfigAndClientsByRestConfig(restConfig) +} + +func (impl *K8sServiceImpl) DeleteNs(namespace string, client *v12.CoreV1Client) error { + err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{}) + return err +} + +func (impl *K8sServiceImpl) getEventKindHeader() ([]string, map[int]string) { + headers := []string{"type", "message", "namespace", "involved object", "source", "count", "age", "last seen"} + columnIndexes := make(map[int]string) + columnIndexes[0] = "last seen" + columnIndexes[1] = "type" + columnIndexes[2] = "namespace" + columnIndexes[3] = "involved object" + columnIndexes[5] = "source" + columnIndexes[6] = "message" + columnIndexes[7] = "age" + columnIndexes[8] = "count" + return headers, columnIndexes +} diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 81e8859b4..f2aadf160 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523085037-e1121de2f5a6 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 From 10419403730f5debfd79d0c451d56aa9273e4e7e Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 15:58:07 +0530 Subject: [PATCH 12/35] bump common lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 +- .../common-lib/utils/k8s/K8sUtil.go | 676 +++--------------- kubelink/vendor/modules.txt | 4 +- 4 files changed, 87 insertions(+), 599 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index 7a917c1b9..b353b9990 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index 1d81b9e2a..879da5fd1 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 h1:rEOoEOPSc8tirrJPNzGpU7f3SQ1Nmu8S5CFAxeAOQWY= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7/go.mod h1:CXQGEo+kZc7JPX5hn4jJf1msal9q/ExSdAYGkHNPnQw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff h1:CN6gTI0H4WvshMeBg0a07LrqJcELoZzVYpmSGuesKPw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 16ec0fbc5..0f313f2c7 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -20,16 +20,11 @@ import ( "context" "encoding/json" error2 "errors" - "flag" "fmt" "github.com/devtron-labs/common-lib/utils" http2 "github.com/devtron-labs/common-lib/utils/http" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "io" - v13 "k8s.io/api/policy/v1" - v1beta12 "k8s.io/api/policy/v1beta1" - "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/dynamic" "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/metrics/pkg/apis/metrics/v1beta1" @@ -38,8 +33,6 @@ import ( "log" "net/http" "net/url" - "os/user" - "path/filepath" "strconv" "strings" "time" @@ -48,8 +41,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/version" - "go.uber.org/zap" - v14 "k8s.io/api/apps/v1" batchV1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" @@ -60,159 +51,19 @@ import ( "k8s.io/client-go/kubernetes" v12 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/yaml" ) -type K8sServiceImpl struct { - logger *zap.SugaredLogger - runTimeConfig *RuntimeConfig - httpClientConfig *CustomK8sHttpTransportConfig - kubeconfig *string -} - -type K8sService interface { - GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request - GetMetricsClientSet(restConfig *rest.Config, k8sHttpClient *http.Client) (*metrics.Clientset, error) - GetNmByName(ctx context.Context, metricsClientSet *metrics.Clientset, name string) (*v1beta1.NodeMetrics, error) - GetNmList(ctx context.Context, metricsClientSet *metrics.Clientset) (*v1beta1.NodeMetricsList, error) - GetPodsListForNamespace(ctx context.Context, k8sClientSet *kubernetes.Clientset, namespace string) (*v1.PodList, error) - GetServerVersionFromDiscoveryClient(k8sClientSet *kubernetes.Clientset) (*version.Info, error) - GetServerGroups(k8sClientSet *kubernetes.Clientset) (*metav1.APIGroupList, error) - GetNodeByName(ctx context.Context, k8sClientSet *kubernetes.Clientset, name string) (*v1.Node, error) - GetNodesList(ctx context.Context, k8sClientSet *kubernetes.Clientset) (*v1.NodeList, error) - GetCoreV1ClientByRestConfig(restConfig *rest.Config) (*v12.CoreV1Client, error) - GetCoreV1ClientInCluster() (*v12.CoreV1Client, error) - GetKubeVersion() (*version.Info, error) - ValidateResource(resourceObj map[string]interface{}, gvk schema.GroupVersionKind, validateCallback func(namespace string, group string, kind string, resourceName string) bool) bool - BuildK8sObjectListTableData(manifest *unstructured.UnstructuredList, namespaced bool, gvk schema.GroupVersionKind, includeMetadata bool, validateResourceAccess func(namespace string, group string, kind string, resourceName string) bool) (*ClusterResourceListMap, error) - ValidateForResource(namespace string, resourceRef interface{}, validateCallback func(namespace string, group string, kind string, resourceName string) bool) bool - GetPodByName(namespace string, name string, client *v12.CoreV1Client) (*v1.Pod, error) - GetK8sInClusterRestConfig() (*rest.Config, error) - GetResourceInfoByLabelSelector(ctx context.Context, namespace string, labelSelector string) (*v1.Pod, error) - GetClientByToken(serverUrl string, token map[string]string) (*v12.CoreV1Client, error) - ListNamespaces(client *v12.CoreV1Client) (*v1.NamespaceList, error) - DeleteAndCreateJob(content []byte, namespace string, clusterConfig *ClusterConfig) error - DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig) error - CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job) error - GetLiveZCall(path string, k8sClientSet *kubernetes.Clientset) ([]byte, error) - DiscoveryClientGetLiveZCall(cluster *ClusterConfig) ([]byte, error) - GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config) (*http.Client, *kubernetes.Clientset, error) - GetK8sConfigAndClients(clusterConfig *ClusterConfig) (*rest.Config, *http.Client, *kubernetes.Clientset, error) - GetK8sInClusterConfigAndDynamicClients() (*rest.Config, *http.Client, dynamic.Interface, error) - GetK8sInClusterConfigAndClients() (*rest.Config, *http.Client, *kubernetes.Clientset, error) - DeleteJob(namespace string, name string, clusterConfig *ClusterConfig) error - DeleteSecret(namespace string, name string, client *v12.CoreV1Client) error - UpdateSecret(namespace string, secret *v1.Secret, client *v12.CoreV1Client) (*v1.Secret, error) - CreateSecretData(namespace string, secret *v1.Secret, v1Client *v12.CoreV1Client) (*v1.Secret, error) - CreateSecret(namespace string, data map[string][]byte, secretName string, secretType v1.SecretType, client *v12.CoreV1Client, labels map[string]string, stringData map[string]string) (*v1.Secret, error) - GetSecret(namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) - GetSecretWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) - PatchConfigMapJsonType(namespace string, clusterConfig *ClusterConfig, name string, data interface{}, path string) (*v1.ConfigMap, error) - PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) - UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) - DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error - GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) - GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) - GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) - CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) - UpdateNSLabels(namespace *v1.Namespace, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, err error) - GetK8sDiscoveryClientInCluster() (*discovery.DiscoveryClient, error) - GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error) - GetClientForInCluster() (*v12.CoreV1Client, error) - GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error) - GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error) - GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) - UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) - DeleteResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, name string, forceDelete bool) (*ManifestResponse, error) - GetPodListByLabel(namespace, label string, clientSet *kubernetes.Clientset) ([]v1.Pod, error) - ExtractK8sServerMajorAndMinorVersion(k8sServerVersion *version.Info) (int, int, error) - GetK8sServerVersion(clientSet *kubernetes.Clientset) (*version.Info, error) - DecodeGroupKindversion(data string) (*schema.GroupVersionKind, error) - GetApiResources(restConfig *rest.Config, includeOnlyVerb string) ([]*K8sApiResource, error) - CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) - PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) - GetResourceList(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, asTable bool, listOptions *metav1.ListOptions) (*ResourceListResponse, bool, error) - GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) - GetPodLogs(ctx context.Context, restConfig *rest.Config, name string, namespace string, sinceTime *metav1.Time, tailLines int, sinceSeconds int, follow bool, containerName string, isPrevContainerLogsEnabled bool) (io.ReadCloser, error) - ListEvents(restConfig *rest.Config, namespace string, groupVersionKind schema.GroupVersionKind, ctx context.Context, name string) (*v1.EventList, error) - GetResourceIf(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) - FetchConnectionStatusForCluster(k8sClientSet *kubernetes.Clientset) error - CreateK8sClientSet(restConfig *rest.Config) (*kubernetes.Clientset, error) - CreateOrUpdateSecretByName(client *v12.CoreV1Client, namespace, uniqueSecretName string, secretLabel map[string]string, secretData map[string]string) error - //CreateK8sClientSetWithCustomHttpTransport(restConfig *rest.Config) (*kubernetes.Clientset, error) - - //below functions are exposed for K8sUtilExtended - GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) - OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) - CreateNsWithLabels(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) - CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) - GetGVRForCRD(config *rest.Config, CRDName string) (schema.GroupVersionResource, error) - GetResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string) (*unstructured.Unstructured, error) - PatchResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string, patchType types.PatchType, patchData []byte) (*unstructured.Unstructured, error) - DeleteResourceByGVR(ctx context.Context, config *rest.Config, GVR schema.GroupVersionResource, resourceName, namespace string, forceDelete bool) error -} - -func NewK8sUtil(logger *zap.SugaredLogger, runTimeConfig *RuntimeConfig) (*K8sServiceImpl, error) { - var kubeconfig *string - if runTimeConfig.LocalDevMode { - usr, err := user.Current() - if err != nil { - logger.Errorw("error in NewK8sUtil, failed to get current user", "err", err) - return nil, err - } - kubeconfig = flag.String("kubeconfig-authenticator-xyz", filepath.Join(usr.HomeDir, ".kube", "config"), "(optional) absolute path to the kubeconfig file") - } - - httpClientConfig := NewCustomK8sHttpTransportConfig() - flag.Parse() - return &K8sServiceImpl{logger: logger, runTimeConfig: runTimeConfig, kubeconfig: kubeconfig, httpClientConfig: httpClientConfig}, nil -} - -func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error) { - restConfig, err := impl.GetRestConfigByClusterWithoutCustomTransport(clusterConfig) - if err != nil { - impl.logger.Errorw("error, GetRestConfigByClusterWithoutCustomTransport", "err", err) - return nil, err - } - restConfig, err = impl.OverrideRestConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err) - } - return restConfig, err -} - -func (impl *K8sServiceImpl) GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) { - bearerToken := clusterConfig.BearerToken - var restConfig *rest.Config - var err error - if clusterConfig.Host == commonBean.DefaultClusterUrl && len(bearerToken) == 0 { - restConfig, err = impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for default cluster", "err", err) - return nil, err - } - } else { - restConfig = &rest.Config{Host: clusterConfig.Host, BearerToken: bearerToken} - clusterConfig.PopulateTlsConfigurationsInto(restConfig) +func (impl *K8sServiceImpl) getOpts(opts []K8sServiceOpts) Options { + customOpts := impl.opts + for _, opt := range opts { + customOpts = opt(customOpts) } - return restConfig, nil + return customOpts } -func (impl *K8sServiceImpl) OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err) - return nil, err - } - return restConfig, nil -} - -func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error) { - cfg, err := impl.GetRestConfigByCluster(clusterConfig) +func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { + cfg, err := impl.GetRestConfigByCluster(clusterConfig, opts...) if err != nil { impl.logger.Errorw("error in getting rest config for default cluster", "err", err) return nil, err @@ -220,20 +71,14 @@ func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig) (*v12. return impl.GetCoreV1ClientByRestConfig(cfg) } -func (impl *K8sServiceImpl) GetClientForInCluster() (*v12.CoreV1Client, error) { +func (impl *K8sServiceImpl) GetClientForInCluster(opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { // creates the in-cluster config - config, err := impl.GetK8sInClusterRestConfig() + config, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Errorw("error in getting config", "err", err) return nil, err } - config, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(config) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - // creates the clientset httpClient, err := OverrideK8sHttpClientWithTracer(config) if err != nil { @@ -248,19 +93,13 @@ func (impl *K8sServiceImpl) GetClientForInCluster() (*v12.CoreV1Client, error) { return clientset, err } -func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error) { - cfg, err := impl.GetRestConfigByCluster(clusterConfig) +func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*discovery.DiscoveryClient, error) { + cfg, err := impl.GetRestConfigByCluster(clusterConfig, opts...) if err != nil { impl.logger.Errorw("error in getting rest config for default cluster", "err", err) return nil, err } - cfg, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(cfg) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(cfg) if err != nil { impl.logger.Errorw("error in getting http client for default cluster", "err", err) @@ -274,19 +113,13 @@ func (impl *K8sServiceImpl) GetK8sDiscoveryClient(clusterConfig *ClusterConfig) return discoveryClient, err } -func (impl *K8sServiceImpl) GetK8sDiscoveryClientInCluster() (*discovery.DiscoveryClient, error) { - config, err := impl.GetK8sInClusterRestConfig() +func (impl *K8sServiceImpl) GetK8sDiscoveryClientInCluster(opts ...K8sServiceOpts) (*discovery.DiscoveryClient, error) { + config, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Errorw("error in getting config", "err", err) return nil, err } - config, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(config) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(config) if err != nil { impl.logger.Errorw("error in getting http client for default cluster", "err", err) @@ -379,11 +212,6 @@ func (impl *K8sServiceImpl) CreateNsWithLabels(namespace string, labels map[stri } } -func (impl *K8sServiceImpl) deleteNs(namespace string, client *v12.CoreV1Client) error { - err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{}) - return err -} - func (impl *K8sServiceImpl) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) { return impl.GetConfigMapWithCtx(context.Background(), namespace, name, client) } @@ -499,12 +327,6 @@ func (impl *K8sServiceImpl) PatchConfigMapJsonType(namespace string, clusterConf return cm, nil } -type JsonPatchType struct { - Op string `json:"op"` - Path string `json:"path"` - Value interface{} `json:"value"` -} - func (impl *K8sServiceImpl) GetSecret(namespace string, name string, client *v12.CoreV1Client) (*v1.Secret, error) { return impl.GetSecretWithCtx(context.Background(), namespace, name, client) } @@ -564,8 +386,8 @@ func (impl *K8sServiceImpl) DeleteSecret(namespace string, name string, client * return nil } -func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConfig *ClusterConfig) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConfig *ClusterConfig, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, DeleteJob", "err", err) return err @@ -589,47 +411,6 @@ func (impl *K8sServiceImpl) DeleteJob(namespace string, name string, clusterConf return nil } -func (impl *K8sServiceImpl) GetK8sInClusterConfigAndClients() (*rest.Config, *http.Client, *kubernetes.Clientset, error) { - restConfig, err := impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for in cluster", "err", err) - return nil, nil, nil, err - } - - k8sHttpClient, k8sClientSet, err := impl.GetK8sConfigAndClientsByRestConfig(restConfig) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) GetK8sInClusterConfigAndDynamicClients() (*rest.Config, *http.Client, dynamic.Interface, error) { - restConfig, err := impl.GetK8sInClusterRestConfig() - if err != nil { - impl.logger.Errorw("error in getting rest config for in cluster", "err", err) - return nil, nil, nil, err - } - - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, nil, nil, err - } - - k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) - if err != nil { - impl.logger.Errorw("error in getting k8s http client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - dynamicClientSet, err := dynamic.NewForConfigAndClient(restConfig, k8sHttpClient) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config for in cluster", "err", err) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, dynamicClientSet, nil -} - func (impl *K8sServiceImpl) GetK8sDynamicClient(restConfig *rest.Config, k8sHttpClient *http.Client) (dynamic.Interface, error) { dynamicClientSet, err := dynamic.NewForConfigAndClient(restConfig, k8sHttpClient) if err != nil { @@ -639,44 +420,8 @@ func (impl *K8sServiceImpl) GetK8sDynamicClient(restConfig *rest.Config, k8sHttp return dynamicClientSet, nil } -func (impl *K8sServiceImpl) GetK8sConfigAndClients(clusterConfig *ClusterConfig) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { - restConfig, err := impl.GetRestConfigByCluster(clusterConfig) - if err != nil { - impl.logger.Errorw("error in getting rest config by cluster", "err", err, "clusterName", clusterConfig.ClusterName) - return nil, nil, nil, err - } - - k8sHttpClient, k8sClientSet, err := impl.GetK8sConfigAndClientsByRestConfig(restConfig) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config", "err", err, "clusterName", clusterConfig.ClusterName) - return nil, nil, nil, err - } - return restConfig, k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config) (*http.Client, *kubernetes.Clientset, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, nil, err - } - - k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) - if err != nil { - impl.logger.Errorw("error in getting k8s http client set by rest config", "err", err) - return nil, nil, err - } - k8sClientSet, err := kubernetes.NewForConfigAndClient(restConfig, k8sHttpClient) - if err != nil { - impl.logger.Errorw("error in getting client set by rest config", "err", err) - return nil, nil, err - } - return k8sHttpClient, k8sClientSet, nil -} - -func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig) ([]byte, error) { - _, _, k8sClientSet, err := impl.GetK8sConfigAndClients(cluster) +func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig, opts ...K8sServiceOpts) ([]byte, error) { + _, _, k8sClientSet, err := impl.GetK8sConfigAndClients(cluster, opts...) if err != nil { impl.logger.Errorw("errir in getting clients and configs", "err", err, "clusterName", cluster.ClusterName) return nil, err @@ -690,6 +435,7 @@ func (impl *K8sServiceImpl) DiscoveryClientGetLiveZCall(cluster *ClusterConfig) return response, err } + func (impl *K8sServiceImpl) GetLiveZCall(path string, k8sClientSet *kubernetes.Clientset) ([]byte, error) { response, err := k8sClientSet.Discovery().RESTClient().Get().AbsPath(path).DoRaw(context.Background()) if err != nil { @@ -699,8 +445,8 @@ func (impl *K8sServiceImpl) GetLiveZCall(path string, k8sClientSet *kubernetes.C return response, err } -func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConfig *ClusterConfig, job *batchV1.Job, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, CreateJob", "err", err) } @@ -727,8 +473,8 @@ func (impl *K8sServiceImpl) CreateJob(namespace string, name string, clusterConf // DeletePod delete pods with label job-name -func (impl *K8sServiceImpl) DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig) error { - _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig) +func (impl *K8sServiceImpl) DeletePodByLabel(namespace string, labels string, clusterConfig *ClusterConfig, opts ...K8sServiceOpts) error { + _, _, clientSet, err := impl.GetK8sConfigAndClients(clusterConfig, opts...) if err != nil { impl.logger.Errorw("clientSet err, DeletePod", "err", err) return err @@ -833,25 +579,6 @@ func (impl *K8sServiceImpl) GetResourceInfoByLabelSelector(ctx context.Context, } } -func (impl *K8sServiceImpl) GetK8sInClusterRestConfig() (*rest.Config, error) { - impl.logger.Debug("getting k8s rest config") - if impl.runTimeConfig.LocalDevMode { - restConfig, err := clientcmd.BuildConfigFromFlags("", *impl.kubeconfig) - if err != nil { - impl.logger.Errorw("Error while building config from flags", "error", err) - return nil, err - } - return restConfig, nil - } else { - clusterConfig, err := rest.InClusterConfig() - if err != nil { - impl.logger.Errorw("error in fetch default cluster config", "err", err) - return nil, err - } - return clusterConfig, nil - } -} - func (impl *K8sServiceImpl) GetPodByName(namespace string, name string, client *v12.CoreV1Client) (*v1.Pod, error) { pod, err := client.Pods(namespace).Get(context.Background(), name, metav1.GetOptions{}) if err != nil { @@ -1057,29 +784,6 @@ func (impl *K8sServiceImpl) ValidateForResource(namespace string, resourceRef in return false } -func (impl *K8sServiceImpl) getEventKindHeader() ([]string, map[int]string) { - headers := []string{"type", "message", "namespace", "involved object", "source", "count", "age", "last seen"} - columnIndexes := make(map[int]string) - columnIndexes[0] = "last seen" - columnIndexes[1] = "type" - columnIndexes[2] = "namespace" - columnIndexes[3] = "involved object" - columnIndexes[5] = "source" - columnIndexes[6] = "message" - columnIndexes[7] = "age" - columnIndexes[8] = "count" - return headers, columnIndexes -} - -func OverrideK8sHttpClientWithTracer(restConfig *rest.Config) (*http.Client, error) { - httpClientFor, err := rest.HTTPClientFor(restConfig) - if err != nil { - fmt.Println("error occurred while overriding k8s client", "reason", err) - return nil, err - } - httpClientFor.Transport = otelhttp.NewTransport(httpClientFor.Transport) - return httpClientFor, nil -} func (impl *K8sServiceImpl) GetKubeVersion() (*version.Info, error) { discoveryClient, err := impl.GetK8sDiscoveryClientInCluster() if err != nil { @@ -1094,9 +798,9 @@ func (impl *K8sServiceImpl) GetKubeVersion() (*version.Info, error) { return k8sServerVersion, err } -func (impl *K8sServiceImpl) GetCoreV1ClientInCluster() (*v12.CoreV1Client, error) { +func (impl *K8sServiceImpl) GetCoreV1ClientInCluster(opts ...K8sServiceOpts) (*v12.CoreV1Client, error) { restConfig := &rest.Config{} - restConfig, err := impl.GetK8sInClusterRestConfig() + restConfig, err := impl.GetK8sInClusterRestConfig(opts...) if err != nil { impl.logger.Error("Error in creating config for default cluster", "err", err) return nil, err @@ -1105,14 +809,6 @@ func (impl *K8sServiceImpl) GetCoreV1ClientInCluster() (*v12.CoreV1Client, error } func (impl *K8sServiceImpl) GetCoreV1ClientByRestConfig(restConfig *rest.Config) (*v12.CoreV1Client, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClientFor, err := rest.HTTPClientFor(restConfig) if err != nil { impl.logger.Error("error occurred while overriding k8s client", "reason", err) @@ -1134,6 +830,7 @@ func (impl *K8sServiceImpl) GetNodesList(ctx context.Context, k8sClientSet *kube } return nodeList, err } + func (impl *K8sServiceImpl) GetNodeByName(ctx context.Context, k8sClientSet *kubernetes.Clientset, name string) (*v1.Node, error) { node, err := k8sClientSet.CoreV1().Nodes().Get(ctx, name, metav1.GetOptions{}) if err != nil { @@ -1172,6 +869,7 @@ func (impl *K8sServiceImpl) GetPodsListForNamespace(ctx context.Context, k8sClie } return podList, err } + func (impl *K8sServiceImpl) GetNmList(ctx context.Context, metricsClientSet *metrics.Clientset) (*v1beta1.NodeMetricsList, error) { nmList, err := metricsClientSet.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{}) if err != nil { @@ -1180,6 +878,7 @@ func (impl *K8sServiceImpl) GetNmList(ctx context.Context, metricsClientSet *met } return nmList, err } + func (impl *K8sServiceImpl) GetNmByName(ctx context.Context, metricsClientSet *metrics.Clientset, name string) (*v1beta1.NodeMetrics, error) { nodeMetrics, err := metricsClientSet.MetricsV1beta1().NodeMetricses().Get(ctx, name, metav1.GetOptions{}) if err != nil { @@ -1188,6 +887,7 @@ func (impl *K8sServiceImpl) GetNmByName(ctx context.Context, metricsClientSet *m } return nodeMetrics, err } + func (impl *K8sServiceImpl) GetMetricsClientSet(restConfig *rest.Config, k8sHttpClient *http.Client) (*metrics.Clientset, error) { metricsClientSet, err := metrics.NewForConfigAndClient(restConfig, k8sHttpClient) if err != nil { @@ -1196,6 +896,7 @@ func (impl *K8sServiceImpl) GetMetricsClientSet(restConfig *rest.Config, k8sHttp } return metricsClientSet, err } + func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request { podLogOpts := &v1.PodLogOptions{ Container: container, @@ -1205,71 +906,7 @@ func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, nam return req } -// DeletePod will delete the given pod, or return an error if it couldn't -func DeletePod(pod v1.Pod, k8sClientSet *kubernetes.Clientset, deleteOptions metav1.DeleteOptions) error { - return k8sClientSet.CoreV1().Pods(pod.Namespace).Delete(context.Background(), pod.Name, deleteOptions) -} - -// EvictPod will evict the given pod, or return an error if it couldn't -func EvictPod(pod v1.Pod, k8sClientSet *kubernetes.Clientset, evictionGroupVersion schema.GroupVersion, deleteOptions metav1.DeleteOptions) error { - switch evictionGroupVersion { - case v13.SchemeGroupVersion: - // send policy/v1 if the server supports it - eviction := &v13.Eviction{ - ObjectMeta: metav1.ObjectMeta{ - Name: pod.Name, - Namespace: pod.Namespace, - }, - DeleteOptions: &deleteOptions, - } - return k8sClientSet.PolicyV1().Evictions(eviction.Namespace).Evict(context.TODO(), eviction) - - default: - // otherwise, fall back to policy/v1beta1, supported by all servers that support the eviction subresource - eviction := &v1beta12.Eviction{ - ObjectMeta: metav1.ObjectMeta{ - Name: pod.Name, - Namespace: pod.Namespace, - }, - DeleteOptions: &deleteOptions, - } - return k8sClientSet.PolicyV1beta1().Evictions(eviction.Namespace).Evict(context.TODO(), eviction) - } -} - -// CheckEvictionSupport uses Discovery API to find out if the server support -// eviction subresource If support, it will return its groupVersion; Otherwise, -// it will return an empty GroupVersion -func CheckEvictionSupport(clientset kubernetes.Interface) (schema.GroupVersion, error) { - discoveryClient := clientset.Discovery() - - // version info available in subresources since v1.8.0 in https://github.com/kubernetes/kubernetes/pull/49971 - resourceList, err := discoveryClient.ServerResourcesForGroupVersion("v1") - if err != nil { - return schema.GroupVersion{}, err - } - for _, resource := range resourceList.APIResources { - if resource.Name == commonBean.EvictionSubresource && resource.Kind == commonBean.EvictionKind && - len(resource.Group) > 0 && len(resource.Version) > 0 { - return schema.GroupVersion{Group: resource.Group, Version: resource.Version}, nil - } - } - return schema.GroupVersion{}, nil -} - -func UpdateNodeUnschedulableProperty(desiredUnschedulable bool, node *v1.Node, k8sClientSet *kubernetes.Clientset) (*v1.Node, error) { - node.Spec.Unschedulable = desiredUnschedulable - node, err := k8sClientSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}) - return node, err -} - func (impl *K8sServiceImpl) CreateK8sClientSet(restConfig *rest.Config) (*kubernetes.Clientset, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } k8sHttpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("service err, OverrideK8sHttpClientWithTracer", "err", err) @@ -1313,29 +950,7 @@ func (impl *K8sServiceImpl) FetchConnectionStatusForCluster(k8sClientSet *kubern return err } -func CheckIfValidLabel(labelKey string, labelValue string) error { - labelKey = strings.TrimSpace(labelKey) - labelValue = strings.TrimSpace(labelValue) - - errs := validation.IsQualifiedName(labelKey) - if len(labelKey) == 0 || len(errs) > 0 { - return error2.New(fmt.Sprintf("Validation error - label key - %s is not satisfying the label key criteria", labelKey)) - } - - errs = validation.IsValidLabelValue(labelValue) - if len(labelValue) == 0 || len(errs) > 0 { - return error2.New(fmt.Sprintf("Validation error - label value - %s is not satisfying the label value criteria for label key - %s", labelValue, labelKey)) - } - return nil -} - func (impl *K8sServiceImpl) GetResourceIf(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { return nil, false, err @@ -1360,12 +975,6 @@ func (impl *K8sServiceImpl) GetResourceIf(restConfig *rest.Config, groupVersionK } func (impl *K8sServiceImpl) ListEvents(restConfig *rest.Config, namespace string, groupVersionKind schema.GroupVersionKind, ctx context.Context, name string) (*v1.EventList, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } _, namespaced, err := impl.GetResourceIf(restConfig, groupVersionKind) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name) @@ -1405,13 +1014,6 @@ func (impl *K8sServiceImpl) ListEvents(restConfig *rest.Config, namespace string } func (impl *K8sServiceImpl) GetPodLogs(ctx context.Context, restConfig *rest.Config, name string, namespace string, sinceTime *metav1.Time, tailLines int, sinceSeconds int, follow bool, containerName string, isPrevContainerLogsEnabled bool) (io.ReadCloser, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("error in getting pod logs", "err", err) @@ -1449,14 +1051,8 @@ func (impl *K8sServiceImpl) GetPodLogs(ctx context.Context, restConfig *rest.Con } return stream, nil } -func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { - - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } +func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Config, groupVersionKind schema.GroupVersionKind, asTable bool) (resourceIf dynamic.NamespaceableResourceInterface, namespaced bool, err error) { httpClient, err := OverrideK8sHttpClientWithTracer(restConfig) if err != nil { impl.logger.Errorw("error in getting http client", "err", err) @@ -1497,26 +1093,7 @@ func (impl *K8sServiceImpl) GetResourceIfWithAcceptHeader(restConfig *rest.Confi return dynamicIf.Resource(resource), apiResource.Namespaced, nil } -func ServerResourceForGroupVersionKind(discoveryClient discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (*metav1.APIResource, error) { - resources, err := discoveryClient.ServerResourcesForGroupVersion(gvk.GroupVersion().String()) - if err != nil { - return nil, err - } - for _, r := range resources.APIResources { - if r.Kind == gvk.Kind { - return &r, nil - } - } - return nil, errors.NewNotFound(schema.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, "") -} func (impl *K8sServiceImpl) GetResourceList(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, asTable bool, listOptions *metav1.ListOptions) (*ResourceListResponse, bool, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, false, err - } - resourceIf, namespaced, err := impl.GetResourceIfWithAcceptHeader(restConfig, gvk, asTable) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1543,15 +1120,8 @@ func (impl *K8sServiceImpl) GetResourceList(ctx context.Context, restConfig *res return &ResourceListResponse{*resp}, namespaced, nil } -func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig *rest.Config, pt types.PatchType, manifest string, name string, namespace string, gvk schema.GroupVersionKind) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name, "namespace", namespace) @@ -1571,15 +1141,9 @@ func (impl *K8sServiceImpl) PatchResourceRequest(ctx context.Context, restConfig return &ManifestResponse{Manifest: *resp}, nil } +// GetApiResources returns the list of api resources from k8s. // if verb is supplied empty, that means - return all func (impl *K8sServiceImpl) GetApiResources(restConfig *rest.Config, includeOnlyVerb string) ([]*K8sApiResource, error) { - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig) if err != nil { impl.logger.Errorw("error in getting dynamic k8s client", "err", err) @@ -1648,15 +1212,8 @@ func (impl *K8sServiceImpl) GetApiResources(restConfig *rest.Config, includeOnly } return apiResources, nil } -func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *rest.Config, manifest string, gvk schema.GroupVersionKind, namespace string) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1680,15 +1237,8 @@ func (impl *K8sServiceImpl) CreateResources(ctx context.Context, restConfig *res } return &ManifestResponse{Manifest: *resp}, nil } -func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1706,15 +1256,8 @@ func (impl *K8sServiceImpl) GetResource(ctx context.Context, namespace string, n } return &ManifestResponse{Manifest: *resp}, nil } -func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } +func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error) { resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "namespace", namespace) @@ -1740,14 +1283,6 @@ func (impl *K8sServiceImpl) UpdateResource(ctx context.Context, restConfig *rest } func (impl *K8sServiceImpl) DeleteResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, name string, forceDelete bool) (*ManifestResponse, error) { - - var err error - restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig) - if err != nil { - impl.logger.Errorw("error in overriding reset config", "err", err) - return nil, err - } - resourceIf, namespaced, err := impl.GetResourceIf(restConfig, gvk) if err != nil { impl.logger.Errorw("error in getting dynamic interface for resource", "err", err, "resource", name, "namespace", namespace) @@ -1822,100 +1357,6 @@ func (impl *K8sServiceImpl) GetPodListByLabel(namespace, label string, clientSet return podList.Items, nil } -//func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unstructured) (*health.HealthStatus, error) { -// return health.GetHealthCheckFunc(gvk) -//} - -func isServiceAccountTokenSecret(un *unstructured.Unstructured) (bool, metav1.OwnerReference) { - ref := metav1.OwnerReference{ - APIVersion: "v1", - Kind: commonBean.ServiceAccountKind, - } - - if typeVal, ok, err := unstructured.NestedString(un.Object, "type"); !ok || err != nil || typeVal != "kubernetes.io/service-account-token" { - return false, ref - } - - annotations := un.GetAnnotations() - if annotations == nil { - return false, ref - } - - id, okId := annotations["kubernetes.io/service-account.uid"] - name, okName := annotations["kubernetes.io/service-account.name"] - if okId && okName { - ref.Name = name - ref.UID = types.UID(id) - } - return ref.Name != "" && ref.UID != "", ref -} - -func ResolveResourceReferences(un *unstructured.Unstructured) ([]metav1.OwnerReference, func(ResourceKey) bool) { - var isInferredParentOf func(_ ResourceKey) bool - ownerRefs := un.GetOwnerReferences() - gvk := un.GroupVersionKind() - - switch { - - // Special case for endpoint. Remove after https://github.com/kubernetes/kubernetes/issues/28483 is fixed - case gvk.Group == "" && gvk.Kind == commonBean.EndpointsKind && len(un.GetOwnerReferences()) == 0: - ownerRefs = append(ownerRefs, metav1.OwnerReference{ - Name: un.GetName(), - Kind: commonBean.ServiceKind, - APIVersion: "v1", - }) - - // Special case for Operator Lifecycle Manager ClusterServiceVersion: - case un.GroupVersionKind().Group == "operators.coreos.com" && un.GetKind() == "ClusterServiceVersion": - if un.GetAnnotations()["olm.operatorGroup"] != "" { - ownerRefs = append(ownerRefs, metav1.OwnerReference{ - Name: un.GetAnnotations()["olm.operatorGroup"], - Kind: "OperatorGroup", - APIVersion: "operators.coreos.com/v1", - }) - } - - // Edge case: consider auto-created service account tokens as a child of service account objects - case un.GetKind() == commonBean.SecretKind && un.GroupVersionKind().Group == "": - if yes, ref := isServiceAccountTokenSecret(un); yes { - ownerRefs = append(ownerRefs, ref) - } - - case (un.GroupVersionKind().Group == "apps" || un.GroupVersionKind().Group == "extensions") && un.GetKind() == commonBean.StatefulSetKind: - if refs, err := isStatefulSetChild(un); err != nil { - fmt.Println("error") - } else { - isInferredParentOf = refs - } - } - - return ownerRefs, isInferredParentOf -} - -func isStatefulSetChild(un *unstructured.Unstructured) (func(ResourceKey) bool, error) { - sts := v14.StatefulSet{} - data, err := json.Marshal(un) - if err != nil { - return nil, err - } - err = json.Unmarshal(data, &sts) - if err != nil { - return nil, err - } - - templates := sts.Spec.VolumeClaimTemplates - return func(key ResourceKey) bool { - if key.Kind == commonBean.PersistentVolumeClaimKind && key.GroupKind().Group == "" { - for _, templ := range templates { - if strings.HasPrefix(key.Name, fmt.Sprintf("%s-%s-", templ.Name, un.GetName())) { - return true - } - } - } - return false - }, nil -} - func (impl *K8sServiceImpl) CreateOrUpdateSecretByName(client *v12.CoreV1Client, namespace, uniqueSecretName string, secretLabel map[string]string, secretData map[string]string) error { secret, err := impl.GetSecret(namespace, uniqueSecretName, client) @@ -2012,3 +1453,50 @@ func (impl *K8sServiceImpl) DeleteResourceByGVR(ctx context.Context, config *res } return nil } + +func (impl *K8sServiceImpl) GetK8sInClusterRestConfig(opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterRestConfig() +} + +func (impl *K8sServiceImpl) GetK8sConfigAndClients(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sConfigAndClients(clusterConfig) +} + +func (impl *K8sServiceImpl) GetK8sInClusterConfigAndDynamicClients(opts ...K8sServiceOpts) (*rest.Config, *http.Client, dynamic.Interface, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterConfigAndDynamicClients() +} + +func (impl *K8sServiceImpl) GetK8sInClusterConfigAndClients(opts ...K8sServiceOpts) (*rest.Config, *http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sInClusterConfigAndClients() +} + +func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig, opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetRestConfigByCluster(clusterConfig) +} + +func (impl *K8sServiceImpl) OverrideRestConfigWithCustomTransport(restConfig *rest.Config, opts ...K8sServiceOpts) (*rest.Config, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).OverrideRestConfigWithCustomTransport(restConfig) +} + +func (impl *K8sServiceImpl) GetK8sConfigAndClientsByRestConfig(restConfig *rest.Config, opts ...K8sServiceOpts) (*http.Client, *kubernetes.Clientset, error) { + return impl.WithHttpTransport(impl.getOpts(opts)).GetK8sConfigAndClientsByRestConfig(restConfig) +} + +func (impl *K8sServiceImpl) DeleteNs(namespace string, client *v12.CoreV1Client) error { + err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{}) + return err +} + +func (impl *K8sServiceImpl) getEventKindHeader() ([]string, map[int]string) { + headers := []string{"type", "message", "namespace", "involved object", "source", "count", "age", "last seen"} + columnIndexes := make(map[int]string) + columnIndexes[0] = "last seen" + columnIndexes[1] = "type" + columnIndexes[2] = "namespace" + columnIndexes[3] = "involved object" + columnIndexes[5] = "source" + columnIndexes[6] = "message" + columnIndexes[7] = "age" + columnIndexes[8] = "count" + return headers, columnIndexes +} diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index f899c9c5c..bf558b4f1 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1361,7 +1361,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523095349-9a252fc137f7 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From 96556fe465ed3b1f2a449151dc26c826270817b1 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 16:18:52 +0530 Subject: [PATCH 13/35] fix --- common-lib/utils/k8s/K8sService.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common-lib/utils/k8s/K8sService.go b/common-lib/utils/k8s/K8sService.go index 51e7b70d7..b3b69c2fc 100644 --- a/common-lib/utils/k8s/K8sService.go +++ b/common-lib/utils/k8s/K8sService.go @@ -76,7 +76,10 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) From 68ba8f20a486912b106335765466c493cfde80f8 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 16:22:20 +0530 Subject: [PATCH 14/35] bump common lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 ++-- .../devtron-labs/common-lib/utils/k8s/k8sService.go | 3 +++ kubewatch/vendor/modules.txt | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 1640cd7d3..7733f9ec2 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed diff --git a/kubewatch/go.sum b/kubewatch/go.sum index 7aff391ce..f466f8066 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 h1:PtkkGF5gADOACsKilvsXyl1P3Ea6nI8s2kTd34/uEmg= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed h1:p2UxZOCGM5LbWIuygdPEAUOfMpuJiGC+Ljqez3OUMn8= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index 51e7b70d7..b3b69c2fc 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -76,7 +76,10 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index f2aadf160..71ce96899 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523101902-c0371be1ca58 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed From 996f82c360f31fffda1cb21468867d3d32abec17 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 16:23:00 +0530 Subject: [PATCH 15/35] bump common lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 ++-- .../devtron-labs/common-lib/utils/k8s/k8sService.go | 3 +++ kubelink/vendor/modules.txt | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index b353b9990..76af7d94d 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index 879da5fd1..0de3d7c88 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff h1:CN6gTI0H4WvshMeBg0a07LrqJcELoZzVYpmSGuesKPw= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed h1:p2UxZOCGM5LbWIuygdPEAUOfMpuJiGC+Ljqez3OUMn8= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index 51e7b70d7..b3b69c2fc 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -76,7 +76,10 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, + labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) + DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error) CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index bf558b4f1..ef2378dd0 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1361,7 +1361,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523102424-df280dc551ff +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From 60bd9242188e3935172e6793402aff6533755e3e Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:18:44 +0530 Subject: [PATCH 16/35] r --- kubewatch/pkg/informer/cluster/informer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/kubewatch/pkg/informer/cluster/informer.go b/kubewatch/pkg/informer/cluster/informer.go index cf6c764c1..9b2a9e0aa 100644 --- a/kubewatch/pkg/informer/cluster/informer.go +++ b/kubewatch/pkg/informer/cluster/informer.go @@ -121,6 +121,7 @@ func (impl *InformerImpl) StartDevtronClusterWatcher() error { AddFuncHandler(addFunc). UpdateFuncHandler(updateFunc). DeleteFuncHandler(deleteFunc) + clusterLabels := informerBean.NewClusterLabels(clusterInfo.ClusterName, clusterInfo.Id) cmFactory, err := informerFactory.GetSharedInformerFactory(restConfig, clusterLabels, eventHandler, labelOptions) if err != nil { From c5591ade649bcdc4035cabc887ee5d5bea7927fa Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:34:55 +0530 Subject: [PATCH 17/35] ClusterModifyEventCmLabelValue = "cluster-request-modify" --- common-lib/informer/bean.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index 11b9b785d..3a5197912 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -24,6 +24,7 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelValue = "cluster-request-modify" ) const ( From 48b990c3efb12d2b26695d2ba23752a129af4c3c Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:38:10 +0530 Subject: [PATCH 18/35] ClusterModifyEventCmLabelValue = "type=cluster-request-modify" --- common-lib/informer/bean.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index 3a5197912..6d49ec8b4 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -24,7 +24,7 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelValue = "cluster-request-modify" + ClusterModifyEventCmLabelValue = "type=cluster-request-modify" ) const ( From 93c5820f74696e04bb843ac9dbb73933b6cc2a30 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:42:44 +0530 Subject: [PATCH 19/35] import label selector from commonb lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 ++-- kubewatch/pkg/informer/bean/bean.go | 4 +--- kubewatch/pkg/informer/cluster/informer.go | 3 ++- .../github.com/devtron-labs/common-lib/informer/bean.go | 1 + kubewatch/vendor/modules.txt | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 7733f9ec2..bab823170 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index f466f8066..140195add 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed h1:p2UxZOCGM5LbWIuygdPEAUOfMpuJiGC+Ljqez3OUMn8= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 h1:MphCFSgdG0o803tU67k7Bi2E4+ad0XIQom+2ZRDkXKg= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/pkg/informer/bean/bean.go b/kubewatch/pkg/informer/bean/bean.go index 6a4e9aedc..ef26b0f01 100644 --- a/kubewatch/pkg/informer/bean/bean.go +++ b/kubewatch/pkg/informer/bean/bean.go @@ -26,9 +26,7 @@ type ClusterInfo struct { } const ( - ClusterModifyEventFieldSelector = "type==cluster.request/modify" - ClusterModifyEventLabelSelector = "type=cluster.request/modify" - InformerAlreadyExistMessage = "INFORMER_ALREADY_EXIST" + InformerAlreadyExistMessage = "INFORMER_ALREADY_EXIST" ExitCode143Error = "Error (exit code 143)" NodeNoLongerExists = "PodGC: node no longer exists" diff --git a/kubewatch/pkg/informer/cluster/informer.go b/kubewatch/pkg/informer/cluster/informer.go index 9b2a9e0aa..b4b08b677 100644 --- a/kubewatch/pkg/informer/cluster/informer.go +++ b/kubewatch/pkg/informer/cluster/informer.go @@ -18,6 +18,7 @@ package cluster import ( "errors" + "github.com/devtron-labs/common-lib/informer" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" repository "github.com/devtron-labs/kubewatch/pkg/cluster" "github.com/devtron-labs/kubewatch/pkg/config" @@ -95,7 +96,7 @@ func (impl *InformerImpl) StartDevtronClusterWatcher() error { } impl.logger.Debug("starting informer, reading new cluster request for default cluster", "clusterId", clusterInfo.Id, "clusterName", clusterInfo.ClusterName) labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = informerBean.ClusterModifyEventLabelSelector + opts.LabelSelector = informer.ClusterModifyEventCmLabelValue }) // addFunc is called when a new cm is created addFunc := func(cmObject *coreV1.ConfigMap) { diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 11b9b785d..6d49ec8b4 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -24,6 +24,7 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelValue = "type=cluster-request-modify" ) const ( diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 71ce96899..9e69d816d 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 From 5ed7eaadfc41d96bc7852378cdc809606215c88e Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:46:11 +0530 Subject: [PATCH 20/35] import label selector from commonb lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 ++-- kubelink/pkg/k8sInformer/K8sInformer.go | 2 +- .../github.com/devtron-labs/common-lib/informer/bean.go | 1 + kubelink/vendor/modules.txt | 4 ++-- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index 76af7d94d..5370d9d05 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index 0de3d7c88..631f379d4 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed h1:p2UxZOCGM5LbWIuygdPEAUOfMpuJiGC+Ljqez3OUMn8= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 h1:jYx4VO8NdxBdZF0gKmrhJ+TvMowMmgvzKNjuRPpjBaI= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index 15f7d68fd..f5a0bf769 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -192,7 +192,7 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { if clusterInfo.ClusterName == commonBean.DEFAULT_CLUSTER { impl.logger.Debugw("starting informer, reading new cluster request for default cluster") labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = "type=cluster.request/modify" + opts.LabelSelector = informerBean.ClusterModifyEventCmLabelValue }) informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clusterClient, 15*time.Minute, labelOptions) stopper := make(chan struct{}) diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 11b9b785d..6d49ec8b4 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -24,6 +24,7 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelValue = "type=cluster-request-modify" ) const ( diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index ef2378dd0..0a03cc083 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1361,7 +1361,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523104852-96556fe465ed +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From 17f2964f5cd9f89760ec6fa932a2492761e01626 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:53:14 +0530 Subject: [PATCH 21/35] ClusterModifyEventCmLabelKeyValue and ClusterModifyEventCmLabelValue --- common-lib/informer/bean.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index 6d49ec8b4..6c9fa9f9b 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -17,14 +17,15 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterModifyEventSecretTypeKey = "type" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - CmFieldAction = "action" - CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelValue = "type=cluster-request-modify" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" + ClusterModifyEventCmLabelValue = "cluster-request-modify" ) const ( From 1205ee28e2202b2a15267f8fab71faa6757c1950 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:56:49 +0530 Subject: [PATCH 22/35] small fix --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 ++-- kubewatch/pkg/informer/cluster/informer.go | 2 +- .../devtron-labs/common-lib/informer/bean.go | 17 +++++++++-------- kubewatch/vendor/modules.txt | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index bab823170..5cc983b42 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index 140195add..e83be93ee 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 h1:MphCFSgdG0o803tU67k7Bi2E4+ad0XIQom+2ZRDkXKg= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 h1:5ujZrqGb1ZZxNL2yfEW7VycmfVb/h5uY62q5lCWWKr4= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/pkg/informer/cluster/informer.go b/kubewatch/pkg/informer/cluster/informer.go index b4b08b677..b9cac5dc5 100644 --- a/kubewatch/pkg/informer/cluster/informer.go +++ b/kubewatch/pkg/informer/cluster/informer.go @@ -96,7 +96,7 @@ func (impl *InformerImpl) StartDevtronClusterWatcher() error { } impl.logger.Debug("starting informer, reading new cluster request for default cluster", "clusterId", clusterInfo.Id, "clusterName", clusterInfo.ClusterName) labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = informer.ClusterModifyEventCmLabelValue + opts.LabelSelector = informer.ClusterModifyEventCmLabelKeyValue }) // addFunc is called when a new cm is created addFunc := func(cmObject *coreV1.ConfigMap) { diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 6d49ec8b4..6c9fa9f9b 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -17,14 +17,15 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterModifyEventSecretTypeKey = "type" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - CmFieldAction = "action" - CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelValue = "type=cluster-request-modify" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" + ClusterModifyEventCmLabelValue = "cluster-request-modify" ) const ( diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 9e69d816d..6d1909222 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1768,4 +1768,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523140810-48b990c3efb1 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 From 01ed8fbeb6e54ccf2d9b5ef774bae03a084013fa Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Fri, 23 May 2025 19:59:30 +0530 Subject: [PATCH 23/35] small fix --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 ++-- kubelink/pkg/k8sInformer/K8sInformer.go | 2 +- .../devtron-labs/common-lib/informer/bean.go | 17 +++++++++-------- kubelink/vendor/modules.txt | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index 5370d9d05..c2a0f40ce 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index 631f379d4..581abf287 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 h1:jYx4VO8NdxBdZF0gKmrhJ+TvMowMmgvzKNjuRPpjBaI= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 h1:7nSMqZGx0/xaPdarByIRbYW2O0PFAf0GTZmM+CSO6is= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index f5a0bf769..f84b0a57e 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -192,7 +192,7 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { if clusterInfo.ClusterName == commonBean.DEFAULT_CLUSTER { impl.logger.Debugw("starting informer, reading new cluster request for default cluster") labelOptions := kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) { - opts.LabelSelector = informerBean.ClusterModifyEventCmLabelValue + opts.LabelSelector = informerBean.ClusterModifyEventCmLabelKeyValue }) informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clusterClient, 15*time.Minute, labelOptions) stopper := make(chan struct{}) diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 6d49ec8b4..6c9fa9f9b 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -17,14 +17,15 @@ package informer const ( - ClusterModifyEventSecretType = "cluster.request/modify" - ClusterModifyEventSecretTypeKey = "type" - ClusterActionAdd = "add" - ClusterActionUpdate = "update" - ClusterActionDelete = "delete" - CmFieldAction = "action" - CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelValue = "type=cluster-request-modify" + ClusterModifyEventSecretType = "cluster.request/modify" + ClusterModifyEventSecretTypeKey = "type" + ClusterActionAdd = "add" + ClusterActionUpdate = "update" + ClusterActionDelete = "delete" + CmFieldAction = "action" + CmFieldClusterId = "cluster_id" + ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" + ClusterModifyEventCmLabelValue = "cluster-request-modify" ) const ( diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index 0a03cc083..38daf6b02 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1361,7 +1361,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523141244-93c5820f7469 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From 222f1e0c7ce7cb36cd2bb6a3296795ea4eb44679 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 12:08:55 +0530 Subject: [PATCH 24/35] add logger for ignoring cluster change event in case label not found --- kubewatch/pkg/informer/cluster/helper.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kubewatch/pkg/informer/cluster/helper.go b/kubewatch/pkg/informer/cluster/helper.go index e38dac66d..b6cd9c6ec 100644 --- a/kubewatch/pkg/informer/cluster/helper.go +++ b/kubewatch/pkg/informer/cluster/helper.go @@ -115,6 +115,7 @@ func (impl *InformerImpl) startInformerForCluster(clusterInfo *repository.Cluste func (impl *InformerImpl) handleClusterChangeEvent(cmObject *coreV1.ConfigMap) error { if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + impl.logger.Infow("label value not found in cm, hence ignoring cluster change event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return nil } data := cmObject.Data @@ -141,6 +142,7 @@ func (impl *InformerImpl) handleClusterChangeEvent(cmObject *coreV1.ConfigMap) e func (impl *InformerImpl) handleClusterDeleteEvent(cmObject *coreV1.ConfigMap) error { if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + impl.logger.Infow("label value not found in cm, hence ignoring cluster delete event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return nil } data := cmObject.Data From 556a5a97537bdb229d733795367650c737f583c4 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 12:11:21 +0530 Subject: [PATCH 25/35] fix --- kubewatch/pkg/informer/cluster/helper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kubewatch/pkg/informer/cluster/helper.go b/kubewatch/pkg/informer/cluster/helper.go index b6cd9c6ec..2e7408e78 100644 --- a/kubewatch/pkg/informer/cluster/helper.go +++ b/kubewatch/pkg/informer/cluster/helper.go @@ -114,7 +114,7 @@ func (impl *InformerImpl) startInformerForCluster(clusterInfo *repository.Cluste } func (impl *InformerImpl) handleClusterChangeEvent(cmObject *coreV1.ConfigMap) error { - if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventCmLabelValue { impl.logger.Infow("label value not found in cm, hence ignoring cluster change event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return nil } @@ -141,7 +141,7 @@ func (impl *InformerImpl) handleClusterChangeEvent(cmObject *coreV1.ConfigMap) e } func (impl *InformerImpl) handleClusterDeleteEvent(cmObject *coreV1.ConfigMap) error { - if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventCmLabelValue { impl.logger.Infow("label value not found in cm, hence ignoring cluster delete event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return nil } From d40f917859f84e003fa0ea546801d47686cae497 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 12:15:57 +0530 Subject: [PATCH 26/35] logger --- kubelink/pkg/k8sInformer/K8sInformer.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kubelink/pkg/k8sInformer/K8sInformer.go b/kubelink/pkg/k8sInformer/K8sInformer.go index f84b0a57e..50377e4de 100644 --- a/kubelink/pkg/k8sInformer/K8sInformer.go +++ b/kubelink/pkg/k8sInformer/K8sInformer.go @@ -202,7 +202,8 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_ADD_INFORMER: cluster cm add event received", "obj", obj, "time", time.Now()) if cmObject, ok := obj.(*coreV1.ConfigMap); ok { - if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventCmLabelValue { + impl.logger.Infow("label value not found in cm, hence ignoring cluster add event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return } data := cmObject.Data @@ -235,7 +236,8 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_UPDATE_INFORMER: cluster cm update event received", "oldObj", oldObj, "newObj", newObj, "time", time.Now()) if cmObject, ok := newObj.(*coreV1.ConfigMap); ok { - if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventCmLabelValue { + impl.logger.Infow("label value not found in cm, hence ignoring cluster update event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return } data := cmObject.Data @@ -265,7 +267,8 @@ func (impl *K8sInformerImpl) startInformer(clusterInfo bean.ClusterInfo) error { startTime := time.Now() impl.logger.Debugw("CLUSTER_DELETE_INFORMER: cm delete event received", "obj", obj, "time", time.Now()) if cmObject, ok := obj.(*coreV1.ConfigMap); ok { - if labelValue, exists := cmObject.Labels["type"]; !exists || labelValue != informerBean.ClusterModifyEventSecretType { + if labelValue, exists := cmObject.Labels[informerBean.ClusterModifyEventSecretTypeKey]; !exists || labelValue != informerBean.ClusterModifyEventCmLabelValue { + impl.logger.Infow("label value not found in cm, hence ignoring cluster delete event", "labelKey", informerBean.ClusterModifyEventSecretTypeKey, "labelValue", labelValue) return } data := cmObject.Data From 5f0ce09e1cf32c7e0630f7b27e2cb115f43c2f01 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 13:21:46 +0530 Subject: [PATCH 27/35] refactoring of CreateConfigMapObject --- common-lib/utils/k8s/K8sService.go | 4 +- common-lib/utils/k8s/K8sUtil.go | 22 +++------- common-lib/utils/k8s/configMap/options.go | 52 +++++++++++++++++++++++ 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 common-lib/utils/k8s/configMap/options.go diff --git a/common-lib/utils/k8s/K8sService.go b/common-lib/utils/k8s/K8sService.go index b3b69c2fc..914624507 100644 --- a/common-lib/utils/k8s/K8sService.go +++ b/common-lib/utils/k8s/K8sService.go @@ -19,6 +19,7 @@ package k8s import ( "context" "flag" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "go.uber.org/zap" "io" batchV1 "k8s.io/api/batch/v1" @@ -76,8 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index 0f313f2c7..2685ff37c 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -24,6 +24,7 @@ import ( "github.com/devtron-labs/common-lib/utils" http2 "github.com/devtron-labs/common-lib/utils/http" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "io" "k8s.io/client-go/dynamic" "k8s.io/kubernetes/pkg/api/legacyscheme" @@ -236,23 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { - configMap := &v1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: configMapName, - }, - } - if labels != nil && len(labels) > 0 { - configMap.ObjectMeta.Labels = labels - } - if annotations != nil && len(annotations) > 0 { - configMap.ObjectMeta.Annotations = annotations - } - if data != nil && len(data) > 0 { - configMap.Data = data - } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{} + for _, option := range opts { + option(configMap) + } return impl.CreateConfigMap(namespace, configMap, client) } diff --git a/common-lib/utils/k8s/configMap/options.go b/common-lib/utils/k8s/configMap/options.go new file mode 100644 index 000000000..afa2122ef --- /dev/null +++ b/common-lib/utils/k8s/configMap/options.go @@ -0,0 +1,52 @@ +package configMap + +import ( + v1 "k8s.io/api/core/v1" +) + +type Option func(*v1.ConfigMap) + +// WithCmName adds config map name to a ConfigMap +func WithCmName(name string) Option { + return func(cm *v1.ConfigMap) { + if len(name) > 0 { + cm.ObjectMeta.Name = name + } + } +} + +// WithLabels adds labels to a ConfigMap +func WithLabels(labels map[string]string) Option { + return func(cm *v1.ConfigMap) { + if labels != nil && len(labels) > 0 { + cm.ObjectMeta.Labels = labels + } + } +} + +// WithAnnotations adds annotations to a ConfigMap +func WithAnnotations(annotations map[string]string) Option { + return func(cm *v1.ConfigMap) { + if annotations != nil && len(annotations) > 0 { + cm.ObjectMeta.Annotations = annotations + } + } +} + +// WithData adds string data to a ConfigMap +func WithData(data map[string]string) Option { + return func(cm *v1.ConfigMap) { + if data != nil && len(data) > 0 { + cm.Data = data + } + } +} + +// WithBinaryData adds binary data to a ConfigMap +func WithBinaryData(binaryData map[string][]byte) Option { + return func(cm *v1.ConfigMap) { + if binaryData != nil && len(binaryData) > 0 { + cm.BinaryData = binaryData + } + } +} From f3a156dfc16680a5315a184df96e1d1eea28c2ec Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 13:27:47 +0530 Subject: [PATCH 28/35] fix --- common-lib/utils/k8s/configMap/{options.go => Options.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename common-lib/utils/k8s/configMap/{options.go => Options.go} (100%) diff --git a/common-lib/utils/k8s/configMap/options.go b/common-lib/utils/k8s/configMap/Options.go similarity index 100% rename from common-lib/utils/k8s/configMap/options.go rename to common-lib/utils/k8s/configMap/Options.go From 323af49b03f08c8155b4a38a513185438a8b0b17 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 13:52:12 +0530 Subject: [PATCH 29/35] bump common lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 +- .../common-lib/utils/grpc/GrpcConfig.go | 6 ++- .../common-lib/utils/k8s/K8sUtil.go | 22 +++----- .../common-lib/utils/k8s/configMap/Options.go | 52 +++++++++++++++++++ .../common-lib/utils/k8s/k8sService.go | 4 +- kubelink/vendor/modules.txt | 5 +- 7 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go diff --git a/kubelink/go.mod b/kubelink/go.mod index c2a0f40ce..6108ef988 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index 581abf287..f085ee9a2 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 h1:7nSMqZGx0/xaPdarByIRbYW2O0PFAf0GTZmM+CSO6is= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a h1:6E0BTI4Pr/zEqd5TVOV9lpbCIV/u9/cRfseu7Ae291w= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/grpc/GrpcConfig.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/grpc/GrpcConfig.go index 0ac82eeda..f9c7e4e9a 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/grpc/GrpcConfig.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/grpc/GrpcConfig.go @@ -2,9 +2,11 @@ package grpc import "github.com/caarlos0/env" +// CATEGORY=INFRA_SETUP type Configuration struct { - KubelinkMaxRecvMsgSize int `env:"KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE" envDefault:"20"` // In mb - KubelinkMaxSendMsgSize int `env:"KUBELINK_GRPC_MAX_SEND_MSG_SIZE" envDefault:"4"` // In mb + KubelinkMaxRecvMsgSize int `env:"KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE" envDefault:"20"` // In mb + KubelinkMaxSendMsgSize int `env:"KUBELINK_GRPC_MAX_SEND_MSG_SIZE" envDefault:"4"` // In mb + KubelinkGRPCServiceConfig string `env:"KUBELINK_GRPC_SERVICE_CONFIG" envDefault:"{\"loadBalancingPolicy\":\"round_robin\"}" description:"kubelink grpc service config"` } func GetConfiguration() (*Configuration, error) { diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 0f313f2c7..2685ff37c 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -24,6 +24,7 @@ import ( "github.com/devtron-labs/common-lib/utils" http2 "github.com/devtron-labs/common-lib/utils/http" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "io" "k8s.io/client-go/dynamic" "k8s.io/kubernetes/pkg/api/legacyscheme" @@ -236,23 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { - configMap := &v1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: configMapName, - }, - } - if labels != nil && len(labels) > 0 { - configMap.ObjectMeta.Labels = labels - } - if annotations != nil && len(annotations) > 0 { - configMap.ObjectMeta.Annotations = annotations - } - if data != nil && len(data) > 0 { - configMap.Data = data - } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{} + for _, option := range opts { + option(configMap) + } return impl.CreateConfigMap(namespace, configMap, client) } diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go new file mode 100644 index 000000000..afa2122ef --- /dev/null +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go @@ -0,0 +1,52 @@ +package configMap + +import ( + v1 "k8s.io/api/core/v1" +) + +type Option func(*v1.ConfigMap) + +// WithCmName adds config map name to a ConfigMap +func WithCmName(name string) Option { + return func(cm *v1.ConfigMap) { + if len(name) > 0 { + cm.ObjectMeta.Name = name + } + } +} + +// WithLabels adds labels to a ConfigMap +func WithLabels(labels map[string]string) Option { + return func(cm *v1.ConfigMap) { + if labels != nil && len(labels) > 0 { + cm.ObjectMeta.Labels = labels + } + } +} + +// WithAnnotations adds annotations to a ConfigMap +func WithAnnotations(annotations map[string]string) Option { + return func(cm *v1.ConfigMap) { + if annotations != nil && len(annotations) > 0 { + cm.ObjectMeta.Annotations = annotations + } + } +} + +// WithData adds string data to a ConfigMap +func WithData(data map[string]string) Option { + return func(cm *v1.ConfigMap) { + if data != nil && len(data) > 0 { + cm.Data = data + } + } +} + +// WithBinaryData adds binary data to a ConfigMap +func WithBinaryData(binaryData map[string][]byte) Option { + return func(cm *v1.ConfigMap) { + if binaryData != nil && len(binaryData) > 0 { + cm.BinaryData = binaryData + } + } +} diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index b3b69c2fc..914624507 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -19,6 +19,7 @@ package k8s import ( "context" "flag" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "go.uber.org/zap" "io" batchV1 "k8s.io/api/batch/v1" @@ -76,8 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index 38daf6b02..4c1b39e05 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -148,6 +148,7 @@ github.com/devtron-labs/common-lib/utils/grpc github.com/devtron-labs/common-lib/utils/http github.com/devtron-labs/common-lib/utils/k8s github.com/devtron-labs/common-lib/utils/k8s/commonBean +github.com/devtron-labs/common-lib/utils/k8s/configMap github.com/devtron-labs/common-lib/utils/k8s/health github.com/devtron-labs/common-lib/utils/k8sObjectsUtil github.com/devtron-labs/common-lib/utils/reflectUtils @@ -1361,7 +1362,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142649-1205ee28e220 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From b13ea5f934098fd4fd2408ff88e053c5ae696fd7 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 14:27:45 +0530 Subject: [PATCH 30/35] bump common lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 +- .../common-lib/utils/k8s/K8sUtil.go | 22 +++----- .../common-lib/utils/k8s/configMap/Options.go | 52 +++++++++++++++++++ .../common-lib/utils/k8s/k8sService.go | 4 +- kubewatch/vendor/modules.txt | 5 +- 6 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 5cc983b42..2929d14e7 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index e83be93ee..5c6af751a 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 h1:5ujZrqGb1ZZxNL2yfEW7VycmfVb/h5uY62q5lCWWKr4= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 h1:l8Ym2QGKO1V6VfAA0aD7Er3mYnkKmJzliFtfk1zmDJQ= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 0f313f2c7..2685ff37c 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -24,6 +24,7 @@ import ( "github.com/devtron-labs/common-lib/utils" http2 "github.com/devtron-labs/common-lib/utils/http" "github.com/devtron-labs/common-lib/utils/k8s/commonBean" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "io" "k8s.io/client-go/dynamic" "k8s.io/kubernetes/pkg/api/legacyscheme" @@ -236,23 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) { - configMap := &v1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: configMapName, - }, - } - if labels != nil && len(labels) > 0 { - configMap.ObjectMeta.Labels = labels - } - if annotations != nil && len(annotations) > 0 { - configMap.ObjectMeta.Annotations = annotations - } - if data != nil && len(data) > 0 { - configMap.Data = data - } +func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{} + for _, option := range opts { + option(configMap) + } return impl.CreateConfigMap(namespace, configMap, client) } diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go new file mode 100644 index 000000000..afa2122ef --- /dev/null +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go @@ -0,0 +1,52 @@ +package configMap + +import ( + v1 "k8s.io/api/core/v1" +) + +type Option func(*v1.ConfigMap) + +// WithCmName adds config map name to a ConfigMap +func WithCmName(name string) Option { + return func(cm *v1.ConfigMap) { + if len(name) > 0 { + cm.ObjectMeta.Name = name + } + } +} + +// WithLabels adds labels to a ConfigMap +func WithLabels(labels map[string]string) Option { + return func(cm *v1.ConfigMap) { + if labels != nil && len(labels) > 0 { + cm.ObjectMeta.Labels = labels + } + } +} + +// WithAnnotations adds annotations to a ConfigMap +func WithAnnotations(annotations map[string]string) Option { + return func(cm *v1.ConfigMap) { + if annotations != nil && len(annotations) > 0 { + cm.ObjectMeta.Annotations = annotations + } + } +} + +// WithData adds string data to a ConfigMap +func WithData(data map[string]string) Option { + return func(cm *v1.ConfigMap) { + if data != nil && len(data) > 0 { + cm.Data = data + } + } +} + +// WithBinaryData adds binary data to a ConfigMap +func WithBinaryData(binaryData map[string][]byte) Option { + return func(cm *v1.ConfigMap) { + if binaryData != nil && len(binaryData) > 0 { + cm.BinaryData = binaryData + } + } +} diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index b3b69c2fc..914624507 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -19,6 +19,7 @@ package k8s import ( "context" "flag" + "github.com/devtron-labs/common-lib/utils/k8s/configMap" "go.uber.org/zap" "io" batchV1 "k8s.io/api/batch/v1" @@ -76,8 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, data map[string]string, configMapName string, client *v12.CoreV1Client, - labels map[string]string, annotations map[string]string) (*v1.ConfigMap, error) + CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 6d1909222..33e77089c 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -233,6 +233,7 @@ github.com/devtron-labs/common-lib/utils/bean github.com/devtron-labs/common-lib/utils/http github.com/devtron-labs/common-lib/utils/k8s github.com/devtron-labs/common-lib/utils/k8s/commonBean +github.com/devtron-labs/common-lib/utils/k8s/configMap github.com/devtron-labs/common-lib/utils/k8s/health github.com/devtron-labs/common-lib/utils/k8sObjectsUtil github.com/devtron-labs/common-lib/utils/remoteConnection/bean @@ -1768,4 +1769,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250523142314-17f2964f5cd9 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 From 626bf7f73dc7047b9c63a802f4fdd89f3b30bc5f Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 16:12:13 +0530 Subject: [PATCH 31/35] remove WithCmName and make cmname as func signature --- common-lib/utils/k8s/K8sService.go | 2 +- common-lib/utils/k8s/K8sUtil.go | 9 ++++++--- common-lib/utils/k8s/configMap/Options.go | 9 --------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/common-lib/utils/k8s/K8sService.go b/common-lib/utils/k8s/K8sService.go index 914624507..94e4cb1e7 100644 --- a/common-lib/utils/k8s/K8sService.go +++ b/common-lib/utils/k8s/K8sService.go @@ -77,7 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) + CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index 2685ff37c..21e1877fe 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -237,9 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { - - configMap := &v1.ConfigMap{} +func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + } for _, option := range opts { option(configMap) } diff --git a/common-lib/utils/k8s/configMap/Options.go b/common-lib/utils/k8s/configMap/Options.go index afa2122ef..5bb176d58 100644 --- a/common-lib/utils/k8s/configMap/Options.go +++ b/common-lib/utils/k8s/configMap/Options.go @@ -6,15 +6,6 @@ import ( type Option func(*v1.ConfigMap) -// WithCmName adds config map name to a ConfigMap -func WithCmName(name string) Option { - return func(cm *v1.ConfigMap) { - if len(name) > 0 { - cm.ObjectMeta.Name = name - } - } -} - // WithLabels adds labels to a ConfigMap func WithLabels(labels map[string]string) Option { return func(cm *v1.ConfigMap) { From 2439202dc0123d089db134e783153ac739852aae Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 19:22:46 +0530 Subject: [PATCH 32/35] code review incorporation --- common-lib/informer/bean.go | 4 ++-- common-lib/utils/k8s/K8sService.go | 2 +- common-lib/utils/k8s/K8sUtil.go | 2 +- common-lib/utils/k8s/configMap/Options.go | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/common-lib/informer/bean.go b/common-lib/informer/bean.go index 6c9fa9f9b..9601f6f8f 100644 --- a/common-lib/informer/bean.go +++ b/common-lib/informer/bean.go @@ -24,8 +24,8 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" - ClusterModifyEventCmLabelValue = "cluster-request-modify" + ClusterModifyEventCmLabelKeyValue = "type=devtron.ai-cluster-request-modify" + ClusterModifyEventCmLabelValue = "devtron.ai-cluster-request-modify" ) const ( diff --git a/common-lib/utils/k8s/K8sService.go b/common-lib/utils/k8s/K8sService.go index 94e4cb1e7..ccd2f49d7 100644 --- a/common-lib/utils/k8s/K8sService.go +++ b/common-lib/utils/k8s/K8sService.go @@ -77,7 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) + CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index 21e1877fe..0d5bc059f 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -237,7 +237,7 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { +func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) { configMap := &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: name, diff --git a/common-lib/utils/k8s/configMap/Options.go b/common-lib/utils/k8s/configMap/Options.go index 5bb176d58..20cca36d1 100644 --- a/common-lib/utils/k8s/configMap/Options.go +++ b/common-lib/utils/k8s/configMap/Options.go @@ -4,10 +4,10 @@ import ( v1 "k8s.io/api/core/v1" ) -type Option func(*v1.ConfigMap) +type ConfigMapOption func(*v1.ConfigMap) // WithLabels adds labels to a ConfigMap -func WithLabels(labels map[string]string) Option { +func WithLabels(labels map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if labels != nil && len(labels) > 0 { cm.ObjectMeta.Labels = labels @@ -16,7 +16,7 @@ func WithLabels(labels map[string]string) Option { } // WithAnnotations adds annotations to a ConfigMap -func WithAnnotations(annotations map[string]string) Option { +func WithAnnotations(annotations map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if annotations != nil && len(annotations) > 0 { cm.ObjectMeta.Annotations = annotations @@ -25,7 +25,7 @@ func WithAnnotations(annotations map[string]string) Option { } // WithData adds string data to a ConfigMap -func WithData(data map[string]string) Option { +func WithData(data map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if data != nil && len(data) > 0 { cm.Data = data @@ -34,7 +34,7 @@ func WithData(data map[string]string) Option { } // WithBinaryData adds binary data to a ConfigMap -func WithBinaryData(binaryData map[string][]byte) Option { +func WithBinaryData(binaryData map[string][]byte) ConfigMapOption { return func(cm *v1.ConfigMap) { if binaryData != nil && len(binaryData) > 0 { cm.BinaryData = binaryData From 483d6a26611995a2477b6d13d492bb0de67a5aa9 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Tue, 27 May 2025 23:38:44 +0530 Subject: [PATCH 33/35] bump common lib --- kubelink/go.mod | 2 +- kubelink/go.sum | 4 ++-- .../devtron-labs/common-lib/informer/bean.go | 4 ++-- .../common-lib/utils/k8s/K8sUtil.go | 9 ++++++--- .../common-lib/utils/k8s/configMap/Options.go | 19 +++++-------------- .../common-lib/utils/k8s/k8sService.go | 2 +- kubelink/vendor/modules.txt | 4 ++-- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/kubelink/go.mod b/kubelink/go.mod index 6108ef988..92a0151f7 100644 --- a/kubelink/go.mod +++ b/kubelink/go.mod @@ -177,7 +177,7 @@ require ( ) replace ( - github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a + github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527135246-2439202dc012 go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 // https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280 k8s.io/api => k8s.io/api v0.29.0 diff --git a/kubelink/go.sum b/kubelink/go.sum index f085ee9a2..6aaa08a9d 100644 --- a/kubelink/go.sum +++ b/kubelink/go.sum @@ -79,8 +79,8 @@ github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a h1:6E0BTI4Pr/zEqd5TVOV9lpbCIV/u9/cRfseu7Ae291w= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527135246-2439202dc012 h1:gDhoQPfKvabZJPF7pqJOSXJxBZzie7Hink8UWcH5qB4= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527135246-2439202dc012/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic= diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 6c9fa9f9b..9601f6f8f 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -24,8 +24,8 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" - ClusterModifyEventCmLabelValue = "cluster-request-modify" + ClusterModifyEventCmLabelKeyValue = "type=devtron.ai-cluster-request-modify" + ClusterModifyEventCmLabelValue = "devtron.ai-cluster-request-modify" ) const ( diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 2685ff37c..0d5bc059f 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -237,9 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { - - configMap := &v1.ConfigMap{} +func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + } for _, option := range opts { option(configMap) } diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go index afa2122ef..20cca36d1 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go @@ -4,19 +4,10 @@ import ( v1 "k8s.io/api/core/v1" ) -type Option func(*v1.ConfigMap) - -// WithCmName adds config map name to a ConfigMap -func WithCmName(name string) Option { - return func(cm *v1.ConfigMap) { - if len(name) > 0 { - cm.ObjectMeta.Name = name - } - } -} +type ConfigMapOption func(*v1.ConfigMap) // WithLabels adds labels to a ConfigMap -func WithLabels(labels map[string]string) Option { +func WithLabels(labels map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if labels != nil && len(labels) > 0 { cm.ObjectMeta.Labels = labels @@ -25,7 +16,7 @@ func WithLabels(labels map[string]string) Option { } // WithAnnotations adds annotations to a ConfigMap -func WithAnnotations(annotations map[string]string) Option { +func WithAnnotations(annotations map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if annotations != nil && len(annotations) > 0 { cm.ObjectMeta.Annotations = annotations @@ -34,7 +25,7 @@ func WithAnnotations(annotations map[string]string) Option { } // WithData adds string data to a ConfigMap -func WithData(data map[string]string) Option { +func WithData(data map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if data != nil && len(data) > 0 { cm.Data = data @@ -43,7 +34,7 @@ func WithData(data map[string]string) Option { } // WithBinaryData adds binary data to a ConfigMap -func WithBinaryData(binaryData map[string][]byte) Option { +func WithBinaryData(binaryData map[string][]byte) ConfigMapOption { return func(cm *v1.ConfigMap) { if binaryData != nil && len(binaryData) > 0 { cm.BinaryData = binaryData diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index 914624507..ccd2f49d7 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -77,7 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) + CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/kubelink/vendor/modules.txt b/kubelink/vendor/modules.txt index 4c1b39e05..61d5bad6f 100644 --- a/kubelink/vendor/modules.txt +++ b/kubelink/vendor/modules.txt @@ -127,7 +127,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527135246-2439202dc012 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1362,7 +1362,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527080653-ddb036d5ca0a +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527135246-2439202dc012 # go.opentelemetry.io/otel/metric => go.opentelemetry.io/otel/metric v1.18.0 # k8s.io/api => k8s.io/api v0.29.0 # k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.29.0 From b913d2d0445f6e30fc5c7889a954e4df643deb09 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Wed, 28 May 2025 12:51:43 +0530 Subject: [PATCH 34/35] make --- kubelink/env_gen.json | 2 +- kubelink/env_gen.md | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/kubelink/env_gen.json b/kubelink/env_gen.json index 24bd483f0..a3ed54653 100644 --- a/kubelink/env_gen.json +++ b/kubelink/env_gen.json @@ -1 +1 @@ -[{"Category":"DEVTRON","Fields":[{"Env":"APP","EnvType":"string","EnvValue":"kubelink","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"CONSUMER_CONFIG_JSON","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"DEFAULT_LOG_TIME_LIMIT","EnvType":"int64","EnvValue":"1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"ENABLE_STATSVIZ","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_CLIENT_MAX_IDLE_CONNS_PER_HOST","EnvType":"int","EnvValue":"25","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_IDLE_CONN_TIMEOUT","EnvType":"int","EnvValue":"300","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_KEEPALIVE","EnvType":"int","EnvValue":"30","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_TIMEOUT","EnvType":"int","EnvValue":"30","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TLS_HANDSHAKE_TIMEOUT","EnvType":"int","EnvValue":"10","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE","EnvType":"int","EnvValue":"20","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"KUBELINK_GRPC_MAX_SEND_MSG_SIZE","EnvType":"int","EnvValue":"4","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"LOG_LEVEL","EnvType":"int","EnvValue":"-1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_ACK_WAIT_IN_SECS","EnvType":"int","EnvValue":"120","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_BUFFER_SIZE","EnvType":"int","EnvValue":"-1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_MAX_AGE","EnvType":"int","EnvValue":"86400","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_PROCESSING_BATCH_SIZE","EnvType":"int","EnvValue":"1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_REPLICAS","EnvType":"int","EnvValue":"0","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_SERVER_HOST","EnvType":"string","EnvValue":"nats://devtron-nats.devtroncd:4222","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_ADDR","EnvType":"string","EnvValue":"127.0.0.1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_DATABASE","EnvType":"string","EnvValue":"orchestrator","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_EXPORT_PROM_METRICS","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_ALL_FAILURE_QUERIES","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_ALL_QUERY","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_SLOW_QUERY","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_PASSWORD","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_PORT","EnvType":"string","EnvValue":"5432","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_QUERY_DUR_THRESHOLD","EnvType":"int64","EnvValue":"5000","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_USER","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"RUNTIME_CONFIG_LOCAL_DEV","EnvType":"LocalDevMode","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"STREAM_CONFIG_JSON","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"USE_CUSTOM_HTTP_TRANSPORT","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"}]},{"Category":"HELM_RELEASE","Fields":[{"Env":"BUILD_NODES_BATCH_SIZE","EnvType":"int","EnvValue":"2","EnvDescription":"Resource tree build nodes parallelism batch size (applied only for depth-1 child objects of a parent object)","Example":"2","Deprecated":"false"},{"Env":"CHART_WORKING_DIRECTORY","EnvType":"string","EnvValue":"/home/devtron/devtroncd/charts/","EnvDescription":"Helm charts working directory","Example":"/home/devtron/devtroncd/charts/","Deprecated":"false"},{"Env":"CHILD_OBJECT_LISTING_PAGE_SIZE","EnvType":"int64","EnvValue":"1000","EnvDescription":"Resource tree child object listing page size","Example":"100","Deprecated":"false"},{"Env":"ENABLE_HELM_RELEASE_CACHE","EnvType":"bool","EnvValue":"true","EnvDescription":"Enable helm releases list cache","Example":"true","Deprecated":"false"},{"Env":"FEAT_CHILD_OBJECT_LISTING_PAGINATION","EnvType":"bool","EnvValue":"true","EnvDescription":"use pagination in listing all the dependent child objects. use 'CHILD_OBJECT_LISTING_PAGE_SIZE' to set the page size.","Example":"true","Deprecated":"false"},{"Env":"MANIFEST_FETCH_BATCH_SIZE","EnvType":"int","EnvValue":"2","EnvDescription":"Manifest fetch parallelism batch size (applied only for parent objects)","Example":"2","Deprecated":"false"},{"Env":"MAX_COUNT_FOR_HELM_RELEASE","EnvType":"int","EnvValue":"20","EnvDescription":"Max count for helm release history list","Example":"20","Deprecated":"false"},{"Env":"PARENT_CHILD_GVK_MAPPING","EnvType":"string","EnvValue":"","EnvDescription":"Parent child GVK mapping for resource tree","Example":"","Deprecated":"false"},{"Env":"RUN_HELM_INSTALL_IN_ASYNC_MODE","EnvType":"bool","EnvValue":"false","EnvDescription":"Run helm install/ upgrade in async mode","Example":"false","Deprecated":"false"}]}] \ No newline at end of file +[{"Category":"DEVTRON","Fields":[{"Env":"APP","EnvType":"string","EnvValue":"kubelink","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"CONSUMER_CONFIG_JSON","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"DEFAULT_LOG_TIME_LIMIT","EnvType":"int64","EnvValue":"1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"ENABLE_STATSVIZ","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_CLIENT_MAX_IDLE_CONNS_PER_HOST","EnvType":"int","EnvValue":"25","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_IDLE_CONN_TIMEOUT","EnvType":"int","EnvValue":"300","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_KEEPALIVE","EnvType":"int","EnvValue":"30","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TCP_TIMEOUT","EnvType":"int","EnvValue":"30","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"K8s_TLS_HANDSHAKE_TIMEOUT","EnvType":"int","EnvValue":"10","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"LOG_LEVEL","EnvType":"int","EnvValue":"-1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_ACK_WAIT_IN_SECS","EnvType":"int","EnvValue":"120","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_BUFFER_SIZE","EnvType":"int","EnvValue":"-1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_MAX_AGE","EnvType":"int","EnvValue":"86400","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_PROCESSING_BATCH_SIZE","EnvType":"int","EnvValue":"1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_MSG_REPLICAS","EnvType":"int","EnvValue":"0","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"NATS_SERVER_HOST","EnvType":"string","EnvValue":"nats://devtron-nats.devtroncd:4222","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_ADDR","EnvType":"string","EnvValue":"127.0.0.1","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_DATABASE","EnvType":"string","EnvValue":"orchestrator","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_EXPORT_PROM_METRICS","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_ALL_FAILURE_QUERIES","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_ALL_QUERY","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_LOG_SLOW_QUERY","EnvType":"bool","EnvValue":"true","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_PASSWORD","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_PORT","EnvType":"string","EnvValue":"5432","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_QUERY_DUR_THRESHOLD","EnvType":"int64","EnvValue":"5000","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"PG_USER","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"RUNTIME_CONFIG_LOCAL_DEV","EnvType":"LocalDevMode","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"STREAM_CONFIG_JSON","EnvType":"string","EnvValue":"","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"USE_CUSTOM_HTTP_TRANSPORT","EnvType":"bool","EnvValue":"false","EnvDescription":"","Example":"","Deprecated":"false"}]},{"Category":"HELM_RELEASE","Fields":[{"Env":"BUILD_NODES_BATCH_SIZE","EnvType":"int","EnvValue":"2","EnvDescription":"Resource tree build nodes parallelism batch size (applied only for depth-1 child objects of a parent object)","Example":"2","Deprecated":"false"},{"Env":"CHART_WORKING_DIRECTORY","EnvType":"string","EnvValue":"/home/devtron/devtroncd/charts/","EnvDescription":"Helm charts working directory","Example":"/home/devtron/devtroncd/charts/","Deprecated":"false"},{"Env":"CHILD_OBJECT_LISTING_PAGE_SIZE","EnvType":"int64","EnvValue":"1000","EnvDescription":"Resource tree child object listing page size","Example":"100","Deprecated":"false"},{"Env":"ENABLE_HELM_RELEASE_CACHE","EnvType":"bool","EnvValue":"true","EnvDescription":"Enable helm releases list cache","Example":"true","Deprecated":"false"},{"Env":"FEAT_CHILD_OBJECT_LISTING_PAGINATION","EnvType":"bool","EnvValue":"true","EnvDescription":"use pagination in listing all the dependent child objects. use 'CHILD_OBJECT_LISTING_PAGE_SIZE' to set the page size.","Example":"true","Deprecated":"false"},{"Env":"MANIFEST_FETCH_BATCH_SIZE","EnvType":"int","EnvValue":"2","EnvDescription":"Manifest fetch parallelism batch size (applied only for parent objects)","Example":"2","Deprecated":"false"},{"Env":"MAX_COUNT_FOR_HELM_RELEASE","EnvType":"int","EnvValue":"20","EnvDescription":"Max count for helm release history list","Example":"20","Deprecated":"false"},{"Env":"PARENT_CHILD_GVK_MAPPING","EnvType":"string","EnvValue":"","EnvDescription":"Parent child GVK mapping for resource tree","Example":"","Deprecated":"false"},{"Env":"RUN_HELM_INSTALL_IN_ASYNC_MODE","EnvType":"bool","EnvValue":"false","EnvDescription":"Run helm install/ upgrade in async mode","Example":"false","Deprecated":"false"}]},{"Category":"INFRA_SETUP","Fields":[{"Env":"KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE","EnvType":"int","EnvValue":"20","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"KUBELINK_GRPC_MAX_SEND_MSG_SIZE","EnvType":"int","EnvValue":"4","EnvDescription":"","Example":"","Deprecated":"false"},{"Env":"KUBELINK_GRPC_SERVICE_CONFIG","EnvType":"string","EnvValue":"{\"loadBalancingPolicy\":\"round_robin\"}","EnvDescription":"kubelink grpc service config","Example":"","Deprecated":"false"}]}] \ No newline at end of file diff --git a/kubelink/env_gen.md b/kubelink/env_gen.md index 3e650cb60..76bd1e6c0 100644 --- a/kubelink/env_gen.md +++ b/kubelink/env_gen.md @@ -12,8 +12,6 @@ | K8s_TCP_KEEPALIVE | int |30 | | | false | | K8s_TCP_TIMEOUT | int |30 | | | false | | K8s_TLS_HANDSHAKE_TIMEOUT | int |10 | | | false | - | KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE | int |20 | | | false | - | KUBELINK_GRPC_MAX_SEND_MSG_SIZE | int |4 | | | false | | LOG_LEVEL | int |-1 | | | false | | NATS_MSG_ACK_WAIT_IN_SECS | int |120 | | | false | | NATS_MSG_BUFFER_SIZE | int |-1 | | | false | @@ -49,3 +47,11 @@ | PARENT_CHILD_GVK_MAPPING | string | | Parent child GVK mapping for resource tree | | false | | RUN_HELM_INSTALL_IN_ASYNC_MODE | bool |false | Run helm install/ upgrade in async mode | false | false | + +## INFRA_SETUP Related Environment Variables +| Key | Type | Default Value | Description | Example | Deprecated | +|-------|----------|-------------------|-------------------|-----------------------|------------------| + | KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE | int |20 | | | false | + | KUBELINK_GRPC_MAX_SEND_MSG_SIZE | int |4 | | | false | + | KUBELINK_GRPC_SERVICE_CONFIG | string |{"loadBalancingPolicy":"round_robin"} | kubelink grpc service config | | false | + From aa57ae910e0f67f4dbd3a8d0986affccf9d927d2 Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Wed, 28 May 2025 12:54:42 +0530 Subject: [PATCH 35/35] bump common lib --- kubewatch/go.mod | 2 +- kubewatch/go.sum | 4 ++-- .../devtron-labs/common-lib/informer/bean.go | 4 ++-- .../common-lib/utils/k8s/K8sUtil.go | 9 ++++++--- .../common-lib/utils/k8s/configMap/Options.go | 19 +++++-------------- .../common-lib/utils/k8s/k8sService.go | 2 +- kubewatch/vendor/modules.txt | 4 ++-- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/kubewatch/go.mod b/kubewatch/go.mod index 2929d14e7..4763459bf 100644 --- a/kubewatch/go.mod +++ b/kubewatch/go.mod @@ -245,4 +245,4 @@ replace ( k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 ) -replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 +replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527180844-483d6a266119 diff --git a/kubewatch/go.sum b/kubewatch/go.sum index 5c6af751a..e5dbc139a 100644 --- a/kubewatch/go.sum +++ b/kubewatch/go.sum @@ -719,8 +719,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 h1:l8Ym2QGKO1V6VfAA0aD7Er3mYnkKmJzliFtfk1zmDJQ= -github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527180844-483d6a266119 h1:yFfFWCSt7onH3Jt6yzZseNvpm2OSh+NMGL75ST7eWXw= +github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527180844-483d6a266119/go.mod h1:HQVUnQI7WHwVq89Bib/18xJqM89S1+xI0O7REctMMrA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go index 6c9fa9f9b..9601f6f8f 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/informer/bean.go @@ -24,8 +24,8 @@ const ( ClusterActionDelete = "delete" CmFieldAction = "action" CmFieldClusterId = "cluster_id" - ClusterModifyEventCmLabelKeyValue = "type=cluster-request-modify" - ClusterModifyEventCmLabelValue = "cluster-request-modify" + ClusterModifyEventCmLabelKeyValue = "type=devtron.ai-cluster-request-modify" + ClusterModifyEventCmLabelValue = "devtron.ai-cluster-request-modify" ) const ( diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go index 2685ff37c..0d5bc059f 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go @@ -237,9 +237,12 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap, } } -func (impl *K8sServiceImpl) CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) { - - configMap := &v1.ConfigMap{} +func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) { + configMap := &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + } for _, option := range opts { option(configMap) } diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go index afa2122ef..20cca36d1 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/configMap/Options.go @@ -4,19 +4,10 @@ import ( v1 "k8s.io/api/core/v1" ) -type Option func(*v1.ConfigMap) - -// WithCmName adds config map name to a ConfigMap -func WithCmName(name string) Option { - return func(cm *v1.ConfigMap) { - if len(name) > 0 { - cm.ObjectMeta.Name = name - } - } -} +type ConfigMapOption func(*v1.ConfigMap) // WithLabels adds labels to a ConfigMap -func WithLabels(labels map[string]string) Option { +func WithLabels(labels map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if labels != nil && len(labels) > 0 { cm.ObjectMeta.Labels = labels @@ -25,7 +16,7 @@ func WithLabels(labels map[string]string) Option { } // WithAnnotations adds annotations to a ConfigMap -func WithAnnotations(annotations map[string]string) Option { +func WithAnnotations(annotations map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if annotations != nil && len(annotations) > 0 { cm.ObjectMeta.Annotations = annotations @@ -34,7 +25,7 @@ func WithAnnotations(annotations map[string]string) Option { } // WithData adds string data to a ConfigMap -func WithData(data map[string]string) Option { +func WithData(data map[string]string) ConfigMapOption { return func(cm *v1.ConfigMap) { if data != nil && len(data) > 0 { cm.Data = data @@ -43,7 +34,7 @@ func WithData(data map[string]string) Option { } // WithBinaryData adds binary data to a ConfigMap -func WithBinaryData(binaryData map[string][]byte) Option { +func WithBinaryData(binaryData map[string][]byte) ConfigMapOption { return func(cm *v1.ConfigMap) { if binaryData != nil && len(binaryData) > 0 { cm.BinaryData = binaryData diff --git a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go index 914624507..ccd2f49d7 100644 --- a/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go +++ b/kubewatch/vendor/github.com/devtron-labs/common-lib/utils/k8s/k8sService.go @@ -77,7 +77,7 @@ type K8sService interface { PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) - CreateConfigMapObject(namespace string, client *v12.CoreV1Client, opts ...configMap.Option) (*v1.ConfigMap, error) + CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) diff --git a/kubewatch/vendor/modules.txt b/kubewatch/vendor/modules.txt index 33e77089c..a47997eb4 100644 --- a/kubewatch/vendor/modules.txt +++ b/kubewatch/vendor/modules.txt @@ -215,7 +215,7 @@ github.com/cyphar/filepath-securejoin # github.com/davecgh/go-spew v1.1.1 ## explicit github.com/davecgh/go-spew/spew -# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 +# github.com/devtron-labs/common-lib v0.0.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527180844-483d6a266119 ## explicit; go 1.21 github.com/devtron-labs/common-lib/async github.com/devtron-labs/common-lib/constants @@ -1769,4 +1769,4 @@ upper.io/db.v3/postgresql # k8s.io/mount-utils => k8s.io/mount-utils v0.29.7 # k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.29.7 # k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.29.7 -# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527082212-323af49b03f0 +# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250527180844-483d6a266119