Skip to content

Commit

Permalink
perf: Don't DeepCopy DaemonSet Pods (kubernetes-sigs#1968)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Feb 6, 2025
1 parent b2e14fe commit 204531a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions pkg/controllers/state/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package state
import (
"context"
"fmt"
"sort"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -478,22 +477,22 @@ 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))
if err != nil {
// Scope down this call to only select the pods in this namespace that specifically match the DaemonSet
// Because we get so many pods from this response, we are not DeepCopying the cached data here
// DO NOT MUTATE pods in this function as this will affect the underlying cached pod
if err := c.kubeClient.List(ctx, pods, client.InNamespace(daemonset.Namespace), client.UnsafeDisableDeepCopy); 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
if len(pods.Items) == 0 {
return nil
}
pod := &pods.Items[0]
for _, p := range pods.Items {
if metav1.IsControlledBy(&p, daemonset) && p.CreationTimestamp.After(pod.CreationTimestamp.Time) {
pod = &p
}
}

c.daemonSetPods.Store(client.ObjectKeyFromObject(daemonset), pod.DeepCopy())
return nil
}

Expand Down

0 comments on commit 204531a

Please sign in to comment.