Skip to content

Commit d54a78a

Browse files
committed
memgraph operator fixes for service discovery config
1 parent 3ef80c7 commit d54a78a

6 files changed

Lines changed: 115 additions & 26 deletions

File tree

‎api/v1alpha1/memgraphcluster_types.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ type MemgraphClusterSpec struct {
7878
// Affinity for pod scheduling
7979
// +optional
8080
Affinity *corev1.Affinity `json:"affinity,omitempty"`
81+
82+
// ServiceNames allows customizing the generated service names
83+
// +optional
84+
ServiceNames *ServiceNamesSpec `json:"serviceNames,omitempty"`
85+
}
86+
87+
// ServiceNamesSpec allows customizing the suffixes for generated services
88+
type ServiceNamesSpec struct {
89+
// HeadlessSuffix is the suffix for the headless service (default: "-hl")
90+
// +kubebuilder:default="-hl"
91+
// +optional
92+
HeadlessSuffix string `json:"headlessSuffix,omitempty"`
93+
94+
// WriteSuffix is the suffix for the write service (default: "-write")
95+
// +kubebuilder:default="-write"
96+
// +optional
97+
WriteSuffix string `json:"writeSuffix,omitempty"`
98+
99+
// ReadSuffix is the suffix for the read service (default: "-read")
100+
// +kubebuilder:default="-read"
101+
// +optional
102+
ReadSuffix string `json:"readSuffix,omitempty"`
81103
}
82104

83105
// StorageSpec defines the persistent storage configuration

‎api/v1alpha1/zz_generated.deepcopy.go‎

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

‎config/crd/bases/memgraph.base14.io_memgraphclusters.yaml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,26 @@ spec:
11021102
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
11031103
type: object
11041104
type: object
1105+
serviceNames:
1106+
description: ServiceNames allows customizing the generated service
1107+
names
1108+
properties:
1109+
headlessSuffix:
1110+
default: -hl
1111+
description: 'HeadlessSuffix is the suffix for the headless service
1112+
(default: "-hl")'
1113+
type: string
1114+
readSuffix:
1115+
default: -read
1116+
description: 'ReadSuffix is the suffix for the read service (default:
1117+
"-read")'
1118+
type: string
1119+
writeSuffix:
1120+
default: -write
1121+
description: 'WriteSuffix is the suffix for the write service
1122+
(default: "-write")'
1123+
type: string
1124+
type: object
11051125
snapshot:
11061126
description: Snapshot defines snapshot and backup configuration
11071127
properties:

‎internal/controller/replication.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (rm *ReplicationManager) ConfigureReplication(ctx context.Context, cluster
7777
}
7878

7979
replicaName := rm.getReplicaName(pod.Name)
80-
replicaHost := rm.getPodFQDN(pod.Name, cluster.Name, cluster.Namespace)
80+
replicaHost := getPodFQDN(pod.Name, cluster)
8181

8282
// Ensure replica role
8383
if err := rm.ensureReplicaRole(ctx, cluster.Namespace, pod.Name); err != nil {
@@ -266,8 +266,9 @@ func (rm *ReplicationManager) getReplicaName(podName string) string {
266266
}
267267

268268
// getPodFQDN returns the fully qualified domain name for a pod
269-
func (rm *ReplicationManager) getPodFQDN(podName, clusterName, namespace string) string {
269+
// Uses the headless service name from the cluster spec
270+
func getPodFQDN(podName string, cluster *memgraphv1alpha1.MemgraphCluster) string {
270271
// Format: pod-name.headless-service.namespace.svc.cluster.local
271-
headlessService := clusterName + "-headless"
272-
return fmt.Sprintf("%s.%s.%s.svc.cluster.local", podName, headlessService, namespace)
272+
headlessService := headlessServiceName(cluster)
273+
return fmt.Sprintf("%s.%s.%s.svc.cluster.local", podName, headlessService, cluster.Namespace)
273274
}

‎internal/controller/replication_test.go‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,42 @@ func TestReplicationManager_getReplicaName(t *testing.T) {
3434
}
3535
}
3636

37-
func TestReplicationManager_getPodFQDN(t *testing.T) {
38-
rm := &ReplicationManager{}
39-
37+
func TestGetPodFQDN(t *testing.T) {
4038
tests := []struct {
41-
podName string
42-
clusterName string
43-
namespace string
44-
expected string
39+
name string
40+
podName string
41+
cluster *memgraphv1alpha1.MemgraphCluster
42+
expected string
4543
}{
4644
{
47-
"my-cluster-0",
48-
"my-cluster",
49-
"default",
50-
"my-cluster-0.my-cluster-headless.default.svc.cluster.local",
45+
name: "default suffix",
46+
podName: "my-cluster-0",
47+
cluster: &memgraphv1alpha1.MemgraphCluster{
48+
ObjectMeta: metav1.ObjectMeta{Name: "my-cluster", Namespace: "default"},
49+
},
50+
expected: "my-cluster-0.my-cluster-hl.default.svc.cluster.local",
5151
},
5252
{
53-
"prod-db-1",
54-
"prod-db",
55-
"production",
56-
"prod-db-1.prod-db-headless.production.svc.cluster.local",
53+
name: "custom suffix",
54+
podName: "prod-db-1",
55+
cluster: &memgraphv1alpha1.MemgraphCluster{
56+
ObjectMeta: metav1.ObjectMeta{Name: "prod-db", Namespace: "production"},
57+
Spec: memgraphv1alpha1.MemgraphClusterSpec{
58+
ServiceNames: &memgraphv1alpha1.ServiceNamesSpec{
59+
HeadlessSuffix: "-headless",
60+
},
61+
},
62+
},
63+
expected: "prod-db-1.prod-db-headless.production.svc.cluster.local",
5764
},
5865
}
5966

6067
for _, tt := range tests {
61-
t.Run(tt.podName, func(t *testing.T) {
62-
result := rm.getPodFQDN(tt.podName, tt.clusterName, tt.namespace)
68+
t.Run(tt.name, func(t *testing.T) {
69+
result := getPodFQDN(tt.podName, tt.cluster)
6370
if result != tt.expected {
64-
t.Errorf("getPodFQDN(%s, %s, %s) = %s, want %s",
65-
tt.podName, tt.clusterName, tt.namespace, result, tt.expected)
71+
t.Errorf("getPodFQDN(%s, cluster) = %s, want %s",
72+
tt.podName, result, tt.expected)
6673
}
6774
})
6875
}

‎internal/controller/service.go‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,36 @@ import (
1010
memgraphv1alpha1 "github.com/base14/memgraph-operator/api/v1alpha1"
1111
)
1212

13+
// Default service name suffixes
14+
const (
15+
defaultHeadlessSuffix = "-hl"
16+
defaultWriteSuffix = "-write"
17+
defaultReadSuffix = "-read"
18+
)
19+
1320
// Service name helpers
1421
func headlessServiceName(cluster *memgraphv1alpha1.MemgraphCluster) string {
15-
return cluster.Name + "-hl"
22+
suffix := defaultHeadlessSuffix
23+
if cluster.Spec.ServiceNames != nil && cluster.Spec.ServiceNames.HeadlessSuffix != "" {
24+
suffix = cluster.Spec.ServiceNames.HeadlessSuffix
25+
}
26+
return cluster.Name + suffix
1627
}
1728

1829
func writeServiceName(cluster *memgraphv1alpha1.MemgraphCluster) string {
19-
return cluster.Name + "-write"
30+
suffix := defaultWriteSuffix
31+
if cluster.Spec.ServiceNames != nil && cluster.Spec.ServiceNames.WriteSuffix != "" {
32+
suffix = cluster.Spec.ServiceNames.WriteSuffix
33+
}
34+
return cluster.Name + suffix
2035
}
2136

2237
func readServiceName(cluster *memgraphv1alpha1.MemgraphCluster) string {
23-
return cluster.Name + "-read"
38+
suffix := defaultReadSuffix
39+
if cluster.Spec.ServiceNames != nil && cluster.Spec.ServiceNames.ReadSuffix != "" {
40+
suffix = cluster.Spec.ServiceNames.ReadSuffix
41+
}
42+
return cluster.Name + suffix
2443
}
2544

2645
// buildHeadlessService creates the headless service for StatefulSet DNS

0 commit comments

Comments
 (0)