Skip to content

Commit ea21298

Browse files
authored
CLOUDP-62444: Adopt functional approach for StatefulSet construction (#63)
1 parent 75bd503 commit ea21298

File tree

11 files changed

+571
-273
lines changed

11 files changed

+571
-273
lines changed

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7+
"k8s.io/apimachinery/pkg/types"
8+
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
)
911

@@ -95,6 +97,10 @@ func (m MongoDB) ConfigMapName() string {
9597
return m.Name + "-config"
9698
}
9799

100+
func (m MongoDB) NamespacedName() types.NamespacedName {
101+
return types.NamespacedName{Name: m.Name, Namespace: m.Namespace}
102+
}
103+
98104
// GetFCV returns the feature compatibility version. If no FeatureCompatibilityVersion is specified.
99105
// It uses the major and minor version for whichever version of MongoDB is configured.
100106
func (m MongoDB) GetFCV() string {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package mongodb
2+
3+
import (
4+
"os"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/probes"
9+
10+
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
11+
12+
"github.com/stretchr/testify/assert"
13+
appsv1 "k8s.io/api/apps/v1"
14+
)
15+
16+
func init() {
17+
18+
os.Setenv(preStopHookImageEnv, "pre-stop-hook-image")
19+
}
20+
21+
func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
22+
mdb := newTestReplicaSet()
23+
stsFunc := buildStatefulSetModificationFunction(mdb)
24+
sts := &appsv1.StatefulSet{}
25+
26+
t.Run("1st Call", func(t *testing.T) {
27+
stsFunc(sts)
28+
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
29+
})
30+
t.Run("2nd Call", func(t *testing.T) {
31+
stsFunc(sts)
32+
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
33+
})
34+
t.Run("3rd Call", func(t *testing.T) {
35+
stsFunc(sts)
36+
assertStatefulSetIsBuiltCorrectly(t, mdb, sts)
37+
})
38+
}
39+
40+
func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDB, sts *appsv1.StatefulSet) {
41+
assert.Len(t, sts.Spec.Template.Spec.Containers, 2)
42+
assert.Len(t, sts.Spec.Template.Spec.InitContainers, 1)
43+
assert.Equal(t, mdb.ServiceName(), sts.Spec.ServiceName)
44+
assert.Equal(t, mdb.Name, sts.Name)
45+
assert.Equal(t, mdb.Namespace, sts.Namespace)
46+
assert.Equal(t, appsv1.RollingUpdateStatefulSetStrategyType, sts.Spec.UpdateStrategy.Type)
47+
assert.Equal(t, operatorServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName)
48+
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 1)
49+
assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 2)
50+
51+
agentContainer := sts.Spec.Template.Spec.Containers[0]
52+
assert.Equal(t, "agent-image", agentContainer.Image)
53+
probe := agentContainer.ReadinessProbe
54+
assert.True(t, reflect.DeepEqual(probes.New(defaultReadiness()), *probe))
55+
assert.Equal(t, int32(240), probe.FailureThreshold)
56+
assert.Equal(t, int32(5), probe.InitialDelaySeconds)
57+
assert.Len(t, agentContainer.VolumeMounts, 3)
58+
59+
mongodContainer := sts.Spec.Template.Spec.Containers[1]
60+
assert.Equal(t, "mongo:4.2.2", mongodContainer.Image)
61+
assert.NotNil(t, sts.Spec.Template.Spec.Containers[0].ReadinessProbe)
62+
assert.Len(t, mongodContainer.VolumeMounts, 3)
63+
64+
initContainer := sts.Spec.Template.Spec.InitContainers[0]
65+
assert.Equal(t, preStopHookName, initContainer.Name)
66+
assert.Equal(t, "pre-stop-hook-image", initContainer.Image)
67+
assert.Len(t, initContainer.VolumeMounts, 1)
68+
}

0 commit comments

Comments
 (0)