@@ -5,12 +5,10 @@ import (
55 "encoding/json"
66 "fmt"
77 "strings"
8- "time"
98
109 "github.com/Masterminds/semver"
1110 "github.com/zalando/postgres-operator/pkg/spec"
1211 "github.com/zalando/postgres-operator/pkg/util"
13- "github.com/zalando/postgres-operator/pkg/util/retryutil"
1412 v1 "k8s.io/api/core/v1"
1513 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1614 "k8s.io/apimachinery/pkg/types"
@@ -108,71 +106,31 @@ func (c *Cluster) removeFailuresAnnotation() error {
108106 return nil
109107}
110108
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
121112
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 }}}
139121 }
140- c .logger .Info ("pods are patched with critical operation label" )
141- return nil
142- }
143122
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+ }
145127 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 {})
153129 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 )
155131 return err
156132 }
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- }
174133 }
175- c .logger .Info ("critical operation label is removed from all pods" )
176134 return nil
177135}
178136
@@ -290,12 +248,12 @@ func (c *Cluster) majorVersionUpgrade() error {
290248 c .logger .Infof ("healthy cluster ready to upgrade, current: %d desired: %d" , c .currentMajorVersion , desiredVersion )
291249 if c .currentMajorVersion < desiredVersion {
292250 defer func () error {
293- if err = c .removeCriticalOperationLabel (pods ); err != nil {
251+ if err = c .criticalOperationLabel (pods , true ); err != nil {
294252 return fmt .Errorf ("failed to remove critical-operation label: %s" , err )
295253 }
296254 return nil
297255 }()
298- if err = c .labelCriticalOperation (pods ); err != nil {
256+ if err = c .criticalOperationLabel (pods , false ); err != nil {
299257 return fmt .Errorf ("failed to assign critical-operation label: %s" , err )
300258 }
301259
0 commit comments