Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Apr 18, 2024
1 parent 5cf41d4 commit f64de14
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 33 deletions.
12 changes: 1 addition & 11 deletions leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ func Become(ctx context.Context, lockName string, opts ...Option) error {
leaderPod := &corev1.Pod{}
key = crclient.ObjectKey{Namespace: ns, Name: existingOwners[0].Name}
err = config.Client.Get(ctx, key, leaderPod)
leaderPod.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Pod"))
switch {
case apierrors.IsNotFound(err):
log.Info("Leader pod has been deleted, waiting for garbage collection to remove the lock.")
Expand All @@ -200,7 +199,7 @@ func Become(ctx context.Context, lockName string, opts ...Option) error {
log.Info("the status of the node where operator pod with leader lock was running has been 'notReady'")
log.Info("Deleting the leader.")

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

// .Get() clears the APIVersion and Kind,
// so we need to set them before returning the object.
pod.TypeMeta.APIVersion = "v1"
pod.TypeMeta.Kind = "Pod"

log.V(1).Info("Found Pod", "Pod.Namespace", ns, "Pod.Name", pod.Name)

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

Expand Down
127 changes: 105 additions & 22 deletions leader/leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
)

var _ = Describe("Leader election", func() {

Describe("Become", func() {
var (
client crclient.Client
)
var client crclient.Client
BeforeEach(func() {
client = fake.NewClientBuilder().WithObjects(
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand All @@ -49,6 +54,13 @@ var _ = Describe("Leader election", func() {
},
},
&corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand Down Expand Up @@ -84,9 +96,15 @@ var _ = Describe("Leader election", func() {
Expect(Become(context.TODO(), "leader-test", WithClient(client))).To(Succeed())
})
It("should become leader when pod is evicted and rescheduled", func() {

evictedPodStatusClient := fake.NewClientBuilder().WithObjects(
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test-new",
Namespace: "testns",
Expand All @@ -100,6 +118,13 @@ var _ = Describe("Leader election", func() {
},
},
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand All @@ -117,6 +142,13 @@ var _ = Describe("Leader election", func() {
},
},
&corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand Down Expand Up @@ -159,9 +191,15 @@ var _ = Describe("Leader election", func() {
Expect(Become(context.TODO(), "leader-test", WithClient(evictedPodStatusClient))).To(Succeed())
})
It("should become leader when pod is preempted and rescheduled", func() {

preemptedPodStatusClient := fake.NewClientBuilder().WithObjects(
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test-new",
Namespace: "testns",
Expand All @@ -175,6 +213,13 @@ var _ = Describe("Leader election", func() {
},
},
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand All @@ -192,6 +237,13 @@ var _ = Describe("Leader election", func() {
},
},
&corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand Down Expand Up @@ -235,9 +287,7 @@ var _ = Describe("Leader election", func() {
})
})
Describe("isPodEvicted", func() {
var (
leaderPod *corev1.Pod
)
var leaderPod *corev1.Pod
BeforeEach(func() {
leaderPod = &corev1.Pod{}
})
Expand All @@ -260,9 +310,7 @@ var _ = Describe("Leader election", func() {
})
})
Describe("isPodPreempted", func() {
var (
leaderPod *corev1.Pod
)
var leaderPod *corev1.Pod
BeforeEach(func() {
leaderPod = &corev1.Pod{}
})
Expand All @@ -285,12 +333,17 @@ var _ = Describe("Leader election", func() {
})
})
Describe("myOwnerRef", func() {
var (
client crclient.Client
)
var client crclient.Client
BeforeEach(func() {
client = fake.NewClientBuilder().WithObjects(
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "mypod",
Namespace: "testns",
Expand Down Expand Up @@ -318,12 +371,17 @@ var _ = Describe("Leader election", func() {
})
})
Describe("getPod", func() {
var (
client crclient.Client
)
var client crclient.Client
BeforeEach(func() {
client = fake.NewClientBuilder().WithObjects(
&corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "mypod",
Namespace: "testns",
Expand Down Expand Up @@ -352,12 +410,17 @@ var _ = Describe("Leader election", func() {
})

Describe("getNode", func() {
var (
client crclient.Client
)
var client crclient.Client
BeforeEach(func() {
client = fake.NewClientBuilder().WithObjects(
&corev1.Node{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Node",
},
ObjectMeta: metav1.ObjectMeta{
Name: "mynode",
},
Expand Down Expand Up @@ -385,6 +448,13 @@ var _ = Describe("Leader election", func() {
BeforeEach(func() {
nodeName = "mynode"
node = &corev1.Node{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Node",
},
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
Expand Down Expand Up @@ -434,6 +504,13 @@ var _ = Describe("Leader election", func() {
)
BeforeEach(func() {
pod = &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand All @@ -447,6 +524,13 @@ var _ = Describe("Leader election", func() {
},
}
configmap = &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: schema.GroupVersion{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
}.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "leader-test",
Namespace: "testns",
Expand Down Expand Up @@ -480,6 +564,5 @@ var _ = Describe("Leader election", func() {
client = fake.NewClientBuilder().WithObjects(pod, configmap).Build()
Expect(deleteLeader(context.TODO(), client, pod, configmap)).To(Succeed())
})

})
})

0 comments on commit f64de14

Please sign in to comment.