From 51d73fb1724cc740259630b124065c57fac14a24 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Fri, 9 Jun 2017 21:20:29 +0200 Subject: [PATCH] Replace service annotations when updating services. In case the whole annotation changes (like the external DNS) we don't want to keep the old one hanging around. Unline specs, we don't expect anyone except the operator to change the annotations. Use StrategicMergePatchType in order to replace the annotations map completely. --- pkg/cluster/resources.go | 15 +++++++++++++++ pkg/cluster/util.go | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/cluster/resources.go b/pkg/cluster/resources.go index aa5964302..d9318ed04 100644 --- a/pkg/cluster/resources.go +++ b/pkg/cluster/resources.go @@ -250,6 +250,20 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error } serviceName := util.NameFromMeta(c.Service[role].ObjectMeta) + if len(newService.ObjectMeta.Annotations) > 0 { + + annotationsPatchData := metadataAnnotationsPatch(newService.ObjectMeta.Annotations) + + _, err := c.KubeClient.Services(c.Service[role].Namespace).Patch( + c.Service[role].Name, + api.StrategicMergePatchType, + []byte(annotationsPatchData), "") + + if err != nil { + return fmt.Errorf("could not replace annotations for the service '%s': %v", serviceName, err) + } + } + patchData, err := specPatch(newService.Spec) if err != nil { return fmt.Errorf("could not form patch for the service '%s': %v", serviceName, err) @@ -267,6 +281,7 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error return nil } + func (c *Cluster) deleteService(role PostgresRole) error { c.logger.Debugf("Deleting service %s", role) if c.Service[role] == nil { diff --git a/pkg/cluster/util.go b/pkg/cluster/util.go index b6416cd7b..71986aa57 100644 --- a/pkg/cluster/util.go +++ b/pkg/cluster/util.go @@ -63,6 +63,16 @@ func specPatch(spec interface{}) ([]byte, error) { }{spec}) } +func metadataAnnotationsPatch(annotations map[string]string) (string) { + annotationsList := make([]string, 0, len(annotations)) + + for name, value := range(annotations) { + annotationsList = append(annotationsList, fmt.Sprintf(`"%s":"%s"`, name, value)) + } + annotationsString := strings.Join(annotationsList, ",") + return fmt.Sprintf(`{"metadata":{"annotations": {"$patch":"replace", %s}}}`, annotationsString) +} + func (c *Cluster) logStatefulSetChanges(old, new *v1beta1.StatefulSet, isUpdate bool, reasons []string) { if isUpdate { c.logger.Infof("statefulset '%s' has been changed",