@@ -230,7 +230,7 @@ func (c *Cluster) syncPatroniConfigMap(suffix string) error {
230230 maps .Copy (annotations , cm .Annotations )
231231 // Patroni can add extra annotations so incl. current annotations in desired annotations
232232 desiredAnnotations := c .annotationsSet (cm .Annotations )
233- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
233+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
234234 patchData , err := metaAnnotationsPatch (desiredAnnotations )
235235 if err != nil {
236236 return fmt .Errorf ("could not form patch for %s config map: %v" , configMapName , err )
@@ -275,7 +275,7 @@ func (c *Cluster) syncPatroniEndpoint(suffix string) error {
275275 maps .Copy (annotations , ep .Annotations )
276276 // Patroni can add extra annotations so incl. current annotations in desired annotations
277277 desiredAnnotations := c .annotationsSet (ep .Annotations )
278- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
278+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
279279 patchData , err := metaAnnotationsPatch (desiredAnnotations )
280280 if err != nil {
281281 return fmt .Errorf ("could not form patch for %s endpoint: %v" , endpointName , err )
@@ -320,7 +320,7 @@ func (c *Cluster) syncPatroniService() error {
320320 maps .Copy (annotations , svc .Annotations )
321321 // Patroni can add extra annotations so incl. current annotations in desired annotations
322322 desiredAnnotations := c .annotationsSet (svc .Annotations )
323- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
323+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
324324 patchData , err := metaAnnotationsPatch (desiredAnnotations )
325325 if err != nil {
326326 return fmt .Errorf ("could not form patch for %s service: %v" , serviceName , err )
@@ -412,7 +412,7 @@ func (c *Cluster) syncEndpoint(role PostgresRole) error {
412412 return fmt .Errorf ("could not update %s endpoint: %v" , role , err )
413413 }
414414 } else {
415- if changed , _ := c .compareAnnotations (ep .Annotations , desiredEp .Annotations ); changed {
415+ if changed , _ := c .compareAnnotations (ep .Annotations , desiredEp .Annotations , nil ); changed {
416416 patchData , err := metaAnnotationsPatch (desiredEp .Annotations )
417417 if err != nil {
418418 return fmt .Errorf ("could not form patch for %s endpoint: %v" , role , err )
@@ -559,42 +559,27 @@ func (c *Cluster) syncStatefulSet() error {
559559 // statefulset is already there, make sure we use its definition in order to compare with the spec.
560560 c .Statefulset = sset
561561
562- cmp := c .compareStatefulSetWith (desiredSts )
562+ deletedPodAnnotations := []string {}
563+ cmp := c .compareStatefulSetWith (desiredSts , & deletedPodAnnotations )
563564 if ! cmp .rollingUpdate {
564- for _ , pod := range pods {
565- if changed , _ := c .compareAnnotations (pod .Annotations , desiredSts .Spec .Template .Annotations ); changed {
566- patchData , err := metaAnnotationsPatch (desiredSts .Spec .Template .Annotations )
567- if err != nil {
568- return fmt .Errorf ("could not form patch for pod %q annotations: %v" , pod .Name , err )
569- }
570- _ , err = c .KubeClient .Pods (pod .Namespace ).Patch (context .TODO (), pod .Name , types .MergePatchType , []byte (patchData ), metav1.PatchOptions {})
571- if err != nil {
572- return fmt .Errorf ("could not patch annotations for pod %q: %v" , pod .Name , err )
573- }
574- }
565+ updatedPodAnnotations := map [string ]* string {}
566+ for _ , anno := range deletedPodAnnotations {
567+ updatedPodAnnotations [anno ] = nil
575568 }
576- metadataReq := map [string ]map [string ]map [string ]* string {"metadata" : {"annotations" : {}}}
577- for anno := range c .Statefulset .Spec .Template .Annotations {
578- if _ , ok := desiredSts .Spec .Template .Annotations [anno ]; ! ok {
579- // template annotation was removed
580- for _ , ignore := range c .OpConfig .IgnoredAnnotations {
581- if anno == ignore {
582- continue
583- }
584- }
585- metadataReq ["metadata" ]["annotations" ][anno ] = nil
586- }
569+ for anno , val := range desiredSts .Spec .Template .Annotations {
570+ updatedPodAnnotations [anno ] = & val
587571 }
588- if len (metadataReq ["metadata" ]["annotations" ]) != 0 {
589- for _ , pod := range pods {
572+ metadataReq := map [string ]map [string ]map [string ]* string {"metadata" : {"annotations" : updatedPodAnnotations }}
573+
574+ for _ , pod := range pods {
575+ if changed , _ := c .compareAnnotations (pod .Annotations , desiredSts .Spec .Template .Annotations , nil ); changed {
590576 patch , err := json .Marshal (metadataReq )
591577 if err != nil {
592- return fmt .Errorf ("could not marshal ObjectMeta for pod %s : %v" , pod .Name , err )
578+ return fmt .Errorf ("could not form patch for pod %q annotations : %v" , pod .Name , err )
593579 }
594580 _ , err = c .KubeClient .Pods (c .Namespace ).Patch (context .Background (), pod .Name , types .StrategicMergePatchType , patch , metav1.PatchOptions {})
595581 if err != nil {
596- c .logger .Errorf ("failed to remove annotations from pod %s: %v" , pod .Name , err )
597- return err
582+ return fmt .Errorf ("could not patch annotations for pod %q: %v" , pod .Name , err )
598583 }
599584 }
600585 }
@@ -1167,7 +1152,7 @@ func (c *Cluster) updateSecret(
11671152 c .Secrets [secret .UID ] = secret
11681153 }
11691154
1170- if changed , _ := c .compareAnnotations (secret .Annotations , generatedSecret .Annotations ); changed {
1155+ if changed , _ := c .compareAnnotations (secret .Annotations , generatedSecret .Annotations , nil ); changed {
11711156 patchData , err := metaAnnotationsPatch (generatedSecret .Annotations )
11721157 if err != nil {
11731158 return fmt .Errorf ("could not form patch for secret %q annotations: %v" , secret .Name , err )
@@ -1612,26 +1597,19 @@ func (c *Cluster) syncLogicalBackupJob() error {
16121597 }
16131598 c .logger .Infof ("logical backup job %s updated" , c .getLogicalBackupJobName ())
16141599 }
1615- if match , reason := c .compareLogicalBackupJob (job , desiredJob ); ! match {
1600+ deletedPodAnnotations := []string {}
1601+ if match , reason := c .compareLogicalBackupJob (job , desiredJob , & deletedPodAnnotations ); ! match {
16161602 c .logger .Infof ("logical job %s is not in the desired state and needs to be updated" ,
16171603 c .getLogicalBackupJobName (),
16181604 )
16191605 if reason != "" {
16201606 c .logger .Infof ("reason: %s" , reason )
16211607 }
1622- if strings . Contains ( reason , "annotations do not match" ) {
1608+ if len ( deletedPodAnnotations ) == 0 {
16231609 templateMetadataReq := map [string ]map [string ]map [string ]map [string ]map [string ]map [string ]map [string ]* string {
16241610 "spec" : {"jobTemplate" : {"spec" : {"template" : {"metadata" : {"annotations" : {}}}}}}}
1625- for anno := range job .Spec .JobTemplate .Spec .Template .Annotations {
1626- if _ , ok := desiredJob .Spec .JobTemplate .Spec .Template .Annotations [anno ]; ! ok {
1627- // template annotation was removed
1628- for _ , ignore := range c .OpConfig .IgnoredAnnotations {
1629- if anno == ignore {
1630- continue
1631- }
1632- }
1633- templateMetadataReq ["spec" ]["jobTemplate" ]["spec" ]["template" ]["metadata" ]["annotations" ][anno ] = nil
1634- }
1611+ for _ , anno := range deletedPodAnnotations {
1612+ templateMetadataReq ["spec" ]["jobTemplate" ]["spec" ]["template" ]["metadata" ]["annotations" ][anno ] = nil
16351613 }
16361614 patch , err := json .Marshal (templateMetadataReq )
16371615 if err != nil {
@@ -1649,7 +1627,7 @@ func (c *Cluster) syncLogicalBackupJob() error {
16491627 }
16501628 c .logger .Info ("the logical backup job is synced" )
16511629 }
1652- if changed , _ := c .compareAnnotations (job .Annotations , desiredJob .Annotations ); changed {
1630+ if changed , _ := c .compareAnnotations (job .Annotations , desiredJob .Annotations , nil ); changed {
16531631 patchData , err := metaAnnotationsPatch (desiredJob .Annotations )
16541632 if err != nil {
16551633 return fmt .Errorf ("could not form patch for the logical backup job %q: %v" , jobName , err )
0 commit comments