Merge branch 'rolling_updates_with_statefulset_annotations' of github.com:zalando-incubator/postgres-operator into rolling_updates_with_statefulset_annotations

This commit is contained in:
Oleksii Kliukin 2018-05-07 08:10:35 +02:00
commit f41a42f922
3 changed files with 21 additions and 9 deletions

View File

@ -189,10 +189,13 @@ func (c *Cluster) updateStatefulSet(newStatefulSet *v1beta1.StatefulSet, include
return fmt.Errorf("could not patch statefulset spec %q: %v", statefulSetName, err) return fmt.Errorf("could not patch statefulset spec %q: %v", statefulSetName, err)
} }
if includeAnnotations && newStatefulSet.Annotations != nil { 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( statefulSet, err = c.KubeClient.StatefulSets(c.Statefulset.Namespace).Patch(
c.Statefulset.Name, c.Statefulset.Name,
types.StrategicMergePatchType, types.MergePatchType,
[]byte(patchData), "") []byte(patchData), "")
if err != nil { if err != nil {
return fmt.Errorf("could not patch statefulset annotations %q: %v", patchData, err) return fmt.Errorf("could not patch statefulset annotations %q: %v", patchData, err)

View File

@ -221,7 +221,7 @@ func (c *Cluster) syncPodDisruptionBudget(isUpdate bool) error {
func (c *Cluster) syncStatefulSet() error { func (c *Cluster) syncStatefulSet() error {
var ( var (
cachedRollingUpdateFlag, podsRollingUpdateRequired bool cachedRollingUpdateFlag, cachedStatefulsetExists, podsRollingUpdateRequired bool
) )
sset, err := c.KubeClient.StatefulSets(c.Namespace).Get(c.statefulSetName(), metav1.GetOptions{}) sset, err := c.KubeClient.StatefulSets(c.Namespace).Get(c.statefulSetName(), metav1.GetOptions{})
if err != nil { 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 // 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 // the actual object in Kubernetes for some reason we want to avoid doing an unnecessary update by relying
// on the 'cached' in-memory flag. // on the 'cached' in-memory flag.
cachedStatefulsetExists = true
cachedRollingUpdateFlag = getRollingUpdateFlag(c.Statefulset, true) cachedRollingUpdateFlag = getRollingUpdateFlag(c.Statefulset, true)
c.logger.Debugf("cached statefulset value exists, rollingUpdate flag is %t", cachedRollingUpdateFlag) 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. // statefulset is already there, make sure we use its definition in order to compare with the spec.
c.Statefulset = sset c.Statefulset = sset
if podsRollingUpdateRequired = getRollingUpdateFlag(c.Statefulset, false); podsRollingUpdateRequired { if podsRollingUpdateRequired = getRollingUpdateFlag(c.Statefulset, false); podsRollingUpdateRequired {
if cachedRollingUpdateFlag { if cachedStatefulsetExists {
c.logger.Infof("found a statefulset with an unfinished pods rolling update") if cachedRollingUpdateFlag {
} else { c.logger.Infof("found a statefulset with an unfinished pods rolling update")
c.logger.Infof("clearing the rolling update flag based on the cached information") } else {
podsRollingUpdateRequired = false 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") c.logger.Infof("pods have been recreated")
setRollingUpdateFlag(c.Statefulset, false) setRollingUpdateFlag(c.Statefulset, false)
if err := c.updateStatefulSet(c.Statefulset, true); err != nil { 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 return nil

View File

@ -139,6 +139,12 @@ func specPatch(spec interface{}) ([]byte, error) {
}{spec}) }{spec})
} }
func metaPatch(meta interface{}) ([]byte, error) {
return json.Marshal(struct {
ObjMeta interface{} `json:"metadata"`
}{meta})
}
func metadataAnnotationsPatch(annotations map[string]string) string { func metadataAnnotationsPatch(annotations map[string]string) string {
annotationsList := make([]string, 0, len(annotations)) annotationsList := make([]string, 0, len(annotations))