diff --git a/pkg/automationconfig/automation_config.go b/pkg/automationconfig/automation_config.go index dac98a16b..d81702432 100644 --- a/pkg/automationconfig/automation_config.go +++ b/pkg/automationconfig/automation_config.go @@ -3,6 +3,7 @@ package automationconfig import ( "bytes" "encoding/json" + "github.com/mongodb/mongodb-kubernetes-operator/pkg/authentication/scramcredentials" "github.com/spf13/cast" "github.com/stretchr/objx" @@ -250,11 +251,19 @@ type EngineConfig struct { CacheSizeGB float32 `json:"cacheSizeGB"` } +// ReplSetForceConfig setting enables us to force reconfigure automation agent when the MongoDB deployment +// is in a broken state - for ex: doesn't have a primary. +// More info: https://www.mongodb.com/docs/ops-manager/current/reference/api/automation-config/automation-config-parameters/#replica-sets +type ReplSetForceConfig struct { + CurrentVersion int64 `json:"currentVersion"` +} + type ReplicaSet struct { - Id string `json:"_id"` - Members []ReplicaSetMember `json:"members"` - ProtocolVersion string `json:"protocolVersion"` - NumberArbiters int `json:"numberArbiters"` + Id string `json:"_id"` + Members []ReplicaSetMember `json:"members"` + ProtocolVersion string `json:"protocolVersion"` + NumberArbiters int `json:"numberArbiters"` + Force *ReplSetForceConfig `json:"force,omitempty"` } type ReplicaSetMember struct { diff --git a/pkg/automationconfig/automation_config_builder.go b/pkg/automationconfig/automation_config_builder.go index 02411c0e1..80df8aec0 100644 --- a/pkg/automationconfig/automation_config_builder.go +++ b/pkg/automationconfig/automation_config_builder.go @@ -39,19 +39,20 @@ type Builder struct { mongodbVersion string previousAC AutomationConfig // MongoDB installable versions - versions []MongoDbVersionConfig - backupVersions []BackupVersion - monitoringVersions []MonitoringVersion - options Options - processModifications []func(int, *Process) - modifications []Modification - auth *Auth - cafilePath string - sslConfig *TLS - tlsConfig *TLS - dataDir string - port int - memberOptions []MemberOptions + versions []MongoDbVersionConfig + backupVersions []BackupVersion + monitoringVersions []MonitoringVersion + options Options + processModifications []func(int, *Process) + modifications []Modification + auth *Auth + cafilePath string + sslConfig *TLS + tlsConfig *TLS + dataDir string + port int + memberOptions []MemberOptions + forceReconfigureToVersion *int64 } func NewBuilder() *Builder { @@ -191,6 +192,11 @@ func (b *Builder) SetAuth(auth Auth) *Builder { return b } +func (b *Builder) SetForceReconfigureToVersion(version int64) *Builder { + b.forceReconfigureToVersion = &version + return b +} + func (b *Builder) AddProcessModification(f func(int, *Process)) *Builder { b.processModifications = append(b.processModifications, f) return b @@ -354,6 +360,11 @@ func (b *Builder) Build() (AutomationConfig, error) { b.versions = append(b.versions, dummyConfig) } + var replSetForceConfig *ReplSetForceConfig + if b.forceReconfigureToVersion != nil { + replSetForceConfig = &ReplSetForceConfig{CurrentVersion: *b.forceReconfigureToVersion} + } + currentAc := AutomationConfig{ Version: b.previousAC.Version, Processes: processes, @@ -363,6 +374,7 @@ func (b *Builder) Build() (AutomationConfig, error) { Members: members, ProtocolVersion: "1", NumberArbiters: b.arbiters, + Force: replSetForceConfig, }, }, MonitoringVersions: b.monitoringVersions, diff --git a/pkg/automationconfig/automation_config_test.go b/pkg/automationconfig/automation_config_test.go index e38fd3db6..b6ff3b04a 100644 --- a/pkg/automationconfig/automation_config_test.go +++ b/pkg/automationconfig/automation_config_test.go @@ -28,13 +28,15 @@ func defaultMongoDbVersion(version string) MongoDbVersionConfig { } func TestBuildAutomationConfig(t *testing.T) { - ac, err := NewBuilder(). + builder := NewBuilder(). SetName("my-rs"). SetDomain("my-ns.svc.cluster.local"). SetMongoDBVersion("4.2.0"). SetMembers(3). SetFCV("4.0"). - Build() + SetForceReconfigureToVersion(-1) + + ac, err := builder.Build() assert.NoError(t, err) assert.Len(t, ac.Processes, 3) @@ -56,6 +58,8 @@ func TestBuildAutomationConfig(t *testing.T) { rs := ac.ReplicaSets[0] assert.Equal(t, rs.Id, "my-rs", "The name provided should be configured to be the rs id") assert.Len(t, rs.Members, 3, "there should be the number of replicas provided") + require.NotNil(t, rs.Force) + assert.Equal(t, ReplSetForceConfig{CurrentVersion: -1}, *rs.Force) for i, member := range rs.Members { assert.Equal(t, 1, *member.Votes) @@ -63,6 +67,13 @@ func TestBuildAutomationConfig(t *testing.T) { assert.Equal(t, i, member.Id) assert.Equal(t, ac.Processes[i].Name, member.Host) } + + builder.SetForceReconfigureToVersion(1) + ac, err = builder.Build() + assert.NoError(t, err) + rs = ac.ReplicaSets[0] + require.NotNil(t, rs.Force) + assert.Equal(t, ReplSetForceConfig{CurrentVersion: 1}, *rs.Force) } func TestBuildAutomationConfigArbiters(t *testing.T) {