From 595e0404c61cf1b838baa28530d6469263dab7c6 Mon Sep 17 00:00:00 2001 From: rajeshmohanty-leanix Date: Fri, 19 May 2023 14:04:32 +0200 Subject: [PATCH 1/3] HEL-2715 Added nil check for selector labels for workloads --- pkg/iris/workloads/services/mapper/cronjob.go | 36 +++++++++++-------- .../workloads/services/mapper/daemonset.go | 34 ++++++++++-------- .../workloads/services/mapper/deployment.go | 35 ++++++++++-------- .../workloads/services/mapper/statefulset.go | 35 ++++++++++-------- 4 files changed, 83 insertions(+), 57 deletions(-) diff --git a/pkg/iris/workloads/services/mapper/cronjob.go b/pkg/iris/workloads/services/mapper/cronjob.go index f1ea8f9..7c79e26 100644 --- a/pkg/iris/workloads/services/mapper/cronjob.go +++ b/pkg/iris/workloads/services/mapper/cronjob.go @@ -1,13 +1,15 @@ package mapper import ( + "reflect" + "strings" + "time" + "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" + "github.com/leanix/leanix-k8s-connector/pkg/logger" batchv1 "k8s.io/api/batch/v1" _ "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" - "reflect" - "strings" - "time" ) func (m *mapworkload) MapCronJobsEcst(clusterName string, cronJobs *batchv1.CronJobList, services *v1.ServiceList) ([]models.Workload, error) { @@ -49,20 +51,24 @@ func (m *mapworkload) CreateCronjobEcst(clusterName string, cronJob batchv1.Cron func ResolveK8sServiceForK8sCronJob(services *v1.ServiceList, cronJob batchv1.CronJob) string { cronJobService := "" - for _, service := range services.Items { - sharedLabelsCronJob := map[string]string{} - sharedLabelsService := map[string]string{} - for label := range service.Spec.Selector { - if _, ok := cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels[label]; ok { - sharedLabelsCronJob[label] = cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels[label] - sharedLabelsService[label] = service.Spec.Selector[label] - } - } + if cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels != nil { + for _, service := range services.Items { + sharedLabelsCronJob := map[string]string{} + sharedLabelsService := map[string]string{} - if len(sharedLabelsCronJob) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsCronJob, sharedLabelsService) { - cronJobService = service.Name - break + for label := range service.Spec.Selector { + if _, ok := cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels[label]; ok { + sharedLabelsCronJob[label] = cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels[label] + sharedLabelsService[label] = service.Spec.Selector[label] + } + } + if len(sharedLabelsCronJob) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsCronJob, sharedLabelsService) { + cronJobService = service.Name + break + } } + } else { + logger.Infof("CronJob %s has no selector labels", cronJob.Name) } return cronJobService } diff --git a/pkg/iris/workloads/services/mapper/daemonset.go b/pkg/iris/workloads/services/mapper/daemonset.go index a581883..35565ca 100644 --- a/pkg/iris/workloads/services/mapper/daemonset.go +++ b/pkg/iris/workloads/services/mapper/daemonset.go @@ -1,12 +1,14 @@ package mapper import ( - workload "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" - appsv1 "k8s.io/api/apps/v1" - v1 "k8s.io/api/core/v1" "reflect" "strings" "time" + + workload "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" + "github.com/leanix/leanix-k8s-connector/pkg/logger" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" ) func (m *mapworkload) MapDaemonSetsEcst(clusterName string, daemonSets *appsv1.DaemonSetList, services *v1.ServiceList) ([]workload.Workload, error) { @@ -47,20 +49,24 @@ func (m *mapworkload) CreateDaemonSetEcst(clusterName string, daemonSet appsv1.D func ResolveK8sServiceForK8sDaemonSet(services *v1.ServiceList, daemonSet appsv1.DaemonSet) string { daemonSetService := "" - for _, service := range services.Items { - sharedLabelsStatefulSet := map[string]string{} - sharedLabelsService := map[string]string{} - for label := range service.Spec.Selector { - if _, ok := daemonSet.Spec.Selector.MatchLabels[label]; ok { - sharedLabelsStatefulSet[label] = daemonSet.Spec.Selector.MatchLabels[label] - sharedLabelsService[label] = service.Spec.Selector[label] + if daemonSet.Spec.Selector.MatchLabels != nil { + for _, service := range services.Items { + sharedLabelsStatefulSet := map[string]string{} + sharedLabelsService := map[string]string{} + for label := range service.Spec.Selector { + if _, ok := daemonSet.Spec.Selector.MatchLabels[label]; ok { + sharedLabelsStatefulSet[label] = daemonSet.Spec.Selector.MatchLabels[label] + sharedLabelsService[label] = service.Spec.Selector[label] + } } - } - if len(sharedLabelsStatefulSet) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsStatefulSet, sharedLabelsService) { - daemonSetService = service.Name - break + if len(sharedLabelsStatefulSet) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsStatefulSet, sharedLabelsService) { + daemonSetService = service.Name + break + } } + } else { + logger.Infof("DaemonSet %s has no selector labels", daemonSet.Name) } return daemonSetService } diff --git a/pkg/iris/workloads/services/mapper/deployment.go b/pkg/iris/workloads/services/mapper/deployment.go index 0efce0e..f36b175 100644 --- a/pkg/iris/workloads/services/mapper/deployment.go +++ b/pkg/iris/workloads/services/mapper/deployment.go @@ -1,12 +1,14 @@ package mapper import ( - "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" - appsv1 "k8s.io/api/apps/v1" - v1 "k8s.io/api/core/v1" "reflect" "strconv" "time" + + "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" + "github.com/leanix/leanix-k8s-connector/pkg/logger" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" ) func (m *mapworkload) MapDeploymentsEcst(clusterName string, deployments *appsv1.DeploymentList, services *v1.ServiceList) ([]models.Workload, error) { @@ -70,20 +72,25 @@ func CreateK8sResources(resourceList v1.ResourceList) models.K8sResources { func ResolveK8sServiceForK8sDeployment(services *v1.ServiceList, deployment appsv1.Deployment) string { deploymentService := "" - for _, service := range services.Items { - sharedLabelsDeployment := map[string]string{} - sharedLabelsService := map[string]string{} - for label := range service.Spec.Selector { - if _, ok := deployment.Spec.Selector.MatchLabels[label]; ok { - sharedLabelsDeployment[label] = deployment.Spec.Selector.MatchLabels[label] - sharedLabelsService[label] = service.Spec.Selector[label] + if deployment.Spec.Selector.MatchLabels != nil { + + for _, service := range services.Items { + sharedLabelsDeployment := map[string]string{} + sharedLabelsService := map[string]string{} + for label := range service.Spec.Selector { + if _, ok := deployment.Spec.Selector.MatchLabels[label]; ok { + sharedLabelsDeployment[label] = deployment.Spec.Selector.MatchLabels[label] + sharedLabelsService[label] = service.Spec.Selector[label] + } } - } - if len(sharedLabelsDeployment) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsDeployment, sharedLabelsService) { - deploymentService = service.Name - break + if len(sharedLabelsDeployment) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsDeployment, sharedLabelsService) { + deploymentService = service.Name + break + } } + } else { + logger.Infof("Deployment %s has no selector labels", deployment.Name) } return deploymentService } diff --git a/pkg/iris/workloads/services/mapper/statefulset.go b/pkg/iris/workloads/services/mapper/statefulset.go index 4d2ceaf..b3927fa 100644 --- a/pkg/iris/workloads/services/mapper/statefulset.go +++ b/pkg/iris/workloads/services/mapper/statefulset.go @@ -1,12 +1,14 @@ package mapper import ( - workload "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" - appsv1 "k8s.io/api/apps/v1" - v1 "k8s.io/api/core/v1" "reflect" "strings" "time" + + workload "github.com/leanix/leanix-k8s-connector/pkg/iris/workloads/models" + "github.com/leanix/leanix-k8s-connector/pkg/logger" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" ) func (m *mapworkload) MapStatefulSetsEcst(clusterName string, statefulSets *appsv1.StatefulSetList, services *v1.ServiceList) ([]workload.Workload, error) { @@ -48,20 +50,25 @@ func (m *mapworkload) CreateStatefulSetEcst(clusterName string, statefulSet apps func ResolveK8sServiceForK8sStatefulSet(services *v1.ServiceList, statefulSet appsv1.StatefulSet) string { statefulSetService := "" - for _, service := range services.Items { - sharedLabelsStatefulSet := map[string]string{} - sharedLabelsService := map[string]string{} - for label := range service.Spec.Selector { - if _, ok := statefulSet.Spec.Selector.MatchLabels[label]; ok { - sharedLabelsStatefulSet[label] = statefulSet.Spec.Selector.MatchLabels[label] - sharedLabelsService[label] = service.Spec.Selector[label] + if statefulSet.Spec.Selector.MatchLabels == nil { + + for _, service := range services.Items { + sharedLabelsStatefulSet := map[string]string{} + sharedLabelsService := map[string]string{} + for label := range service.Spec.Selector { + if _, ok := statefulSet.Spec.Selector.MatchLabels[label]; ok { + sharedLabelsStatefulSet[label] = statefulSet.Spec.Selector.MatchLabels[label] + sharedLabelsService[label] = service.Spec.Selector[label] + } } - } - if len(sharedLabelsStatefulSet) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsStatefulSet, sharedLabelsService) { - statefulSetService = service.Name - break + if len(sharedLabelsStatefulSet) != 0 && len(sharedLabelsService) != 0 && reflect.DeepEqual(sharedLabelsStatefulSet, sharedLabelsService) { + statefulSetService = service.Name + break + } } + } else { + logger.Infof("StatefulSet %s has no selector labels", statefulSet.Name) } return statefulSetService } From d12e5b9d68aa3c183ddfb71bb7c8ce4703611767 Mon Sep 17 00:00:00 2001 From: rajeshmohanty-leanix Date: Fri, 19 May 2023 15:29:49 +0200 Subject: [PATCH 2/3] HEL-2717 Updated for stateful sets --- pkg/iris/workloads/services/mapper/statefulset.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/iris/workloads/services/mapper/statefulset.go b/pkg/iris/workloads/services/mapper/statefulset.go index b3927fa..aa44694 100644 --- a/pkg/iris/workloads/services/mapper/statefulset.go +++ b/pkg/iris/workloads/services/mapper/statefulset.go @@ -50,7 +50,7 @@ func (m *mapworkload) CreateStatefulSetEcst(clusterName string, statefulSet apps func ResolveK8sServiceForK8sStatefulSet(services *v1.ServiceList, statefulSet appsv1.StatefulSet) string { statefulSetService := "" - if statefulSet.Spec.Selector.MatchLabels == nil { + if statefulSet.Spec.Selector.MatchLabels != nil { for _, service := range services.Items { sharedLabelsStatefulSet := map[string]string{} From 5aedc264f2bb5b9be07ea77a09b941eb91df54af Mon Sep 17 00:00:00 2001 From: rajeshmohanty-leanix Date: Mon, 22 May 2023 09:56:56 +0200 Subject: [PATCH 3/3] HEL-2715 Changed Selector nil pointer check --- pkg/iris/workloads/services/mapper/cronjob.go | 2 +- pkg/iris/workloads/services/mapper/daemonset.go | 2 +- pkg/iris/workloads/services/mapper/deployment.go | 2 +- pkg/iris/workloads/services/mapper/statefulset.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/iris/workloads/services/mapper/cronjob.go b/pkg/iris/workloads/services/mapper/cronjob.go index 7c79e26..d9eb5b4 100644 --- a/pkg/iris/workloads/services/mapper/cronjob.go +++ b/pkg/iris/workloads/services/mapper/cronjob.go @@ -51,7 +51,7 @@ func (m *mapworkload) CreateCronjobEcst(clusterName string, cronJob batchv1.Cron func ResolveK8sServiceForK8sCronJob(services *v1.ServiceList, cronJob batchv1.CronJob) string { cronJobService := "" - if cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels != nil { + if cronJob.Spec.JobTemplate.Spec.Selector != nil { for _, service := range services.Items { sharedLabelsCronJob := map[string]string{} sharedLabelsService := map[string]string{} diff --git a/pkg/iris/workloads/services/mapper/daemonset.go b/pkg/iris/workloads/services/mapper/daemonset.go index 35565ca..6a224c8 100644 --- a/pkg/iris/workloads/services/mapper/daemonset.go +++ b/pkg/iris/workloads/services/mapper/daemonset.go @@ -49,7 +49,7 @@ func (m *mapworkload) CreateDaemonSetEcst(clusterName string, daemonSet appsv1.D func ResolveK8sServiceForK8sDaemonSet(services *v1.ServiceList, daemonSet appsv1.DaemonSet) string { daemonSetService := "" - if daemonSet.Spec.Selector.MatchLabels != nil { + if daemonSet.Spec.Selector != nil { for _, service := range services.Items { sharedLabelsStatefulSet := map[string]string{} sharedLabelsService := map[string]string{} diff --git a/pkg/iris/workloads/services/mapper/deployment.go b/pkg/iris/workloads/services/mapper/deployment.go index f36b175..410f582 100644 --- a/pkg/iris/workloads/services/mapper/deployment.go +++ b/pkg/iris/workloads/services/mapper/deployment.go @@ -72,7 +72,7 @@ func CreateK8sResources(resourceList v1.ResourceList) models.K8sResources { func ResolveK8sServiceForK8sDeployment(services *v1.ServiceList, deployment appsv1.Deployment) string { deploymentService := "" - if deployment.Spec.Selector.MatchLabels != nil { + if deployment.Spec.Selector != nil { for _, service := range services.Items { sharedLabelsDeployment := map[string]string{} diff --git a/pkg/iris/workloads/services/mapper/statefulset.go b/pkg/iris/workloads/services/mapper/statefulset.go index aa44694..a8751f8 100644 --- a/pkg/iris/workloads/services/mapper/statefulset.go +++ b/pkg/iris/workloads/services/mapper/statefulset.go @@ -50,7 +50,7 @@ func (m *mapworkload) CreateStatefulSetEcst(clusterName string, statefulSet apps func ResolveK8sServiceForK8sStatefulSet(services *v1.ServiceList, statefulSet appsv1.StatefulSet) string { statefulSetService := "" - if statefulSet.Spec.Selector.MatchLabels != nil { + if statefulSet.Spec.Selector != nil { for _, service := range services.Items { sharedLabelsStatefulSet := map[string]string{}