Skip to content

Commit 744dd5a

Browse files
committed
optimize ut in podopslifecycle controller
1 parent 3afbc70 commit 744dd5a

File tree

6 files changed

+246
-51
lines changed

6 files changed

+246
-51
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 8 deletions
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"

pkg/controllers/podopslifecycle/podopslifecycle_controller_test.go

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
. "github.com/onsi/gomega"
2828
appsv1 "k8s.io/api/apps/v1"
2929
corev1 "k8s.io/api/core/v1"
30+
"k8s.io/apimachinery/pkg/api/errors"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/types"
3234
"sigs.k8s.io/controller-runtime/pkg/client"
3335
"sigs.k8s.io/controller-runtime/pkg/envtest"
3436
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -88,15 +90,15 @@ var _ = BeforeSuite(func() {
8890
})
8991

9092
var _ = AfterSuite(func() {
91-
By("tearing down the test environment")
93+
By("Tearing down the test environment")
9294

9395
cancel()
9496

9597
err := env.Stop()
9698
Expect(err).NotTo(HaveOccurred())
9799
})
98100

99-
var _ = Describe("podopslifecycle controller", func() {
101+
var _ = Describe("PodOpsLifecycle controller", func() {
100102
var (
101103
podSpec = corev1.PodSpec{
102104
Containers: []corev1.Container{
@@ -111,15 +113,17 @@ var _ = Describe("podopslifecycle controller", func() {
111113
},
112114
},
113115
}
116+
name = "test"
117+
namespace = "default"
114118
id = "123"
115119
timestamp = "1402144848"
116120
)
117121

118122
AfterEach(func() {
119123
pod := &corev1.Pod{
120124
ObjectMeta: metav1.ObjectMeta{
121-
Name: "test",
122-
Namespace: "default",
125+
Name: name,
126+
Namespace: namespace,
123127
},
124128
}
125129
err := mgr.GetClient().Delete(context.Background(), pod)
@@ -133,11 +137,11 @@ var _ = Describe("podopslifecycle controller", func() {
133137
}
134138
})
135139

136-
It("update pod with stage pre-check", func() {
140+
It("Create a pod controlled by kusionstack", func() {
137141
pod := &corev1.Pod{
138142
ObjectMeta: metav1.ObjectMeta{
139-
Name: "test",
140-
Namespace: "default",
143+
Name: name,
144+
Namespace: namespace,
141145
Labels: map[string]string{v1alpha1.ControlledByKusionStackLabelKey: "true"},
142146
},
143147
Spec: podSpec,
@@ -147,22 +151,33 @@ var _ = Describe("podopslifecycle controller", func() {
147151

148152
<-request
149153

154+
// Not found the pod
150155
pod = &corev1.Pod{}
151156
err = mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
152-
Name: "test",
153-
Namespace: "default",
157+
Name: name + "1",
158+
Namespace: namespace,
159+
}, pod)
160+
Expect(errors.IsNotFound(err)).To(Equal(true))
161+
162+
_, err = podOpsLifecycle.Reconcile(context.Background(), reconcile.Request{types.NamespacedName{Name: name, Namespace: namespace}})
163+
Expect(err).NotTo(HaveOccurred())
164+
165+
pod = &corev1.Pod{}
166+
err = mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
167+
Name: name,
168+
Namespace: namespace,
154169
}, pod)
155170
Expect(err).NotTo(HaveOccurred())
156171
Expect(pod.Status.Conditions).To(HaveLen(1))
157172
Expect(string(pod.Status.Conditions[0].Type)).To(Equal(v1alpha1.ReadinessGatePodServiceReady))
158173
Expect(pod.Status.Conditions[0].Status).To(Equal(corev1.ConditionTrue))
159174
})
160175

161-
It("create pod with label prepare", func() {
176+
It("Create pod with label preparing", func() {
162177
pod := &corev1.Pod{
163178
ObjectMeta: metav1.ObjectMeta{
164-
Name: "test",
165-
Namespace: "default",
179+
Name: name,
180+
Namespace: namespace,
166181
Labels: map[string]string{
167182
v1alpha1.ControlledByKusionStackLabelKey: "true",
168183
fmt.Sprintf("%s/%s", v1alpha1.PodOperatingLabelPrefix, id): timestamp,
@@ -177,8 +192,8 @@ var _ = Describe("podopslifecycle controller", func() {
177192
pod = &corev1.Pod{}
178193
Eventually(func() error {
179194
if err := mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
180-
Name: "test",
181-
Namespace: "default",
195+
Name: name,
196+
Namespace: namespace,
182197
}, pod); err != nil {
183198
return fmt.Errorf("fail to get pod: %s", err)
184199
}
@@ -196,14 +211,14 @@ var _ = Describe("podopslifecycle controller", func() {
196211
}
197212

198213
return nil
199-
}, 5*time.Second, 1*time.Second).Should(BeNil())
214+
}, 3*time.Second, 200*time.Millisecond).Should(BeNil())
200215
})
201216

202-
It("create pod with label complete", func() {
217+
It("Create pod with label completing", func() {
203218
pod := &corev1.Pod{
204219
ObjectMeta: metav1.ObjectMeta{
205-
Name: "test",
206-
Namespace: "default",
220+
Name: name,
221+
Namespace: namespace,
207222
Labels: map[string]string{
208223
v1alpha1.ControlledByKusionStackLabelKey: "true",
209224
fmt.Sprintf("%s/%s", v1alpha1.PodOperateLabelPrefix, id): timestamp,
@@ -220,8 +235,8 @@ var _ = Describe("podopslifecycle controller", func() {
220235
pod = &corev1.Pod{}
221236
Eventually(func() error {
222237
if err := mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
223-
Name: "test",
224-
Namespace: "default",
238+
Name: name,
239+
Namespace: namespace,
225240
}, pod); err != nil {
226241
return fmt.Errorf("fail to get pod: %s", err)
227242
}
@@ -239,14 +254,14 @@ var _ = Describe("podopslifecycle controller", func() {
239254
}
240255

241256
return nil
242-
}, 5*time.Second, 1*time.Second).Should(BeNil())
257+
}, 3*time.Second, 200*time.Millisecond).Should(BeNil())
243258
})
244259

245-
It("update pod with label complete", func() {
260+
It("Update pod with label compling", func() {
246261
pod := &corev1.Pod{
247262
ObjectMeta: metav1.ObjectMeta{
248-
Name: "test",
249-
Namespace: "default",
263+
Name: name,
264+
Namespace: namespace,
250265
Labels: map[string]string{v1alpha1.ControlledByKusionStackLabelKey: "true"},
251266
},
252267
Spec: podSpec,
@@ -256,28 +271,31 @@ var _ = Describe("podopslifecycle controller", func() {
256271

257272
<-request
258273

259-
pod = &corev1.Pod{}
260-
err = mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
261-
Name: "test",
262-
Namespace: "default",
263-
}, pod)
264-
Expect(err).NotTo(HaveOccurred())
274+
Eventually(func() error {
275+
pod = &corev1.Pod{}
276+
err = mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
277+
Name: name,
278+
Namespace: namespace,
279+
}, pod)
280+
if err != nil {
281+
return fmt.Errorf("fail to get pod: %v", err)
282+
}
265283

266-
pod.ObjectMeta.Labels = map[string]string{
267-
v1alpha1.ControlledByKusionStackLabelKey: "true",
268-
fmt.Sprintf("%s/%s", v1alpha1.PodOperateLabelPrefix, id): timestamp,
269-
fmt.Sprintf("%s/%s", v1alpha1.PodCompletingLabelPrefix, id): timestamp,
270-
}
271-
err = mgr.GetClient().Update(context.Background(), pod)
272-
Expect(err).NotTo(HaveOccurred())
284+
pod.ObjectMeta.Labels = map[string]string{
285+
v1alpha1.ControlledByKusionStackLabelKey: "true",
286+
fmt.Sprintf("%s/%s", v1alpha1.PodOperateLabelPrefix, id): timestamp,
287+
fmt.Sprintf("%s/%s", v1alpha1.PodCompletingLabelPrefix, id): timestamp,
288+
}
289+
return mgr.GetClient().Update(context.Background(), pod)
290+
}, 3*time.Second, 200*time.Millisecond).Should(BeNil())
273291

274292
pod = &corev1.Pod{}
275293
Eventually(func() error {
276294
if err := mgr.GetAPIReader().Get(context.Background(), client.ObjectKey{
277-
Name: "test",
278-
Namespace: "default",
295+
Name: name,
296+
Namespace: namespace,
279297
}, pod); err != nil {
280-
return fmt.Errorf("fail to get pod: %s", err)
298+
return fmt.Errorf("fail to get pod: %v", err)
281299
}
282300

283301
if len(pod.Status.Conditions) != 1 {
@@ -293,7 +311,7 @@ var _ = Describe("podopslifecycle controller", func() {
293311
}
294312

295313
return nil
296-
}, 5*time.Second, 1*time.Second).Should(BeNil())
314+
}, 3*time.Second, 200*time.Millisecond).Should(BeNil())
297315
})
298316
})
299317

pkg/controllers/podopslifecycle/predicate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type NeedOpsLifecycle func(oldPod, newPod *corev1.Pod) bool
2525

2626
type PodPredicate struct {
27-
NeedOpsLifecycle // check if pod need use lifecycle
27+
NeedOpsLifecycle // Check if pod need be reconciled
2828
}
2929

3030
func (pp *PodPredicate) Create(evt event.CreateEvent) bool {

0 commit comments

Comments
 (0)