Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 7900ab7

Browse files
authored
Add support for split horizon in replica set. (#219)
1 parent e95497e commit 7900ab7

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

deploy/crds/mongodb.com_mongodb_crd.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ spec:
4949
members:
5050
description: Members is the number of members in the replica set
5151
type: integer
52+
replicaSetHorizons:
53+
description: Add this parameter and values if you need your database
54+
to be accessed outside of Kubernetes. This setting allows you to
55+
provide different DNS settings within the Kubernetes cluster and
56+
to the Kubernetes cluster. The Kubernetes Operator uses split horizon
57+
DNS for replica set members. This feature allows communication both
58+
within the Kubernetes cluster and from outside Kubernetes.
59+
items:
60+
properties: {}
61+
type: object
62+
type: array
5263
security:
5364
description: Security configures security features, such as TLS, and
5465
authentication settings for a deployment

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
appsv1 "k8s.io/api/apps/v1"
99

10+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
1011
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/scale"
1112

1213
"k8s.io/apimachinery/pkg/runtime"
@@ -50,6 +51,11 @@ type MongoDBSpec struct {
5051
// +optional
5152
FeatureCompatibilityVersion string `json:"featureCompatibilityVersion,omitempty"`
5253

54+
// ReplicaSetHorizons allows providing different DNS settings within the
55+
// Kubernetes cluster and to the Kubernetes cluster.
56+
// +optional
57+
ReplicaSetHorizons ReplicaSetHorizonConfiguration `json:"replicaSetHorizons,omitempty"`
58+
5359
// Security configures security features, such as TLS, and authentication settings for a deployment
5460
// +required
5561
Security Security `json:"security"`
@@ -68,6 +74,10 @@ type MongoDBSpec struct {
6874
AdditionalMongodConfig MongodConfiguration `json:"additionalMongodConfig,omitempty"`
6975
}
7076

77+
// ReplicaSetHorizonConfiguration holds the split horizon DNS settings for
78+
// replica set members.
79+
type ReplicaSetHorizonConfiguration []automationconfig.ReplicaSetHorizons
80+
7181
// StatefulSetConfiguration holds the optional custom StatefulSet
7282
// that should be merged into the operator created one.
7383
type StatefulSetConfiguration struct {

pkg/automationconfig/automation_config.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,24 @@ type ReplicaSet struct {
9494
}
9595

9696
type ReplicaSetMember struct {
97-
Id int `json:"_id"`
98-
Host string `json:"host"`
99-
Priority int `json:"priority"`
100-
ArbiterOnly bool `json:"arbiterOnly"`
101-
Votes int `json:"votes"`
97+
Id int `json:"_id"`
98+
Host string `json:"host"`
99+
Priority int `json:"priority"`
100+
ArbiterOnly bool `json:"arbiterOnly"`
101+
Votes int `json:"votes"`
102+
Horizons ReplicaSetHorizons `json:"horizons,omitempty"`
102103
}
103104

104-
func newReplicaSetMember(p Process, id int) ReplicaSetMember {
105+
type ReplicaSetHorizons map[string]string
106+
107+
func newReplicaSetMember(p Process, id int, horizons ReplicaSetHorizons) ReplicaSetMember {
105108
return ReplicaSetMember{
106109
Id: id,
107110
Host: p.Name,
108111
Priority: 1,
109112
ArbiterOnly: false,
110113
Votes: 1,
114+
Horizons: horizons,
111115
}
112116
}
113117

pkg/automationconfig/automation_config_builder.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ func NOOP() Modification {
2424
}
2525

2626
type Builder struct {
27-
enabler AuthEnabler
28-
processes []Process
29-
replicaSets []ReplicaSet
30-
members int
31-
domain string
32-
name string
33-
fcv string
34-
topology Topology
35-
mongodbVersion string
36-
previousAC AutomationConfig
27+
enabler AuthEnabler
28+
processes []Process
29+
replicaSets []ReplicaSet
30+
replicaSetHorizons []ReplicaSetHorizons
31+
members int
32+
domain string
33+
name string
34+
fcv string
35+
topology Topology
36+
mongodbVersion string
37+
previousAC AutomationConfig
3738
// MongoDB installable versions
3839
versions []MongoDbVersionConfig
3940
toolsVersion ToolsVersion
@@ -59,6 +60,11 @@ func (b *Builder) SetTopology(topology Topology) *Builder {
5960
return b
6061
}
6162

63+
func (b *Builder) SetReplicaSetHorizons(horizons []ReplicaSetHorizons) *Builder {
64+
b.replicaSetHorizons = horizons
65+
return b
66+
}
67+
6268
func (b *Builder) SetMembers(members int) *Builder {
6369
b.members = members
6470
return b
@@ -124,7 +130,12 @@ func (b *Builder) Build() (AutomationConfig, error) {
124130

125131
process := newProcess(toHostName(b.name, i), h, b.mongodbVersion, b.name, opts...)
126132
processes[i] = process
127-
members[i] = newReplicaSetMember(process, i)
133+
134+
if b.replicaSetHorizons != nil {
135+
members[i] = newReplicaSetMember(process, i, b.replicaSetHorizons[i])
136+
} else {
137+
members[i] = newReplicaSetMember(process, i, nil)
138+
}
128139
}
129140

130141
auth := disabledAuth()

pkg/automationconfig/automation_config_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ func TestBuildAutomationConfig(t *testing.T) {
6262
}
6363
}
6464

65+
func TestReplicaSetHorizons(t *testing.T) {
66+
ac, err := NewBuilder().
67+
SetName("my-rs").
68+
SetDomain("my-ns.svc.cluster.local").
69+
SetMongoDBVersion("4.2.0").
70+
SetMembers(3).
71+
SetReplicaSetHorizons([]ReplicaSetHorizons{
72+
{"horizon": "test-horizon-0"},
73+
{"horizon": "test-horizon-1"},
74+
{"horizon": "test-horizon-2"},
75+
}).
76+
Build()
77+
78+
assert.NoError(t, err)
79+
80+
for i, member := range ac.ReplicaSets[0].Members {
81+
assert.NotEmpty(t, member.Horizons)
82+
assert.Contains(t, member.Horizons, "horizon")
83+
assert.Equal(t, fmt.Sprintf("test-horizon-%d", i), member.Horizons["horizon"])
84+
}
85+
}
86+
6587
func TestMongoDbVersions(t *testing.T) {
6688
ac, err := NewBuilder().
6789
SetName("my-rs").

pkg/controller/mongodb/replica_set_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ func buildAutomationConfig(mdb mdbv1.MongoDB, mdbVersionConfig automationconfig.
470470
SetName(mdb.Name).
471471
SetDomain(domain).
472472
SetMembers(mdb.AutomationConfigMembersThisReconciliation()).
473+
SetReplicaSetHorizons(mdb.Spec.ReplicaSetHorizons).
473474
SetPreviousAutomationConfig(currentAc).
474475
SetMongoDBVersion(mdb.Spec.Version).
475476
SetFCV(mdb.GetFCV()).

0 commit comments

Comments
 (0)