@@ -185,20 +185,19 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
185185 v1alpha1 .PodPreparingLabelPrefix : false , // Set readiness gate to false
186186 v1alpha1 .PodCompletingLabelPrefix : true , // Set readiness gate to true
187187 }
188- for _ , k := range []string {v1alpha1 .PodPreparingLabelPrefix , v1alpha1 .PodCompletingLabelPrefix } {
189- v := expected [k ]
188+ for phaseLabel , expectedReadinessGateStatus := range expected {
190189 keeped := false
191190 for _ , labels := range idToLabelsMap {
192- if _ , ok := labels [k ]; ! ok {
191+ if _ , ok := labels [phaseLabel ]; ! ok {
193192 continue
194193 }
195194
196- updated , err := r .updateServiceReadiness (ctx , pod , v )
195+ updated , err := r .updateServiceReadiness (ctx , pod , expectedReadinessGateStatus )
197196 if err != nil {
198197 return reconcile.Result {}, err // Only need set once
199198 }
200199 if updated {
201- logger .Info ("update readiness gate" , "status" , v )
200+ logger .Info ("update readiness gate" , "status" , expectedReadinessGateStatus )
202201 }
203202 keeped = true
204203 }
@@ -221,7 +220,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
221220// addServiceAvailable try to add service available label to pod
222221func (r * ReconcilePodOpsLifecycle ) addServiceAvailable (pod * corev1.Pod ) (bool , error ) {
223222 if pod .Labels == nil {
224- return false , nil
223+ pod . Labels = map [ string ] string {}
225224 }
226225 if _ , ok := pod .Labels [v1alpha1 .PodServiceAvailableLabel ]; ok {
227226 return false , nil
@@ -248,10 +247,29 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
248247 return false , nil
249248 }
250249
251- podLabelsToAdd := map [string ]string {
252- v1alpha1 .PodServiceAvailableLabel : strconv .FormatInt (time .Now ().UnixNano (), 10 ),
250+ key := controllerKey (pod )
251+ r .expectation .ExpectUpdate (key , pod .ResourceVersion )
252+ err = retry .RetryOnConflict (retry .DefaultRetry , func () error {
253+ newPod := & corev1.Pod {}
254+ err := r .Client .Get (context .Background (), types.NamespacedName {Namespace : pod .Namespace , Name : pod .Name }, newPod )
255+ if err != nil {
256+ return err
257+ }
258+ if newPod .Labels == nil {
259+ newPod .Labels = map [string ]string {}
260+ }
261+ newPod .Labels [v1alpha1 .PodServiceAvailableLabel ] = strconv .FormatInt (time .Now ().UnixNano (), 10 )
262+ delete (newPod .Labels , v1alpha1 .PodCreatingLabel )
263+ delete (newPod .Labels , v1alpha1 .PodCompletingLabel )
264+
265+ return r .Client .Update (context .Background (), newPod )
266+ })
267+ if err != nil {
268+ r .Logger .Error (err , "failed to set Pod as service available" , "pod" , utils .ObjectKeyString (pod ))
269+ r .expectation .DeleteExpectations (key )
253270 }
254- return true , r .addLabels (context .Background (), pod , podLabelsToAdd )
271+
272+ return true , err
255273}
256274
257275func (r * ReconcilePodOpsLifecycle ) removeDirtyExpectedFinalizer (pod * corev1.Pod , notSatisfiedFinalizers map [string ]string ) (bool , error ) {
0 commit comments