From 52869656efcf1df6417eb096c25fcd36ed1481ba Mon Sep 17 00:00:00 2001 From: Lucas Rodriguez Date: Wed, 3 Apr 2024 21:02:48 -0500 Subject: [PATCH 1/2] Filter on running pods when finding an image for injector pod --- src/pkg/cluster/injector.go | 15 ++++++--------- src/pkg/k8s/pods.go | 10 ++++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/pkg/cluster/injector.go b/src/pkg/cluster/injector.go index 7403c567aa..4b8ef15c2c 100644 --- a/src/pkg/cluster/injector.go +++ b/src/pkg/cluster/injector.go @@ -23,6 +23,7 @@ import ( "github.com/mholt/archiver/v3" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) @@ -445,24 +446,20 @@ func (c *Cluster) getImagesAndNodesForInjection(timeoutDuration time.Duration) ( // After delay, try running default: - pods, err := c.GetPods(corev1.NamespaceAll) + pods, err := c.GetPods(corev1.NamespaceAll, &metav1.ListOptions{ + FieldSelector: fmt.Sprintf("status.phase=%s", corev1.PodRunning), + }) if err != nil { - return nil, fmt.Errorf("unable to get the list of pods in the cluster") + return nil, fmt.Errorf("unable to get the list of %q pods in the cluster: %w", corev1.PodRunning, err) } findImages: for _, pod := range pods.Items { nodeName := pod.Spec.NodeName - // If this pod doesn't have a node (i.e. is Pending), skip it - if nodeName == "" { - continue - } - nodeDetails, err := c.GetNode(nodeName) - if err != nil { - return nil, fmt.Errorf("unable to get the node %s", pod.Spec.NodeName) + return nil, fmt.Errorf("unable to get the node %q: %w", nodeName, err) } if nodeDetails.Status.Allocatable.Cpu().Cmp(injectorRequestedCPU) < 0 || diff --git a/src/pkg/k8s/pods.go b/src/pkg/k8s/pods.go index 6d0a1e2121..0fa08cb3c8 100644 --- a/src/pkg/k8s/pods.go +++ b/src/pkg/k8s/pods.go @@ -82,13 +82,15 @@ func (k *K8s) CreatePod(pod *corev1.Pod) (*corev1.Pod, error) { // GetAllPods returns a list of pods from the cluster for all namespaces. func (k *K8s) GetAllPods() (*corev1.PodList, error) { - return k.GetPods(corev1.NamespaceAll) + return k.GetPods(corev1.NamespaceAll, nil) } // GetPods returns a list of pods from the cluster by namespace. -func (k *K8s) GetPods(namespace string) (*corev1.PodList, error) { - metaOptions := metav1.ListOptions{} - return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), metaOptions) +func (k *K8s) GetPods(namespace string, listOpts *metav1.ListOptions) (*corev1.PodList, error) { + if listOpts == nil { + listOpts = &metav1.ListOptions{} + } + return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), *listOpts) } // WaitForPodsAndContainers attempts to find pods matching the given selector and optional inclusion filter From d3be1b8f4dd2a5ab8b2dfec36d2e6dcb1fcc4613 Mon Sep 17 00:00:00 2001 From: Lucas Rodriguez Date: Wed, 10 Apr 2024 15:27:07 -0500 Subject: [PATCH 2/2] Use listOpts as a struct instead of pointer --- src/pkg/cluster/injector.go | 2 +- src/pkg/k8s/pods.go | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/pkg/cluster/injector.go b/src/pkg/cluster/injector.go index 4b8ef15c2c..716a260b96 100644 --- a/src/pkg/cluster/injector.go +++ b/src/pkg/cluster/injector.go @@ -446,7 +446,7 @@ func (c *Cluster) getImagesAndNodesForInjection(timeoutDuration time.Duration) ( // After delay, try running default: - pods, err := c.GetPods(corev1.NamespaceAll, &metav1.ListOptions{ + pods, err := c.GetPods(corev1.NamespaceAll, metav1.ListOptions{ FieldSelector: fmt.Sprintf("status.phase=%s", corev1.PodRunning), }) if err != nil { diff --git a/src/pkg/k8s/pods.go b/src/pkg/k8s/pods.go index f344d6a7ab..bf29291587 100644 --- a/src/pkg/k8s/pods.go +++ b/src/pkg/k8s/pods.go @@ -79,15 +79,12 @@ func (k *K8s) CreatePod(pod *corev1.Pod) (*corev1.Pod, error) { // GetAllPods returns a list of pods from the cluster for all namespaces. func (k *K8s) GetAllPods() (*corev1.PodList, error) { - return k.GetPods(corev1.NamespaceAll, nil) + return k.GetPods(corev1.NamespaceAll, metav1.ListOptions{}) } // GetPods returns a list of pods from the cluster by namespace. -func (k *K8s) GetPods(namespace string, listOpts *metav1.ListOptions) (*corev1.PodList, error) { - if listOpts == nil { - listOpts = &metav1.ListOptions{} - } - return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), *listOpts) +func (k *K8s) GetPods(namespace string, listOpts metav1.ListOptions) (*corev1.PodList, error) { + return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), listOpts) } // WaitForPodsAndContainers attempts to find pods matching the given selector and optional inclusion filter