Skip to content

Commit

Permalink
fix: filter on running pods when finding an image for injector pod (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
Co-authored-by: razzle <[email protected]>
  • Loading branch information
3 people authored Apr 15, 2024
1 parent f9dea9e commit 4b599ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 ||
Expand Down
7 changes: 3 additions & 4 deletions src/pkg/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b599ad

Please sign in to comment.