Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] support to add annotations on k8s service #139

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/crd/bases/starrocks.com_starrocksclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,11 @@ spec:
description: Service defines the template for the associated Kubernetes
Service object. the service for user access be.
properties:
annotations:
additionalProperties:
type: string
description: Annotations store Kubernetes Service annotations.
type: object
loadBalancerIP:
description: 'Only applies to Service Type: LoadBalancer.
This feature depends on whether the underlying cloud-provider
Expand Down Expand Up @@ -3097,6 +3102,11 @@ spec:
description: Service defines the template for the associated Kubernetes
Service object. the service for user access cn.
properties:
annotations:
additionalProperties:
type: string
description: Annotations store Kubernetes Service annotations.
type: object
loadBalancerIP:
description: 'Only applies to Service Type: LoadBalancer.
This feature depends on whether the underlying cloud-provider
Expand Down Expand Up @@ -4314,6 +4324,11 @@ spec:
description: Service defines the template for the associated Kubernetes
Service object.
properties:
annotations:
additionalProperties:
type: string
description: Annotations store Kubernetes Service annotations.
type: object
loadBalancerIP:
description: 'Only applies to Service Type: LoadBalancer.
This feature depends on whether the underlying cloud-provider
Expand Down
12 changes: 12 additions & 0 deletions deploy/starrocks.com_starrocksclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ spec:
type: array
service:
properties:
annotations:
additionalProperties:
type: string
type: object
loadBalancerIP:
type: string
name:
Expand Down Expand Up @@ -1461,6 +1465,10 @@ spec:
type: array
service:
properties:
annotations:
additionalProperties:
type: string
type: object
loadBalancerIP:
type: string
name:
Expand Down Expand Up @@ -2020,6 +2028,10 @@ spec:
type: array
service:
properties:
annotations:
additionalProperties:
type: string
type: object
loadBalancerIP:
type: string
name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spec:
{{- if and (eq "LoadBalancer" .Values.starrocksFESpec.service.type) .Values.starrocksFESpec.service.loadbalancerIP }}
loadBalancerIP: {{ .Values.starrocksFESpec.service.loadbalancerIP }}
{{- end }}
{{- if .Values.starrocksFESpec.service.annotations }}
annotations:
{{ toYaml .Values.starrocksFESpec.service.annotations | indent 8 }}
{{- end }}
{{- end }}
{{- if .Values.starrocksFESpec.annotations }}
annotations:
Expand Down Expand Up @@ -108,6 +112,10 @@ spec:
{{- if and (eq "LoadBalancer" .Values.starrocksBeSpec.service.type) .Values.starrocksBeSpec.service.loadbalancerIP }}
loadBalancerIP: {{ .Values.starrocksBeSpec.service.loadbalancerIP }}
{{- end }}
{{- if .Values.starrocksBeSpec.service.annotations }}
annotations:
{{ toYaml .Values.starrocksBeSpec.service.annotations | indent 8 }}
{{- end }}
{{- end }}
{{- if .Values.starrocksBeSpec.annotations }}
annotations:
Expand Down Expand Up @@ -228,6 +236,10 @@ spec:
{{- if and (eq "LoadBalancer" .Values.starrocksCnSpec.service.type) .Values.starrocksCnSpec.service.loadbalancerIP }}
loadBalancerIP: {{ .Values.starrocksCnSpec.service.loadbalancerIP }}
{{- end }}
{{- if .Values.starrocksCnSpec.service.annotations }}
annotations:
{{ toYaml .Values.starrocksCnSpec.service.annotations | indent 8 }}
{{- end }}
{{- end }}
{{- if .Values.starrocksCnSpec.annotations }}
annotations:
Expand Down
14 changes: 10 additions & 4 deletions helm-charts/charts/kube-starrocks/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ starrocksFESpec:
type: "ClusterIP"
# the loadBalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty.
loadbalancerIP: ""
# add annotations for fe service.
annotations: {}

# contains the secret name
imagePullSecrets: []
Expand Down Expand Up @@ -183,16 +185,18 @@ starrocksCnSpec:
# specify the service name and port config and serviceType
# the service type refer https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
service:
# the fe service type, only supported ClusterIP, NodePort, LoadBalancer, ExternalName
# the cn service type, only supported ClusterIP, NodePort, LoadBalancer, ExternalName
type: "ClusterIP"
# the loadBalancerIP for static ip config when the type=LoadBalancer and loadBalancerIp is not empty.
loadbalancerIP: ""
# add annotations for cn service.
annotations: {}

# contains the secret name
imagePullSecrets: []
# - name: "image-pull-secret"

# serviceAccount for fe access cloud service.
# serviceAccount for cn access cloud service.
serviceAccount: ""

# If specified, the pod's nodeSelector,displayName="Map of nodeSelectors to match when scheduling pods on nodes"
Expand Down Expand Up @@ -318,16 +322,18 @@ starrocksBeSpec:
# specify the service name and port config and serviceType
# the service type refer https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
service:
# the fe service type, only supported ClusterIP, NodePort, LoadBalancer, ExternalName
# the be service type, only supported ClusterIP, NodePort, LoadBalancer, ExternalName
type: "ClusterIP"
# the loadbalancerIP for static ip config when the type=LoadBalancer and loadbalancerIp is not empty.
loadbalancerIP: ""
# add annotations for be service.
annotations: {}

# contains the secret name
imagePullSecrets: []
# - name: "image-pull-secret"

# serviceAccount for fe access cloud service.
# serviceAccount for be access cloud service.
serviceAccount: ""

# If specified, the pod's nodeSelector,displayName="Map of nodeSelectors to match when scheduling pods on nodes"
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/starrocks/v1/starrockscluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ type StarRocksService struct {
//Ports the components exposed ports and listen ports in pod.
// +optional
Ports []StarRocksServicePort `json:"ports"`

// Annotations store Kubernetes Service annotations.
Annotations map[string]string `json:"annotations,omitempty"`
}

type StarRocksServicePort struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/starrocks/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions pkg/common/resource_utils/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resource_utils

import (
srapi "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1"

"github.com/StarRocks/starrocks-kubernetes-operator/pkg/common/hash"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -44,25 +43,29 @@ func BuildExternalService(src *srapi.StarRocksCluster, name string, serviceType
}
svc.Finalizers = append(svc.Finalizers, srapi.SERVICE_FINALIZER)

anno := map[string]string{}
if serviceType == FeService {
if svc.Name == "" {
svc.Name = src.Name + "-" + srapi.DEFAULT_FE
}
setServiceType(src.Spec.StarRocksFeSpec.Service, &svc)
anno = getServiceAnnotations(src.Spec.StarRocksFeSpec.Service)
srPorts = getFeServicePorts(config)
} else if serviceType == BeService {
if svc.Name == "" {
svc.Name = src.Name + "-" + srapi.DEFAULT_BE
}

setServiceType(src.Spec.StarRocksBeSpec.Service, &svc)
anno = getServiceAnnotations(src.Spec.StarRocksBeSpec.Service)
srPorts = getBeServicePorts(config)
} else if serviceType == CnService {
if svc.Name == "" {
svc.Name = src.Name + "-" + srapi.DEFAULT_CN
}

setServiceType(src.Spec.StarRocksCnSpec.Service, &svc)
anno = getServiceAnnotations(src.Spec.StarRocksCnSpec.Service)
srPorts = getCnServicePorts(config)
}

Expand All @@ -84,12 +87,12 @@ func BuildExternalService(src *srapi.StarRocksCluster, name string, serviceType
TargetPort: intstr.FromInt(int(sp.ContainerPort)),
})
}
// set Ports field before calculate resource hash
svc.Spec.Ports = ports

hso := serviceHashObject(&svc)
anno := map[string]string{}
anno[srapi.ComponentResourceHash] = hash.HashObject(hso)
svc.Annotations = anno
svc.Spec.Ports = ports
return svc
}

Expand Down Expand Up @@ -158,6 +161,17 @@ func setServiceType(svc *srapi.StarRocksService, service *corev1.Service) {
}
}

func getServiceAnnotations(svc *srapi.StarRocksService) map[string]string {
if svc != nil && svc.Annotations != nil {
annotations := map[string]string{}
for key, val := range svc.Annotations {
annotations[key] = val
}
return annotations
}
return map[string]string{}
}

func ServiceDeepEqual(nsvc, oldsvc *corev1.Service) bool {
var nhsvcValue, ohsvcValue string
if _, ok := nsvc.Annotations[srapi.ComponentResourceHash]; ok {
Expand Down
38 changes: 38 additions & 0 deletions pkg/common/resource_utils/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"testing"
)

Expand All @@ -27,3 +28,40 @@ func Test_BuildExternalService(t *testing.T) {
svc := BuildExternalService(&src, "test", FeService, make(map[string]interface{}), make(map[string]string), make(map[string]string))
require.Equal(t, corev1.ServiceTypeLoadBalancer, svc.Spec.Type)
}

func Test_getServiceAnnotations(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 annotations",
args: args{
svc: &srapi.StarRocksService{
Annotations: 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 := getServiceAnnotations(tt.args.svc); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getServiceAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 0 additions & 1 deletion pkg/common/resource_utils/volume.go

This file was deleted.