Skip to content

Commit

Permalink
Leverage the type system for drier code
Browse files Browse the repository at this point in the history
  • Loading branch information
mociarain committed Jan 30, 2024
1 parent be3cacb commit 5ba1557
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions test/e2e/adminapi_csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
)

Expand Down
16 changes: 9 additions & 7 deletions test/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5ba1557

Please sign in to comment.