From 4b599ad1e810897494f818047a19e6dc67be4d44 Mon Sep 17 00:00:00 2001 From: Lucas Rodriguez Date: Sun, 14 Apr 2024 23:54:27 -0500 Subject: [PATCH] fix: filter on running pods when finding an image for injector pod (#2415) ## Description filter on running pods when finding an image for injector pod https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase Description of the `Running` pod phase: > The Pod has been bound to a node, and all of the containers have been created. At least one container is still running, or is in the process of starting or restarting. ## Related Issue Fixes #2356 Fixes #2410 ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> Co-authored-by: razzle --- src/pkg/cluster/injector.go | 15 ++++++--------- src/pkg/k8s/pods.go | 7 +++---- 2 files changed, 9 insertions(+), 13 deletions(-) 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