Skip to content

Commit d441860

Browse files
authored
Fix update after scaling (#807)
1 parent fb3b23a commit d441860

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

api/v1/coherenceresource_types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ func (in *Coherence) GetHeadlessServiceIPFamily() []corev1.IPFamily {
178178
// return either the actual Replicas value or the default (DefaultReplicas const)
179179
// if the Replicas field is nil.
180180
func (in *Coherence) GetReplicas() int32 {
181-
if in == nil || in.Spec.Replicas == nil {
181+
if in == nil {
182+
return DefaultReplicas
183+
}
184+
if in.Spec.Replicas == nil {
185+
if in.Status.Replicas > 0 {
186+
return in.Status.Replicas
187+
}
182188
return DefaultReplicas
183189
}
184190
return in.Spec.GetReplicas()

controllers/coherence_controller.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (in *CoherenceReconciler) Reconcile(ctx context.Context, request ctrl.Reque
158158
return ctrl.Result{}, nil
159159
}
160160

161-
// The request is an add or update
161+
// This is an add request or update request
162162

163163
if deployment.Spec.AllowUnsafeDelete != nil && *deployment.Spec.AllowUnsafeDelete {
164164
if controllerutil.ContainsFinalizer(deployment, coh.CoherenceFinalizer) {
@@ -174,8 +174,7 @@ func (in *CoherenceReconciler) Reconcile(ctx context.Context, request ctrl.Reque
174174
log.Info("Finalizer not added to Coherence resource as AllowUnsafeDelete has been set to true")
175175
}
176176
} else {
177-
// Add finalizer for this CR if required (it should have been added by the web-hook but may not have been if the
178-
// Coherence resource was added when the Operator was uninstalled)
177+
// Add a finalizer for this CR if required
179178
if finalizerApplied, err := in.finalizerManager.EnsureFinalizerApplied(ctx, deployment); finalizerApplied || err != nil {
180179
var msg string
181180
if err != nil {
@@ -199,10 +198,9 @@ func (in *CoherenceReconciler) Reconcile(ctx context.Context, request ctrl.Reque
199198
}
200199

201200
// Check whether the deployment has a replica count specified
202-
// Ideally we'd do this with a validating/defaulting web-hook but maybe in a later version.
203201
if deployment.Spec.Replicas == nil {
204202
// No replica count, so we patch the deployment to have the default replicas value.
205-
// The reason we do this, is because the kubectl scale command will fail if the replicas
203+
// The reason we do this is the kubectl scale command will fail if the replicas
206204
// field is not set to a non-nil value.
207205
patch := &coh.Coherence{}
208206
deployment.DeepCopyInto(patch)

test/certification/certifiy_deployment_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func TestCertifyScaling(t *testing.T) {
8484
g.Expect(err).NotTo(HaveOccurred())
8585
}
8686

87-
// Test the scenario where we create a Coherence cluster without a replicas field, which will default to three Pods.
87+
// Test the scenario where we create a Coherence cluster without a "replicas" field, which will default to three Pods.
8888
// Then scale up the cluster to four.
89-
// The apply an update using the same Coherence resource with no replicas field.
89+
// Then apply an update using the same Coherence resource with no replicas field.
9090
// After the update is applied, the cluster should still be four and not revert to three.
9191
func TestCertifyScalingWithUpdate(t *testing.T) {
9292
// Ensure that everything is cleaned up after the test!
@@ -118,6 +118,40 @@ func TestCertifyScalingWithUpdate(t *testing.T) {
118118
g.Expect(err).NotTo(HaveOccurred())
119119
}
120120

121+
// Test the scenario where we create a Coherence cluster with a "replicas" field.
122+
// Then scale up the cluster to four.
123+
// Then apply an update using the same Coherence resource with no replicas field.
124+
// After the update is applied, the cluster should still be four and not revert to three.
125+
func TestCertifyScalingClusterWithReplicasThenUpdate(t *testing.T) {
126+
// Ensure that everything is cleaned up after the test!
127+
testContext.CleanupAfterTest(t)
128+
g := NewGomegaWithT(t)
129+
130+
ns := helper.GetTestClusterNamespace()
131+
132+
// the name of the cluster from scale-with-update-one.yaml and scale-with-update-two.yaml
133+
name := "certify-scale-update"
134+
135+
// Start with the two replicas
136+
err := apply(t, ns, "scale-with-update-with-replicas.yaml")
137+
g.Expect(err).NotTo(HaveOccurred())
138+
_, err = helper.WaitForPodsWithLabel(testContext, ns, "one=testOne", 2, time.Second*10, time.Minute*10)
139+
g.Expect(err).NotTo(HaveOccurred())
140+
141+
// Scale Up to four
142+
err = scale(t, ns, name, 4)
143+
g.Expect(err).NotTo(HaveOccurred())
144+
_, err = helper.WaitForStatefulSet(testContext, ns, name, 4, time.Second*10, time.Minute*5)
145+
g.Expect(err).NotTo(HaveOccurred())
146+
147+
// apply the update
148+
err = apply(t, ns, "scale-with-update-two.yaml")
149+
g.Expect(err).NotTo(HaveOccurred())
150+
// There should eventually be four Pods with the additional label
151+
_, err = helper.WaitForPodsWithLabel(testContext, ns, "two=testTwo", 4, time.Second*10, time.Minute*10)
152+
g.Expect(err).NotTo(HaveOccurred())
153+
}
154+
121155
func scale(t *testing.T, namespace, name string, replicas int32) error {
122156
cmd := exec.Command("kubectl", "-n", namespace, "scale", fmt.Sprintf("--replicas=%d", replicas), "coherence/"+name)
123157
cmd.Stdout = os.Stdout
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: coherence.oracle.com/v1
2+
kind: Coherence
3+
metadata:
4+
name: certify-scale-update
5+
spec:
6+
replicas: 2
7+
labels:
8+
one: testOne
9+
readinessProbe:
10+
initialDelaySeconds: 10
11+
periodSeconds: 10

0 commit comments

Comments
 (0)