From 5ba1557d0caf52bfe2e027709346b5cde2149049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maiti=C3=BA=20=C3=93=20Ciar=C3=A1in?= Date: Tue, 30 Jan 2024 11:00:27 +0100 Subject: [PATCH] Leverage the type system for drier code --- test/e2e/adminapi_csr.go | 9 ++++++--- test/e2e/helpers.go | 16 +++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/test/e2e/adminapi_csr.go b/test/e2e/adminapi_csr.go index 6161b51fcb7..4cd0c40eb63 100644 --- a/test/e2e/adminapi_csr.go +++ b/test/e2e/adminapi_csr.go @@ -70,8 +70,11 @@ func testCSRApproveOK(ctx context.Context, objName, namespace string) { Expect(resp.StatusCode).To(Equal(http.StatusOK)) By("checking that the CSR was approved via Kubernetes API") - testcsr := GetK8sObjectWithRetry[*certificatesv1.CertificateSigningRequest]( - ctx, clients.Kubernetes.CertificatesV1().CertificateSigningRequests().Get, objName, metav1.GetOptions{}, + testcsr, _ := PerformK8sCallWithRetry( + ctx, + clients.Kubernetes.CertificatesV1().CertificateSigningRequests().Get, + objName, + metav1.GetOptions{}, ) approved := false @@ -94,7 +97,7 @@ func testCSRMassApproveOK(ctx context.Context, namePrefix, namespace string, csr By("checking that all CSRs were approved via Kubernetes API") for i := 1; i < csrCount; i++ { - testcsr := GetK8sObjectWithRetry[*certificatesv1.CertificateSigningRequest]( + testcsr, _ := PerformK8sCallWithRetry( ctx, clients.Kubernetes.CertificatesV1().CertificateSigningRequests().Get, namePrefix+strconv.Itoa(i), metav1.GetOptions{}, ) diff --git a/test/e2e/helpers.go b/test/e2e/helpers.go index ce895e3c2cd..70660b6fb7f 100644 --- a/test/e2e/helpers.go +++ b/test/e2e/helpers.go @@ -17,18 +17,20 @@ var ( PollingInterval = 250 * time.Millisecond ) -type K8sGetFunc[T any] func(ctx context.Context, name string, getOptions metav1.GetOptions) (T, error) +type allowedK8sOptions interface { + metav1.CreateOptions | metav1.DeleteOptions | metav1.GetOptions +} + +type K8sFunc[T any, U allowedK8sOptions] func(ctx context.Context, name string, options U) (T, error) -// This function takes a get function like clients.Kubernetes.CertificatesV1().CertificateSigningRequests().Get +// This function takes a k8s function like clients.Kubernetes.CertificatesV1().CertificateSigningRequests().Get // and the parameters for it and returns the result after asserting there were no errors. // // By default the call is retried for 5s with a 250ms interval. -func GetK8sObjectWithRetry[T any](ctx context.Context, get K8sGetFunc[T], name string, getOptions metav1.GetOptions) T { - var object T +func PerformK8sCallWithRetry[T any, U allowedK8sOptions](ctx context.Context, k8sFunction K8sFunc[T, U], name string, options U) (result T, err error) { Eventually(func(g Gomega, ctx context.Context) { - result, err := get(ctx, name, metav1.GetOptions{}) + result, err = k8sFunction(ctx, name, options) g.Expect(err).NotTo(HaveOccurred()) - object = result }).WithContext(ctx).WithTimeout(DefaultTimeout).WithPolling(PollingInterval).Should(Succeed()) - return object + return }