Skip to content

Commit

Permalink
Use pointer field for priority in member options
Browse files Browse the repository at this point in the history
  • Loading branch information
mircea-cosbuc committed Aug 21, 2024
1 parent f1d5c04 commit a66c0b7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
k8s.io/api v0.27.12
k8s.io/apimachinery v0.27.12
k8s.io/client-go v0.27.12
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
sigs.k8s.io/controller-runtime v0.15.3
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
k8s.io/component-base v0.27.12 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5F
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f/go.mod h1:byini6yhqGC14c3ebc/QwanvYwhuMWF6yz2F8uwW8eg=
k8s.io/utils v0.0.0-20230209194617-a36077c30491 h1:r0BAOLElQnnFhE/ApUsg3iHdVYYPBjNSSOMowRZxxsY=
k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 h1:jgGTlFYnhF1PM1Ax/lAlxUPE+KfCIXHaathvJg1C3ak=
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.15.3 h1:L+t5heIaI3zeejoIyyvLQs5vTVu/67IU2FfisVzFlBc=
sigs.k8s.io/controller-runtime v0.15.3/go.mod h1:kp4jckA4vTx281S/0Yk2LFEEQe67mjg+ev/yknv47Ds=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
6 changes: 3 additions & 3 deletions pkg/automationconfig/automation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ type ReplicaSetMember struct {
// is different in AC from the CR(CR don't support float) - hence all the members are declared
// separately
Votes *int `json:"votes,omitempty"`
Priority float32 `json:"priority,omitempty"`
Priority *float32 `json:"priority,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}

Expand All @@ -294,7 +294,7 @@ func newReplicaSetMember(name string, id int, horizons ReplicaSetHorizons, isArb
// ensure that the number of voting members in the replica set is not more than 7
// as this is the maximum number of voting members.
votes := 0
priority := 0.0
priority := float32(0.0)

if isVotingMember {
votes = 1
Expand All @@ -307,7 +307,7 @@ func newReplicaSetMember(name string, id int, horizons ReplicaSetHorizons, isArb
ArbiterOnly: isArbiter,
Horizons: horizons,
Votes: &votes,
Priority: float32(priority),
Priority: &priority,
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/automationconfig/automation_config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"strings"

"github.com/blang/semver"

"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/versions"

"k8s.io/utils/ptr"
)

type Topology string
Expand Down Expand Up @@ -351,7 +352,7 @@ func (b *Builder) Build() (AutomationConfig, error) {
if len(b.memberOptions) > i {
// override the member options if explicitly specified in the spec
members[i].Votes = b.memberOptions[i].Votes
members[i].Priority = b.memberOptions[i].GetPriority()
members[i].Priority = ptr.To(b.memberOptions[i].GetPriority())
members[i].Tags = b.memberOptions[i].Tags
}
}
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 @@ -477,6 +477,23 @@ func TestAreEqual(t *testing.T) {
assert.NoError(t, err)
assert.False(t, areEqual)
})

t.Run("Automation Configs with nil and zero values are not equal", func(t *testing.T) {
votes := 1
priority := "0.0"
firstBuilder := NewBuilder().SetName("name0").SetMongoDBVersion("mdbVersion0").SetOptions(Options{DownloadBase: "downloadBase0"}).SetDomain("domain0").SetMembers(2).SetAuth(Auth{Disabled: true})
firstBuilder.SetMemberOptions([]MemberOptions{MemberOptions{Votes: &votes, Priority: &priority}})
firstAc, _ := firstBuilder.Build()
firstAc.Version = 2
secondBuilder := NewBuilder().SetName("name0").SetMongoDBVersion("mdbVersion0").SetOptions(Options{DownloadBase: "downloadBase0"}).SetDomain("domain0").SetMembers(2).SetAuth(Auth{Disabled: true})
secondBuilder.SetMemberOptions([]MemberOptions{MemberOptions{Votes: &votes, Priority: nil}})
secondAc, _ := secondBuilder.Build()
secondAc.Version = 2

areEqual, err := AreEqual(firstAc, secondAc)
assert.NoError(t, err)
assert.False(t, areEqual)
})
}

func TestValidateFCV(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/mongodbtests/mongodbtests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -36,11 +35,6 @@ func SkipTestIfLocal(t *testing.T, msg string, f func(t *testing.T)) {
t.Run(msg, f)
}

func strPointer(n float32) *string {
s := strconv.FormatFloat(float64(n), 'f', -1, 64)
return &s
}

// StatefulSetBecomesReady ensures that the underlying stateful set
// reaches the running state.
func StatefulSetBecomesReady(ctx context.Context, mdb *mdbv1.MongoDBCommunity, opts ...wait.Configuration) func(t *testing.T) {
Expand Down Expand Up @@ -438,7 +432,13 @@ func AutomationConfigHasVoteTagPriorityConfigured(ctx context.Context, mdb *mdbv
rsMemebers := currentAc.ReplicaSets

for _, m := range rsMemebers[0].Members {
acMemberOptions = append(acMemberOptions, automationconfig.MemberOptions{Votes: m.Votes, Priority: strPointer(m.Priority), Tags: m.Tags})
var priorityPtr *string
var priority string
if m.Priority != nil {
priority = fmt.Sprintf("%f", *m.Priority)
priorityPtr = &priority
}
acMemberOptions = append(acMemberOptions, automationconfig.MemberOptions{Votes: m.Votes, Priority: priorityPtr, Tags: m.Tags})
}
assert.ElementsMatch(t, memberOptions, acMemberOptions)
}
Expand Down

0 comments on commit a66c0b7

Please sign in to comment.