Skip to content

Commit bbe7c07

Browse files
committed
Fix an issue where creating a Coherence deployment with volume claim templates, or enabling persistence with a PVC, causes the Operator to keep continually re-reconciling and patching the StatefulSet. This does not actually change the StatefulSet or affect the running cluster but the Operator is needlessly causing update events.
1 parent fa58a83 commit bbe7c07

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
github.com/operator-framework/operator-sdk v0.18.0
1616
github.com/pborman/uuid v1.2.0
1717
github.com/pkg/errors v0.9.1
18-
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24
1918
github.com/shuLhan/go-bindata v3.4.0+incompatible // indirect
2019
github.com/spf13/pflag v1.0.5
2120
github.com/tebeka/go2xunit v1.4.10

pkg/controller/statefulset/statefulset_controller.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,13 @@ func (in *ReconcileStatefulSet) patchStatefulSet(deployment *coh.Coherence, curr
288288
name := current.GetName()
289289
original, _ := storage.GetPrevious().GetResource(coh.ResourceTypeStatefulSet, name)
290290

291-
// We NEVER change the replicas or Phase in an update.
291+
// We NEVER change the replicas or Status in an update.
292292
// Replicas is handled by scaling so we always set the desired replicas to match the current replicas
293293
desired.Spec.Replicas = current.Spec.Replicas
294+
// We need to ensure we do not create a patch due to differences in StatefulSet Status
294295
desired.Status = current.Status
296+
// If the StatefulSet has PVs we need to ensure we do not create a patch due to ignorable differences in PV
297+
in.mergeVolumeClaims(desired, current)
295298

296299
// a callback function that the 3-way patch method will call just before it applies a patch
297300
callback := func() {
@@ -315,6 +318,37 @@ func (in *ReconcileStatefulSet) patchStatefulSet(deployment *coh.Coherence, curr
315318
return patched, err
316319
}
317320

321+
// If the desired StatefulSet has PVs update the desired PV to avid needless merges
322+
func (in *ReconcileStatefulSet) mergeVolumeClaims(desired, current *appsv1.StatefulSet) {
323+
if len(desired.Spec.VolumeClaimTemplates) == 0 {
324+
return
325+
}
326+
327+
// Make a map of current PVs by name
328+
m := make(map[string]corev1.PersistentVolumeClaim)
329+
for _, pv := range current.Spec.VolumeClaimTemplates {
330+
m[pv.Name] = pv
331+
}
332+
333+
dfltVolumeMode := corev1.PersistentVolumeFilesystem
334+
335+
for i, pv := range desired.Spec.VolumeClaimTemplates {
336+
currentPV, found := m[pv.Name]
337+
if found {
338+
// Update the desired PV statuses
339+
pv.Status = currentPV.Status
340+
341+
// Set the desired volume mode to the default if it is not set
342+
if pv.Spec.VolumeMode == nil {
343+
pv.Spec.VolumeMode = &dfltVolumeMode
344+
}
345+
346+
// set the updated PV back into the desired array
347+
desired.Spec.VolumeClaimTemplates[i] = pv
348+
}
349+
}
350+
}
351+
318352
// Scale will scale a StatefulSet up or down
319353
func (in *ReconcileStatefulSet) scale(deployment *coh.Coherence, sts *appsv1.StatefulSet, current, desired int32) (reconcile.Result, error) {
320354
// if the StatefulSet is not stable we cannot scale (e.g. it might already be in the middle of a rolling upgrade)

0 commit comments

Comments
 (0)