Skip to content

Commit 79ea018

Browse files
authored
feat: add headless service configuration support (#1219)
* feat: add headless service configuration support - Introduced a new `Headless` service configuration in `ServiceConfig` struct - Added method `GetHeadlessServiceAnnotations()` to retrieve headless service annotations - Updated CRD bases to include headless service configuration - Modified service creation logic in Redis cluster and standalone services to support headless service annotations - Added e2e test cases for standalone Redis with headless service configuration This enhancement provides more flexibility in configuring headless services for Redis deployments. Signed-off-by: drivebyer <wuyangmuc@gmail.com> * fix Signed-off-by: drivebyer <wuyangmuc@gmail.com> * fix Signed-off-by: drivebyer <wuyangmuc@gmail.com> --------- Signed-off-by: drivebyer <wuyangmuc@gmail.com>
1 parent 2979e21 commit 79ea018

15 files changed

+390
-8
lines changed

api/common_types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,32 @@ func (in *KubernetesConfig) GetServiceAnnotations() map[string]string {
3333
return in.Service.ServiceAnnotations
3434
}
3535

36+
func (in *KubernetesConfig) GetHeadlessServiceAnnotations() map[string]string {
37+
if in.Service == nil {
38+
return nil
39+
}
40+
if in.Service.Headless == nil {
41+
return nil
42+
}
43+
return in.Service.Headless.AdditionalAnnotations
44+
}
45+
3646
// ServiceConfig define the type of service to be created and its annotations
3747
// +k8s:deepcopy-gen=true
3848
type ServiceConfig struct {
3949
// +kubebuilder:validation:Enum=LoadBalancer;NodePort;ClusterIP
4050
ServiceType string `json:"serviceType,omitempty"`
4151
ServiceAnnotations map[string]string `json:"annotations,omitempty"`
52+
Headless *Service `json:"headless,omitempty"`
53+
}
54+
55+
// Service is the struct to define the service type and its annotations
56+
// +k8s:deepcopy-gen=true
57+
type Service struct {
58+
// +kubebuilder:validation:Enum=LoadBalancer;NodePort;ClusterIP
59+
// +kubebuilder:default:=ClusterIP
60+
Type string `json:"type,omitempty"`
61+
AdditionalAnnotations map[string]string `json:"additionalAnnotations,omitempty"`
4262
}
4363

4464
// ExistingPasswordSecret is the struct to access the existing secret

api/zz_generated.deepcopy.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/redis.redis.opstreelabs.in_redis.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,22 @@ spec:
11031103
additionalProperties:
11041104
type: string
11051105
type: object
1106+
headless:
1107+
description: Service is the struct to define the service type
1108+
and its annotations
1109+
properties:
1110+
additionalAnnotations:
1111+
additionalProperties:
1112+
type: string
1113+
type: object
1114+
type:
1115+
default: ClusterIP
1116+
enum:
1117+
- LoadBalancer
1118+
- NodePort
1119+
- ClusterIP
1120+
type: string
1121+
type: object
11061122
serviceType:
11071123
enum:
11081124
- LoadBalancer
@@ -6022,6 +6038,22 @@ spec:
60226038
additionalProperties:
60236039
type: string
60246040
type: object
6041+
headless:
6042+
description: Service is the struct to define the service type
6043+
and its annotations
6044+
properties:
6045+
additionalAnnotations:
6046+
additionalProperties:
6047+
type: string
6048+
type: object
6049+
type:
6050+
default: ClusterIP
6051+
enum:
6052+
- LoadBalancer
6053+
- NodePort
6054+
- ClusterIP
6055+
type: string
6056+
type: object
60256057
serviceType:
60266058
enum:
60276059
- LoadBalancer

config/crd/bases/redis.redis.opstreelabs.in_redisclusters.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ spec:
240240
additionalProperties:
241241
type: string
242242
type: object
243+
headless:
244+
description: Service is the struct to define the service type
245+
and its annotations
246+
properties:
247+
additionalAnnotations:
248+
additionalProperties:
249+
type: string
250+
type: object
251+
type:
252+
default: ClusterIP
253+
enum:
254+
- LoadBalancer
255+
- NodePort
256+
- ClusterIP
257+
type: string
258+
type: object
243259
serviceType:
244260
enum:
245261
- LoadBalancer
@@ -6910,6 +6926,22 @@ spec:
69106926
additionalProperties:
69116927
type: string
69126928
type: object
6929+
headless:
6930+
description: Service is the struct to define the service type
6931+
and its annotations
6932+
properties:
6933+
additionalAnnotations:
6934+
additionalProperties:
6935+
type: string
6936+
type: object
6937+
type:
6938+
default: ClusterIP
6939+
enum:
6940+
- LoadBalancer
6941+
- NodePort
6942+
- ClusterIP
6943+
type: string
6944+
type: object
69136945
serviceType:
69146946
enum:
69156947
- LoadBalancer

config/crd/bases/redis.redis.opstreelabs.in_redisreplications.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,22 @@ spec:
11051105
additionalProperties:
11061106
type: string
11071107
type: object
1108+
headless:
1109+
description: Service is the struct to define the service type
1110+
and its annotations
1111+
properties:
1112+
additionalAnnotations:
1113+
additionalProperties:
1114+
type: string
1115+
type: object
1116+
type:
1117+
default: ClusterIP
1118+
enum:
1119+
- LoadBalancer
1120+
- NodePort
1121+
- ClusterIP
1122+
type: string
1123+
type: object
11081124
serviceType:
11091125
enum:
11101126
- LoadBalancer
@@ -6037,6 +6053,22 @@ spec:
60376053
additionalProperties:
60386054
type: string
60396055
type: object
6056+
headless:
6057+
description: Service is the struct to define the service type
6058+
and its annotations
6059+
properties:
6060+
additionalAnnotations:
6061+
additionalProperties:
6062+
type: string
6063+
type: object
6064+
type:
6065+
default: ClusterIP
6066+
enum:
6067+
- LoadBalancer
6068+
- NodePort
6069+
- ClusterIP
6070+
type: string
6071+
type: object
60406072
serviceType:
60416073
enum:
60426074
- LoadBalancer

config/crd/bases/redis.redis.opstreelabs.in_redissentinels.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,22 @@ spec:
11061106
additionalProperties:
11071107
type: string
11081108
type: object
1109+
headless:
1110+
description: Service is the struct to define the service type
1111+
and its annotations
1112+
properties:
1113+
additionalAnnotations:
1114+
additionalProperties:
1115+
type: string
1116+
type: object
1117+
type:
1118+
default: ClusterIP
1119+
enum:
1120+
- LoadBalancer
1121+
- NodePort
1122+
- ClusterIP
1123+
type: string
1124+
type: object
11091125
serviceType:
11101126
enum:
11111127
- LoadBalancer
@@ -3570,6 +3586,22 @@ spec:
35703586
additionalProperties:
35713587
type: string
35723588
type: object
3589+
headless:
3590+
description: Service is the struct to define the service type
3591+
and its annotations
3592+
properties:
3593+
additionalAnnotations:
3594+
additionalProperties:
3595+
type: string
3596+
type: object
3597+
type:
3598+
default: ClusterIP
3599+
enum:
3600+
- LoadBalancer
3601+
- NodePort
3602+
- ClusterIP
3603+
type: string
3604+
type: object
35733605
serviceType:
35743606
enum:
35753607
- LoadBalancer

pkg/k8sutils/redis-cluster.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,24 @@ func (service RedisClusterService) CreateRedisClusterService(ctx context.Context
311311
} else {
312312
epp = disableMetrics
313313
}
314-
annotations := generateServiceAnots(cr.ObjectMeta, nil, epp)
315-
objectMetaInfo := generateObjectMetaInformation(serviceName, cr.Namespace, labels, annotations)
316-
headlessObjectMetaInfo := generateObjectMetaInformation(serviceName+"-headless", cr.Namespace, labels, annotations)
317-
additionalObjectMetaInfo := generateObjectMetaInformation(serviceName+"-additional", cr.Namespace, labels, generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetServiceAnnotations(), epp))
314+
objectMetaInfo := generateObjectMetaInformation(
315+
serviceName,
316+
cr.Namespace,
317+
labels,
318+
generateServiceAnots(cr.ObjectMeta, nil, epp),
319+
)
320+
headlessObjectMetaInfo := generateObjectMetaInformation(
321+
serviceName+"-headless",
322+
cr.Namespace,
323+
labels,
324+
generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetHeadlessServiceAnnotations(), epp),
325+
)
326+
additionalObjectMetaInfo := generateObjectMetaInformation(
327+
serviceName+"-additional",
328+
cr.Namespace,
329+
labels,
330+
generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetServiceAnnotations(), epp),
331+
)
318332
err := CreateOrUpdateService(ctx, cr.Namespace, headlessObjectMetaInfo, redisClusterAsOwner(cr), disableMetrics, true, "ClusterIP", *cr.Spec.Port, cl)
319333
if err != nil {
320334
log.FromContext(ctx).Error(err, "Cannot create headless service for Redis", "Setup.Type", service.RedisServiceRole)

pkg/k8sutils/redis-standalone.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ func CreateStandaloneService(ctx context.Context, cr *redisv1beta2.Redis, cl kub
2222
} else {
2323
epp = disableMetrics
2424
}
25-
annotations := generateServiceAnots(cr.ObjectMeta, nil, epp)
26-
objectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name, cr.Namespace, labels, annotations)
27-
headlessObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-headless", cr.Namespace, labels, annotations)
28-
additionalObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-additional", cr.Namespace, labels, generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetServiceAnnotations(), epp))
25+
objectMetaInfo := generateObjectMetaInformation(
26+
cr.ObjectMeta.Name,
27+
cr.Namespace,
28+
labels,
29+
generateServiceAnots(cr.ObjectMeta, nil, epp),
30+
)
31+
headlessObjectMetaInfo := generateObjectMetaInformation(
32+
cr.ObjectMeta.Name+"-headless",
33+
cr.Namespace,
34+
labels,
35+
generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetHeadlessServiceAnnotations(), epp),
36+
)
37+
additionalObjectMetaInfo := generateObjectMetaInformation(
38+
cr.ObjectMeta.Name+"-additional",
39+
cr.Namespace,
40+
labels,
41+
generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.GetServiceAnnotations(), epp),
42+
)
2943
err := CreateOrUpdateService(ctx, cr.Namespace, headlessObjectMetaInfo, redisAsOwner(cr), disableMetrics, true, "ClusterIP", redisPort, cl)
3044
if err != nil {
3145
log.FromContext(ctx).Error(err, "Cannot create standalone headless service for Redis")

tests/e2e-chainsaw/v1beta2/setup/redis-cluster/cluster.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ spec:
1111
runAsUser: 1000
1212
fsGroup: 1000
1313
kubernetesConfig:
14+
service:
15+
headless:
16+
additionalAnnotations:
17+
test: test
1418
image: quay.io/opstree/redis:latest
1519
imagePullPolicy: Always
1620
resources:

tests/e2e-chainsaw/v1beta2/setup/redis-cluster/ready-svc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ metadata:
7575
prometheus.io/scrape: 'true'
7676
redis.opstreelabs.in: 'true'
7777
redis.opstreelabs.instance: redis-cluster-v1beta2
78+
test: test
7879
labels:
7980
app: redis-cluster-v1beta2-leader
8081
redis_setup_type: cluster
@@ -176,6 +177,7 @@ metadata:
176177
prometheus.io/scrape: 'true'
177178
redis.opstreelabs.in: 'true'
178179
redis.opstreelabs.instance: redis-cluster-v1beta2
180+
test: test
179181
labels:
180182
app: redis-cluster-v1beta2-follower
181183
redis_setup_type: cluster

0 commit comments

Comments
 (0)