Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.
Merged
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
20 changes: 4 additions & 16 deletions pkg/apis/mongodb/v1/mongodb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Phase string

const (
Running Phase = "Running"
Failed Phase = "Failed"
Pending Phase = "Pending"
)

const (
Expand Down Expand Up @@ -197,6 +199,8 @@ type AuthMode string
type MongoDBStatus struct {
MongoURI string `json:"mongoUri"`
Phase Phase `json:"phase"`
Members int `json:"members"`
Message string `json:"message,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -214,11 +218,6 @@ type MongoDB struct {
Status MongoDBStatus `json:"status,omitempty"`
}

func (m *MongoDB) UpdateSuccess() {
m.Status.MongoURI = m.MongoURI()
m.Status.Phase = Running
}

// MongoURI returns a mongo uri which can be used to connect to this deployment
func (m MongoDB) MongoURI() string {
members := make([]string, m.Spec.Members)
Expand All @@ -229,17 +228,6 @@ func (m MongoDB) MongoURI() string {
return fmt.Sprintf("mongodb://%s", strings.Join(members, ","))
}

// TODO: this is a temporary function which will be used in the e2e tests
// which will be removed in the following PR to clean up our mongo client testing
func (m MongoDB) SCRAMMongoURI(username, password string) string {
members := make([]string, m.Spec.Members)
clusterDomain := "svc.cluster.local" // TODO: make this configurable
for i := 0; i < m.Spec.Members; i++ {
members[i] = fmt.Sprintf("%s-%d.%s.%s.%s:%d", m.Name, i, m.ServiceName(), m.Namespace, clusterDomain, 27017)
}
return fmt.Sprintf("mongodb://%s:%s@%s/?authMechanism=SCRAM-SHA-256", username, password, strings.Join(members, ","))
}

func (m MongoDB) Hosts() []string {
hosts := make([]string, m.Spec.Members)
clusterDomain := "svc.cluster.local" // TODO: make this configurable
Expand Down
173 changes: 173 additions & 0 deletions pkg/controller/mongodb/mongodb_status_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package mongodb

import (
"time"

mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
"go.uber.org/zap"

"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/status"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// severity indicates the severity level
// at which the message should be logged
type severity string

const (
Info severity = "INFO"
Warn severity = "WARN"
Error severity = "ERROR"
None severity = "NONE"
)

// optionBuilder is in charge of constructing a slice of options that
// will be applied on top of the MongoDB resource that has been provided
type optionBuilder struct {
options []status.Option
}

// GetOptions implements the OptionBuilder interface
func (o *optionBuilder) GetOptions() []status.Option {
return o.options
}

// options returns an initialized optionBuilder
func statusOptions() *optionBuilder {
return &optionBuilder{
options: []status.Option{},
}
}

func (o *optionBuilder) withMongoURI(uri string) *optionBuilder {
o.options = append(o.options,
mongoUriOption{
mongoUri: uri,
})
return o
}

type mongoUriOption struct {
mongoUri string
}

func (m mongoUriOption) ApplyOption(mdb *mdbv1.MongoDB) {
mdb.Status.MongoURI = m.mongoUri
}

func (m mongoUriOption) GetResult() (reconcile.Result, error) {
return okResult()
}

func (o *optionBuilder) withMembers(members int) *optionBuilder {
o.options = append(o.options,
membersOption{
members: members,
})
return o
}

type membersOption struct {
members int
}

func (m membersOption) ApplyOption(mdb *mdbv1.MongoDB) {
mdb.Status.Members = m.members
}

func (m membersOption) GetResult() (reconcile.Result, error) {
return okResult()
}
func (o *optionBuilder) withPhase(phase mdbv1.Phase, retryAfter int) *optionBuilder {
o.options = append(o.options,
phaseOption{
phase: phase,
retryAfter: retryAfter,
})
return o
}

type message struct {
messageString string
severityLevel severity
}

type messageOption struct {
message message
}

func (m messageOption) ApplyOption(mdb *mdbv1.MongoDB) {
mdb.Status.Message = m.message.messageString
if m.message.severityLevel == Error {
zap.S().Error(m.message)
}
if m.message.severityLevel == Warn {
zap.S().Warn(m.message)
}
if m.message.severityLevel == Info {
zap.S().Info(m.message)
}
}

func (m messageOption) GetResult() (reconcile.Result, error) {
return okResult()
}

func (o *optionBuilder) withMessage(severityLevel severity, msg string) *optionBuilder {
o.options = append(o.options, messageOption{
message: message{
messageString: msg,
severityLevel: severityLevel,
},
})
return o
}

func (o *optionBuilder) withFailedPhase() *optionBuilder {
return o.withPhase(mdbv1.Failed, 0)
}

func (o *optionBuilder) withPendingPhase(retryAfter int) *optionBuilder {
return o.withPhase(mdbv1.Pending, retryAfter)
}

func (o *optionBuilder) withRunningPhase() *optionBuilder {
return o.withPhase(mdbv1.Running, -1)
}

type phaseOption struct {
phase mdbv1.Phase
retryAfter int
}

func (p phaseOption) ApplyOption(mdb *mdbv1.MongoDB) {
mdb.Status.Phase = p.phase
}

func (p phaseOption) GetResult() (reconcile.Result, error) {
if p.phase == mdbv1.Running {
return okResult()
}
if p.phase == mdbv1.Pending {
return retryResult(p.retryAfter)
}
if p.phase == mdbv1.Failed {
return failedResult()
}
return okResult()
}

// helper functions which return reconciliation results which should be
// returned from the main reconciliation loop

func okResult() (reconcile.Result, error) {
return reconcile.Result{}, nil
}

func retryResult(after int) (reconcile.Result, error) {
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * time.Duration(after)}, nil
}

func failedResult() (reconcile.Result, error) {
return retryResult(0)
}
73 changes: 73 additions & 0 deletions pkg/controller/mongodb/mongodb_status_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package mongodb

import (
"testing"

mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/stretchr/testify/assert"
)

func TestMongoUriOption_ApplyOption(t *testing.T) {

mdb := newReplicaSet(3, "my-rs", "my-ns")

opt := mongoUriOption{
mongoUri: "my-uri",
}

opt.ApplyOption(&mdb)

assert.Equal(t, "my-uri", mdb.Status.MongoURI, "Status should be updated")
}

func TestMembersOption_ApplyOption(t *testing.T) {
mdb := newReplicaSet(3, "my-rs", "my-ns")

opt := membersOption{
members: 5,
}

opt.ApplyOption(&mdb)

assert.Equal(t, 3, mdb.Spec.Members, "Spec should remain unchanged")
assert.Equal(t, 5, mdb.Status.Members, "Status should be updated")
}

func TestOptionBuilder_RunningPhase(t *testing.T) {
mdb := newReplicaSet(3, "my-rs", "my-ns")

statusOptions().withRunningPhase().GetOptions()[0].ApplyOption(&mdb)

assert.Equal(t, mdbv1.Running, mdb.Status.Phase)
}

func TestOptionBuilder_PendingPhase(t *testing.T) {
mdb := newReplicaSet(3, "my-rs", "my-ns")

statusOptions().withPendingPhase(10).GetOptions()[0].ApplyOption(&mdb)

assert.Equal(t, mdbv1.Pending, mdb.Status.Phase)
}

func TestOptionBuilder_FailedPhase(t *testing.T) {
mdb := newReplicaSet(3, "my-rs", "my-ns")

statusOptions().withFailedPhase().GetOptions()[0].ApplyOption(&mdb)

assert.Equal(t, mdbv1.Failed, mdb.Status.Phase)
}

func newReplicaSet(members int, name, namespace string) mdbv1.MongoDB {
return mdbv1.MongoDB{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: mdbv1.MongoDBSpec{
Members: members,
},
}
}
Loading