Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize ut in podopslifecycle controller #136

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}
}

// 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 @@
}

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 @@

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/podopslifecycle/podopslifecycle_controller.go#L181

Added line #L181 was not covered by tests
}
if updated {
r.Recorder.Eventf(pod, corev1.EventTypeNormal, "ReadinessGate", "Set service ready readiness gate to %v", v)
Expand All @@ -186,6 +189,7 @@
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 @@
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 @@
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) removeDirtyExpectedFinalizer(pod *corev1.Pod, notSatisfiedFinalizers map[string]string) (bool, error) {
var allDirty bool
var allDirty bool // Whether all not atisfied finalizers are dirty

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

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/podopslifecycle/podopslifecycle_controller.go#L229

Added line #L229 was not covered by tests
dirtyExpectedFinalizer := make(map[string]string)

for expectedFlzKey, finalizer := range notSatisfiedFinalizers {
Expand Down Expand Up @@ -348,7 +353,7 @@
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 @@
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
Loading