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

Add overriding Mongod settings via the CRD #1472

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion api/v1/mongodbcommunity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,14 @@ type AuthenticationRestriction struct {

// AutomationConfigOverride contains fields which will be overridden in the operator created config.
type AutomationConfigOverride struct {
Processes []OverrideProcess `json:"processes"`
Processes []OverrideProcess `json:"processes,omitempty"`
ReplicaSet OverrideReplicaSet `json:"replicaSet,omitempty"`
}

type OverrideReplicaSet struct {
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Settings MapWrapper `json:"settings,omitempty"`
}

// Note: We do not use the automationconfig.Process type directly here as unmarshalling cannot happen directly
Expand Down
17 changes: 17 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 @@ -180,8 +180,16 @@ spec:
- name
type: object
type: array
required:
- processes
replicaSet:
properties:
settings:
description: MapWrapper is a wrapper for a map to be used
by other structs. The CRD generator does not support map[string]interface{}
on the top level and hence we need to work around this with
a wrapping struct.
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
type: object
featureCompatibilityVersion:
description: FeatureCompatibilityVersion configures the feature compatibility
Expand Down
6 changes: 6 additions & 0 deletions controllers/replica_set_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
arbitersCount = mdb.Status.CurrentMongoDBArbiters
}

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

return automationconfig.NewBuilder().
IsEnterprise(guessEnterprise(mdb)).
SetTopology(automationconfig.ReplicaSetTopology).
Expand All @@ -553,6 +558,7 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
SetFCV(mdb.Spec.FeatureCompatibilityVersion).
SetOptions(automationconfig.Options{DownloadBase: "/var/lib/mongodb-mms-automation"}).
SetAuth(auth).
SetSettings(acOverrideSettings).
SetMemberOptions(mdb.Spec.MemberConfig).
SetDataDir(mdb.GetMongodConfiguration().GetDBDataDir()).
AddModifications(getMongodConfigModification(mdb)).
Expand Down
11 changes: 6 additions & 5 deletions pkg/automationconfig/automation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@ type ReplSetForceConfig struct {
}

type ReplicaSet struct {
Id string `json:"_id"`
Members []ReplicaSetMember `json:"members"`
ProtocolVersion string `json:"protocolVersion"`
NumberArbiters int `json:"numberArbiters"`
Force *ReplSetForceConfig `json:"force,omitempty"`
Id string `json:"_id"`
Members []ReplicaSetMember `json:"members"`
ProtocolVersion string `json:"protocolVersion"`
NumberArbiters int `json:"numberArbiters"`
Force *ReplSetForceConfig `json:"force,omitempty"`
Settings map[string]interface{} `json:"settings,omitempty"`
}

type ReplicaSetMember struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/automationconfig/automation_config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Builder struct {
port int
memberOptions []MemberOptions
forceReconfigureToVersion *int64
settings map[string]interface{}
}

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

func (b *Builder) SetSettings(settings map[string]interface{}) *Builder {
b.settings = settings
return b
}

func (b *Builder) SetForceReconfigureToVersion(version int64) *Builder {
b.forceReconfigureToVersion = &version
return b
Expand Down Expand Up @@ -375,6 +381,7 @@ func (b *Builder) Build() (AutomationConfig, error) {
ProtocolVersion: "1",
NumberArbiters: b.arbiters,
Force: replSetForceConfig,
Settings: b.settings,
},
},
MonitoringVersions: b.monitoringVersions,
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/mongodbtests/mongodbtests.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ func AutomationConfigHasLogRotationConfig(mdb *mdbv1.MongoDBCommunity, lrc *auto
}
}

func AutomationConfigHasSettings(mdb *mdbv1.MongoDBCommunity, settings map[string]interface{}) func(t *testing.T) {
return func(t *testing.T) {
currentAc := getAutomationConfig(t, mdb)
assert.Equal(t, currentAc.ReplicaSets[0].Settings, settings)
}
}

// AutomationConfigReplicaSetsHaveExpectedArbiters verifies that the automation config has the expected version.
func AutomationConfigReplicaSetsHaveExpectedArbiters(mdb *mdbv1.MongoDBCommunity, expectedArbiters int) func(t *testing.T) {
return func(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/replica_set/replica_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

v1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
e2eutil "github.com/mongodb/mongodb-kubernetes-operator/test/e2e"
"github.com/mongodb/mongodb-kubernetes-operator/test/e2e/mongodbtests"
Expand Down Expand Up @@ -77,6 +78,13 @@ func TestReplicaSet(t *testing.T) {
}
mdb.Spec.MemberConfig = memberOptions

settings := map[string]interface{}{
"electionTimeoutMillis": float64(20),
}
mdb.Spec.AutomationConfigOverride = &v1.AutomationConfigOverride{
ReplicaSet: v1.OverrideReplicaSet{Settings: v1.MapWrapper{Object: settings}},
}

tester, err := FromResource(t, mdb)
if err != nil {
t.Fatal(err)
Expand All @@ -94,5 +102,6 @@ func TestReplicaSet(t *testing.T) {
tester.ConnectivitySucceeds(WithURI(mongodbtests.GetSrvConnectionStringForUser(mdb, scramUser))))
t.Run("Ensure Authentication", tester.EnsureAuthenticationIsConfigured(3))
t.Run("AutomationConfig has the correct version", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 1))
t.Run("AutomationCondig has correct member options", mongodbtests.AutomationConfigHasVoteTagPriorityConfigured(&mdb, memberOptions))
t.Run("AutomationConfig has correct member options", mongodbtests.AutomationConfigHasVoteTagPriorityConfigured(&mdb, memberOptions))
t.Run("AutomationConfig has correct settings", mongodbtests.AutomationConfigHasSettings(&mdb, settings))
}
Loading