@@ -5,12 +5,10 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"strings"
8
- "time"
9
8
10
9
"github.com/Masterminds/semver"
11
10
"github.com/zalando/postgres-operator/pkg/spec"
12
11
"github.com/zalando/postgres-operator/pkg/util"
13
- "github.com/zalando/postgres-operator/pkg/util/retryutil"
14
12
v1 "k8s.io/api/core/v1"
15
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
14
"k8s.io/apimachinery/pkg/types"
@@ -108,71 +106,31 @@ func (c *Cluster) removeFailuresAnnotation() error {
108
106
return nil
109
107
}
110
108
111
- func (c * Cluster ) labelCriticalOperation (pods []v1.Pod ) error {
112
- for _ , pod := range pods {
113
- var meta metav1.ObjectMeta
114
- meta .Labels = map [string ]string {"critical-operaton" : "true" }
115
- patchData , err := json .Marshal (struct {
116
- ObjMeta interface {} `json:"metadata"`
117
- }{& meta })
118
- if err != nil {
119
- return fmt .Errorf ("could not form patch for critical operation label: %v" , err )
120
- }
109
+ func (c * Cluster ) criticalOperationLabel (pods []v1.Pod , remove bool ) error {
110
+ var action string
111
+ var metadataReq map [string ]map [string ]map [string ]* string
121
112
122
- err = retryutil .Retry (1 * time .Second , 5 * time .Second ,
123
- func () (bool , error ) {
124
- _ , err2 := c .KubeClient .Pods (pod .Namespace ).Patch (
125
- context .TODO (),
126
- pod .Name ,
127
- types .MergePatchType ,
128
- []byte (patchData ),
129
- metav1.PatchOptions {},
130
- "" )
131
- if err2 != nil {
132
- return false , err2
133
- }
134
- return true , nil
135
- })
136
- if err != nil {
137
- return fmt .Errorf ("could not patch pod critical operation label: %v" , err )
138
- }
113
+ if remove {
114
+ action = "remove"
115
+ metadataReq = map [string ]map [string ]map [string ]* string {"metadata" : {"labels" : {"critical-operaton" : nil }}}
116
+
117
+ } else {
118
+ action = "assign"
119
+ val := "true"
120
+ metadataReq = map [string ]map [string ]map [string ]* string {"metadata" : {"labels" : {"critical-operaton" : & val }}}
139
121
}
140
- c .logger .Info ("pods are patched with critical operation label" )
141
- return nil
142
- }
143
122
144
- func (c * Cluster ) removeCriticalOperationLabel (pods []v1.Pod ) error {
123
+ patchReq , err := json .Marshal (metadataReq )
124
+ if err != nil {
125
+ return fmt .Errorf ("could not marshal ObjectMeta to %s critical operation label: %v" , action , err )
126
+ }
145
127
for _ , pod := range pods {
146
- annotationToRemove := []map [string ]string {
147
- {
148
- "op" : "remove" ,
149
- "path" : fmt .Sprintf ("/metadata/labels/%s" , "critical-operaton" ),
150
- },
151
- }
152
- removePatch , err := json .Marshal (annotationToRemove )
128
+ _ , err = c .KubeClient .Pods (c .Namespace ).Patch (context .TODO (), pod .Name , types .StrategicMergePatchType , patchReq , metav1.PatchOptions {})
153
129
if err != nil {
154
- c .logger .Errorf ("could not form patch for critical operation label: %v" , err )
130
+ c .logger .Errorf ("failed to %s critical operation label for pod %s : %v" , action , pod . Name , err )
155
131
return err
156
132
}
157
- err = retryutil .Retry (1 * time .Second , 5 * time .Second ,
158
- func () (bool , error ) {
159
- _ , err2 := c .KubeClient .Pods (pod .Namespace ).Patch (
160
- context .TODO (),
161
- pod .Name ,
162
- types .JSONPatchType ,
163
- []byte (removePatch ),
164
- metav1.PatchOptions {},
165
- "" )
166
- if err2 != nil {
167
- return false , err2
168
- }
169
- return true , nil
170
- })
171
- if err != nil {
172
- return fmt .Errorf ("failed to remove pod critical operation label: %v" , err )
173
- }
174
133
}
175
- c .logger .Info ("critical operation label is removed from all pods" )
176
134
return nil
177
135
}
178
136
@@ -290,12 +248,12 @@ func (c *Cluster) majorVersionUpgrade() error {
290
248
c .logger .Infof ("healthy cluster ready to upgrade, current: %d desired: %d" , c .currentMajorVersion , desiredVersion )
291
249
if c .currentMajorVersion < desiredVersion {
292
250
defer func () error {
293
- if err = c .removeCriticalOperationLabel (pods ); err != nil {
251
+ if err = c .criticalOperationLabel (pods , true ); err != nil {
294
252
return fmt .Errorf ("failed to remove critical-operation label: %s" , err )
295
253
}
296
254
return nil
297
255
}()
298
- if err = c .labelCriticalOperation (pods ); err != nil {
256
+ if err = c .criticalOperationLabel (pods , false ); err != nil {
299
257
return fmt .Errorf ("failed to assign critical-operation label: %s" , err )
300
258
}
301
259
0 commit comments