Skip to content

Commit

Permalink
controllers: use apireader to not assert on cached information
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi authored and k8s-infra-cherrypick-robot committed Feb 19, 2024
1 parent 4ab4d31 commit 6019513
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions controllers/serviceaccount_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func createTargetSecretWithInvalidToken(ctx context.Context, guestClient client.
Expect(guestClient.Create(ctx, secret)).To(Succeed())
}

func assertEventuallyExistsInNamespace(ctx context.Context, c client.Client, namespace, name string, obj client.Object) {
func assertEventuallyExistsInNamespace(ctx context.Context, c client.Reader, namespace, name string, obj client.Object) {
EventuallyWithOffset(2, func() error {
key := client.ObjectKey{Namespace: namespace, Name: name}
return c.Get(ctx, key, obj)
}).Should(Succeed())
}, time.Second*3).Should(Succeed())
}

func assertNoEntities(ctx context.Context, ctrlClient client.Client, namespace string) {
Expand Down
8 changes: 4 additions & 4 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ var _ = Describe("Service Discovery controller integration tests", func() {
It("Should reconcile headless svc", func() {
By("creating a service and endpoints using the VIP in the guest cluster")
headlessSvc := &corev1.Service{}
assertEventuallyExistsInNamespace(ctx, intCtx.Client, "kube-system", "kube-apiserver-lb-svc", headlessSvc)
assertHeadlessSvcWithVIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
assertEventuallyExistsInNamespace(ctx, testEnv.Manager.GetAPIReader(), "kube-system", "kube-apiserver-lb-svc", headlessSvc)
assertHeadlessSvcWithVIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
})
})

Expand All @@ -67,7 +67,7 @@ var _ = Describe("Service Discovery controller integration tests", func() {
})
It("Should reconcile headless svc", func() {
By("creating a service and endpoints using the FIP in the guest cluster")
assertHeadlessSvcWithFIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
assertHeadlessSvcWithFIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
})
})
Context("When headless svc and endpoints already exists", func() {
Expand All @@ -86,7 +86,7 @@ var _ = Describe("Service Discovery controller integration tests", func() {
})
It("Should reconcile headless svc", func() {
By("updating the service and endpoints using the VIP in the guest cluster")
assertHeadlessSvcWithUpdatedVIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
assertHeadlessSvcWithUpdatedVIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName)
})
})
})
7 changes: 4 additions & 3 deletions controllers/servicediscovery_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"strconv"
"strings"
"time"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,10 +74,10 @@ func assertEventuallyDoesNotExistInNamespace(ctx context.Context, guestClient cl

func assertHeadlessSvc(ctx context.Context, guestClient client.Client, namespace, name string) {
headlessSvc := &corev1.Service{}
EventuallyWithOffset(4, func() error {
Eventually(func() error {
key := client.ObjectKey{Namespace: namespace, Name: name}
return guestClient.Get(ctx, key, headlessSvc)
}).Should(Succeed())
}, time.Second*3).Should(Succeed())
Expect(headlessSvc.Spec.Ports[0].Port).To(Equal(int32(supervisorHeadlessSvcPort)))
Expect(headlessSvc.Spec.Ports[0].TargetPort.IntVal).To(Equal(int32(supervisorAPIServerPort)))
}
Expand Down Expand Up @@ -137,7 +138,7 @@ func assertHeadlessSvcWithUpdatedVIPEndpoints(ctx context.Context, guestClient c
assertHeadlessSvc(ctx, guestClient, namespace, name)
headlessEndpoints := &corev1.Endpoints{}
assertEventuallyExistsInNamespace(ctx, guestClient, namespace, name, headlessEndpoints)
EventuallyWithOffset(2, func() string {
Eventually(func() string {
key := client.ObjectKey{Namespace: namespace, Name: name}
Expect(guestClient.Get(ctx, key, headlessEndpoints)).Should(Succeed())
return headlessEndpoints.Subsets[0].Addresses[0].IP
Expand Down
18 changes: 13 additions & 5 deletions internal/test/helpers/vmware/intg_test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
type IntegrationTestContext struct {
Client client.Client
GuestClient client.Client
GuestAPIReader client.Client
Namespace string
VSphereCluster *vmwarev1.VSphereCluster
Cluster *clusterv1.Cluster
Expand Down Expand Up @@ -101,11 +102,6 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
vsphereClusterName := capiutil.RandomString(6)
testCtx.Cluster = createCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName)

By("Create a vsphere cluster and wait for it to exist", func() {
testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName())
testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster)
})

var config *rest.Config

By("Creating guest cluster control plane", func() {
Expand All @@ -121,6 +117,13 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
Expect(err).ShouldNot(HaveOccurred())
Expect(testCtx.GuestClient).ShouldNot(BeNil())

// Create the API Reader, a client with no cache.
testCtx.GuestAPIReader, err = client.New(config, client.Options{
Scheme: testCtx.GuestClient.Scheme(),
Mapper: testCtx.GuestClient.RESTMapper(),
})
Expect(err).ShouldNot(HaveOccurred())

testCtx.envTest = envTest
})
By("Create the kubeconfig secret for the cluster", func() {
Expand Down Expand Up @@ -149,6 +152,11 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
}).Should(Succeed())
})

By("Create a vsphere cluster and wait for it to exist", func() {
testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName())
testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster)
})

return testCtx
}

Expand Down

0 comments on commit 6019513

Please sign in to comment.