From ebb98abf8fbf92b38b3650267278720da6677385 Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Tue, 31 Oct 2023 20:49:23 +0530 Subject: [PATCH] feat: Add generics method to check status of sts svc (#2) Signed-off-by: chandankumar4 --- testing/fixtures/util.go | 17 +++++++++++------ testing/fixtures/when.go | 14 +++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/testing/fixtures/util.go b/testing/fixtures/util.go index c7eadd5..ac11f30 100644 --- a/testing/fixtures/util.go +++ b/testing/fixtures/util.go @@ -124,8 +124,7 @@ func WaitForISBSvcReady(ctx context.Context, isbSvcClient flowpkg.InterStepBuffe } } -func WaitForISBSvcStatefulSetReady(ctx context.Context, kubeClient kubernetes.Interface, namespace, isbSvcName string, timeout time.Duration) error { - labelSelector := fmt.Sprintf("%s=isbsvc-controller,%s=%s", dfv1.KeyManagedBy, dfv1.KeyISBSvcName, isbSvcName) +func WaitForStatefulSetReady(ctx context.Context, kubeClient kubernetes.Interface, timeout time.Duration, namespace, labelSelector string) error { opts := metav1.ListOptions{LabelSelector: labelSelector} watch, err := kubeClient.AppsV1().StatefulSets(namespace).Watch(ctx, opts) if err != nil { @@ -138,6 +137,11 @@ func WaitForISBSvcStatefulSetReady(ctx context.Context, kubeClient kubernetes.In timeoutCh <- true }() + var ( + replicas int32 + svcName string + ) + statefulSetWatch: for { select { @@ -145,13 +149,15 @@ statefulSetWatch: ss, ok := event.Object.(*appsv1.StatefulSet) if ok { if ss.Status.Replicas == ss.Status.ReadyReplicas { + replicas = ss.Status.Replicas + svcName = ss.Name break statefulSetWatch } } else { return fmt.Errorf("not statefulset") } case <-timeoutCh: - return fmt.Errorf("timeout after %v waiting for ISB svc StatefulSet ready", timeout) + return fmt.Errorf("timeout after %v waiting for %s svc StatefulSet ready", timeout, svcName) } } @@ -169,8 +175,7 @@ statefulSetWatch: podNames := make(map[string]bool) for { - if len(podNames) == 3 { - // defaults to 3 Pods + if len(podNames) == int(replicas) { return nil } select { @@ -194,7 +199,7 @@ statefulSetWatch: return fmt.Errorf("not pod") } case <-podTimeoutCh: - return fmt.Errorf("timeout after %v waiting for ISB svc Pod ready", timeout) + return fmt.Errorf("timeout after %v waiting for %s Pod ready", timeout, svcName) } } } diff --git a/testing/fixtures/when.go b/testing/fixtures/when.go index b383c91..3577b0d 100644 --- a/testing/fixtures/when.go +++ b/testing/fixtures/when.go @@ -159,7 +159,19 @@ func (w *When) WaitForISBSvcReady() *When { if err := WaitForISBSvcReady(ctx, w.isbSvcClient, w.isbSvc.Name, defaultTimeout); err != nil { w.t.Fatal(err) } - if err := WaitForISBSvcStatefulSetReady(ctx, w.kubeClient, Namespace, w.isbSvc.Name, 5*time.Minute); err != nil { + + isbSvclabels := fmt.Sprintf("%s=isbsvc-controller,%s=%s", dfv1.KeyManagedBy, dfv1.KeyISBSvcName, w.isbSvc.Name) + if err := w.WaitForStatefulSetReady(isbSvclabels); err != nil { + w.t.Fatal(err) + } + + return w +} + +func (w *When) WaitForStatefulSetReady(labelSelector string) *When { + w.t.Helper() + ctx := context.Background() + if err := WaitForStatefulSetReady(ctx, w.kubeClient, 5*time.Minute, Namespace, labelSelector); err != nil { w.t.Fatal(err) } return w