github.com/openshift/installer@v1.4.17/pkg/agent/waitfor.go (about)

     1  package agent
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/sirupsen/logrus"
     9  	"k8s.io/apimachinery/pkg/util/wait"
    10  )
    11  
    12  // WaitForBootstrapComplete Wait for the bootstrap process to complete on
    13  // cluster installations triggered by the agent installer.
    14  func WaitForBootstrapComplete(cluster *Cluster) error {
    15  	timeout := 60 * time.Minute
    16  	waitContext, cancel := context.WithTimeout(cluster.Ctx, timeout)
    17  	defer cancel()
    18  
    19  	var lastErrOnExit error
    20  	var lastErrStr string
    21  	wait.Until(func() {
    22  		bootstrap, exitOnErr, err := cluster.IsBootstrapComplete()
    23  		if bootstrap && err == nil {
    24  			logrus.Info("cluster bootstrap is complete")
    25  			cancel()
    26  		}
    27  
    28  		if err != nil {
    29  			if exitOnErr {
    30  				lastErrOnExit = err
    31  				cancel()
    32  			} else {
    33  				if err.Error() != lastErrStr {
    34  					logrus.Info(err)
    35  					lastErrStr = err.Error()
    36  				}
    37  			}
    38  		}
    39  	}, 2*time.Second, waitContext.Done())
    40  
    41  	waitErr := waitContext.Err()
    42  	if waitErr != nil {
    43  		if errors.Is(waitErr, context.Canceled) && lastErrOnExit != nil {
    44  			return errors.Wrap(lastErrOnExit, "bootstrap process returned error")
    45  		}
    46  		if errors.Is(waitErr, context.DeadlineExceeded) {
    47  			return errors.Wrap(waitErr, "bootstrap process timed out")
    48  		}
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // WaitForInstallComplete Waits for the cluster installation triggered by the
    55  // agent installer to be complete.
    56  func WaitForInstallComplete(cluster *Cluster) error {
    57  	timeout := 90 * time.Minute
    58  	waitContext, cancel := context.WithTimeout(cluster.Ctx, timeout)
    59  	defer cancel()
    60  
    61  	wait.Until(func() {
    62  		installed, err := cluster.IsInstallComplete()
    63  		if installed && err == nil {
    64  			logrus.Info("Cluster is installed")
    65  			cancel()
    66  		}
    67  
    68  	}, 2*time.Second, waitContext.Done())
    69  
    70  	waitErr := waitContext.Err()
    71  	if waitErr != nil && waitErr != context.Canceled {
    72  		return errors.Wrap(waitErr, "Cluster installation timed out")
    73  	}
    74  	return nil
    75  }