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 pod logs check in smokes by always getting a fresh list of pods to find ready pods from #912

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions inttest/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -58,9 +57,9 @@ func WaitForDaemonSet(kc *kubernetes.Clientset, name string) error {
}

// WaitForPod waits for pod be running
func WaitForPod(kc *kubernetes.Clientset, name string) error {
func WaitForPod(kc *kubernetes.Clientset, name, namespace string) error {
return wait.PollImmediate(100*time.Millisecond, 5*time.Minute, func() (done bool, err error) {
ds, err := kc.CoreV1().Pods("kube-system").Get(context.TODO(), name, v1.GetOptions{})
ds, err := kc.CoreV1().Pods(namespace).Get(context.TODO(), name, v1.GetOptions{})
if err != nil {
return false, nil
}
Expand All @@ -69,21 +68,27 @@ func WaitForPod(kc *kubernetes.Clientset, name string) error {
})
}

// WaitForPodLogs picks the first Ready pod from the list of pods and gets the logs of it
func WaitForPodLogs(kc *kubernetes.Clientset, pods []corev1.Pod) error {
var readyPod *corev1.Pod
for _, p := range pods {
if p.Status.Phase == "Running" {
readyPod = &p
}
}
if readyPod == nil {
return fmt.Errorf("could not find ANY pod that is in Ready state")
}
// WaitForPodLogs picks the first Ready pod from the list of pods in given namespace and gets the logs of it
func WaitForPodLogs(kc *kubernetes.Clientset, namespace string) error {
return wait.PollImmediate(100*time.Millisecond, 5*time.Minute, func() (done bool, err error) {
pods, err := kc.CoreV1().Pods(namespace).List(context.TODO(), v1.ListOptions{
Limit: 100,
})
if err != nil {
return false, err // stop polling with error in case the pod listing fails
}
var readyPod *corev1.Pod
for _, p := range pods.Items {
if p.Status.Phase == "Running" {
readyPod = &p
}
}
if readyPod == nil {
return false, nil // do not return the error so we keep on polling
}
_, err = kc.CoreV1().Pods(readyPod.Namespace).GetLogs(readyPod.Name, &corev1.PodLogOptions{Container: readyPod.Spec.Containers[0].Name}).Stream(context.Background())
if err != nil {
return false, nil
return false, nil // do not return the error so we keep on polling
}

return true, nil
Expand Down
7 changes: 3 additions & 4 deletions inttest/customports/customports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ func (ds *Suite) TestControllerJoinsWithCustomPort() {
ds.Require().NoError(err)

podCount := len(pods.Items)
//
ds.T().Logf("found %d pods in kube-system", podCount)
ds.Greater(podCount, 0, "expecting to see few pods in kube-system namespace")
//

ds.T().Log("waiting to see calico pods ready")
ds.Require().NoError(common.WaitForKubeRouterReady(kc), "calico did not start")
ds.T().Log("waiting to see konnectivity-agent pods ready")
ds.Require().NoError(common.WaitForDaemonSet(kc, "konnectivity-agent"), "konnectivity-agent did not start")
ds.Require().NoError(common.WaitForPod(kc, pods.Items[0].Name), "Pod %s did not start", pods.Items[0].Name)

ds.T().Log("waiting to get logs from pods")
ds.Require().NoError(common.WaitForPodLogs(kc, pods.Items))
ds.Require().NoError(common.WaitForPodLogs(kc, "kube-system"))
}