Skip to content

Commit

Permalink
Only select DaemonSet pods that match DaemonSet labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Feb 5, 2025
1 parent 1427fc8 commit 690b60f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/controllers/state/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,24 @@ func (c *Cluster) GetDaemonSetPod(daemonset *appsv1.DaemonSet) *corev1.Pod {

func (c *Cluster) UpdateDaemonSet(ctx context.Context, daemonset *appsv1.DaemonSet) error {
pods := &corev1.PodList{}
err := c.kubeClient.List(ctx, pods, client.InNamespace(daemonset.Namespace))
// Scope down this call to only select the pods in this namespace that specifically match the DaemonSet
opts := []client.ListOption{client.InNamespace(daemonset.Namespace)}
if selector, err := metav1.LabelSelectorAsSelector(daemonset.Spec.Selector); err == nil {
opts = append(opts, client.MatchingLabelsSelector{Selector: selector})
}
err := c.kubeClient.List(ctx, pods, opts...)
if err != nil {
return err
}

sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].CreationTimestamp.Unix() > pods.Items[j].CreationTimestamp.Unix()
})

for i := range pods.Items {
if metav1.IsControlledBy(&pods.Items[i], daemonset) {
c.daemonSetPods.Store(client.ObjectKeyFromObject(daemonset), &pods.Items[i])
break
}
}

return nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/controllers/state/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,12 @@ var _ = Describe("Cluster State Sync", func() {

var _ = Describe("DaemonSet Controller", func() {
It("should not update daemonsetCache when daemonset pod is not present", func() {
labels := map[string]string{"key": "value"}
daemonset := test.DaemonSet(
test.DaemonSetOptions{PodOptions: test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
ResourceRequirements: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1"), corev1.ResourceMemory: resource.MustParse("1Gi")}},
}},
)
Expand All @@ -1387,15 +1391,20 @@ var _ = Describe("DaemonSet Controller", func() {
Expect(daemonsetPod).To(BeNil())
})
It("should update daemonsetCache when daemonset pod is created", func() {
labels := map[string]string{"key": "value"}
daemonset := test.DaemonSet(
test.DaemonSetOptions{PodOptions: test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
ResourceRequirements: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1"), corev1.ResourceMemory: resource.MustParse("1Gi")}},
}},
)
ExpectApplied(ctx, env.Client, daemonset)
daemonsetPod := test.UnschedulablePod(
test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Expand All @@ -1415,15 +1424,20 @@ var _ = Describe("DaemonSet Controller", func() {
Expect(cluster.GetDaemonSetPod(daemonset)).To(Equal(daemonsetPod))
})
It("should update daemonsetCache with the newest created pod", func() {
labels := map[string]string{"key": "value"}
daemonset := test.DaemonSet(
test.DaemonSetOptions{PodOptions: test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
ResourceRequirements: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1"), corev1.ResourceMemory: resource.MustParse("1Gi")}},
}},
)
ExpectApplied(ctx, env.Client, daemonset)
daemonsetPod1 := test.UnschedulablePod(
test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Expand All @@ -1445,6 +1459,7 @@ var _ = Describe("DaemonSet Controller", func() {
daemonsetPod2 := test.UnschedulablePod(
test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Expand All @@ -1464,15 +1479,20 @@ var _ = Describe("DaemonSet Controller", func() {
Expect(cluster.GetDaemonSetPod(daemonset)).To(Equal(daemonsetPod2))
})
It("should delete daemonset in cache when daemonset is deleted", func() {
labels := map[string]string{"key": "value"}
daemonset := test.DaemonSet(
test.DaemonSetOptions{PodOptions: test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
ResourceRequirements: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1"), corev1.ResourceMemory: resource.MustParse("1Gi")}},
}},
)
ExpectApplied(ctx, env.Client, daemonset)
daemonsetPod := test.UnschedulablePod(
test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Expand Down

0 comments on commit 690b60f

Please sign in to comment.