From a6bf69fb8f41bc95bcca49432e292fe97d519725 Mon Sep 17 00:00:00 2001 From: prakhar katiyar Date: Wed, 29 Jan 2025 19:03:29 +0530 Subject: [PATCH 1/4] namespace with labels --- common-lib/utils/k8s/K8sUtil.go | 12 ++++++------ common-lib/utils/k8s/K8sUtil_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index f3adf4a53..f01d28e06 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -114,7 +114,7 @@ type K8sService interface { 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) + CreateNsIfNotExists(namespace string, labels map[string]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) @@ -144,7 +144,7 @@ type K8sService interface { //below functions are exposed for K8sUtilExtended GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) - CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) + CreateNs(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) } func NewK8sUtil(logger *zap.SugaredLogger, runTimeConfig *RuntimeConfig) *K8sServiceImpl { @@ -291,7 +291,7 @@ func (impl *K8sServiceImpl) GetK8sDiscoveryClientInCluster() (*discovery.Discove return discoveryClient, err } -func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) { +func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) { v12Client, err := impl.GetCoreV1Client(clusterConfig) if err != nil { impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig) @@ -308,7 +308,7 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig return ns, nsCreated, nil } impl.logger.Infow("ns not exists creating", "ns", namespace) - ns, err = impl.CreateNs(namespace, v12Client) + ns, err = impl.CreateNs(namespace, labels, v12Client) if err != nil { impl.logger.Errorw("error in creating ns", "namespace", namespace, "err", err) return nil, false, err @@ -348,8 +348,8 @@ func (impl *K8sServiceImpl) GetNsIfExists(namespace string, client *v12.CoreV1Cl } -func (impl *K8sServiceImpl) CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) { - nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} +func (impl *K8sServiceImpl) CreateNs(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) { + nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace, Labels: labels}} ns, err = client.Namespaces().Create(context.Background(), nsSpec, metav1.CreateOptions{}) if err != nil { impl.logger.Errorw("error in creating ns", "err", err) diff --git a/common-lib/utils/k8s/K8sUtil_test.go b/common-lib/utils/k8s/K8sUtil_test.go index f654eb414..9525feaaa 100644 --- a/common-lib/utils/k8s/K8sUtil_test.go +++ b/common-lib/utils/k8s/K8sUtil_test.go @@ -86,7 +86,7 @@ func TestK8sUtil_CreateNsIfNotExists(t *testing.T) { t.SkipNow() t.Run(tt.name, func(t *testing.T) { impl := k8sUtilClient - if _, _, err := impl.CreateNsIfNotExists(tt.namespace, clusterConfig); (err != nil) != tt.wantErr { + if _, _, err := impl.CreateNsIfNotExists(tt.namespace, nil, clusterConfig); (err != nil) != tt.wantErr { t.Errorf("K8sServiceImpl.CreateNsIfNotExists() error = %v, wantErr %v", err, tt.wantErr) } k8s, _ := impl.GetCoreV1Client(clusterConfig) From 65346a33f8c4ca292c7e84313eb65f390e832c78 Mon Sep 17 00:00:00 2001 From: prakhar katiyar Date: Thu, 30 Jan 2025 12:57:59 +0530 Subject: [PATCH 2/4] namespace with labels --- common-lib/utils/k8s/K8sUtil.go | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index f3adf4a53..d2943feb5 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -115,6 +115,7 @@ type K8sService interface { 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) + CreateNsWithLabelsIfNotExists(namespace string, labels map[string]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) @@ -144,6 +145,7 @@ type K8sService interface { //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) } @@ -317,6 +319,32 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig return ns, nsCreated, err } +func (impl *K8sServiceImpl) CreateNsWithLabelsIfNotExists(namespace string, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) { + v12Client, err := impl.GetCoreV1Client(clusterConfig) + if err != nil { + impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig) + return nil, false, err + } + ns, exists, err := impl.GetNsIfExists(namespace, v12Client) + if err != nil { + impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig) + return ns, false, err + } + if exists { + nsCreated = false + impl.logger.Infow("namesapce already exist") + return ns, nsCreated, nil + } + impl.logger.Infow("ns not exists creating", "ns", namespace) + ns, err = impl.CreateNsWithLabels(namespace, labels, v12Client) + if err != nil { + impl.logger.Errorw("error in creating ns", "namespace", namespace, "err", err) + return nil, false, err + } + nsCreated = true + return ns, nsCreated, err +} + func (impl *K8sServiceImpl) UpdateNSLabels(namespace *v1.Namespace, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, err error) { v12Client, err := impl.GetCoreV1Client(clusterConfig) if err != nil { @@ -359,6 +387,17 @@ func (impl *K8sServiceImpl) CreateNs(namespace string, client *v12.CoreV1Client) } } +func (impl *K8sServiceImpl) CreateNsWithLabels(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) { + nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace, Labels: labels}} + ns, err = client.Namespaces().Create(context.Background(), nsSpec, metav1.CreateOptions{}) + if err != nil { + impl.logger.Errorw("error in creating ns", "err", err) + return nil, err + } else { + return ns, nil + } +} + func (impl *K8sServiceImpl) deleteNs(namespace string, client *v12.CoreV1Client) error { err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{}) return err From 380f988fb5cd647d85edfac351da1f609a192e77 Mon Sep 17 00:00:00 2001 From: prakhar katiyar Date: Thu, 30 Jan 2025 13:00:04 +0530 Subject: [PATCH 3/4] namespace with labels --- common-lib/utils/k8s/K8sUtil_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-lib/utils/k8s/K8sUtil_test.go b/common-lib/utils/k8s/K8sUtil_test.go index 9525feaaa..f654eb414 100644 --- a/common-lib/utils/k8s/K8sUtil_test.go +++ b/common-lib/utils/k8s/K8sUtil_test.go @@ -86,7 +86,7 @@ func TestK8sUtil_CreateNsIfNotExists(t *testing.T) { t.SkipNow() t.Run(tt.name, func(t *testing.T) { impl := k8sUtilClient - if _, _, err := impl.CreateNsIfNotExists(tt.namespace, nil, clusterConfig); (err != nil) != tt.wantErr { + if _, _, err := impl.CreateNsIfNotExists(tt.namespace, clusterConfig); (err != nil) != tt.wantErr { t.Errorf("K8sServiceImpl.CreateNsIfNotExists() error = %v, wantErr %v", err, tt.wantErr) } k8s, _ := impl.GetCoreV1Client(clusterConfig) From 37b43d9c326cae6036c5d24894f24997bcc88d9a Mon Sep 17 00:00:00 2001 From: prakhar katiyar Date: Fri, 31 Jan 2025 17:56:08 +0530 Subject: [PATCH 4/4] logging --- common-lib/utils/k8s/K8sUtil.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common-lib/utils/k8s/K8sUtil.go b/common-lib/utils/k8s/K8sUtil.go index d2943feb5..792bfa9d1 100644 --- a/common-lib/utils/k8s/K8sUtil.go +++ b/common-lib/utils/k8s/K8sUtil.go @@ -301,12 +301,12 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig } ns, exists, err := impl.GetNsIfExists(namespace, v12Client) if err != nil { - impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig) + impl.logger.Errorw("error", "error", err) return ns, false, err } if exists { nsCreated = false - impl.logger.Infow("namesapce already exist") + impl.logger.Infow("namespace already exist", "namespace", namespace) return ns, nsCreated, nil } impl.logger.Infow("ns not exists creating", "ns", namespace) @@ -327,12 +327,12 @@ func (impl *K8sServiceImpl) CreateNsWithLabelsIfNotExists(namespace string, labe } ns, exists, err := impl.GetNsIfExists(namespace, v12Client) if err != nil { - impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig) + impl.logger.Errorw("error", "error", err) return ns, false, err } if exists { nsCreated = false - impl.logger.Infow("namesapce already exist") + impl.logger.Infow("namespace already exist", "namespace", namespace) return ns, nsCreated, nil } impl.logger.Infow("ns not exists creating", "ns", namespace)