From b5a83b3769d4587b0eaf69d99c6eb044c3cddb33 Mon Sep 17 00:00:00 2001 From: lostbean Date: Thu, 3 Oct 2024 10:50:38 -0300 Subject: [PATCH] fix comments --- kontrol-service/engine/docker.go | 41 +++++++++++---------------- kontrol-service/engine/flow/render.go | 2 +- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/kontrol-service/engine/docker.go b/kontrol-service/engine/docker.go index 40a546c..80238a0 100644 --- a/kontrol-service/engine/docker.go +++ b/kontrol-service/engine/docker.go @@ -151,19 +151,18 @@ func processGatewayAndRouteConfigs(gatewayConfigs []apitypes.GatewayConfig, rout func getDeploymentForService( serviceConfig apitypes.ServiceConfig, - workloadConfigs []apitypes.DeploymentConfig, + deploymentConfigs []apitypes.DeploymentConfig, ) *apitypes.DeploymentConfig { service := serviceConfig.Service - workload, foundworkload := lo.Find(workloadConfigs, func(workloadConfig apitypes.DeploymentConfig) bool { + workload, foundworkload := lo.Find(deploymentConfigs, func(workloadConfig apitypes.DeploymentConfig) bool { deploymentLabels := workloadConfig.Deployment.GetLabels() - matchSelectors := true for key, value := range service.Spec.Selector { label, found := deploymentLabels[key] if !found || value != label { return false } } - return matchSelectors + return true }) if foundworkload { @@ -175,19 +174,18 @@ func getDeploymentForService( func getSatefulSetForService( serviceConfig apitypes.ServiceConfig, - workloadConfigs []apitypes.StatefulSetConfig, + statefulSetConfigs []apitypes.StatefulSetConfig, ) *apitypes.StatefulSetConfig { service := serviceConfig.Service - workload, foundworkload := lo.Find(workloadConfigs, func(workloadConfig apitypes.StatefulSetConfig) bool { + workload, foundworkload := lo.Find(statefulSetConfigs, func(workloadConfig apitypes.StatefulSetConfig) bool { workloadLabel := workloadConfig.StatefulSet.GetLabels() - matchSelectors := true for key, value := range service.Spec.Selector { label, found := workloadLabel[key] if !found || value != label { return false } } - return matchSelectors + return true }) if foundworkload { @@ -222,7 +220,7 @@ func processServiceConfigs( deploymentConfig := getDeploymentForService(serviceConfig, deploymentConfigs) statefulSetConfig := getSatefulSetForService(serviceConfig, statefulSetConfigs) - clusterTopologyService, error := newClusterTopologyServiceFromServiceConfig(serviceConfig, deploymentConfig, statefulSetConfig, version) + clusterTopologyService, error := newClusterTopologyServiceFromConfigs(serviceConfig, deploymentConfig, statefulSetConfig, version) if error != nil { return nil, nil, stacktrace.Propagate(error, "An error occurred creating new cluster topology service from service config '%s'", service.Name) } @@ -327,7 +325,7 @@ func newStatefulPluginsAndExternalServicesFromServiceConfig(serviceConfig apityp return serviceStatefulPlugins, externalServices, externalServiceDependencies, nil } -func newClusterTopologyServiceFromServiceConfig( +func newClusterTopologyServiceFromConfigs( serviceConfig apitypes.ServiceConfig, deploymentConfig *apitypes.DeploymentConfig, statefulSetConfig *apitypes.StatefulSetConfig, @@ -335,11 +333,17 @@ func newClusterTopologyServiceFromServiceConfig( ) (resolved.Service, error) { service := serviceConfig.Service serviceName := service.GetObjectMeta().GetName() + serviceAnnotations := service.GetObjectMeta().GetAnnotations() + + clusterTopologyService := resolved.Service{ + ServiceID: service.GetObjectMeta().GetName(), + Version: version, + ServiceSpec: &service.Spec, + } if deploymentConfig == nil && statefulSetConfig == nil { logrus.Warnf("Service %s has no workload", serviceName) } - if deploymentConfig != nil && statefulSetConfig != nil { workloads := []string{ deploymentConfig.Deployment.GetObjectMeta().GetName(), @@ -347,15 +351,6 @@ func newClusterTopologyServiceFromServiceConfig( } logrus.Error("Service %s is associated with more than one workload: %v", serviceName, workloads) } - - serviceAnnotations := service.GetObjectMeta().GetAnnotations() - - clusterTopologyService := resolved.Service{ - ServiceID: service.GetObjectMeta().GetName(), - Version: version, - ServiceSpec: &service.Spec, - } - if deploymentConfig != nil { workload := kardinal.NewDeploymentWorkloadSpec(deploymentConfig.Deployment.Spec) clusterTopologyService.WorkloadSpec = &workload @@ -365,12 +360,8 @@ func newClusterTopologyServiceFromServiceConfig( clusterTopologyService.WorkloadSpec = &workload } - if clusterTopologyService.WorkloadSpec == nil { - return clusterTopologyService, stacktrace.NewError("Service %s has no workload", serviceName) - } - // Set default for IsStateful to true if the workload is a StatefulSet, otherwise false - clusterTopologyService.IsExternal = clusterTopologyService.WorkloadSpec.IsStatefulSet() + clusterTopologyService.IsStateful = clusterTopologyService.WorkloadSpec.IsStatefulSet() // Override the IsStateful value by manual annotations isStateful, ok := serviceAnnotations["kardinal.dev.service/stateful"] diff --git a/kontrol-service/engine/flow/render.go b/kontrol-service/engine/flow/render.go index 53c5d56..a9512b3 100644 --- a/kontrol-service/engine/flow/render.go +++ b/kontrol-service/engine/flow/render.go @@ -318,7 +318,7 @@ func getStatefulSet(service *resolved.Service, namespace string) *appsv1.Statefu statefulSet := appsv1.StatefulSet{ TypeMeta: metav1.TypeMeta{ APIVersion: "apps/v1", - Kind: "statefulSet", + Kind: "StatefulSet", }, ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-%s", service.ServiceID, service.Version),