diff --git a/pkg/cluster/resources.go b/pkg/cluster/resources.go index a4135a534..51b7b57da 100644 --- a/pkg/cluster/resources.go +++ b/pkg/cluster/resources.go @@ -189,10 +189,13 @@ func (c *Cluster) updateStatefulSet(newStatefulSet *v1beta1.StatefulSet, include return fmt.Errorf("could not patch statefulset spec %q: %v", statefulSetName, err) } if includeAnnotations && newStatefulSet.Annotations != nil { - patchData := metadataAnnotationsPatch(newStatefulSet.Annotations) + c.logger.Debugf("updating statefulset annotations") + if patchData, err = metaPatch(newStatefulSet.ObjectMeta); err != nil { + return fmt.Errorf("could not form patch for the stetefulset metadata: %v", err) + } statefulSet, err = c.KubeClient.StatefulSets(c.Statefulset.Namespace).Patch( c.Statefulset.Name, - types.StrategicMergePatchType, + types.MergePatchType, []byte(patchData), "") if err != nil { return fmt.Errorf("could not patch statefulset annotations %q: %v", patchData, err) diff --git a/pkg/cluster/sync.go b/pkg/cluster/sync.go index 1c1fcba61..ef987b03c 100644 --- a/pkg/cluster/sync.go +++ b/pkg/cluster/sync.go @@ -221,7 +221,7 @@ func (c *Cluster) syncPodDisruptionBudget(isUpdate bool) error { func (c *Cluster) syncStatefulSet() error { var ( - cachedRollingUpdateFlag, podsRollingUpdateRequired bool + cachedRollingUpdateFlag, cachedStatefulsetExists, podsRollingUpdateRequired bool ) sset, err := c.KubeClient.StatefulSets(c.Namespace).Get(c.statefulSetName(), metav1.GetOptions{}) if err != nil { @@ -253,17 +253,20 @@ func (c *Cluster) syncStatefulSet() error { // if we reset the rolling update flag in the statefulset structure in memory but didn't manage to update // the actual object in Kubernetes for some reason we want to avoid doing an unnecessary update by relying // on the 'cached' in-memory flag. + cachedStatefulsetExists = true cachedRollingUpdateFlag = getRollingUpdateFlag(c.Statefulset, true) c.logger.Debugf("cached statefulset value exists, rollingUpdate flag is %t", cachedRollingUpdateFlag) } // statefulset is already there, make sure we use its definition in order to compare with the spec. c.Statefulset = sset if podsRollingUpdateRequired = getRollingUpdateFlag(c.Statefulset, false); podsRollingUpdateRequired { - if cachedRollingUpdateFlag { - c.logger.Infof("found a statefulset with an unfinished pods rolling update") - } else { - c.logger.Infof("clearing the rolling update flag based on the cached information") - podsRollingUpdateRequired = false + if cachedStatefulsetExists { + if cachedRollingUpdateFlag { + c.logger.Infof("found a statefulset with an unfinished pods rolling update") + } else { + c.logger.Infof("clearing the rolling update flag based on the cached information") + podsRollingUpdateRequired = false + } } } @@ -302,7 +305,7 @@ func (c *Cluster) syncStatefulSet() error { c.logger.Infof("pods have been recreated") setRollingUpdateFlag(c.Statefulset, false) if err := c.updateStatefulSet(c.Statefulset, true); err != nil { - c.logger.Warningf("could not clear rolling update for the statefulset") + c.logger.Warningf("could not clear rolling update for the statefulset: %v", err) } } return nil diff --git a/pkg/cluster/util.go b/pkg/cluster/util.go index b72835311..cb06467d2 100644 --- a/pkg/cluster/util.go +++ b/pkg/cluster/util.go @@ -139,6 +139,12 @@ func specPatch(spec interface{}) ([]byte, error) { }{spec}) } +func metaPatch(meta interface{}) ([]byte, error) { + return json.Marshal(struct { + ObjMeta interface{} `json:"metadata"` + }{meta}) +} + func metadataAnnotationsPatch(annotations map[string]string) string { annotationsList := make([]string, 0, len(annotations))