This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add waitfor logic to test valid restconfig
Signed-off-by: Darren Shepherd <[email protected]>
- Loading branch information
1 parent
c4f82d7
commit 384c159
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |