Skip to content

Commit 2c9ceed

Browse files
committed
Refactoring to use a single API call
1 parent 024db78 commit 2c9ceed

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

pkg/cluster/connection_pooler.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,8 @@ func (c *Cluster) syncConnectionPoolerWorker(oldSpec, newSpec *acidv1.Postgresql
10471047
syncReason = append(syncReason, []string{"new connection pooler's pod template annotations do not match the current ones: " + reason}...)
10481048

10491049
if strings.Contains(reason, "Removed") {
1050+
annotationToRemove := `{"metadata":{"annotations":{`
1051+
annotationToRemoveTemplate := `{"spec":{"template":{"metadata":{"annotations":{`
10501052
for anno := range deployment.Spec.Template.Annotations {
10511053
if _, ok := newPodAnnotations[anno]; !ok {
10521054
// template annotation was removed
@@ -1055,23 +1057,24 @@ func (c *Cluster) syncConnectionPoolerWorker(oldSpec, newSpec *acidv1.Postgresql
10551057
continue
10561058
}
10571059
}
1058-
annotationToRemove := []byte(fmt.Sprintf(`{"metadata":{"annotations":{"%s":null}}}`, anno))
1059-
annotationToRemoveTemplate := []byte(fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"%s":null}}}}}`, anno))
1060-
deployment, err = c.KubeClient.Deployments(c.Namespace).Patch(context.TODO(),
1061-
deployment.Name, types.StrategicMergePatchType, annotationToRemoveTemplate, metav1.PatchOptions{}, "")
1062-
if err != nil {
1063-
c.logger.Errorf("failed to remove annotation %s from %s connection pooler's pod template: %v",
1064-
anno, role, err)
1065-
return nil, err
1066-
}
1067-
for _, pod := range pods {
1068-
_, err = c.KubeClient.Pods(c.Namespace).Patch(context.TODO(), pod.Name,
1069-
types.StrategicMergePatchType, annotationToRemove, metav1.PatchOptions{})
1070-
if err != nil {
1071-
c.logger.Errorf("failed to remove annotation %s from pod %s: %v", anno, pod.Name, err)
1072-
return nil, err
1073-
}
1074-
}
1060+
annotationToRemove += fmt.Sprintf(`"%s":null,`, anno)
1061+
annotationToRemoveTemplate += fmt.Sprintf(`"%s":null,`, anno)
1062+
}
1063+
}
1064+
annotationToRemove = strings.TrimSuffix(annotationToRemove, ",") + `}}}`
1065+
annotationToRemoveTemplate = strings.TrimSuffix(annotationToRemoveTemplate, ",") + `}}}}}`
1066+
deployment, err = c.KubeClient.Deployments(c.Namespace).Patch(context.TODO(),
1067+
deployment.Name, types.StrategicMergePatchType, []byte(annotationToRemoveTemplate), metav1.PatchOptions{}, "")
1068+
if err != nil {
1069+
c.logger.Errorf("failed to remove annotations from %s connection pooler's pod template: %v", role, err)
1070+
return nil, err
1071+
}
1072+
for _, pod := range pods {
1073+
_, err = c.KubeClient.Pods(c.Namespace).Patch(context.TODO(), pod.Name,
1074+
types.StrategicMergePatchType, []byte(annotationToRemove), metav1.PatchOptions{})
1075+
if err != nil {
1076+
c.logger.Errorf("failed to remove annotations from pod %s: %v", pod.Name, err)
1077+
return nil, err
10751078
}
10761079
}
10771080
}

pkg/cluster/sync.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ func (c *Cluster) syncStatefulSet() error {
573573
}
574574
}
575575
}
576+
annotationToRemove := ""
576577
for anno := range c.Statefulset.Spec.Template.Annotations {
577578
if _, ok := desiredSts.Spec.Template.Annotations[anno]; !ok {
578579
// template annotation was removed
@@ -581,14 +582,21 @@ func (c *Cluster) syncStatefulSet() error {
581582
continue
582583
}
583584
}
584-
annotationToRemove := []byte(fmt.Sprintf(`{"metadata":{"annotations":{"%s":null}}}`, anno))
585-
for _, pod := range pods {
586-
_, err = c.KubeClient.Pods(c.Namespace).Patch(context.Background(), pod.Name,
587-
types.StrategicMergePatchType, annotationToRemove, metav1.PatchOptions{})
588-
if err != nil {
589-
c.logger.Errorf("failed to remove annotation %s from pod %s: %v", anno, pod.Name, err)
590-
return err
591-
}
585+
if annotationToRemove != "" {
586+
annotationToRemove = `{"metadata":{"annotations":{`
587+
}
588+
annotationToRemove += fmt.Sprintf(`"%s":null,`, anno)
589+
// annotationToRemove := []byte(fmt.Sprintf(`{"metadata":{"annotations":{"%s":null}}}`, anno))
590+
}
591+
}
592+
if annotationToRemove != "" {
593+
annotationToRemove = strings.TrimSuffix(annotationToRemove, ",") + `}}}`
594+
for _, pod := range pods {
595+
_, err = c.KubeClient.Pods(c.Namespace).Patch(context.Background(), pod.Name,
596+
types.StrategicMergePatchType, []byte(annotationToRemove), metav1.PatchOptions{})
597+
if err != nil {
598+
c.logger.Errorf("failed to remove annotations from pod %s: %v", pod.Name, err)
599+
return err
592600
}
593601
}
594602
}
@@ -1614,6 +1622,7 @@ func (c *Cluster) syncLogicalBackupJob() error {
16141622
c.logger.Infof("reason: %s", reason)
16151623
}
16161624
if strings.Contains(reason, "annotations do not match") {
1625+
annotationToRemoveTemplate := `{"spec":{"jobTemplate":{"spec":{"template":{"metadata":{"annotations":{`
16171626
for anno := range job.Spec.JobTemplate.Spec.Template.Annotations {
16181627
if _, ok := desiredJob.Spec.JobTemplate.Spec.Template.Annotations[anno]; !ok {
16191628
// template annotation was removed
@@ -1622,16 +1631,16 @@ func (c *Cluster) syncLogicalBackupJob() error {
16221631
continue
16231632
}
16241633
}
1625-
annotationToRemoveTemplate := []byte(fmt.Sprintf(
1626-
`{"spec":{"jobTemplate":{"spec":{"template":{"metadata":{"annotations":{"%s":null}}}}}}}`, anno))
1627-
job, err = c.KubeClient.CronJobs(c.Namespace).Patch(context.TODO(),
1628-
jobName, types.StrategicMergePatchType, annotationToRemoveTemplate, metav1.PatchOptions{}, "")
1629-
if err != nil {
1630-
c.logger.Errorf("failed to remove annotation %s from the logical backup job %q pod template: %v", anno, jobName, err)
1631-
return err
1632-
}
1634+
annotationToRemoveTemplate += fmt.Sprintf(`"%s":null,`, anno)
16331635
}
16341636
}
1637+
annotationToRemoveTemplate = strings.TrimSuffix(annotationToRemoveTemplate, ",") + `}}}}}}}`
1638+
job, err = c.KubeClient.CronJobs(c.Namespace).Patch(context.TODO(),
1639+
jobName, types.StrategicMergePatchType, []byte(annotationToRemoveTemplate), metav1.PatchOptions{}, "")
1640+
if err != nil {
1641+
c.logger.Errorf("failed to remove annotations from the logical backup job %q pod template: %v", jobName, err)
1642+
return err
1643+
}
16351644
}
16361645
if err = c.patchLogicalBackupJob(desiredJob); err != nil {
16371646
return fmt.Errorf("could not update logical backup job to match desired state: %v", err)

pkg/cluster/sync_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestPodAnnotationsSync(t *testing.T) {
146146
clusterName := "acid-test-cluster-2"
147147
namespace := "default"
148148
podAnnotation := "no-scale-down"
149-
podAnnotations := map[string]string{"no-scale-down": "true"}
149+
podAnnotations := map[string]string{podAnnotation: "true"}
150150

151151
ctrl := gomock.NewController(t)
152152
defer ctrl.Finish()
@@ -271,7 +271,7 @@ func TestPodAnnotationsSync(t *testing.T) {
271271
stsList, err = cluster.KubeClient.StatefulSets(namespace).List(context.TODO(), clusterOptions)
272272
assert.NoError(t, err)
273273
for _, sts := range stsList.Items {
274-
assert.NotContains(t, sts.Spec.Template.Annotations, "no-scale-down")
274+
assert.NotContains(t, sts.Spec.Template.Annotations, podAnnotation)
275275
}
276276

277277
for _, role := range []PostgresRole{Master, Replica} {
@@ -286,7 +286,7 @@ func TestPodAnnotationsSync(t *testing.T) {
286286
podList, err = cluster.KubeClient.Pods(namespace).List(context.TODO(), clusterOptions)
287287
assert.NoError(t, err)
288288
for _, pod := range podList.Items {
289-
assert.NotContains(t, pod.Annotations, "no-scale-down",
289+
assert.NotContains(t, pod.Annotations, podAnnotation,
290290
fmt.Sprintf("pod %s should not contain annotation %s, found %#v", pod.Name, podAnnotation, pod.Annotations))
291291
}
292292

0 commit comments

Comments
 (0)