Skip to content

Commit f64de14

Browse files
committed
cleanup
Signed-off-by: everettraven <[email protected]>
1 parent 5cf41d4 commit f64de14

File tree

2 files changed

+106
-33
lines changed

2 files changed

+106
-33
lines changed

leader/leader.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ func Become(ctx context.Context, lockName string, opts ...Option) error {
174174
leaderPod := &corev1.Pod{}
175175
key = crclient.ObjectKey{Namespace: ns, Name: existingOwners[0].Name}
176176
err = config.Client.Get(ctx, key, leaderPod)
177-
leaderPod.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Pod"))
178177
switch {
179178
case apierrors.IsNotFound(err):
180179
log.Info("Leader pod has been deleted, waiting for garbage collection to remove the lock.")
@@ -200,7 +199,7 @@ func Become(ctx context.Context, lockName string, opts ...Option) error {
200199
log.Info("the status of the node where operator pod with leader lock was running has been 'notReady'")
201200
log.Info("Deleting the leader.")
202201

203-
// Mark the termainating status to the leaderPod and Delete the configmap lock
202+
//Mark the termainating status to the leaderPod and Delete the configmap lock
204203
if err := deleteLeader(ctx, config.Client, leaderPod, existing); err != nil {
205204
return err
206205
}
@@ -275,11 +274,6 @@ func getPod(ctx context.Context, client crclient.Client, ns string) (*corev1.Pod
275274
return nil, err
276275
}
277276

278-
// .Get() clears the APIVersion and Kind,
279-
// so we need to set them before returning the object.
280-
pod.TypeMeta.APIVersion = "v1"
281-
pod.TypeMeta.Kind = "Pod"
282-
283277
log.V(1).Info("Found Pod", "Pod.Namespace", ns, "Pod.Name", pod.Name)
284278

285279
return pod, nil
@@ -292,10 +286,6 @@ func getNode(ctx context.Context, client crclient.Client, nodeName string, node
292286
log.Error(err, "Failed to get Node", "Node.Name", nodeName)
293287
return err
294288
}
295-
// .Get() clears the APIVersion and Kind,
296-
// so we need to set them before returning the object.
297-
node.TypeMeta.APIVersion = "v1"
298-
node.TypeMeta.Kind = "Node"
299289
return nil
300290
}
301291

leader/leader_test.go

Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ import (
2222
. "github.com/onsi/gomega"
2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
2526
crclient "sigs.k8s.io/controller-runtime/pkg/client"
2627
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2728
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2829
)
2930

3031
var _ = Describe("Leader election", func() {
31-
3232
Describe("Become", func() {
33-
var (
34-
client crclient.Client
35-
)
33+
var client crclient.Client
3634
BeforeEach(func() {
3735
client = fake.NewClientBuilder().WithObjects(
3836
&corev1.Pod{
37+
TypeMeta: metav1.TypeMeta{
38+
APIVersion: schema.GroupVersion{
39+
Group: corev1.SchemeGroupVersion.Group,
40+
Version: corev1.SchemeGroupVersion.Version,
41+
}.String(),
42+
Kind: "Pod",
43+
},
3944
ObjectMeta: metav1.ObjectMeta{
4045
Name: "leader-test",
4146
Namespace: "testns",
@@ -49,6 +54,13 @@ var _ = Describe("Leader election", func() {
4954
},
5055
},
5156
&corev1.ConfigMap{
57+
TypeMeta: metav1.TypeMeta{
58+
APIVersion: schema.GroupVersion{
59+
Group: corev1.SchemeGroupVersion.Group,
60+
Version: corev1.SchemeGroupVersion.Version,
61+
}.String(),
62+
Kind: "ConfigMap",
63+
},
5264
ObjectMeta: metav1.ObjectMeta{
5365
Name: "leader-test",
5466
Namespace: "testns",
@@ -84,9 +96,15 @@ var _ = Describe("Leader election", func() {
8496
Expect(Become(context.TODO(), "leader-test", WithClient(client))).To(Succeed())
8597
})
8698
It("should become leader when pod is evicted and rescheduled", func() {
87-
8899
evictedPodStatusClient := fake.NewClientBuilder().WithObjects(
89100
&corev1.Pod{
101+
TypeMeta: metav1.TypeMeta{
102+
APIVersion: schema.GroupVersion{
103+
Group: corev1.SchemeGroupVersion.Group,
104+
Version: corev1.SchemeGroupVersion.Version,
105+
}.String(),
106+
Kind: "Pod",
107+
},
90108
ObjectMeta: metav1.ObjectMeta{
91109
Name: "leader-test-new",
92110
Namespace: "testns",
@@ -100,6 +118,13 @@ var _ = Describe("Leader election", func() {
100118
},
101119
},
102120
&corev1.Pod{
121+
TypeMeta: metav1.TypeMeta{
122+
APIVersion: schema.GroupVersion{
123+
Group: corev1.SchemeGroupVersion.Group,
124+
Version: corev1.SchemeGroupVersion.Version,
125+
}.String(),
126+
Kind: "Pod",
127+
},
103128
ObjectMeta: metav1.ObjectMeta{
104129
Name: "leader-test",
105130
Namespace: "testns",
@@ -117,6 +142,13 @@ var _ = Describe("Leader election", func() {
117142
},
118143
},
119144
&corev1.ConfigMap{
145+
TypeMeta: metav1.TypeMeta{
146+
APIVersion: schema.GroupVersion{
147+
Group: corev1.SchemeGroupVersion.Group,
148+
Version: corev1.SchemeGroupVersion.Version,
149+
}.String(),
150+
Kind: "ConfigMap",
151+
},
120152
ObjectMeta: metav1.ObjectMeta{
121153
Name: "leader-test",
122154
Namespace: "testns",
@@ -159,9 +191,15 @@ var _ = Describe("Leader election", func() {
159191
Expect(Become(context.TODO(), "leader-test", WithClient(evictedPodStatusClient))).To(Succeed())
160192
})
161193
It("should become leader when pod is preempted and rescheduled", func() {
162-
163194
preemptedPodStatusClient := fake.NewClientBuilder().WithObjects(
164195
&corev1.Pod{
196+
TypeMeta: metav1.TypeMeta{
197+
APIVersion: schema.GroupVersion{
198+
Group: corev1.SchemeGroupVersion.Group,
199+
Version: corev1.SchemeGroupVersion.Version,
200+
}.String(),
201+
Kind: "Pod",
202+
},
165203
ObjectMeta: metav1.ObjectMeta{
166204
Name: "leader-test-new",
167205
Namespace: "testns",
@@ -175,6 +213,13 @@ var _ = Describe("Leader election", func() {
175213
},
176214
},
177215
&corev1.Pod{
216+
TypeMeta: metav1.TypeMeta{
217+
APIVersion: schema.GroupVersion{
218+
Group: corev1.SchemeGroupVersion.Group,
219+
Version: corev1.SchemeGroupVersion.Version,
220+
}.String(),
221+
Kind: "Pod",
222+
},
178223
ObjectMeta: metav1.ObjectMeta{
179224
Name: "leader-test",
180225
Namespace: "testns",
@@ -192,6 +237,13 @@ var _ = Describe("Leader election", func() {
192237
},
193238
},
194239
&corev1.ConfigMap{
240+
TypeMeta: metav1.TypeMeta{
241+
APIVersion: schema.GroupVersion{
242+
Group: corev1.SchemeGroupVersion.Group,
243+
Version: corev1.SchemeGroupVersion.Version,
244+
}.String(),
245+
Kind: "ConfigMap",
246+
},
195247
ObjectMeta: metav1.ObjectMeta{
196248
Name: "leader-test",
197249
Namespace: "testns",
@@ -235,9 +287,7 @@ var _ = Describe("Leader election", func() {
235287
})
236288
})
237289
Describe("isPodEvicted", func() {
238-
var (
239-
leaderPod *corev1.Pod
240-
)
290+
var leaderPod *corev1.Pod
241291
BeforeEach(func() {
242292
leaderPod = &corev1.Pod{}
243293
})
@@ -260,9 +310,7 @@ var _ = Describe("Leader election", func() {
260310
})
261311
})
262312
Describe("isPodPreempted", func() {
263-
var (
264-
leaderPod *corev1.Pod
265-
)
313+
var leaderPod *corev1.Pod
266314
BeforeEach(func() {
267315
leaderPod = &corev1.Pod{}
268316
})
@@ -285,12 +333,17 @@ var _ = Describe("Leader election", func() {
285333
})
286334
})
287335
Describe("myOwnerRef", func() {
288-
var (
289-
client crclient.Client
290-
)
336+
var client crclient.Client
291337
BeforeEach(func() {
292338
client = fake.NewClientBuilder().WithObjects(
293339
&corev1.Pod{
340+
TypeMeta: metav1.TypeMeta{
341+
APIVersion: schema.GroupVersion{
342+
Group: corev1.SchemeGroupVersion.Group,
343+
Version: corev1.SchemeGroupVersion.Version,
344+
}.String(),
345+
Kind: "Pod",
346+
},
294347
ObjectMeta: metav1.ObjectMeta{
295348
Name: "mypod",
296349
Namespace: "testns",
@@ -318,12 +371,17 @@ var _ = Describe("Leader election", func() {
318371
})
319372
})
320373
Describe("getPod", func() {
321-
var (
322-
client crclient.Client
323-
)
374+
var client crclient.Client
324375
BeforeEach(func() {
325376
client = fake.NewClientBuilder().WithObjects(
326377
&corev1.Pod{
378+
TypeMeta: metav1.TypeMeta{
379+
APIVersion: schema.GroupVersion{
380+
Group: corev1.SchemeGroupVersion.Group,
381+
Version: corev1.SchemeGroupVersion.Version,
382+
}.String(),
383+
Kind: "Pod",
384+
},
327385
ObjectMeta: metav1.ObjectMeta{
328386
Name: "mypod",
329387
Namespace: "testns",
@@ -352,12 +410,17 @@ var _ = Describe("Leader election", func() {
352410
})
353411

354412
Describe("getNode", func() {
355-
var (
356-
client crclient.Client
357-
)
413+
var client crclient.Client
358414
BeforeEach(func() {
359415
client = fake.NewClientBuilder().WithObjects(
360416
&corev1.Node{
417+
TypeMeta: metav1.TypeMeta{
418+
APIVersion: schema.GroupVersion{
419+
Group: corev1.SchemeGroupVersion.Group,
420+
Version: corev1.SchemeGroupVersion.Version,
421+
}.String(),
422+
Kind: "Node",
423+
},
361424
ObjectMeta: metav1.ObjectMeta{
362425
Name: "mynode",
363426
},
@@ -385,6 +448,13 @@ var _ = Describe("Leader election", func() {
385448
BeforeEach(func() {
386449
nodeName = "mynode"
387450
node = &corev1.Node{
451+
TypeMeta: metav1.TypeMeta{
452+
APIVersion: schema.GroupVersion{
453+
Group: corev1.SchemeGroupVersion.Group,
454+
Version: corev1.SchemeGroupVersion.Version,
455+
}.String(),
456+
Kind: "Node",
457+
},
388458
ObjectMeta: metav1.ObjectMeta{
389459
Name: nodeName,
390460
},
@@ -434,6 +504,13 @@ var _ = Describe("Leader election", func() {
434504
)
435505
BeforeEach(func() {
436506
pod = &corev1.Pod{
507+
TypeMeta: metav1.TypeMeta{
508+
APIVersion: schema.GroupVersion{
509+
Group: corev1.SchemeGroupVersion.Group,
510+
Version: corev1.SchemeGroupVersion.Version,
511+
}.String(),
512+
Kind: "Pod",
513+
},
437514
ObjectMeta: metav1.ObjectMeta{
438515
Name: "leader-test",
439516
Namespace: "testns",
@@ -447,6 +524,13 @@ var _ = Describe("Leader election", func() {
447524
},
448525
}
449526
configmap = &corev1.ConfigMap{
527+
TypeMeta: metav1.TypeMeta{
528+
APIVersion: schema.GroupVersion{
529+
Group: corev1.SchemeGroupVersion.Group,
530+
Version: corev1.SchemeGroupVersion.Version,
531+
}.String(),
532+
Kind: "ConfigMap",
533+
},
450534
ObjectMeta: metav1.ObjectMeta{
451535
Name: "leader-test",
452536
Namespace: "testns",
@@ -480,6 +564,5 @@ var _ = Describe("Leader election", func() {
480564
client = fake.NewClientBuilder().WithObjects(pod, configmap).Build()
481565
Expect(deleteLeader(context.TODO(), client, pod, configmap)).To(Succeed())
482566
})
483-
484567
})
485568
})

0 commit comments

Comments
 (0)