Skip to content

Commit f4d5326

Browse files
lsierantirajdeep
andauthored
CLOUDP-200133: Add force reconfigure to automation config builder (#1382)
Co-authored-by: Rajdeep Das <rajdeep.das@mongodb.com>
1 parent 8a7fc2a commit f4d5326

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

pkg/automationconfig/automation_config.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package automationconfig
33
import (
44
"bytes"
55
"encoding/json"
6+
67
"github.com/mongodb/mongodb-kubernetes-operator/pkg/authentication/scramcredentials"
78
"github.com/spf13/cast"
89
"github.com/stretchr/objx"
@@ -250,11 +251,19 @@ type EngineConfig struct {
250251
CacheSizeGB float32 `json:"cacheSizeGB"`
251252
}
252253

254+
// ReplSetForceConfig setting enables us to force reconfigure automation agent when the MongoDB deployment
255+
// is in a broken state - for ex: doesn't have a primary.
256+
// More info: https://www.mongodb.com/docs/ops-manager/current/reference/api/automation-config/automation-config-parameters/#replica-sets
257+
type ReplSetForceConfig struct {
258+
CurrentVersion int64 `json:"currentVersion"`
259+
}
260+
253261
type ReplicaSet struct {
254-
Id string `json:"_id"`
255-
Members []ReplicaSetMember `json:"members"`
256-
ProtocolVersion string `json:"protocolVersion"`
257-
NumberArbiters int `json:"numberArbiters"`
262+
Id string `json:"_id"`
263+
Members []ReplicaSetMember `json:"members"`
264+
ProtocolVersion string `json:"protocolVersion"`
265+
NumberArbiters int `json:"numberArbiters"`
266+
Force *ReplSetForceConfig `json:"force,omitempty"`
258267
}
259268

260269
type ReplicaSetMember struct {

pkg/automationconfig/automation_config_builder.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ type Builder struct {
3939
mongodbVersion string
4040
previousAC AutomationConfig
4141
// MongoDB installable versions
42-
versions []MongoDbVersionConfig
43-
backupVersions []BackupVersion
44-
monitoringVersions []MonitoringVersion
45-
options Options
46-
processModifications []func(int, *Process)
47-
modifications []Modification
48-
auth *Auth
49-
cafilePath string
50-
sslConfig *TLS
51-
tlsConfig *TLS
52-
dataDir string
53-
port int
54-
memberOptions []MemberOptions
42+
versions []MongoDbVersionConfig
43+
backupVersions []BackupVersion
44+
monitoringVersions []MonitoringVersion
45+
options Options
46+
processModifications []func(int, *Process)
47+
modifications []Modification
48+
auth *Auth
49+
cafilePath string
50+
sslConfig *TLS
51+
tlsConfig *TLS
52+
dataDir string
53+
port int
54+
memberOptions []MemberOptions
55+
forceReconfigureToVersion *int64
5556
}
5657

5758
func NewBuilder() *Builder {
@@ -191,6 +192,11 @@ func (b *Builder) SetAuth(auth Auth) *Builder {
191192
return b
192193
}
193194

195+
func (b *Builder) SetForceReconfigureToVersion(version int64) *Builder {
196+
b.forceReconfigureToVersion = &version
197+
return b
198+
}
199+
194200
func (b *Builder) AddProcessModification(f func(int, *Process)) *Builder {
195201
b.processModifications = append(b.processModifications, f)
196202
return b
@@ -354,6 +360,11 @@ func (b *Builder) Build() (AutomationConfig, error) {
354360
b.versions = append(b.versions, dummyConfig)
355361
}
356362

363+
var replSetForceConfig *ReplSetForceConfig
364+
if b.forceReconfigureToVersion != nil {
365+
replSetForceConfig = &ReplSetForceConfig{CurrentVersion: *b.forceReconfigureToVersion}
366+
}
367+
357368
currentAc := AutomationConfig{
358369
Version: b.previousAC.Version,
359370
Processes: processes,
@@ -363,6 +374,7 @@ func (b *Builder) Build() (AutomationConfig, error) {
363374
Members: members,
364375
ProtocolVersion: "1",
365376
NumberArbiters: b.arbiters,
377+
Force: replSetForceConfig,
366378
},
367379
},
368380
MonitoringVersions: b.monitoringVersions,

pkg/automationconfig/automation_config_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ func defaultMongoDbVersion(version string) MongoDbVersionConfig {
2828
}
2929

3030
func TestBuildAutomationConfig(t *testing.T) {
31-
ac, err := NewBuilder().
31+
builder := NewBuilder().
3232
SetName("my-rs").
3333
SetDomain("my-ns.svc.cluster.local").
3434
SetMongoDBVersion("4.2.0").
3535
SetMembers(3).
3636
SetFCV("4.0").
37-
Build()
37+
SetForceReconfigureToVersion(-1)
38+
39+
ac, err := builder.Build()
3840

3941
assert.NoError(t, err)
4042
assert.Len(t, ac.Processes, 3)
@@ -56,13 +58,22 @@ func TestBuildAutomationConfig(t *testing.T) {
5658
rs := ac.ReplicaSets[0]
5759
assert.Equal(t, rs.Id, "my-rs", "The name provided should be configured to be the rs id")
5860
assert.Len(t, rs.Members, 3, "there should be the number of replicas provided")
61+
require.NotNil(t, rs.Force)
62+
assert.Equal(t, ReplSetForceConfig{CurrentVersion: -1}, *rs.Force)
5963

6064
for i, member := range rs.Members {
6165
assert.Equal(t, 1, *member.Votes)
6266
assert.False(t, member.ArbiterOnly)
6367
assert.Equal(t, i, member.Id)
6468
assert.Equal(t, ac.Processes[i].Name, member.Host)
6569
}
70+
71+
builder.SetForceReconfigureToVersion(1)
72+
ac, err = builder.Build()
73+
assert.NoError(t, err)
74+
rs = ac.ReplicaSets[0]
75+
require.NotNil(t, rs.Force)
76+
assert.Equal(t, ReplSetForceConfig{CurrentVersion: 1}, *rs.Force)
6677
}
6778

6879
func TestBuildAutomationConfigArbiters(t *testing.T) {

0 commit comments

Comments
 (0)