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
6 changes: 6 additions & 0 deletions pkg/apis/mongodb/v1/mongodb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/types"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -95,6 +97,10 @@ func (m MongoDB) ConfigMapName() string {
return m.Name + "-config"
}

func (m MongoDB) NamespacedName() types.NamespacedName {
return types.NamespacedName{Name: m.Name, Namespace: m.Namespace}
}

// GetFCV returns the feature compatibility version. If no FeatureCompatibilityVersion is specified.
// It uses the major and minor version for whichever version of MongoDB is configured.
func (m MongoDB) GetFCV() string {
Expand Down
68 changes: 68 additions & 0 deletions pkg/controller/mongodb/build_statefulset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package mongodb

import (
"os"
"reflect"
"testing"

"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/probes"

mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
)

func init() {

os.Setenv(preStopHookImageEnv, "pre-stop-hook-image")
}

func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
mdb := newTestReplicaSet()
stsFunc := buildStatefulSetModificationFunction(mdb)
sts := &appsv1.StatefulSet{}

t.Run("1st Call", func(t *testing.T) {
stsFunc(sts)
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
})
t.Run("2nd Call", func(t *testing.T) {
stsFunc(sts)
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
})
t.Run("3rd Call", func(t *testing.T) {
stsFunc(sts)
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
})
}

func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDB, sts *appsv1.StatefulSet) {
assert.Len(t, sts.Spec.Template.Spec.Containers, 2)
assert.Len(t, sts.Spec.Template.Spec.InitContainers, 1)
assert.Equal(t, mdb.ServiceName(), sts.Spec.ServiceName)
assert.Equal(t, mdb.Name, sts.Name)
assert.Equal(t, mdb.Namespace, sts.Namespace)
assert.Equal(t, appsv1.RollingUpdateStatefulSetStrategyType, sts.Spec.UpdateStrategy.Type)
assert.Equal(t, operatorServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName)
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 1)
assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 2)

agentContainer := sts.Spec.Template.Spec.Containers[0]
assert.Equal(t, "agent-image", agentContainer.Image)
probe := agentContainer.ReadinessProbe
assert.True(t, reflect.DeepEqual(probes.New(defaultReadiness()), *probe))
assert.Equal(t, int32(240), probe.FailureThreshold)
assert.Equal(t, int32(5), probe.InitialDelaySeconds)
assert.Len(t, agentContainer.VolumeMounts, 3)

mongodContainer := sts.Spec.Template.Spec.Containers[1]
assert.Equal(t, "mongo:4.2.2", mongodContainer.Image)
assert.NotNil(t, sts.Spec.Template.Spec.Containers[0].ReadinessProbe)
assert.Len(t, mongodContainer.VolumeMounts, 3)

initContainer := sts.Spec.Template.Spec.InitContainers[0]
assert.Equal(t, preStopHookName, initContainer.Name)
assert.Equal(t, "pre-stop-hook-image", initContainer.Image)
assert.Len(t, initContainer.VolumeMounts, 1)
}
Loading