Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement option to override id of replicaSet (see #1650) #1656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/v1/mongodbcommunity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ type AutomationConfigOverride struct {
}

type OverrideReplicaSet struct {
// Id can be used together with additionalMongodConfig.replication.replSetName
// to manage clusters where replSetName differs from the MongoDBCommunity resource name
Id *string `json:"id,omitempty"`
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Settings MapWrapper `json:"settings,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ spec:
type: array
replicaSet:
properties:
id:
description: |-
Id can be used together with additionalMongodConfig.replication.replSetName
to manage clusters where replSetName differs from the MongoDBCommunity resource name
type: string
settings:
description: |-
MapWrapper is a wrapper for a map to be used by other structs.
Expand Down
3 changes: 3 additions & 0 deletions controllers/replica_set_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,10 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
}

var acOverrideSettings map[string]interface{}
var acReplicaSetId *string
if mdb.Spec.AutomationConfigOverride != nil {
acOverrideSettings = mdb.Spec.AutomationConfigOverride.ReplicaSet.Settings.Object
acReplicaSetId = mdb.Spec.AutomationConfigOverride.ReplicaSet.Id
}

return automationconfig.NewBuilder().
Expand All @@ -530,6 +532,7 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
SetFCV(mdb.Spec.FeatureCompatibilityVersion).
SetOptions(automationconfig.Options{DownloadBase: "/var/lib/mongodb-mms-automation"}).
SetAuth(auth).
SetReplicaSetId(acReplicaSetId).
SetSettings(acOverrideSettings).
SetMemberOptions(mdb.Spec.MemberConfig).
SetDataDir(mdb.GetMongodConfiguration().GetDBDataDir()).
Expand Down
13 changes: 12 additions & 1 deletion pkg/automationconfig/automation_config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Builder struct {
port int
memberOptions []MemberOptions
forceReconfigureToVersion *int64
replicaSetId *string
settings map[string]interface{}
}

Expand Down Expand Up @@ -194,6 +195,11 @@ func (b *Builder) SetAuth(auth Auth) *Builder {
return b
}

func (b *Builder) SetReplicaSetId(id *string) *Builder {
b.replicaSetId = id
return b
}

func (b *Builder) SetSettings(settings map[string]interface{}) *Builder {
b.settings = settings
return b
Expand Down Expand Up @@ -372,12 +378,17 @@ func (b *Builder) Build() (AutomationConfig, error) {
replSetForceConfig = &ReplSetForceConfig{CurrentVersion: *b.forceReconfigureToVersion}
}

replicaSetId := b.name
if b.replicaSetId != nil {
replicaSetId = *b.replicaSetId
}

currentAc := AutomationConfig{
Version: b.previousAC.Version,
Processes: processes,
ReplicaSets: []ReplicaSet{
{
Id: b.name,
Id: replicaSetId,
Members: members,
ProtocolVersion: "1",
NumberArbiters: b.arbiters,
Expand Down
17 changes: 17 additions & 0 deletions pkg/automationconfig/automation_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,20 @@ func createAutomationConfig(name, mongodbVersion, domain string, opts Options, a
ac.Version = acVersion
return ac
}

func TestReplicaSetId(t *testing.T) {
id := "rs0"
ac, err := NewBuilder().
SetName("my-rs").
SetDomain("my-ns.svc.cluster.local").
SetMongoDBVersion("4.2.0").
SetMembers(3).
AddVersion(defaultMongoDbVersion("4.3.2")).
SetReplicaSetId(&id).
Build()

assert.NoError(t, err)
assert.Len(t, ac.ReplicaSets, 1)
rs := ac.ReplicaSets[0]
assert.Equal(t, rs.Id, id, "The provided id should be used")
}