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

Commit d94c20a

Browse files
authored
fix(pkg/automationconfig): do not try to access non-declared split horizon dns records (#890)
* fix(pkg/automationconfig): do not try to access non-declared split horizon dns records The issue occurs during scaling down, as the automation config is tried to be built with actual numbers of replicas with desired split-horizon dns records. Then a panic with out of range of the related slice is triggered. Signed-off-by: maxgio92 <[email protected]> * test(pkg/automationconfig): add test for multiple dns horizons Signed-off-by: maxgio92 <[email protected]>
1 parent 12a79b3 commit d94c20a

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

pkg/automationconfig/automation_config_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (b *Builder) Build() (AutomationConfig, error) {
283283
processes[i] = *process
284284

285285
var horizon ReplicaSetHorizons
286-
if b.replicaSetHorizons != nil {
286+
if b.replicaSetHorizons != nil && i < len(b.replicaSetHorizons) {
287287
horizon = b.replicaSetHorizons[i]
288288
}
289289

pkg/automationconfig/automation_config_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,74 @@ func TestBuildAutomationConfigArbiters(t *testing.T) {
159159
assert.Equal(t, 1, m[11].Votes)
160160
}
161161

162+
func TestReplicaSetMultipleHorizonsScaleDown(t *testing.T) {
163+
var expected ReplicaSetHorizons
164+
165+
horizons := []ReplicaSetHorizons{
166+
{
167+
"internal": "test-horizon-0",
168+
"external": "test-horizon-0",
169+
},
170+
{
171+
"internal": "test-horizon-1",
172+
"external": "test-horizon-1",
173+
},
174+
{
175+
"internal": "test-horizon-2",
176+
"external": "test-horizon-2",
177+
},
178+
}
179+
ac, err := NewBuilder().
180+
SetName("my-rs").
181+
SetDomain("my-ns.svc.cluster.local").
182+
SetMongoDBVersion("4.2.0").
183+
SetMembers(4).
184+
SetReplicaSetHorizons(horizons).
185+
Build()
186+
187+
assert.NoError(t, err)
188+
189+
for i, member := range ac.ReplicaSets[0].Members {
190+
if i >= len(horizons) {
191+
expected = nil
192+
} else {
193+
expected = ReplicaSetHorizons{
194+
"internal": fmt.Sprintf("test-horizon-%d", i),
195+
"external": fmt.Sprintf("test-horizon-%d", i),
196+
}
197+
}
198+
assert.Equal(t, expected, member.Horizons)
199+
}
200+
}
201+
202+
func TestReplicaSetHorizonsScaleDown(t *testing.T) {
203+
var expected ReplicaSetHorizons
204+
205+
horizons := []ReplicaSetHorizons{
206+
{"horizon": "test-horizon-0"},
207+
{"horizon": "test-horizon-1"},
208+
{"horizon": "test-horizon-2"},
209+
}
210+
ac, err := NewBuilder().
211+
SetName("my-rs").
212+
SetDomain("my-ns.svc.cluster.local").
213+
SetMongoDBVersion("4.2.0").
214+
SetMembers(4).
215+
SetReplicaSetHorizons(horizons).
216+
Build()
217+
218+
assert.NoError(t, err)
219+
220+
for i, member := range ac.ReplicaSets[0].Members {
221+
if i >= len(horizons) {
222+
expected = nil
223+
} else {
224+
expected = ReplicaSetHorizons{"horizon": fmt.Sprintf("test-horizon-%d", i)}
225+
}
226+
assert.Equal(t, expected, member.Horizons)
227+
}
228+
}
229+
162230
func TestReplicaSetHorizons(t *testing.T) {
163231
ac, err := NewBuilder().
164232
SetName("my-rs").

0 commit comments

Comments
 (0)