diff --git a/config/crd/bases/starrocks.com_starrocksclusters.yaml b/config/crd/bases/starrocks.com_starrocksclusters.yaml index 6ca3e1ed..bdedd60b 100644 --- a/config/crd/bases/starrocks.com_starrocksclusters.yaml +++ b/config/crd/bases/starrocks.com_starrocksclusters.yaml @@ -2721,7 +2721,16 @@ spec: annotations: additionalProperties: type: string - description: Annotations store Kubernetes Service annotations. + description: |- + Annotations store Kubernetes Service annotations. These will be added to the external service + only (not internal). + type: object + labels: + additionalProperties: + type: string + description: |- + Labels store Kubernetes Service labels. These will be added to the external service only (not + internal). StarRocks may add its own default labels. type: object loadBalancerIP: description: |- @@ -7697,7 +7706,16 @@ spec: annotations: additionalProperties: type: string - description: Annotations store Kubernetes Service annotations. + description: |- + Annotations store Kubernetes Service annotations. These will be added to the external service + only (not internal). + type: object + labels: + additionalProperties: + type: string + description: |- + Labels store Kubernetes Service labels. These will be added to the external service only (not + internal). StarRocks may add its own default labels. type: object loadBalancerIP: description: |- @@ -10526,7 +10544,16 @@ spec: annotations: additionalProperties: type: string - description: Annotations store Kubernetes Service annotations. + description: |- + Annotations store Kubernetes Service annotations. These will be added to the external service + only (not internal). + type: object + labels: + additionalProperties: + type: string + description: |- + Labels store Kubernetes Service labels. These will be added to the external service only (not + internal). StarRocks may add its own default labels. type: object loadBalancerIP: description: |- @@ -13550,7 +13577,16 @@ spec: annotations: additionalProperties: type: string - description: Annotations store Kubernetes Service annotations. + description: |- + Annotations store Kubernetes Service annotations. These will be added to the external service + only (not internal). + type: object + labels: + additionalProperties: + type: string + description: |- + Labels store Kubernetes Service labels. These will be added to the external service only (not + internal). StarRocks may add its own default labels. type: object loadBalancerIP: description: |- diff --git a/config/crd/bases/starrocks.com_starrockswarehouses.yaml b/config/crd/bases/starrocks.com_starrockswarehouses.yaml index 66f04428..0f670021 100644 --- a/config/crd/bases/starrocks.com_starrockswarehouses.yaml +++ b/config/crd/bases/starrocks.com_starrockswarehouses.yaml @@ -3317,7 +3317,16 @@ spec: annotations: additionalProperties: type: string - description: Annotations store Kubernetes Service annotations. + description: |- + Annotations store Kubernetes Service annotations. These will be added to the external service + only (not internal). + type: object + labels: + additionalProperties: + type: string + description: |- + Labels store Kubernetes Service labels. These will be added to the external service only (not + internal). StarRocks may add its own default labels. type: object loadBalancerIP: description: |- diff --git a/deploy/starrocks.com_starrocksclusters.yaml b/deploy/starrocks.com_starrocksclusters.yaml index 7917d297..7a233c5e 100644 --- a/deploy/starrocks.com_starrocksclusters.yaml +++ b/deploy/starrocks.com_starrocksclusters.yaml @@ -1327,6 +1327,10 @@ spec: additionalProperties: type: string type: object + labels: + additionalProperties: + type: string + type: object loadBalancerIP: type: string loadBalancerSourceRanges: @@ -3684,6 +3688,10 @@ spec: additionalProperties: type: string type: object + labels: + additionalProperties: + type: string + type: object loadBalancerIP: type: string loadBalancerSourceRanges: @@ -4987,6 +4995,10 @@ spec: additionalProperties: type: string type: object + labels: + additionalProperties: + type: string + type: object loadBalancerIP: type: string loadBalancerSourceRanges: @@ -6389,6 +6401,10 @@ spec: additionalProperties: type: string type: object + labels: + additionalProperties: + type: string + type: object loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/deploy/starrocks.com_starrockswarehouses.yaml b/deploy/starrocks.com_starrockswarehouses.yaml index af7436ca..122f2d8a 100644 --- a/deploy/starrocks.com_starrockswarehouses.yaml +++ b/deploy/starrocks.com_starrockswarehouses.yaml @@ -1641,6 +1641,10 @@ spec: additionalProperties: type: string type: object + labels: + additionalProperties: + type: string + type: object loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/doc/api.md b/doc/api.md index be84f5a3..64566cf1 100644 --- a/doc/api.md +++ b/doc/api.md @@ -2044,7 +2044,21 @@ map[string]string (Optional) -

Annotations store Kubernetes Service annotations.

+

Annotations store Kubernetes Service annotations. These will be added to the external service +only (not internal).

+ + + + +labels
+ +map[string]string + + + +(Optional) +

Labels store Kubernetes Service labels. These will be added to the external service only (not +internal). StarRocks may add its own default labels.

@@ -2509,5 +2523,5 @@ AutoScalingPolicy

Generated with gen-crd-api-reference-docs -on git commit 42e17ab. +on git commit dbe00d8.

diff --git a/helm-charts/charts/kube-starrocks/charts/starrocks/templates/starrockscluster.yaml b/helm-charts/charts/kube-starrocks/charts/starrocks/templates/starrockscluster.yaml index 5cae0562..0230271b 100644 --- a/helm-charts/charts/kube-starrocks/charts/starrocks/templates/starrockscluster.yaml +++ b/helm-charts/charts/kube-starrocks/charts/starrocks/templates/starrockscluster.yaml @@ -69,6 +69,10 @@ spec: {{- toYaml .Values.starrocksFESpec.service.annotations | nindent 8 }} {{- end }} {{- end }} + {{- if .Values.starrocksFESpec.service.labels}} + labels: + {{- toYaml .Values.starrocksFESpec.service.labels | nindent 8 }} + {{- end }} {{- end }} annotations: app.starrocks.io/fe-config-hash: "{{template "starrockscluster.fe.config.hash" . }}" @@ -326,6 +330,10 @@ spec: {{- toYaml .Values.starrocksBeSpec.service.annotations | nindent 8 }} {{- end }} {{- end }} + {{- if .Values.starrocksBeSpec.service.labels}} + labels: + {{- toYaml .Values.starrocksBeSpec.service.labels | nindent 8 }} + {{- end }} {{- end }} annotations: app.starrocks.io/be-config-hash: "{{template "starrockscluster.be.config.hash" . }}" @@ -751,6 +759,10 @@ spec: {{- toYaml .Values.starrocksCnSpec.service.annotations | nindent 8 }} {{- end }} {{- end }} + {{- if .Values.starrocksCnSpec.service.labels}} + labels: + {{- toYaml .Values.starrocksCnSpec.service.labels | nindent 8 }} + {{- end }} {{- end }} annotations: app.starrocks.io/cn-config-hash: "{{template "starrockscluster.cn.config.hash" . }}" @@ -875,6 +887,10 @@ spec: annotations: {{- toYaml .Values.starrocksFeProxySpec.service.annotations | nindent 8 }} {{- end }} + {{- if .Values.starrocksFeProxySpec.service.labels}} + labels: + {{- toYaml .Values.starrocksFeProxySpec.service.labels | nindent 8 }} + {{- end }} {{- if and (eq "LoadBalancer" .Values.starrocksFeProxySpec.service.type) .Values.starrocksFeProxySpec.service.loadbalancerIP }} loadBalancerIP: {{ .Values.starrocksFeProxySpec.service.loadbalancerIP }} {{- end }} diff --git a/helm-charts/charts/kube-starrocks/charts/starrocks/values.yaml b/helm-charts/charts/kube-starrocks/charts/starrocks/values.yaml index 6e91cca4..d4988f8a 100644 --- a/helm-charts/charts/kube-starrocks/charts/starrocks/values.yaml +++ b/helm-charts/charts/kube-starrocks/charts/starrocks/values.yaml @@ -197,8 +197,10 @@ starrocksFESpec: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for fe service. + # add annotations for external fe service. annotations: {} + # Add labels for external fe service. The operator may add its own default labels. + labels: {} # config the service port for fe service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -490,8 +492,10 @@ starrocksCnSpec: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadBalancerIp is not empty. loadbalancerIP: "" - # add annotations for cn service. + # add annotations for external cn service. annotations: {} + # Add labels for external cn service. The operator may add its own default labels. + labels: {} # config the service port for cn service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -825,8 +829,10 @@ starrocksBeSpec: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for be service. + # add annotations for external be service. annotations: {} + # Add labels for external be service. The operator may add its own default labels. + labels: {} # config the service port for be service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -1134,8 +1140,10 @@ starrocksFeProxySpec: type: ClusterIP # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for fe service. + # add annotations for external fe proxy service. annotations: {} + # Add labels for external fe proxy service. The operator may add its own default labels. + labels: {} # config the service port for fe proxy service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. diff --git a/helm-charts/charts/kube-starrocks/values.yaml b/helm-charts/charts/kube-starrocks/values.yaml index 71dc9173..0e1bb212 100644 --- a/helm-charts/charts/kube-starrocks/values.yaml +++ b/helm-charts/charts/kube-starrocks/values.yaml @@ -309,8 +309,10 @@ starrocks: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for fe service. + # add annotations for external fe service. annotations: {} + # Add labels for external fe service. The operator may add its own default labels. + labels: {} # config the service port for fe service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -602,8 +604,10 @@ starrocks: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadBalancerIp is not empty. loadbalancerIP: "" - # add annotations for cn service. + # add annotations for external cn service. annotations: {} + # Add labels for external cn service. The operator may add its own default labels. + labels: {} # config the service port for cn service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -937,8 +941,10 @@ starrocks: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for be service. + # add annotations for external be service. annotations: {} + # Add labels for external be service. The operator may add its own default labels. + labels: {} # config the service port for be service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. @@ -1246,8 +1252,10 @@ starrocks: type: ClusterIP # the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty. loadbalancerIP: "" - # add annotations for fe service. + # add annotations for external fe proxy service. annotations: {} + # Add labels for external fe proxy service. The operator may add its own default labels. + labels: {} # config the service port for fe proxy service. # To assign a specific port or nodePort to a service, you should specify them by the corresponding name or # containerPort in the service configuration. If both containerPort and name are specified, containerPort takes precedence. diff --git a/helm-charts/charts/warehouse/templates/starrockswarehouse.yaml b/helm-charts/charts/warehouse/templates/starrockswarehouse.yaml index 5c17aad1..78a6f943 100644 --- a/helm-charts/charts/warehouse/templates/starrockswarehouse.yaml +++ b/helm-charts/charts/warehouse/templates/starrockswarehouse.yaml @@ -78,6 +78,10 @@ spec: {{- toYaml .Values.spec.service.annotations | nindent 8 }} {{- end }} {{- end }} + {{- if .Values.spec.service.labels }} + labels: + {{- toYaml .Values.spec.service.labels | nindent 8 }} + {{- end }} {{- end }} annotations: app.starrocks.io/cn-config-hash: "{{template "starrockswarehouse.config.hash" . }}" diff --git a/helm-charts/charts/warehouse/values.yaml b/helm-charts/charts/warehouse/values.yaml index eb2f56c2..fd39305f 100644 --- a/helm-charts/charts/warehouse/values.yaml +++ b/helm-charts/charts/warehouse/values.yaml @@ -50,8 +50,10 @@ spec: type: "ClusterIP" # the loadBalancerIP for static ip config when the type=LoadBalancer and loadBalancerIp is not empty. loadbalancerIP: "" - # add annotations for service. + # add annotations for external service. annotations: {} + # add labels for external service. + labels: {} # config the service port for service. # if you want to use a dedicated port for service, you can config the port. # see https://kubernetes.io/docs/concepts/services-networking/service/#field-spec-ports for more details. diff --git a/pkg/apis/starrocks/v1/load_type.go b/pkg/apis/starrocks/v1/load_type.go index 66a359d1..48058a93 100644 --- a/pkg/apis/starrocks/v1/load_type.go +++ b/pkg/apis/starrocks/v1/load_type.go @@ -173,10 +173,16 @@ type StarRocksLoadSpec struct { // StarRocksService defines external service for starrocks component. type StarRocksService struct { - // Annotations store Kubernetes Service annotations. + // Annotations store Kubernetes Service annotations. These will be added to the external service + // only (not internal). // +optional Annotations map[string]string `json:"annotations,omitempty"` + // Labels store Kubernetes Service labels. These will be added to the external service only (not + // internal). StarRocks may add its own default labels. + // +optional + Labels map[string]string `json:"labels,omitempty"` + // type of service,the possible value for the service type are : ClusterIP, NodePort, LoadBalancer,ExternalName. // More info: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types // +optional diff --git a/pkg/apis/starrocks/v1/zz_generated.deepcopy.go b/pkg/apis/starrocks/v1/zz_generated.deepcopy.go index 80ae2576..c3d43f92 100644 --- a/pkg/apis/starrocks/v1/zz_generated.deepcopy.go +++ b/pkg/apis/starrocks/v1/zz_generated.deepcopy.go @@ -734,6 +734,13 @@ func (in *StarRocksService) DeepCopyInto(out *StarRocksService) { (*out)[key] = val } } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } if in.Ports != nil { in, out := &in.Ports, &out.Ports *out = make([]StarRocksServicePort, len(*in)) diff --git a/pkg/common/resource_utils/service.go b/pkg/common/resource_utils/service.go index c28fcb3d..81f35bf7 100644 --- a/pkg/common/resource_utils/service.go +++ b/pkg/common/resource_utils/service.go @@ -73,7 +73,16 @@ func BuildExternalService(object object.StarRocksObject, spec srapi.SpecInterfac starRocksService := spec.GetService() setServiceType(starRocksService, &svc) - anno := getServiceAnnotations(starRocksService) + anno := getExternalServiceAnnotations(starRocksService) + userSuppliedLabels := getExternalServiceLabels(starRocksService) + newLabels := map[string]string{} + for key, val := range labels { + newLabels[key] = val + } + for key, val := range userSuppliedLabels { + newLabels[key] = val + } + svc.Labels = newLabels switch spec.(type) { case *srapi.StarRocksFeSpec: srPorts = getFeServicePorts(config, starRocksService) @@ -226,7 +235,7 @@ func mergePort(service *srapi.StarRocksService, defaultPort srapi.StarRocksServi return port } -func getServiceAnnotations(svc *srapi.StarRocksService) map[string]string { +func getExternalServiceAnnotations(svc *srapi.StarRocksService) map[string]string { if svc != nil && svc.Annotations != nil { annotations := map[string]string{} for key, val := range svc.Annotations { @@ -237,6 +246,17 @@ func getServiceAnnotations(svc *srapi.StarRocksService) map[string]string { return map[string]string{} } +func getExternalServiceLabels(svc *srapi.StarRocksService) map[string]string { + if svc != nil && svc.Labels != nil { + labels := map[string]string{} + for key, val := range svc.Labels { + labels[key] = val + } + return labels + } + return map[string]string{} +} + func ServiceDeepEqual(expectSvc, actualSvc *corev1.Service) bool { var expectHashValue string if _, ok := expectSvc.Annotations[srapi.ComponentResourceHash]; ok { diff --git a/pkg/common/resource_utils/service_test.go b/pkg/common/resource_utils/service_test.go index dfa9231f..d41f5ffd 100644 --- a/pkg/common/resource_utils/service_test.go +++ b/pkg/common/resource_utils/service_test.go @@ -27,7 +27,7 @@ import ( "github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils/templates/object" ) -func Test_getServiceAnnotations(t *testing.T) { +func Test_getExternalServiceAnnotations(t *testing.T) { type args struct { svc *srapi.StarRocksService } @@ -57,8 +57,45 @@ func Test_getServiceAnnotations(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := getServiceAnnotations(tt.args.svc); !reflect.DeepEqual(got, tt.want) { - t.Errorf("getServiceAnnotations() = %v, want %v", got, tt.want) + if got := getExternalServiceAnnotations(tt.args.svc); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getExternalServiceAnnotations() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getExternalServiceLabels(t *testing.T) { + type args struct { + svc *srapi.StarRocksService + } + tests := []struct { + name string + args args + want map[string]string + }{ + { + name: "empty service", + args: args{ + svc: &srapi.StarRocksService{}, + }, + want: map[string]string{}, + }, + { + name: "service with labels", + args: args{ + svc: &srapi.StarRocksService{ + Labels: map[string]string{ + "test": "test", + }, + }, + }, + want: map[string]string{"test": "test"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getExternalServiceLabels(tt.args.svc); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getExternalServiceLabels() = %v, want %v", got, tt.want) } }) } @@ -171,6 +208,9 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Service: &srapi.StarRocksService{ Type: corev1.ServiceTypeLoadBalancer, LoadBalancerIP: "127.0.0.1", + Labels: map[string]string{ + "user_supplied_label": "test", + }, }, }, }, @@ -181,6 +221,9 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Service: &srapi.StarRocksService{ Type: corev1.ServiceTypeLoadBalancer, LoadBalancerIP: "127.0.0.1", + Labels: map[string]string{ + "user_supplied_label": "test", + }, }, }, }, @@ -191,6 +234,9 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Service: &srapi.StarRocksService{ Type: corev1.ServiceTypeLoadBalancer, LoadBalancerIP: "127.0.0.1", + Labels: map[string]string{ + "user_supplied_label": "test", + }, }, }, }, @@ -218,7 +264,11 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Name: "test-fe-service", Namespace: "default", Annotations: map[string]string{ - srapi.ComponentResourceHash: "1826250052", + srapi.ComponentResourceHash: "2323011486", + }, + Labels: map[string]string{ + "starrocks_default_label": "test", + "user_supplied_label": "test", }, OwnerReferences: func() []metav1.OwnerReference { ref := metav1.NewControllerRef(src, src.GroupVersionKind()) @@ -254,7 +304,11 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Name: "test-be-service", Namespace: "default", Annotations: map[string]string{ - srapi.ComponentResourceHash: "2188319972", + srapi.ComponentResourceHash: "2811174334", + }, + Labels: map[string]string{ + "starrocks_default_label": "test", + "user_supplied_label": "test", }, OwnerReferences: func() []metav1.OwnerReference { ref := metav1.NewControllerRef(src, src.GroupVersionKind()) @@ -287,7 +341,11 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { Name: "test-cn-service", Namespace: "default", Annotations: map[string]string{ - srapi.ComponentResourceHash: "4058356074", + srapi.ComponentResourceHash: "4173513860", + }, + Labels: map[string]string{ + "starrocks_default_label": "test", + "user_supplied_label": "test", }, OwnerReferences: func() []metav1.OwnerReference { ref := metav1.NewControllerRef(src, src.GroupVersionKind()) @@ -333,17 +391,20 @@ func TestBuildExternalService_ForStarRocksCluster(t *testing.T) { } } + starrocksDefaultLabels := map[string]string{ + "starrocks_default_label": "test", + } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { object := object.NewFromCluster(src) gotFeService := BuildExternalService(object, src.Spec.StarRocksFeSpec, - map[string]interface{}{}, map[string]string{}, map[string]string{}) + map[string]interface{}{}, map[string]string{}, starrocksDefaultLabels) equal(gotFeService, tt.wantFeService) gotBeService := BuildExternalService(object, src.Spec.StarRocksBeSpec, - map[string]interface{}{}, map[string]string{}, map[string]string{}) + map[string]interface{}{}, map[string]string{}, starrocksDefaultLabels) equal(gotBeService, tt.wantBeService) gotCnService := BuildExternalService(object, src.Spec.StarRocksCnSpec, - map[string]interface{}{}, map[string]string{}, map[string]string{}) + map[string]interface{}{}, map[string]string{}, starrocksDefaultLabels) equal(gotCnService, tt.wantCnService) }) } diff --git a/pkg/k8sutils/templates/service/spec.go b/pkg/k8sutils/templates/service/spec.go index bd65c53f..80507440 100644 --- a/pkg/k8sutils/templates/service/spec.go +++ b/pkg/k8sutils/templates/service/spec.go @@ -20,13 +20,18 @@ import ( v1 "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1" ) -func MakeSearchService(serviceName string, externalService *corev1.Service, ports []corev1.ServicePort) *corev1.Service { +func MakeSearchService(serviceName string, externalService *corev1.Service, ports []corev1.ServicePort, + defaultLabels map[string]string) *corev1.Service { searchSvc := &corev1.Service{} externalService.ObjectMeta.DeepCopyInto(&searchSvc.ObjectMeta) - // some service annotations can only be used when `type` is 'LoadBalancer', e.g. service.beta.kubernetes.io/load-balancer-source-ranges - // we do not need to annotations for search service + // Set annotations to nil so external service annotations aren't copied. Interal / search service annotations aren't + // needed, and adding them can be an issue since some service annotations can only be used when `type` is + // 'LoadBalancer', e.g. service.beta.kubernetes.io/load-balancer-source-ranges. searchSvc.Annotations = nil searchSvc.Name = serviceName + // Set labels to the default labels so external service labels aren't copied. Since labels are used to select objects, + // adding them to the internal / search service can be detrimental. + searchSvc.Labels = defaultLabels searchSvc.Spec = corev1.ServiceSpec{ ClusterIP: "None", Ports: ports, diff --git a/pkg/k8sutils/templates/service/spec_test.go b/pkg/k8sutils/templates/service/spec_test.go index 8f2f647d..2843380e 100644 --- a/pkg/k8sutils/templates/service/spec_test.go +++ b/pkg/k8sutils/templates/service/spec_test.go @@ -29,6 +29,7 @@ func TestMakeSearchService(t *testing.T) { serviceName string externalService *corev1.Service ports []corev1.ServicePort + labels map[string]string } tests := []struct { name string @@ -40,6 +41,11 @@ func TestMakeSearchService(t *testing.T) { args: args{ serviceName: "test", externalService: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "label_to_be_discarded": "test", + }, + }, Spec: corev1.ServiceSpec{ Selector: map[string]string{ "test": "test", @@ -52,10 +58,16 @@ func TestMakeSearchService(t *testing.T) { Port: 18030, }, }, + labels: map[string]string{ + "test": "test", + }, }, want: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "test", + Labels: map[string]string{ + "test": "test", + }, }, Spec: corev1.ServiceSpec{ ClusterIP: "None", @@ -75,7 +87,7 @@ func TestMakeSearchService(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := MakeSearchService(tt.args.serviceName, tt.args.externalService, tt.args.ports); !reflect.DeepEqual(got, tt.want) { + if got := MakeSearchService(tt.args.serviceName, tt.args.externalService, tt.args.ports, tt.args.labels); !reflect.DeepEqual(got, tt.want) { t.Errorf("MakeSearchService() = %v, want %v", got, tt.want) } }) diff --git a/pkg/subcontrollers/be/be_controller.go b/pkg/subcontrollers/be/be_controller.go index 0438cd21..22d8e2c7 100644 --- a/pkg/subcontrollers/be/be_controller.go +++ b/pkg/subcontrollers/be/be_controller.go @@ -106,10 +106,11 @@ func (be *BeController) SyncCluster(ctx context.Context, src *srapi.StarRocksClu // add query port from fe config. config[rutils.QUERY_PORT] = strconv.FormatInt(int64(rutils.GetPort(feConfig, rutils.QUERY_PORT)), 10) // generate new be external service. + defaultLabels := load.Labels(src.Name, beSpec) externalsvc := rutils.BuildExternalService(object.NewFromCluster(src), - beSpec, config, load.Selector(src.Name, beSpec), load.Labels(src.Name, beSpec)) + beSpec, config, load.Selector(src.Name, beSpec), defaultLabels) // generate internal fe service, update the status of cn on src. - internalService := be.generateInternalService(ctx, src, &externalsvc, config) + internalService := be.generateInternalService(ctx, src, &externalsvc, config, defaultLabels) // create be statefulset podTemplateSpec, err := be.buildPodTemplate(src, config) @@ -188,7 +189,8 @@ func (be *BeController) UpdateClusterStatus(ctx context.Context, src *srapi.Star } func (be *BeController) generateInternalService(ctx context.Context, - src *srapi.StarRocksCluster, externalService *corev1.Service, config map[string]interface{}) *corev1.Service { + src *srapi.StarRocksCluster, externalService *corev1.Service, config map[string]interface{}, + labels map[string]string) *corev1.Service { logger := logr.FromContextOrDiscard(ctx) spec := src.Spec.StarRocksBeSpec searchServiceName := service.SearchServiceName(src.Name, spec) @@ -198,7 +200,7 @@ func (be *BeController) generateInternalService(ctx context.Context, Port: rutils.GetPort(config, rutils.HEARTBEAT_SERVICE_PORT), TargetPort: intstr.FromInt(int(rutils.GetPort(config, rutils.HEARTBEAT_SERVICE_PORT))), }, - }) + }, labels) // for compatible version < v1.5 var esearchSvc corev1.Service diff --git a/pkg/subcontrollers/cn/cn_controller.go b/pkg/subcontrollers/cn/cn_controller.go index d222973d..937165ad 100644 --- a/pkg/subcontrollers/cn/cn_controller.go +++ b/pkg/subcontrollers/cn/cn_controller.go @@ -186,8 +186,9 @@ func (cc *CnController) SyncCnSpec(ctx context.Context, object object.StarRocksO } // build and deploy service + defaultLabels := load.Labels(object.AliasName, cnSpec) externalsvc := rutils.BuildExternalService(object, cnSpec, config, - load.Selector(object.AliasName, cnSpec), load.Labels(object.AliasName, cnSpec)) + load.Selector(object.AliasName, cnSpec), defaultLabels) searchServiceName := service.SearchServiceName(object.AliasName, cnSpec) internalService := service.MakeSearchService(searchServiceName, &externalsvc, []corev1.ServicePort{ { @@ -195,7 +196,7 @@ func (cc *CnController) SyncCnSpec(ctx context.Context, object object.StarRocksO Port: rutils.GetPort(config, rutils.HEARTBEAT_SERVICE_PORT), TargetPort: intstr.FromInt(int(rutils.GetPort(config, rutils.HEARTBEAT_SERVICE_PORT))), }, - }) + }, defaultLabels) if err := k8sutils.ApplyService(ctx, cc.k8sClient, &externalsvc, rutils.ServiceDeepEqual); err != nil { logger.Error(err, "sync CN external service failed") diff --git a/pkg/subcontrollers/fe/fe_controller.go b/pkg/subcontrollers/fe/fe_controller.go index 8d758640..cf01674f 100644 --- a/pkg/subcontrollers/fe/fe_controller.go +++ b/pkg/subcontrollers/fe/fe_controller.go @@ -91,7 +91,8 @@ func (fc *FeController) SyncCluster(ctx context.Context, src *srapi.StarRocksClu // generate new fe service. logger.V(log.DebugLevel).Info("build fe service", "StarRocksCluster", src) object := object.NewFromCluster(src) - svc := rutils.BuildExternalService(object, feSpec, feConfig, load.Selector(src.Name, feSpec), load.Labels(src.Name, feSpec)) + defaultLabels := load.Labels(src.Name, feSpec) + svc := rutils.BuildExternalService(object, feSpec, feConfig, load.Selector(src.Name, feSpec), defaultLabels) searchServiceName := service.SearchServiceName(src.Name, feSpec) internalService := service.MakeSearchService(searchServiceName, &svc, []corev1.ServicePort{ { @@ -100,7 +101,7 @@ func (fc *FeController) SyncCluster(ctx context.Context, src *srapi.StarRocksClu TargetPort: intstr.FromInt(int(rutils.GetPort(feConfig, rutils.QUERY_PORT))), AppProtocol: func() *string { mysql := "mysql"; return &mysql }(), }, - }) + }, defaultLabels) // first deploy statefulset for compatible v1.5, apply statefulset for update pod. podTemplateSpec, err := fc.buildPodTemplate(src, feConfig)