Skip to content

Commit

Permalink
update, podopslifecycle uses recorder (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Aug 28, 2023
1 parent 74d27c7 commit fb252cd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ const (
controllerName = "podopslifecycle-controller"
)

var (
expectation *expectations.ResourceVersionExpectation
)

func Add(mgr manager.Manager) error {
return AddToMgr(mgr, NewReconciler(mgr))
}
Expand Down Expand Up @@ -79,7 +75,7 @@ func AddToMgr(mgr manager.Manager, r reconcile.Reconciler) error {
var _ reconcile.Reconciler = &ReconcilePodOpsLifecycle{}

func NewReconciler(mgr manager.Manager) *ReconcilePodOpsLifecycle {
expectation = expectations.NewResourceVersionExpectation()
expectation := expectations.NewResourceVersionExpectation()

r := &ReconcilePodOpsLifecycle{
Client: mgr.GetClient(),
Expand Down Expand Up @@ -131,11 +127,13 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
if len(idToLabelsMap) == 0 {
updated, err := r.addServiceAvailable(pod)
if updated {
r.recorder.Eventf(pod, corev1.EventTypeNormal, "ServiceAvailable", "Label pod as service available, error: %v", err)
return reconcile.Result{}, err
}

updated, err = r.updateServiceReadiness(ctx, pod, true)
if updated {
r.recorder.Eventf(pod, corev1.EventTypeNormal, "ReadinessGate", "Set service ready readiness gate to true, error: %v", err)
return reconcile.Result{}, err
}
}
Expand Down Expand Up @@ -178,6 +176,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
return reconcile.Result{}, err // only need set once
}
if updated {
r.recorder.Eventf(pod, corev1.EventTypeNormal, "ReadinessGate", "Set service ready readiness gate to %v", v)
return reconcile.Result{}, nil
}
}
Expand Down Expand Up @@ -326,7 +325,7 @@ func (r *ReconcilePodOpsLifecycle) addLabels(ctx context.Context, pod *corev1.Po
}

key := controllerKey(pod)
expectation.ExpectUpdate(key, pod.ResourceVersion)
r.expectation.ExpectUpdate(key, pod.ResourceVersion)
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
newPod := &corev1.Pod{}
err := r.Client.Get(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}, newPod)
Expand All @@ -343,8 +342,10 @@ func (r *ReconcilePodOpsLifecycle) addLabels(ctx context.Context, pod *corev1.Po
})
if err != nil {
klog.Errorf("failed to update pod %s with labels: %v: %s", key, labels, err)
expectation.DeleteExpectations(key)
r.expectation.DeleteExpectations(key)
}
r.recorder.Eventf(pod, corev1.EventTypeNormal, "UpdatePod", "Succeed to update labels: %v", labels)

return err
}

Expand Down

0 comments on commit fb252cd

Please sign in to comment.