@@ -27,8 +27,10 @@ import (
27
27
. "github.com/onsi/gomega"
28
28
appsv1 "k8s.io/api/apps/v1"
29
29
corev1 "k8s.io/api/core/v1"
30
+ "k8s.io/apimachinery/pkg/api/errors"
30
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
32
"k8s.io/apimachinery/pkg/runtime"
33
+ "k8s.io/apimachinery/pkg/types"
32
34
"sigs.k8s.io/controller-runtime/pkg/client"
33
35
"sigs.k8s.io/controller-runtime/pkg/envtest"
34
36
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -88,15 +90,15 @@ var _ = BeforeSuite(func() {
88
90
})
89
91
90
92
var _ = AfterSuite (func () {
91
- By ("tearing down the test environment" )
93
+ By ("Tearing down the test environment" )
92
94
93
95
cancel ()
94
96
95
97
err := env .Stop ()
96
98
Expect (err ).NotTo (HaveOccurred ())
97
99
})
98
100
99
- var _ = Describe ("podopslifecycle controller" , func () {
101
+ var _ = Describe ("PodOpsLifecycle controller" , func () {
100
102
var (
101
103
podSpec = corev1.PodSpec {
102
104
Containers : []corev1.Container {
@@ -111,15 +113,17 @@ var _ = Describe("podopslifecycle controller", func() {
111
113
},
112
114
},
113
115
}
116
+ name = "test"
117
+ namespace = "default"
114
118
id = "123"
115
119
timestamp = "1402144848"
116
120
)
117
121
118
122
AfterEach (func () {
119
123
pod := & corev1.Pod {
120
124
ObjectMeta : metav1.ObjectMeta {
121
- Name : "test" ,
122
- Namespace : "default" ,
125
+ Name : name ,
126
+ Namespace : namespace ,
123
127
},
124
128
}
125
129
err := mgr .GetClient ().Delete (context .Background (), pod )
@@ -133,11 +137,11 @@ var _ = Describe("podopslifecycle controller", func() {
133
137
}
134
138
})
135
139
136
- It ("update pod with stage pre-check " , func () {
140
+ It ("Create a pod controlled by kusionstack " , func () {
137
141
pod := & corev1.Pod {
138
142
ObjectMeta : metav1.ObjectMeta {
139
- Name : "test" ,
140
- Namespace : "default" ,
143
+ Name : name ,
144
+ Namespace : namespace ,
141
145
Labels : map [string ]string {v1alpha1 .ControlledByKusionStackLabelKey : "true" },
142
146
},
143
147
Spec : podSpec ,
@@ -147,22 +151,33 @@ var _ = Describe("podopslifecycle controller", func() {
147
151
148
152
<- request
149
153
154
+ // Not found the pod
150
155
pod = & corev1.Pod {}
151
156
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 ,
154
169
}, pod )
155
170
Expect (err ).NotTo (HaveOccurred ())
156
171
Expect (pod .Status .Conditions ).To (HaveLen (1 ))
157
172
Expect (string (pod .Status .Conditions [0 ].Type )).To (Equal (v1alpha1 .ReadinessGatePodServiceReady ))
158
173
Expect (pod .Status .Conditions [0 ].Status ).To (Equal (corev1 .ConditionTrue ))
159
174
})
160
175
161
- It ("create pod with label prepare " , func () {
176
+ It ("Create pod with label preparing " , func () {
162
177
pod := & corev1.Pod {
163
178
ObjectMeta : metav1.ObjectMeta {
164
- Name : "test" ,
165
- Namespace : "default" ,
179
+ Name : name ,
180
+ Namespace : namespace ,
166
181
Labels : map [string ]string {
167
182
v1alpha1 .ControlledByKusionStackLabelKey : "true" ,
168
183
fmt .Sprintf ("%s/%s" , v1alpha1 .PodOperatingLabelPrefix , id ): timestamp ,
@@ -177,8 +192,8 @@ var _ = Describe("podopslifecycle controller", func() {
177
192
pod = & corev1.Pod {}
178
193
Eventually (func () error {
179
194
if err := mgr .GetAPIReader ().Get (context .Background (), client.ObjectKey {
180
- Name : "test" ,
181
- Namespace : "default" ,
195
+ Name : name ,
196
+ Namespace : namespace ,
182
197
}, pod ); err != nil {
183
198
return fmt .Errorf ("fail to get pod: %s" , err )
184
199
}
@@ -196,14 +211,14 @@ var _ = Describe("podopslifecycle controller", func() {
196
211
}
197
212
198
213
return nil
199
- }, 5 * time .Second , 1 * time .Second ).Should (BeNil ())
214
+ }, 3 * time .Second , 200 * time .Millisecond ).Should (BeNil ())
200
215
})
201
216
202
- It ("create pod with label complete " , func () {
217
+ It ("Create pod with label completing " , func () {
203
218
pod := & corev1.Pod {
204
219
ObjectMeta : metav1.ObjectMeta {
205
- Name : "test" ,
206
- Namespace : "default" ,
220
+ Name : name ,
221
+ Namespace : namespace ,
207
222
Labels : map [string ]string {
208
223
v1alpha1 .ControlledByKusionStackLabelKey : "true" ,
209
224
fmt .Sprintf ("%s/%s" , v1alpha1 .PodOperateLabelPrefix , id ): timestamp ,
@@ -220,8 +235,8 @@ var _ = Describe("podopslifecycle controller", func() {
220
235
pod = & corev1.Pod {}
221
236
Eventually (func () error {
222
237
if err := mgr .GetAPIReader ().Get (context .Background (), client.ObjectKey {
223
- Name : "test" ,
224
- Namespace : "default" ,
238
+ Name : name ,
239
+ Namespace : namespace ,
225
240
}, pod ); err != nil {
226
241
return fmt .Errorf ("fail to get pod: %s" , err )
227
242
}
@@ -239,14 +254,14 @@ var _ = Describe("podopslifecycle controller", func() {
239
254
}
240
255
241
256
return nil
242
- }, 5 * time .Second , 1 * time .Second ).Should (BeNil ())
257
+ }, 3 * time .Second , 200 * time .Millisecond ).Should (BeNil ())
243
258
})
244
259
245
- It ("update pod with label complete " , func () {
260
+ It ("Update pod with label compling " , func () {
246
261
pod := & corev1.Pod {
247
262
ObjectMeta : metav1.ObjectMeta {
248
- Name : "test" ,
249
- Namespace : "default" ,
263
+ Name : name ,
264
+ Namespace : namespace ,
250
265
Labels : map [string ]string {v1alpha1 .ControlledByKusionStackLabelKey : "true" },
251
266
},
252
267
Spec : podSpec ,
@@ -256,28 +271,31 @@ var _ = Describe("podopslifecycle controller", func() {
256
271
257
272
<- request
258
273
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
+ }
265
283
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 ())
273
291
274
292
pod = & corev1.Pod {}
275
293
Eventually (func () error {
276
294
if err := mgr .GetAPIReader ().Get (context .Background (), client.ObjectKey {
277
- Name : "test" ,
278
- Namespace : "default" ,
295
+ Name : name ,
296
+ Namespace : namespace ,
279
297
}, pod ); err != nil {
280
- return fmt .Errorf ("fail to get pod: %s " , err )
298
+ return fmt .Errorf ("fail to get pod: %v " , err )
281
299
}
282
300
283
301
if len (pod .Status .Conditions ) != 1 {
@@ -293,7 +311,7 @@ var _ = Describe("podopslifecycle controller", func() {
293
311
}
294
312
295
313
return nil
296
- }, 5 * time .Second , 1 * time .Second ).Should (BeNil ())
314
+ }, 3 * time .Second , 200 * time .Millisecond ).Should (BeNil ())
297
315
})
298
316
})
299
317
0 commit comments