Wait until Rancher components are ready before installing Rancher Monitoring

Signed-off-by: Silvio Moioli <silvio@moioli.net>
This commit is contained in:
Silvio Moioli 2024-08-29 15:58:02 +02:00
parent caf0ae9977
commit 0f64afa0a4
No known key found for this signature in database
2 changed files with 17 additions and 10 deletions

View File

@ -101,7 +101,7 @@ func Deploy(cli *cli.Context) error {
return err
}
// Wait Rancher Deployment to be complete, or subsequent steps may fail
// Wait for Rancher deployments to be complete, or subsequent steps may fail
if err = kubectl.WaitRancher(upstream.Kubeconfig); err != nil {
return err
}
@ -542,12 +542,12 @@ func importDownstreamClusterDo(r *dart.Dart, rancherImageTag string, tf *tofu.To
}
if err := kubectl.WaitForReadyCondition(clusters["upstream"].Kubeconfig,
"clusters.management.cattle.io", clusterID, "", 10); err != nil {
"clusters.management.cattle.io", clusterID, "", "ready", 10); err != nil {
errCh <- fmt.Errorf("%s import failed: %w", clusterName, err)
return
}
if err := kubectl.WaitForReadyCondition(clusters["upstream"].Kubeconfig,
"cluster.fleet.cattle.io", clusterName, "fleet-default", 10); err != nil {
"cluster.fleet.cattle.io", clusterName, "fleet-default", "ready", 10); err != nil {
errCh <- fmt.Errorf("%s import failed: %w", clusterName, err)
return
}

View File

@ -73,21 +73,28 @@ func Apply(kubePath, filePath string) error {
}
func WaitRancher(kubePath string) error {
return Exec(kubePath, log.Writer(), "wait", "deployment/rancher",
"--namespace", "cattle-system",
"--for", "condition=Available=true", "--timeout=1h")
err := WaitForReadyCondition(kubePath, "deployment", "rancher", "cattle-system", "available", 60)
if err != nil {
return err
}
err = WaitForReadyCondition(kubePath, "deployment", "rancher-webhook", "cattle-system", "available", 60)
if err != nil {
return err
}
err = WaitForReadyCondition(kubePath, "deployment", "fleet-controller", "cattle-fleet-system", "available", 60)
return err
}
func WaitForReadyCondition(kubePath, resource, name, namespace string, minutes int) error {
func WaitForReadyCondition(kubePath, resource, name, namespace string, condition string, minutes int) error {
var err error
args := []string{"wait", resource, name}
if len(namespace) > 0 {
args = append(args, "--namespace", namespace)
}
args = append(args, "--for", "condition=ready=true", fmt.Sprintf("--timeout=%dm", minutes))
args = append(args, "--for", fmt.Sprintf("condition=%s=true", condition), fmt.Sprintf("--timeout=%dm", minutes))
maxRetries := 10
maxRetries := minutes * 30
for i := 1; i < maxRetries; i++ {
err = Exec(kubePath, log.Writer(), args...)
if err == nil {
@ -96,7 +103,7 @@ func WaitForReadyCondition(kubePath, resource, name, namespace string, minutes i
// Check if by chance the resource is not yet available
if strings.Contains(err.Error(), fmt.Sprintf("%q not found", name)) {
log.Printf("resource %s/%s not available yet, retry %d/%d\n", namespace, name, i, maxRetries)
time.Sleep(time.Second)
time.Sleep(2 * time.Second)
} else {
return err
}