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

e2e: lookup pod by ancestry instead of by label #247

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions e2e/internal/kubeclient/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,36 @@ func NewForTest(t *testing.T) *Kubeclient {
}

// PodsFromDeployment returns the pods from a deployment in a namespace.
//
// A pod is considered to belong to a deployment if it is owned by a ReplicaSet which is in turn
// owned by the Deployment in question.
func (c *Kubeclient) PodsFromDeployment(ctx context.Context, namespace, deployment string) ([]v1.Pod, error) {
pods, err := c.client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s", deployment),
})
replicasets, err := c.client.AppsV1().ReplicaSets(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("listing replicasets: %w", err)
}
pods, err := c.client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("listing pods: %w", err)
}

return pods.Items, nil
var out []v1.Pod
for _, replicaset := range replicasets.Items {
for _, ref := range replicaset.OwnerReferences {
if ref.Kind != "Deployment" || ref.Name != deployment {
continue
}
for _, pod := range pods.Items {
for _, ref := range pod.OwnerReferences {
if ref.Kind == "ReplicaSet" && ref.UID == replicaset.UID {
out = append(out, pod)
}
}
}
}
}

return out, nil
}

// Exec executes a process in a pod and returns the stdout and stderr.
Expand Down