diff --git a/src/pkg/cluster/injector.go b/src/pkg/cluster/injector.go index 7403c567aa..716a260b96 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 5e9b52d4ea..bf29291587 100644 --- a/src/pkg/k8s/pods.go +++ b/src/pkg/k8s/pods.go @@ -79,13 +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) + return k.GetPods(corev1.NamespaceAll, metav1.ListOptions{}) } // 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) { + return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), listOpts) } // WaitForPodsAndContainers attempts to find pods matching the given selector and optional inclusion filter