Skip to content

Commit a302b54

Browse files
authored
optimize ut in podopslifecycle controller (#136)
1 parent 3afbc70 commit a302b54

File tree

6 files changed

+455
-69
lines changed

6 files changed

+455
-69
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module kusionstack.io/operating
33
go 1.19
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1
76
github.com/docker/distribution v2.8.2+incompatible
87
github.com/go-logr/logr v1.2.4
98
github.com/google/uuid v1.3.0
@@ -50,6 +49,7 @@ require (
5049
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5150
github.com/clbanning/mxj/v2 v2.5.5 // indirect
5251
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
52+
github.com/davecgh/go-spew v1.1.1 // indirect
5353
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
5454
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
5555
github.com/fsnotify/fsnotify v1.6.0 // indirect

pkg/controllers/podopslifecycle/podopslifecycle_controller.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
125125
if err != nil {
126126
return reconcile.Result{}, err
127127
}
128+
129+
// If all lifecycle is finished, or the is no lifecycle begined
128130
if len(idToLabelsMap) == 0 {
129131
updated, err := r.addServiceAvailable(pod)
130132
if updated {
@@ -139,6 +141,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
139141
}
140142
}
141143

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

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

176179
updated, err := r.updateServiceReadiness(ctx, pod, v)
177180
if err != nil {
178-
return reconcile.Result{}, err // only need set once
181+
return reconcile.Result{}, err // Only need set once
179182
}
180183
if updated {
181184
r.Recorder.Eventf(pod, corev1.EventTypeNormal, "ReadinessGate", "Set service ready readiness gate to %v", v)
@@ -186,6 +189,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
186189
return reconcile.Result{}, nil
187190
}
188191

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

197-
satisfied, notSatisfiedFinalizers, err := controllerutils.IsExpectedFinalizerSatisfied(pod) // whether all expected finalizers are satisfied
201+
// Whether all expected finalizers are satisfied
202+
satisfied, notSatisfiedFinalizers, err := controllerutils.IsExpectedFinalizerSatisfied(pod)
198203
if err != nil {
199204
return false, err
200205
}
@@ -207,7 +212,7 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
207212
if !allDirty {
208213
return false, nil
209214
}
210-
// all not satisfied expected finalizers are dirty, so actually the pod satisfied expected finalizer now
215+
// All not satisfied finalizers are dirty, so actually the pod satisfied expected finalizers now
211216
}
212217

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

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

227232
for expectedFlzKey, finalizer := range notSatisfiedFinalizers {
@@ -348,7 +353,7 @@ func (r *ReconcilePodOpsLifecycle) setServiceReadiness(pod *corev1.Pod, isReady
348353
if !isReady {
349354
status = corev1.ConditionFalse
350355
}
351-
if index == -1 { // append readiness gate
356+
if index == -1 { // Append readiness gate
352357
pod.Status.Conditions = append(pod.Status.Conditions, corev1.PodCondition{
353358
Type: v1alpha1.ReadinessGatePodServiceReady,
354359
Status: status,
@@ -362,7 +367,7 @@ func (r *ReconcilePodOpsLifecycle) setServiceReadiness(pod *corev1.Pod, isReady
362367
return false, ""
363368
}
364369

365-
// update readiness gate
370+
// Update readiness gate
366371
pod.Status.Conditions[index].Status = status
367372
pod.Status.Conditions[index].LastTransitionTime = metav1.Now()
368373
pod.Status.Conditions[index].Message = "updated by PodOpsLifecycle"

0 commit comments

Comments
 (0)