Skip to content

Commit

Permalink
update event reasons of podopslifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Mar 26, 2024
1 parent 4544135 commit 383257f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
6 changes: 6 additions & 0 deletions apis/apps/v1alpha1/well_known_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const (
ProtectFinalizer = "finalizer.operating.kusionstack.io/protected"
)

// well known event reasons
const (
PodDeletionEvent = "PodDeletion"
ServiceReadyEvent = "ServiceReady"
)

// well known variables
const (
PodOpsLifecyclePreCheckStage = "PreCheck"
Expand Down
1 change: 0 additions & 1 deletion pkg/controllers/poddeletion/lifecycle_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ func (a *PodDeleteOpsLifecycleAdapter) WhenBegin(obj client.Object) (bool, error

// WhenFinish will be executed when finish a lifecycle
func (a *PodDeleteOpsLifecycleAdapter) WhenFinish(_ client.Object) (bool, error) {

return false, nil
}
7 changes: 5 additions & 2 deletions pkg/controllers/poddeletion/poddeletion_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"kusionstack.io/operating/apis/apps/v1alpha1"
appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/collaset/pvccontrol"
"kusionstack.io/operating/pkg/controllers/utils/expectations"
Expand Down Expand Up @@ -123,14 +124,16 @@ func (r *PodDeletionReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}

// if Pod is allow to operate, delete it
// if Pod is allowed to operate, delete it
if _, allowed := podopslifecycle.AllowOps(OpsLifecycleAdapter, 0, instance); allowed {
logger.Info("try to delete Pod with deletion indication")
if err := r.Client.Delete(context.TODO(), instance); err != nil {
return ctrl.Result{}, fmt.Errorf("fail to delete Pod %s with deletion indication: %s", req, err)
} else {
r.Recorder.Event(instance, corev1.EventTypeNormal, v1alpha1.PodDeletionEvent, "Deleted pod")

if err := activeExpectations.ExpectDelete(instance, expectations.Pod, instance.Name); err != nil {
return ctrl.Result{}, fmt.Errorf("fail to expect Pod %s deleted: %s", req, err)
return ctrl.Result{}, fmt.Errorf("fail to expect Pod %s to be deleted: %s", req, err)

Check warning on line 136 in pkg/controllers/poddeletion/poddeletion_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/poddeletion/poddeletion_controller.go#L136

Added line #L136 was not covered by tests
}
}
// if this pod in replaced update, delete pvcs
Expand Down
19 changes: 12 additions & 7 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,23 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
return reconcile.Result{}, err
}

// If all lifecycle is finished, or the is no lifecycle begined
// All lifecycles are finished, or no lifecycle begined
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)
if err != nil {
return reconcile.Result{}, err
}
if updated {
return reconcile.Result{}, nil
}

Check warning on line 137 in pkg/controllers/podopslifecycle/podopslifecycle_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/podopslifecycle/podopslifecycle_controller.go#L136-L137

Added lines #L136 - L137 were not covered by tests

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)
if err != nil {
return reconcile.Result{}, err
}
if updated {
return reconcile.Result{}, nil
}
}

// Get the state of pod managed by TransitionRule
Expand Down Expand Up @@ -181,7 +185,6 @@ 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 @@ -325,6 +328,9 @@ func (r *ReconcilePodOpsLifecycle) updateServiceReadiness(ctx context.Context, p

return false, err
}

r.Recorder.Eventf(pod, corev1.EventTypeNormal, v1alpha1.ServiceReadyEvent, "Set ReadinessGate service-ready to %v", isReady)

return true, nil
}

Expand Down Expand Up @@ -436,7 +442,6 @@ func (r *ReconcilePodOpsLifecycle) addLabels(ctx context.Context, pod *corev1.Po
r.Logger.Error(err, "failed to update pod with labels", "pod", utils.ObjectKeyString(pod), "labels", labels)
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 383257f

Please sign in to comment.