@@ -230,7 +230,7 @@ func (c *Cluster) syncPatroniConfigMap(suffix string) error {
230
230
maps .Copy (annotations , cm .Annotations )
231
231
// Patroni can add extra annotations so incl. current annotations in desired annotations
232
232
desiredAnnotations := c .annotationsSet (cm .Annotations )
233
- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
233
+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
234
234
patchData , err := metaAnnotationsPatch (desiredAnnotations )
235
235
if err != nil {
236
236
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 {
275
275
maps .Copy (annotations , ep .Annotations )
276
276
// Patroni can add extra annotations so incl. current annotations in desired annotations
277
277
desiredAnnotations := c .annotationsSet (ep .Annotations )
278
- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
278
+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
279
279
patchData , err := metaAnnotationsPatch (desiredAnnotations )
280
280
if err != nil {
281
281
return fmt .Errorf ("could not form patch for %s endpoint: %v" , endpointName , err )
@@ -320,7 +320,7 @@ func (c *Cluster) syncPatroniService() error {
320
320
maps .Copy (annotations , svc .Annotations )
321
321
// Patroni can add extra annotations so incl. current annotations in desired annotations
322
322
desiredAnnotations := c .annotationsSet (svc .Annotations )
323
- if changed , _ := c .compareAnnotations (annotations , desiredAnnotations ); changed {
323
+ if changed , _ := c .compareAnnotations (annotations , desiredAnnotations , nil ); changed {
324
324
patchData , err := metaAnnotationsPatch (desiredAnnotations )
325
325
if err != nil {
326
326
return fmt .Errorf ("could not form patch for %s service: %v" , serviceName , err )
@@ -412,7 +412,7 @@ func (c *Cluster) syncEndpoint(role PostgresRole) error {
412
412
return fmt .Errorf ("could not update %s endpoint: %v" , role , err )
413
413
}
414
414
} else {
415
- if changed , _ := c .compareAnnotations (ep .Annotations , desiredEp .Annotations ); changed {
415
+ if changed , _ := c .compareAnnotations (ep .Annotations , desiredEp .Annotations , nil ); changed {
416
416
patchData , err := metaAnnotationsPatch (desiredEp .Annotations )
417
417
if err != nil {
418
418
return fmt .Errorf ("could not form patch for %s endpoint: %v" , role , err )
@@ -559,42 +559,27 @@ func (c *Cluster) syncStatefulSet() error {
559
559
// statefulset is already there, make sure we use its definition in order to compare with the spec.
560
560
c .Statefulset = sset
561
561
562
- cmp := c .compareStatefulSetWith (desiredSts )
562
+ deletedPodAnnotations := []string {}
563
+ cmp := c .compareStatefulSetWith (desiredSts , & deletedPodAnnotations )
563
564
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
575
568
}
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
587
571
}
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 {
590
576
patch , err := json .Marshal (metadataReq )
591
577
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 )
593
579
}
594
580
_ , err = c .KubeClient .Pods (c .Namespace ).Patch (context .Background (), pod .Name , types .StrategicMergePatchType , patch , metav1.PatchOptions {})
595
581
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 )
598
583
}
599
584
}
600
585
}
@@ -1167,7 +1152,7 @@ func (c *Cluster) updateSecret(
1167
1152
c .Secrets [secret .UID ] = secret
1168
1153
}
1169
1154
1170
- if changed , _ := c .compareAnnotations (secret .Annotations , generatedSecret .Annotations ); changed {
1155
+ if changed , _ := c .compareAnnotations (secret .Annotations , generatedSecret .Annotations , nil ); changed {
1171
1156
patchData , err := metaAnnotationsPatch (generatedSecret .Annotations )
1172
1157
if err != nil {
1173
1158
return fmt .Errorf ("could not form patch for secret %q annotations: %v" , secret .Name , err )
@@ -1612,26 +1597,19 @@ func (c *Cluster) syncLogicalBackupJob() error {
1612
1597
}
1613
1598
c .logger .Infof ("logical backup job %s updated" , c .getLogicalBackupJobName ())
1614
1599
}
1615
- if match , reason := c .compareLogicalBackupJob (job , desiredJob ); ! match {
1600
+ deletedPodAnnotations := []string {}
1601
+ if match , reason := c .compareLogicalBackupJob (job , desiredJob , & deletedPodAnnotations ); ! match {
1616
1602
c .logger .Infof ("logical job %s is not in the desired state and needs to be updated" ,
1617
1603
c .getLogicalBackupJobName (),
1618
1604
)
1619
1605
if reason != "" {
1620
1606
c .logger .Infof ("reason: %s" , reason )
1621
1607
}
1622
- if strings . Contains ( reason , "annotations do not match" ) {
1608
+ if len ( deletedPodAnnotations ) == 0 {
1623
1609
templateMetadataReq := map [string ]map [string ]map [string ]map [string ]map [string ]map [string ]map [string ]* string {
1624
1610
"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
1635
1613
}
1636
1614
patch , err := json .Marshal (templateMetadataReq )
1637
1615
if err != nil {
@@ -1649,7 +1627,7 @@ func (c *Cluster) syncLogicalBackupJob() error {
1649
1627
}
1650
1628
c .logger .Info ("the logical backup job is synced" )
1651
1629
}
1652
- if changed , _ := c .compareAnnotations (job .Annotations , desiredJob .Annotations ); changed {
1630
+ if changed , _ := c .compareAnnotations (job .Annotations , desiredJob .Annotations , nil ); changed {
1653
1631
patchData , err := metaAnnotationsPatch (desiredJob .Annotations )
1654
1632
if err != nil {
1655
1633
return fmt .Errorf ("could not form patch for the logical backup job %q: %v" , jobName , err )
0 commit comments