diff --git a/framework/pod.go b/framework/pod.go index fadd5afe..a883ce64 100644 --- a/framework/pod.go +++ b/framework/pod.go @@ -9,10 +9,9 @@ import ( "github.com/spidernet-io/e2eframework/tools" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" api_errors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/fields" - "k8s.io/apimachinery/pkg/watch" "k8s.io/kubectl/pkg/util/podutils" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -94,55 +93,33 @@ func (f *Framework) GetPodList(opts ...client.ListOption) (*corev1.PodList, erro } func (f *Framework) WaitPodStarted(name, namespace string, ctx context.Context) (*corev1.Pod, error) { - - if name == "" || namespace == "" { - return nil, ErrWrongInput - } - - // refer to https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/watch_test.go - l := &client.ListOptions{ - Namespace: namespace, - FieldSelector: fields.OneTermEqualSelector("metadata.name", name), - } - watchInterface, err := f.KClient.Watch(ctx, &corev1.PodList{}, l) - if err != nil { - return nil, ErrWatch - } - defer watchInterface.Stop() - + var pod corev1.Pod for { select { - // if pod not exist , got no event - case event, ok := <-watchInterface.ResultChan(): - if !ok { - return nil, ErrChanelClosed - } - f.Log("pod %v/%v %v event \n", namespace, name, event.Type) - // Added EventType = "ADDED" - // Modified EventType = "MODIFIED" - // Deleted EventType = "DELETED" - // Bookmark EventType = "BOOKMARK" - // Error EventType = "ERROR" - switch event.Type { - case watch.Error: - return nil, fmt.Errorf("received error event: %+v", event) - case watch.Deleted: - return nil, fmt.Errorf("resource is deleted") - default: - pod, ok := event.Object.(*corev1.Pod) - // metaObject, ok := event.Object.(metav1.Object) - if !ok { - return nil, fmt.Errorf("failed to get metaObject") + case <-ctx.Done(): + f.Log("pod %s/%s is still in phase %s \n", namespace, name, pod.Status.Phase) + podEvents, err := f.GetEvents(ctx, "Pod", name, namespace) + if nil == err { + for _, item := range podEvents.Items { + f.Log("pod %s/%s events: %s\n", namespace, name, item.String()) } - f.Log("pod %v/%v status=%+v\n", namespace, name, pod.Status.Phase) - if pod.Status.Phase == corev1.PodPending || pod.Status.Phase == corev1.PodUnknown { - break - } else { - return pod, nil + } else { + f.Log("failed to get pod %s/%s events, error: %v \n", namespace, name, err) + } + return nil, fmt.Errorf("time out to wait pod %s/%s running", namespace, name) + default: + pod, err := f.GetPod(name, namespace) + if nil != err { + if errors.IsNotFound(err) { + time.Sleep(time.Second) + continue } + return nil, err } - case <-ctx.Done(): - return nil, ErrTimeOut + if pod.Status.Phase == corev1.PodRunning { + return pod, nil + } + time.Sleep(time.Second) } } } diff --git a/framework/pod_test.go b/framework/pod_test.go index 770c7fdb..6a090c36 100644 --- a/framework/pod_test.go +++ b/framework/pod_test.go @@ -67,7 +67,7 @@ var _ = Describe("test pod", Label("pod"), func() { // in the real environment, this issue does not exist time.Sleep(2 * time.Second) // generate pod yaml - pod := generateExamplePodYaml(podName, namespace, label, "") + pod := generateExamplePodYaml(podName, namespace, label, "Running") // create pod e := f.CreatePod(pod)