Skip to content

Commit 83b011f

Browse files
authored
CLOUDP-69096: Implement correct scale down logic (#190)
1 parent bc30b30 commit 83b011f

File tree

21 files changed

+495
-163
lines changed

21 files changed

+495
-163
lines changed

.evergreen.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ task_groups:
167167
- e2e_test_replica_set
168168
- e2e_test_replica_set_readiness_probe
169169
- e2e_test_replica_set_scale
170+
- e2e_test_replica_set_scale_down
170171
- e2e_test_replica_set_change_version
171172
- e2e_test_feature_compatibility_version
172173
- e2e_test_feature_compatibility_version_upgrade
@@ -279,6 +280,13 @@ tasks:
279280
vars:
280281
test: replica_set_scale
281282

283+
- name: e2e_test_replica_set_scale_down
284+
exec_timeout_secs: 3600
285+
commands:
286+
- func: run_e2e_test
287+
vars:
288+
test: replica_set_scale_down
289+
282290
- name: e2e_test_replica_set_change_version
283291
commands:
284292
- func: run_e2e_test

agent/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ RUN mkdir -p agent \
1919
&& chmod -R +r /var/lib/automation/config \
2020
&& rm agent/mongodb-agent.tar.gz \
2121
&& rm -r mongodb-mms-automation-agent-*
22-
2322
RUN mkdir -p /var/lib/mongodb-mms-automation/probes/ \
24-
&& curl --retry 3 https://readinessprobe.s3-us-west-1.amazonaws.com/readiness -o /var/lib/mongodb-mms-automation/probes/readinessprobe \
23+
# && curl --retry 3 https://readinessprobe.s3-us-west-1.amazonaws.com/readiness -o /var/lib/mongodb-mms-automation/probes/readinessprobe \
24+
&& curl --retry 3 https://readiness-probe-scale-test.s3.amazonaws.com/readiness -o /var/lib/mongodb-mms-automation/probes/readinessprobe \
2525
&& chmod +x /var/lib/mongodb-mms-automation/probes/readinessprobe \
2626
&& mkdir -p /var/log/mongodb-mms-automation/ \
2727
&& chmod -R +wr /var/log/mongodb-mms-automation/ \

cmd/testrunner/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ func withTest(test string) func(obj runtime.Object) {
271271
"--kubeconfig",
272272
"/etc/config/kubeconfig",
273273
"--go-test-flags",
274-
"-timeout=20m",
274+
// TODO: allow this to be configurable per test, this is only large due to scale down test
275+
"-timeout=60m",
275276
}
276277
}
277278
}

deploy/crds/mongodb.com_mongodb_crd.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,16 @@ spec:
175175
status:
176176
description: MongoDBStatus defines the observed state of MongoDB
177177
properties:
178-
members:
178+
currentStatefulSetReplicas:
179+
type: integer
180+
currentMongoDBMembers:
179181
type: integer
180182
message:
181183
type: string
182184
mongoUri:
183185
type: string
184186
phase:
185187
type: string
186-
required:
187-
- members
188-
- mongoUri
189-
- phase
190188
type: object
191189
type: object
192190
version: v1

deploy/operator.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ spec:
3131
- name: OPERATOR_NAME
3232
value: "mongodb-kubernetes-operator"
3333
- name: AGENT_IMAGE # The MongoDB Agent the operator will deploy to manage MongoDB deployments
34-
value: quay.io/mongodb/mongodb-agent:10.19.0.6562-1
34+
value: quay.io/mongodb/mongodb-agent-dev:scaledown
35+
# value: quay.io/mongodb/mongodb-agent:10.19.0.6562-1
3536
- name: VERSION_UPGRADE_HOOK_IMAGE
3637
value: quay.io/mongodb/mongodb-kubernetes-operator-version-upgrade-post-start-hook:1.0.2
3738
- name: MONGODB_IMAGE

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ type AuthMode string
201201
type MongoDBStatus struct {
202202
MongoURI string `json:"mongoUri"`
203203
Phase Phase `json:"phase"`
204-
Members int `json:"members"`
205-
Message string `json:"message,omitempty"`
204+
205+
CurrentStatefulSetReplicas int `json:"currentStatefulSetReplicas"`
206+
CurrentMongoDBMembers int `json:"currentMongoDBMembers"`
207+
208+
Message string `json:"message,omitempty"`
206209
}
207210

208211
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -220,16 +223,13 @@ type MongoDB struct {
220223
Status MongoDBStatus `json:"status,omitempty"`
221224
}
222225

223-
func (m MongoDB) DesiredReplicas() int {
224-
return m.Spec.Members
225-
}
226-
227-
func (m MongoDB) CurrentReplicas() int {
228-
return m.Status.Members
229-
}
230-
231-
func (m *MongoDB) ReplicasThisReconciliation() int {
232-
return scale.ReplicasThisReconciliation(m)
226+
func (m MongoDB) AutomationConfigMembersThisReconciliation() int {
227+
// determine the correct number of automation config replica set members
228+
// based on our desired number, and our current number
229+
return scale.ReplicasThisReconciliation(automationConfigReplicasScaler{
230+
desired: m.Spec.Members,
231+
current: m.Status.CurrentMongoDBMembers,
232+
})
233233
}
234234

235235
// MongoURI returns a mongo uri which can be used to connect to this deployment
@@ -298,6 +298,30 @@ func (m MongoDB) GetFCV() string {
298298
return strings.Join(parts[:minorIndex+1], ".")
299299
}
300300

301+
func (m MongoDB) DesiredReplicas() int {
302+
return m.Spec.Members
303+
}
304+
305+
func (m MongoDB) CurrentReplicas() int {
306+
return m.Status.CurrentStatefulSetReplicas
307+
}
308+
309+
func (m *MongoDB) StatefulSetReplicasThisReconciliation() int {
310+
return scale.ReplicasThisReconciliation(m)
311+
}
312+
313+
type automationConfigReplicasScaler struct {
314+
current, desired int
315+
}
316+
317+
func (a automationConfigReplicasScaler) DesiredReplicas() int {
318+
return a.desired
319+
}
320+
321+
func (a automationConfigReplicasScaler) CurrentReplicas() int {
322+
return a.current
323+
}
324+
301325
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
302326

303327
// MongoDBList contains a list of MongoDB

pkg/controller/mongodb/build_statefulset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDB, sts *app
5454
assert.Equal(t, "agent-image", agentContainer.Image)
5555
probe := agentContainer.ReadinessProbe
5656
assert.True(t, reflect.DeepEqual(probes.New(defaultReadiness()), *probe))
57-
assert.Equal(t, int32(240), probe.FailureThreshold)
57+
assert.Equal(t, probes.New(defaultReadiness()).FailureThreshold, probe.FailureThreshold)
5858
assert.Equal(t, int32(5), probe.InitialDelaySeconds)
5959
assert.Len(t, agentContainer.VolumeMounts, 4)
6060

pkg/controller/mongodb/mongodb_status_options.go

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package mongodb
22

33
import (
4-
"time"
5-
64
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
5+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/result"
76
"go.uber.org/zap"
87

98
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/status"
@@ -16,6 +15,7 @@ type severity string
1615

1716
const (
1817
Info severity = "INFO"
18+
Debug severity = "DEBUG"
1919
Warn severity = "WARN"
2020
Error severity = "ERROR"
2121
None severity = "NONE"
@@ -56,28 +56,9 @@ func (m mongoUriOption) ApplyOption(mdb *mdbv1.MongoDB) {
5656
}
5757

5858
func (m mongoUriOption) GetResult() (reconcile.Result, error) {
59-
return okResult()
59+
return result.OK()
6060
}
6161

62-
func (o *optionBuilder) withMembers(members int) *optionBuilder {
63-
o.options = append(o.options,
64-
membersOption{
65-
members: members,
66-
})
67-
return o
68-
}
69-
70-
type membersOption struct {
71-
members int
72-
}
73-
74-
func (m membersOption) ApplyOption(mdb *mdbv1.MongoDB) {
75-
mdb.Status.Members = m.members
76-
}
77-
78-
func (m membersOption) GetResult() (reconcile.Result, error) {
79-
return okResult()
80-
}
8162
func (o *optionBuilder) withPhase(phase mdbv1.Phase, retryAfter int) *optionBuilder {
8263
o.options = append(o.options,
8364
phaseOption{
@@ -99,18 +80,35 @@ type messageOption struct {
9980
func (m messageOption) ApplyOption(mdb *mdbv1.MongoDB) {
10081
mdb.Status.Message = m.message.messageString
10182
if m.message.severityLevel == Error {
102-
zap.S().Error(m.message)
83+
zap.S().Error(m.message.messageString)
10384
}
10485
if m.message.severityLevel == Warn {
105-
zap.S().Warn(m.message)
86+
zap.S().Warn(m.message.messageString)
10687
}
10788
if m.message.severityLevel == Info {
108-
zap.S().Info(m.message)
89+
zap.S().Info(m.message.messageString)
90+
}
91+
if m.message.severityLevel == Debug {
92+
zap.S().Debug(m.message.messageString)
10993
}
11094
}
11195

11296
func (m messageOption) GetResult() (reconcile.Result, error) {
113-
return okResult()
97+
return result.OK()
98+
}
99+
100+
func (o *optionBuilder) withMongoDBMembers(members int) *optionBuilder {
101+
o.options = append(o.options, mongoDBReplicasOption{
102+
mongoDBMembers: members,
103+
})
104+
return o
105+
}
106+
107+
func (o *optionBuilder) withStatefulSetReplicas(members int) *optionBuilder {
108+
o.options = append(o.options, statefulSetReplicasOption{
109+
replicas: members,
110+
})
111+
return o
114112
}
115113

116114
func (o *optionBuilder) withMessage(severityLevel severity, msg string) *optionBuilder {
@@ -146,28 +144,37 @@ func (p phaseOption) ApplyOption(mdb *mdbv1.MongoDB) {
146144

147145
func (p phaseOption) GetResult() (reconcile.Result, error) {
148146
if p.phase == mdbv1.Running {
149-
return okResult()
147+
return result.OK()
150148
}
151149
if p.phase == mdbv1.Pending {
152-
return retryResult(p.retryAfter)
150+
return result.Retry(p.retryAfter)
153151
}
154152
if p.phase == mdbv1.Failed {
155-
return failedResult()
153+
return result.Failed()
156154
}
157-
return okResult()
155+
return result.OK()
156+
}
157+
158+
type mongoDBReplicasOption struct {
159+
mongoDBMembers int
158160
}
159161

160-
// helper functions which return reconciliation results which should be
161-
// returned from the main reconciliation loop
162+
func (a mongoDBReplicasOption) ApplyOption(mdb *mdbv1.MongoDB) {
163+
mdb.Status.CurrentMongoDBMembers = a.mongoDBMembers
164+
}
165+
166+
func (a mongoDBReplicasOption) GetResult() (reconcile.Result, error) {
167+
return result.OK()
168+
}
162169

163-
func okResult() (reconcile.Result, error) {
164-
return reconcile.Result{}, nil
170+
type statefulSetReplicasOption struct {
171+
replicas int
165172
}
166173

167-
func retryResult(after int) (reconcile.Result, error) {
168-
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * time.Duration(after)}, nil
174+
func (s statefulSetReplicasOption) ApplyOption(mdb *mdbv1.MongoDB) {
175+
mdb.Status.CurrentStatefulSetReplicas = s.replicas
169176
}
170177

171-
func failedResult() (reconcile.Result, error) {
172-
return retryResult(0)
178+
func (s statefulSetReplicasOption) GetResult() (reconcile.Result, error) {
179+
return result.OK()
173180
}

pkg/controller/mongodb/mongodb_status_options_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ func TestMongoUriOption_ApplyOption(t *testing.T) {
2222
assert.Equal(t, "my-uri", mdb.Status.MongoURI, "Status should be updated")
2323
}
2424

25-
func TestMembersOption_ApplyOption(t *testing.T) {
26-
mdb := newReplicaSet(3, "my-rs", "my-ns")
27-
28-
opt := membersOption{
29-
members: 5,
30-
}
31-
32-
opt.ApplyOption(&mdb)
33-
34-
assert.Equal(t, 3, mdb.Spec.Members, "Spec should remain unchanged")
35-
assert.Equal(t, 5, mdb.Status.Members, "Status should be updated")
36-
}
37-
3825
func TestOptionBuilder_RunningPhase(t *testing.T) {
3926
mdb := newReplicaSet(3, "my-rs", "my-ns")
4027

0 commit comments

Comments
 (0)