Skip to content

Commit

Permalink
refactor: optimize the events about pods (#173)
Browse files Browse the repository at this point in the history
update event reasons of podopslifecycle
  • Loading branch information
shaofan-hs authored Mar 28, 2024
1 parent 9e6fe89 commit 9f93834
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 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)
}
}
// 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
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/klogr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -41,6 +43,8 @@ import (
"kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/podtransitionrule"
"kusionstack.io/operating/pkg/controllers/podtransitionrule/checker"
"kusionstack.io/operating/pkg/controllers/utils/expectations"
"kusionstack.io/operating/pkg/utils/mixin"
)

var (
Expand Down Expand Up @@ -411,7 +415,7 @@ var _ = Describe("Stage processing", func() {
})
})

var _ = Describe("Expected finalizer processng", func() {
var _ = Describe("Finalizer processing", func() {
var (
name = "test"
namespace = "default"
Expand Down Expand Up @@ -506,6 +510,61 @@ var _ = Describe("Expected finalizer processng", func() {
})
})

var _ = Describe("Label service-available processing", func() {
scheme := runtime.NewScheme()
err := corev1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Namespace: "default",
Labels: map[string]string{
v1alpha1.ControlledByKusionStackLabelKey: "true",
},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{
{
Type: corev1.PodReady,
Status: corev1.ConditionTrue,
},
},
},
}

fakeClient := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(pod).
Build()

podOpsLifecycle := &ReconcilePodOpsLifecycle{
ReconcilerMixin: &mixin.ReconcilerMixin{
Client: fakeClient,
Logger: klogr.New().WithName(controllerName),
},
expectation: expectations.NewResourceVersionExpectation(),
}

It("Service is available", func() {
_, err := podOpsLifecycle.Reconcile(context.Background(), reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "test1",
Namespace: "default",
},
})
Expect(err).NotTo(HaveOccurred())

pod1 := &corev1.Pod{}
fakeClient.Get(context.Background(), client.ObjectKey{
Name: "test1",
Namespace: "default",
}, pod1)
Expect(pod1.Labels[v1alpha1.PodServiceAvailableLabel]).NotTo(BeEmpty())
})
})

func testReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
requests := make(chan reconcile.Request, 5)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
Expand Down

0 comments on commit 9f93834

Please sign in to comment.