Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add waitfor logic to test valid restconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <darren@acorn.io>
  • Loading branch information
ibuildthecloud committed Jan 11, 2024
1 parent c4f82d7 commit 384c159
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/restconfig/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package restconfig

import (
"context"
"errors"
"fmt"
"time"

"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
)

func WaitFor(ctx context.Context, cfg *rest.Config) error {
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()

start := time.Now()
log := logrus.WithField("server", cfg.Host)
log.Info("Waiting for Kubernetes API to be ready")

cli, err := rest.UnversionedRESTClientFor(cfg)
if err != nil {
return err
}

for {
select {
case <-ctx.Done():
// Return close error along with the last ready error, if any
return errors.Join(fmt.Errorf("context closed before %q was ready: %w", cfg.Host, ctx.Err()), err)
default:
}

resp := cli.Get().AbsPath("/readyz").Do(ctx)
log = log.WithField("elapsed", time.Since(start))
if err = resp.Error(); err == nil {
log.Info("Kubernetes API ready")
break
}

log.WithError(err).Debug("Kubernetes API not ready")
time.Sleep(2 * time.Second)
}
return nil
}

0 comments on commit 384c159

Please sign in to comment.