Skip to content

Commit

Permalink
feat: Add generics method to check status of sts svc (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: chandankumar4 <[email protected]>
  • Loading branch information
chandankumar4 committed Oct 31, 2023
1 parent e2700cf commit ebb98ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 11 additions & 6 deletions testing/fixtures/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -138,20 +137,27 @@ func WaitForISBSvcStatefulSetReady(ctx context.Context, kubeClient kubernetes.In
timeoutCh <- true
}()

var (
replicas int32
svcName string
)

statefulSetWatch:
for {
select {
case event := <-watch.ResultChan():
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)
}
}

Expand All @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion testing/fixtures/when.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebb98ab

Please sign in to comment.