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.
This commit is contained in:
Oleksii Kliukin 2017-06-09 21:20:29 +02:00 committed by Oleksii Kliukin
parent e0dacd0ca9
commit 51d73fb172
2 changed files with 25 additions and 0 deletions

View File

@ -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 {

View File

@ -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",