Skip to content

Commit

Permalink
Fix StatefulSet and DaemonSet pod owners ids (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao authored Oct 6, 2023
1 parent 90f2350 commit 1f45f17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions kube/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (c *Controller) GetPodOwnerID(pod *corev1.Pod) string {
ref := pod.OwnerReferences[0]

switch ref.Kind {
case "DaemonSet", "StatefulSet":
return string(ref.UID)
case "ReplicaSet":
c.deltasMu.RLock()
defer c.deltasMu.RUnlock()
Expand Down
28 changes: 26 additions & 2 deletions kube/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,21 @@ func TestController(t *testing.T) {
},
}

// Pods with well known owners.
statefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID(uuid.New().String()),
Name: "st1",
},
}

ds := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID(uuid.New().String()),
Name: "ds",
},
}

// Pods with well known 2 level owners.
p1 := createTestPod(testPod{ownerRef: nil})
p2 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: rsWithDeployment.UID, Kind: "ReplicaSet"}})
p3 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: rsWithoutDeployment.UID, Kind: "ReplicaSet"}})
Expand All @@ -195,19 +209,26 @@ func TestController(t *testing.T) {
p6 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: customCrdObjectID, Kind: "ArgoRollout"}})
p7 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: jobManagedByCustomCrd.UID, Kind: "Job"}})

// Pods with daemon set or statefulset.
p8 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: statefulSet.UID, Kind: "StatefulSet"}})
p9 := createTestPod(testPod{ownerRef: &metav1.OwnerReference{UID: ds.UID, Kind: "DaemonSet"}})

clientset := fake.NewSimpleClientset(
rsWithDeployment,
rsWithoutDeployment,
jobManagedByCronjob,
jobManagedByCustomCrd,
standaloneJob,
statefulSet,
p1,
p2,
p3,
p4,
p5,
p6,
p7,
p8,
p9,
)
informersFactory := informers.NewSharedInformerFactory(clientset, 0)

Expand All @@ -228,7 +249,7 @@ func TestController(t *testing.T) {
case err := <-errc:
t.Fatal(err)
case <-time.After(100 * time.Millisecond):
r.Equal(12, testSub.getAddedObjectsCount())
r.Equal(15, testSub.getAddedObjectsCount())
}

r.Equal(string(p1.UID), ctrl.GetPodOwnerID(p1))
Expand All @@ -237,6 +258,8 @@ func TestController(t *testing.T) {
r.Equal(string(jobManagedByCronjob.OwnerReferences[0].UID), ctrl.GetPodOwnerID(p4))
r.Equal(string(p6.UID), ctrl.GetPodOwnerID(p6))
r.Equal(string(jobManagedByCustomCrd.UID), ctrl.GetPodOwnerID(p7))
r.Equal(string(statefulSet.UID), ctrl.GetPodOwnerID(p8))
r.Equal(string(ds.UID), ctrl.GetPodOwnerID(p9))
})
}

Expand Down Expand Up @@ -322,6 +345,7 @@ func (t *testSubscriber) RequiredInformers() []reflect.Type {
reflect.TypeOf(&corev1.Namespace{}),
reflect.TypeOf(&appsv1.DaemonSet{}),
reflect.TypeOf(&appsv1.ReplicaSet{}),
reflect.TypeOf(&appsv1.StatefulSet{}),
reflect.TypeOf(&corev1.Pod{}),
reflect.TypeOf(&corev1.Node{}),
reflect.TypeOf(&batchv1.Job{}),
Expand Down

0 comments on commit 1f45f17

Please sign in to comment.