Skip to content

Commit

Permalink
Merge pull request #275 from leanix/bugfix/HEL-2715
Browse files Browse the repository at this point in the history
HEL-2715 Added nil check for selector labels for workloads
  • Loading branch information
rajeshmohanty-leanix authored May 22, 2023
2 parents b294cb3 + 5aedc26 commit 44bde60
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 57 deletions.
36 changes: 21 additions & 15 deletions pkg/iris/workloads/services/mapper/cronjob.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 != 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
}
34 changes: 20 additions & 14 deletions pkg/iris/workloads/services/mapper/daemonset.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 != 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
}
35 changes: 21 additions & 14 deletions pkg/iris/workloads/services/mapper/deployment.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 != 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
}
35 changes: 21 additions & 14 deletions pkg/iris/workloads/services/mapper/statefulset.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 != 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
}

0 comments on commit 44bde60

Please sign in to comment.