Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter on running pods when finding an image for injector pod #2415

Merged
merged 10 commits into from
Apr 15, 2024
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
10 changes: 6 additions & 4 deletions src/pkg/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,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)
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
}

// WaitForPodsAndContainers attempts to find pods matching the given selector and optional inclusion filter
Expand Down
Loading