From ac417072668c56b2836ac1df0d91a0c3853ab090 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Sat, 7 Aug 2021 20:38:28 +0200 Subject: [PATCH] test(e2e): fixing flakiness for Service and EP metadata --- e2e/service_metadata_test.go | 64 +++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/e2e/service_metadata_test.go b/e2e/service_metadata_test.go index c35c3a5b9..c10a2d289 100644 --- a/e2e/service_metadata_test.go +++ b/e2e/service_metadata_test.go @@ -17,11 +17,48 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" ) var _ = Describe("adding metadata to Service objects", func() { + // Mocking EndpointSlice controller Cluster-Role in case it's missing + cr := &rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{ + Name: "system:controller:endpointslice-controller", + }, + } + + _, err := controllerutil.CreateOrUpdate(context.Background(), k8sClient, cr, func() error { + cr.Rules = []rbacv1.PolicyRule{ + { + Verbs: []string{"get", "list", "watch"}, + APIGroups: []string{""}, + Resources: []string{"nodes", "pod", "services"}, + }, + { + Verbs: []string{"update"}, + APIGroups: []string{""}, + Resources: []string{"services/finalizers"}, + }, + { + Verbs: []string{"create", "delete", "get", "list", "update"}, + APIGroups: []string{"discovery.k8s.io"}, + Resources: []string{"endpointslices"}, + }, + { + Verbs: []string{"create", "patch", "update"}, + APIGroups: []string{"", "events.k8s.io"}, + Resources: []string{"events"}, + }, + } + + return nil + }) + + Expect(err).Should(Succeed()) + tnt := &capsulev1beta1.Tenant{ ObjectMeta: metav1.ObjectMeta{ Name: "service-metadata", @@ -93,8 +130,11 @@ var _ = Describe("adding metadata to Service objects", func() { }, }, } - EventuallyCreation(func() error { - return k8sClient.Create(context.TODO(), svc) + + EventuallyCreation(func() (err error) { + _, err = ownerClient(tnt.Spec.Owners[0]).CoreV1().Services(ns.GetName()).Create(context.Background(), svc, metav1.CreateOptions{}) + + return }).Should(Succeed()) By("checking additional labels", func() { @@ -109,6 +149,7 @@ var _ = Describe("adding metadata to Service objects", func() { return true }, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue()) }) + By("checking additional annotations", func() { Eventually(func() (ok bool) { Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: svc.GetName(), Namespace: ns.GetName()}, svc)).Should(Succeed()) @@ -149,9 +190,13 @@ var _ = Describe("adding metadata to Service objects", func() { }, }, } - EventuallyCreation(func() error { - return k8sClient.Create(context.TODO(), ep) + + EventuallyCreation(func() (err error) { + _, err = ownerClient(tnt.Spec.Owners[0]).CoreV1().Endpoints(ns.GetName()).Create(context.Background(), ep, metav1.CreateOptions{}) + + return }).Should(Succeed()) + By("checking additional labels", func() { Eventually(func() (ok bool) { Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ep.GetName(), Namespace: ns.GetName()}, ep)).Should(Succeed()) @@ -164,6 +209,7 @@ var _ = Describe("adding metadata to Service objects", func() { return true }, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue()) }) + By("checking additional annotations", func() { Eventually(func() (ok bool) { Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ep.GetName(), Namespace: ns.GetName()}, ep)).Should(Succeed()) @@ -179,8 +225,7 @@ var _ = Describe("adding metadata to Service objects", func() { }) It("should apply them to EndpointSlice", func() { - maj, min, v := GetKubernetesSemVer() - if maj == 1 && min <= 16 { + if maj, min, v := GetKubernetesSemVer(); maj == 1 && min <= 16 { Skip("Running test on Kubernetes " + v + ", doesn't provide EndpointSlice resource") } @@ -207,8 +252,10 @@ var _ = Describe("adding metadata to Service objects", func() { }, } - EventuallyCreation(func() error { - return k8sClient.Create(context.TODO(), eps) + EventuallyCreation(func() (err error) { + _, err = ownerClient(tnt.Spec.Owners[0]).DiscoveryV1beta1().EndpointSlices(ns.GetName()).Create(context.Background(), eps, metav1.CreateOptions{}) + + return }).Should(Succeed()) By("checking additional annotations EndpointSlice", func() { @@ -223,6 +270,7 @@ var _ = Describe("adding metadata to Service objects", func() { return true }, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue()) }) + By("checking additional labels on EndpointSlice", func() { Eventually(func() (ok bool) { Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: eps.GetName(), Namespace: ns.GetName()}, eps)).Should(Succeed())