From df8994c202c7fe93cf1f9c4af47ec64c74922f22 Mon Sep 17 00:00:00 2001 From: Tomas Nozicka Date: Fri, 8 Mar 2019 15:34:58 +0100 Subject: [PATCH] Fix watches in e2e framework --- test/e2e/apps/deployment.go | 13 +++++++++---- test/e2e/framework/util.go | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/test/e2e/apps/deployment.go b/test/e2e/apps/deployment.go index 59112320572..3df717a6791 100644 --- a/test/e2e/apps/deployment.go +++ b/test/e2e/apps/deployment.go @@ -25,6 +25,8 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/onsi/ginkgo" "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/client-go/tools/cache" appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" @@ -1024,9 +1026,12 @@ func watchRecreateDeployment(c clientset.Interface, d *appsv1.Deployment) error return fmt.Errorf("deployment %q does not use a Recreate strategy: %s", d.Name, d.Spec.Strategy.Type) } - w, err := c.AppsV1().Deployments(d.Namespace).Watch(context.TODO(), metav1.SingleObject(metav1.ObjectMeta{Name: d.Name, ResourceVersion: d.ResourceVersion})) - if err != nil { - return err + fieldSelector := fields.OneTermEqualSelector("metadata.name", d.Name).String() + w := &cache.ListWatch{ + WatchFunc: func(options metav1.ListOptions) (i watch.Interface, e error) { + options.FieldSelector = fieldSelector + return c.AppsV1().Deployments(d.Namespace).Watch(context.TODO(), options) + }, } status := d.Status @@ -1053,7 +1058,7 @@ func watchRecreateDeployment(c clientset.Interface, d *appsv1.Deployment) error ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) defer cancel() - _, err = watchtools.UntilWithoutRetry(ctx, w, condition) + _, err := watchtools.Until(ctx, d.ResourceVersion, w, condition) if err == wait.ErrWaitTimeout { err = fmt.Errorf("deployment %q never completed: %#v", d.Name, status) } diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index 327e4a9f783..f9aeb09e254 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -44,6 +44,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/uuid" @@ -52,6 +53,7 @@ import ( clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" restclient "k8s.io/client-go/rest" + "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" watchtools "k8s.io/client-go/tools/watch" @@ -260,13 +262,20 @@ func WaitForNamespacesDeleted(c clientset.Interface, namespaces []string, timeou } func waitForServiceAccountInNamespace(c clientset.Interface, ns, serviceAccountName string, timeout time.Duration) error { - w, err := c.CoreV1().ServiceAccounts(ns).Watch(context.TODO(), metav1.SingleObject(metav1.ObjectMeta{Name: serviceAccountName})) - if err != nil { - return err + fieldSelector := fields.OneTermEqualSelector("metadata.name", serviceAccountName).String() + lw := &cache.ListWatch{ + ListFunc: func(options metav1.ListOptions) (object runtime.Object, e error) { + options.FieldSelector = fieldSelector + return c.CoreV1().ServiceAccounts(ns).List(context.TODO(), options) + }, + WatchFunc: func(options metav1.ListOptions) (i watch.Interface, e error) { + options.FieldSelector = fieldSelector + return c.CoreV1().ServiceAccounts(ns).Watch(context.TODO(), options) + }, } ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), timeout) defer cancel() - _, err = watchtools.UntilWithoutRetry(ctx, w, serviceAccountHasSecrets) + _, err := watchtools.UntilWithSync(ctx, lw, &v1.ServiceAccount{}, nil, serviceAccountHasSecrets) return err }