Skip to content

Commit

Permalink
optimize ut in podopslifecycle controller (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Dec 26, 2023
1 parent 3afbc70 commit a302b54
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 69 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module kusionstack.io/operating
go 1.19

require (
github.com/davecgh/go-spew v1.1.1
github.com/docker/distribution v2.8.2+incompatible
github.com/go-logr/logr v1.2.4
github.com/google/uuid v1.3.0
Expand Down Expand Up @@ -50,6 +49,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/clbanning/mxj/v2 v2.5.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
21 changes: 13 additions & 8 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
if err != nil {
return reconcile.Result{}, err
}

// If all lifecycle is finished, or the is no lifecycle begined
if len(idToLabelsMap) == 0 {
updated, err := r.addServiceAvailable(pod)
if updated {
Expand All @@ -139,6 +141,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
}
}

// Get the state of pod managed by TransitionRule
state, err := r.podTransitionRuleManager.GetState(ctx, r.Client, pod)
if err != nil {
logger.Error(err, "failed to get pod state")
Expand All @@ -164,8 +167,8 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
}

expected := map[string]bool{
v1alpha1.PodPreparingLabelPrefix: false, // set readiness gate to false, traffic off
v1alpha1.PodCompletingLabelPrefix: true, // set readiness gate to true, traffic on
v1alpha1.PodPreparingLabelPrefix: false, // Set readiness gate to false
v1alpha1.PodCompletingLabelPrefix: true, // Set readiness gate to true
}
for _, labels := range idToLabelsMap {
for k, v := range expected {
Expand All @@ -175,7 +178,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc

updated, err := r.updateServiceReadiness(ctx, pod, v)
if err != nil {
return reconcile.Result{}, err // only need set once
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)
Expand All @@ -186,6 +189,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
return reconcile.Result{}, nil
}

// addServiceAvailable try to add service available label to pod
func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, error) {
if pod.Labels == nil {
return false, nil
Expand All @@ -194,7 +198,8 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
return false, nil
}

satisfied, notSatisfiedFinalizers, err := controllerutils.IsExpectedFinalizerSatisfied(pod) // whether all expected finalizers are satisfied
// Whether all expected finalizers are satisfied
satisfied, notSatisfiedFinalizers, err := controllerutils.IsExpectedFinalizerSatisfied(pod)
if err != nil {
return false, err
}
Expand All @@ -207,7 +212,7 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
if !allDirty {
return false, nil
}
// all not satisfied expected finalizers are dirty, so actually the pod satisfied expected finalizer now
// All not satisfied finalizers are dirty, so actually the pod satisfied expected finalizers now
}

if !controllerutils.IsPodReady(pod) {
Expand All @@ -221,7 +226,7 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
}

func (r *ReconcilePodOpsLifecycle) removeDirtyExpectedFinalizer(pod *corev1.Pod, notSatisfiedFinalizers map[string]string) (bool, error) {
var allDirty bool
var allDirty bool // Whether all not atisfied finalizers are dirty
dirtyExpectedFinalizer := make(map[string]string)

for expectedFlzKey, finalizer := range notSatisfiedFinalizers {
Expand Down Expand Up @@ -348,7 +353,7 @@ func (r *ReconcilePodOpsLifecycle) setServiceReadiness(pod *corev1.Pod, isReady
if !isReady {
status = corev1.ConditionFalse
}
if index == -1 { // append readiness gate
if index == -1 { // Append readiness gate
pod.Status.Conditions = append(pod.Status.Conditions, corev1.PodCondition{
Type: v1alpha1.ReadinessGatePodServiceReady,
Status: status,
Expand All @@ -362,7 +367,7 @@ func (r *ReconcilePodOpsLifecycle) setServiceReadiness(pod *corev1.Pod, isReady
return false, ""
}

// update readiness gate
// Update readiness gate
pod.Status.Conditions[index].Status = status
pod.Status.Conditions[index].LastTransitionTime = metav1.Now()
pod.Status.Conditions[index].Message = "updated by PodOpsLifecycle"
Expand Down
Loading

0 comments on commit a302b54

Please sign in to comment.