Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion test/e2e/pod/test_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var _ = ginkgo.Describe("Pod E2E Tests", ginkgo.Ordered, func() {
})

ginkgo.AfterEach(func() {
if ginkgo.CurrentSpecReport().Failed() {
ginkgo.By("Check pod detailed after each test")
utils.CheckPodDetails(clientSet)
}
ginkgo.By("Cleanup pod after each test")
cleanupPod(newPod, clientSet)
})
Expand Down Expand Up @@ -170,7 +174,6 @@ func checkPodPendingDueToFiltering(clientSet *kubernetes.Clientset, pod *corev1.
gomega.Expect(err).NotTo(gomega.HaveOccurred())

for _, event := range events {
fmt.Printf("Event: Reason=%s, Message=%s\n", event.Reason, event.Message)
if strings.Contains(event.Reason, utils.ErrReasonFilteringFailed) &&
strings.Contains(event.Message, utils.ErrMessageFilteringFailed) {
return true
Expand Down
85 changes: 82 additions & 3 deletions test/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package utils

import (
"bytes"
"context"
"fmt"
"io"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -85,11 +87,11 @@ func DeletePod(clientSet *kubernetes.Clientset, namespace, podName string) error

func WaitForPodRunning(clientSet kubernetes.Interface, namespace, podName string) error {
const (
checkInterval = 5 * time.Second // Interval for checking Pod status
timeout = 5 * time.Minute // Increased timeout for GPU Pods
checkInterval = 30 * time.Second // Interval for checking Pod status
timeout = 5 * time.Minute // Increased timeout for GPU Pods
)

return wait.PollUntilContextTimeout(context.TODO(), checkInterval, timeout, true, func(context.Context) (bool, error) {
return wait.PollUntilContextTimeout(context.TODO(), checkInterval, timeout, true, func(ctx context.Context) (bool, error) {
// Fetch the Pod object from the Kubernetes API
pod, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -123,3 +125,80 @@ func WaitForPodRunning(clientSet kubernetes.Interface, namespace, podName string
return false, nil
})
}

Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Add a doc comment to describe the purpose and behavior of this public function for better maintainability.

Suggested change
// GetNamespaceList retrieves a list of all namespaces in the Kubernetes cluster.
// It takes a Kubernetes clientset as input and returns a slice of namespace names
// or an error if the operation fails.

Copilot uses AI. Check for mistakes.

func GetNamespaceList(clientSet *kubernetes.Clientset) ([]string, error) {
namespaces, err := clientSet.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
klog.Errorf("Failed to list namespaces: %v", err)
return nil, err
}

var nsList []string
for _, ns := range namespaces.Items {
nsList = append(nsList, ns.Name)
}

return nsList, err
}

func GetPodLogs(clientSet *kubernetes.Clientset, namespace, podName string) (string, error) {
req := clientSet.CoreV1().Pods(namespace).GetLogs(podName, &corev1.PodLogOptions{})
podLogs, err := req.Stream(context.TODO())
if err != nil {
return "", err
}
defer podLogs.Close()
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, podLogs); err != nil {
return "", err
}
return buf.String(), nil
}

func CheckPodDetails(clientSet *kubernetes.Clientset) {
namespaces, err := GetNamespaceList(clientSet)
if err != nil {
klog.Errorf("Failed to get namespaces: %v", err)
return
}

for _, ns := range namespaces {
pods, err := GetPods(clientSet, ns)
if err != nil {
klog.Errorf("Failed to get pods in namespace %s: %v", ns, err)
continue
}

for _, pod := range pods.Items {
status := pod.Status.Phase

if status == corev1.PodRunning || status == corev1.PodSucceeded {
continue
}

klog.Infof("Pod %s/%s is in %s status", ns, pod.Name, status)

klog.Infof("Show events for %s/%s:", ns, pod.Name)
events, err := GetPodEvents(clientSet, ns, pod.Name)
if err != nil {
klog.Errorf("Failed to get events for %s/%s: %v", ns, pod.Name, err)
return
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning here stops logging details for other pods. Consider using continue to proceed to the next pod and log all failures.

Suggested change
return
continue

Copilot uses AI. Check for mistakes.

}

if len(events) > 0 {
for _, event := range events {
klog.Infof("Reason: %s, Message: %s \n", event.Reason, event.Message)
}
}

logs, err := GetPodLogs(clientSet, ns, pod.Name)
if err != nil {
klog.Errorf("Failed to get logs for %s/%s: %v", ns, pod.Name, err)
return
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with events, use continue instead of return so that other pods are still checked and logged.

Suggested change
return
continue

Copilot uses AI. Check for mistakes.

}

klog.Infof("Show logs for %s/%s:", ns, pod.Name)
klog.Infof(logs)
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Passing raw logs to Infof can misinterpret formatting verbs—use klog.Info(logs) or klog.Infof("%s", logs) instead.

Suggested change
klog.Infof(logs)
klog.Infof("%s", logs)

Copilot uses AI. Check for mistakes.

}
}
}