Skip to content

Commit 66eaa2d

Browse files
authored
feat: add configurable Kubernetes service DNS domain (#1580)
* feat: add configurable Kubernetes service DNS domain Add support for custom Kubernetes service DNS domain suffix to enable operator deployment in clusters using non-default DNS configurations. The default value remains "cluster.local" ensuring backward compatibility. Users can override via Helm: --set redisOperator.serviceDNSDomain=custom.local Signed-off-by: drivebyer <[email protected]> * add docs Signed-off-by: drivebyer <[email protected]> * fix test Signed-off-by: drivebyer <[email protected]> --------- Signed-off-by: drivebyer <[email protected]>
1 parent 42c635f commit 66eaa2d

File tree

8 files changed

+30
-11
lines changed

8 files changed

+30
-11
lines changed

charts/redis-operator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ kubectl create secret tls <webhook-server-cert> --key tls.key --cert tls.crt -n
122122
| redisOperator.podLabels | object | `{}` | |
123123
| redisOperator.pprof.bindAddress | string | `":6060"` | |
124124
| redisOperator.pprof.enabled | bool | `false` | |
125+
| redisOperator.serviceDNSDomain | string | `"cluster.local"` | The DNS domain suffix used for Kubernetes service discovery. Default is "cluster.local". Change this if your cluster uses a custom DNS domain. |
125126
| redisOperator.watchNamespace | string | `""` | |
126127
| redisOperator.webhook | bool | `false` | |
127128
| replicas | int | `1` | |

charts/redis-operator/templates/operator-deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ spec:
9494
- name: WATCH_NAMESPACE
9595
value: {{ .Values.redisOperator.watchNamespace | quote }}
9696
{{- end }}
97+
{{- if .Values.redisOperator.serviceDNSDomain }}
98+
- name: SERVICE_DNS_DOMAIN
99+
value: {{ .Values.redisOperator.serviceDNSDomain | quote }}
100+
{{- end }}
97101
{{- range $env := .Values.redisOperator.env }}
98102
- name: {{ $env.name }}
99103
value: {{ $env.value | quote }}

charts/redis-operator/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ redisOperator:
1616
extraArgs: []
1717
# When not specified, the operator will watch all namespaces. It can be set to a specific namespace or multiple namespaces separated by commas.
1818
watchNamespace: ""
19+
# -- The DNS domain suffix used for Kubernetes service discovery.
20+
# Default is "cluster.local". Change this if your cluster uses a custom DNS domain.
21+
serviceDNSDomain: "cluster.local"
1922
env: []
2023
# If set to true, webhook server will be enabled for masterSlaveAntiAffinity feature
2124
# When enabled, you need to either:

internal/controller/common/redis/heal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
commonapi "github.com/OT-CONTAINER-KIT/redis-operator/api/common/v1beta2"
1111
rsvb2 "github.com/OT-CONTAINER-KIT/redis-operator/api/redissentinel/v1beta2"
1212
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common"
13+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
1314
"github.com/OT-CONTAINER-KIT/redis-operator/internal/service/redis"
1415
"github.com/OT-CONTAINER-KIT/redis-operator/internal/util/cryptutil"
1516
v1 "k8s.io/api/core/v1"
@@ -254,7 +255,7 @@ func createConnectionInfo(ctx context.Context, pod v1.Pod, password string, tlsC
254255
// Configure TLS if enabled
255256
if tlsConfig != nil && tlsConfig.Secret.SecretName != "" {
256257
serviceName := common.GetHeadlessServiceNameFromPodName(pod.Name)
257-
connInfo.Host = fmt.Sprintf("%s.%s.%s.svc.cluster.local", pod.Name, serviceName, namespace)
258+
connInfo.Host = fmt.Sprintf("%s.%s.%s.svc.%s", pod.Name, serviceName, namespace, env.GetServiceDNSDomain())
258259
// Get TLS configuration
259260
tlsCfg := getRedisTLSConfig(ctx, k8sClient, namespace, tlsConfig.Secret.SecretName)
260261
connInfo.TLSConfig = tlsCfg

internal/controller/redissentinel/redissentinel_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common"
1111
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common/redis"
1212
intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/internal/controllerutil"
13+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
1314
"github.com/OT-CONTAINER-KIT/redis-operator/internal/k8sutils"
1415
appsv1 "k8s.io/api/apps/v1"
1516
"k8s.io/apimachinery/pkg/types"
@@ -132,7 +133,7 @@ func (r *RedisSentinelReconciler) reconcileSentinel(ctx context.Context, instanc
132133
return intctrlutil.RequeueE(ctx, err, "")
133134
} else {
134135
if instance.Spec.RedisSentinelConfig.ResolveHostnames == "yes" {
135-
monitorAddr = fmt.Sprintf("%s.%s.%s.svc.cluster.local", master.Name, common.GetHeadlessServiceNameFromPodName(master.Name), rr.Namespace)
136+
monitorAddr = fmt.Sprintf("%s.%s.%s.svc.%s", master.Name, common.GetHeadlessServiceNameFromPodName(master.Name), rr.Namespace, env.GetServiceDNSDomain())
136137
} else {
137138
monitorAddr = master.Status.PodIP
138139
}

internal/env/env.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"os"
2121
"strconv"
2222
"strings"
23+
24+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/util"
2325
)
2426

2527
// Environment variable keys
@@ -38,8 +40,14 @@ const (
3840

3941
// OperatorImageEnv defines the image of the operator
4042
OperatorImageEnv = "OPERATOR_IMAGE"
43+
44+
ServiceDNSDomain = "SERVICE_DNS_DOMAIN"
4145
)
4246

47+
func GetServiceDNSDomain() string {
48+
return util.Coalesce(os.Getenv(ServiceDNSDomain), "cluster.local")
49+
}
50+
4351
// GetOperatorImage returns the image of the operator
4452
func GetOperatorImage() string {
4553
return os.Getenv(OperatorImageEnv)

internal/k8sutils/redis.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
rcvb2 "github.com/OT-CONTAINER-KIT/redis-operator/api/rediscluster/v1beta2"
1616
rrvb2 "github.com/OT-CONTAINER-KIT/redis-operator/api/redisreplication/v1beta2"
1717
common "github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common"
18+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
1819
retry "github.com/avast/retry-go"
1920
redis "github.com/redis/go-redis/v9"
2021
"github.com/samber/lo"
@@ -33,7 +34,7 @@ type RedisDetails struct {
3334
}
3435

3536
func (rd *RedisDetails) FQDN() string {
36-
return fmt.Sprintf("%s.%s.%s.svc", rd.PodName, common.GetHeadlessServiceNameFromPodName(rd.PodName), rd.Namespace)
37+
return fmt.Sprintf("%s.%s.%s.svc.%s", rd.PodName, common.GetHeadlessServiceNameFromPodName(rd.PodName), rd.Namespace, env.GetServiceDNSDomain())
3738
}
3839

3940
func (rd *RedisDetails) String() string {
@@ -692,7 +693,7 @@ func configureRedisReplicationClient(ctx context.Context, client kubernetes.Inte
692693
}
693694

694695
func getRedisReplicationHostname(redisInfo RedisDetails, cr *rrvb2.RedisReplication) string {
695-
return fmt.Sprintf("%s.%s-headless.%s.svc.cluster.local", redisInfo.PodName, cr.Name, cr.Namespace)
696+
return fmt.Sprintf("%s.%s-headless.%s.svc.%s", redisInfo.PodName, cr.Name, cr.Namespace, env.GetServiceDNSDomain())
696697
}
697698

698699
// Get Redis nodes by it's role i.e. master, slave and sentinel

internal/k8sutils/redis_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ func TestCreateMultipleLeaderRedisCommand(t *testing.T) {
339339
},
340340
expectedCommands: []string{
341341
"redis-cli", "--cluster", "create",
342-
"mycluster-leader-0.mycluster-leader-headless.default.svc:6379",
343-
"mycluster-leader-1.mycluster-leader-headless.default.svc:6379",
344-
"mycluster-leader-2.mycluster-leader-headless.default.svc:6379",
342+
"mycluster-leader-0.mycluster-leader-headless.default.svc.cluster.local:6379",
343+
"mycluster-leader-1.mycluster-leader-headless.default.svc.cluster.local:6379",
344+
"mycluster-leader-2.mycluster-leader-headless.default.svc.cluster.local:6379",
345345
"--cluster-yes",
346346
},
347347
},
@@ -455,8 +455,8 @@ func TestCreateRedisReplicationCommand(t *testing.T) {
455455
},
456456
expectedCommand: []string{
457457
"redis-cli", "--cluster", "add-node",
458-
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc:6379",
459-
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc:6379",
458+
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc.cluster.local:6379",
459+
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc.cluster.local:6379",
460460
"--cluster-slave",
461461
"-a", "password",
462462
},
@@ -495,8 +495,8 @@ func TestCreateRedisReplicationCommand(t *testing.T) {
495495
},
496496
expectedCommand: []string{
497497
"redis-cli", "--cluster", "add-node",
498-
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc:6379",
499-
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc:6379",
498+
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc.cluster.local:6379",
499+
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc.cluster.local:6379",
500500
"--cluster-slave",
501501
},
502502
},

0 commit comments

Comments
 (0)