Skip to content

Commit

Permalink
Merge pull request #912 from jnummelin/fix-pod-logs-in-smokes
Browse files Browse the repository at this point in the history
Fix pod logs check in smokes by always getting a fresh list of pods to find ready pods from
  • Loading branch information
ncopa committed May 20, 2021
2 parents 65f45b5 + 3b84aa8 commit 9f3ae39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
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"))
}

0 comments on commit 9f3ae39

Please sign in to comment.