github.com/openshift/installer@v1.4.17/cmd/openshift-install/agent/waitfor.go (about)

     1  package agent
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/openshift/installer/cmd/openshift-install/command"
    12  	agentpkg "github.com/openshift/installer/pkg/agent"
    13  	"github.com/openshift/installer/pkg/asset/agent/workflow"
    14  )
    15  
    16  const (
    17  	exitCodeInstallConfigError = iota + 3
    18  	exitCodeInfrastructureFailed
    19  	exitCodeBootstrapFailed
    20  	exitCodeInstallFailed
    21  )
    22  
    23  // NewWaitForCmd create the commands for waiting the completion of the agent based cluster installation.
    24  func NewWaitForCmd() *cobra.Command {
    25  	cmd := &cobra.Command{
    26  		Use:   "wait-for",
    27  		Short: "Wait for install-time events",
    28  		Args:  cobra.ExactArgs(0),
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			return cmd.Help()
    31  		},
    32  	}
    33  
    34  	cmd.AddCommand(newWaitForBootstrapCompleteCmd())
    35  	cmd.AddCommand(newWaitForInstallCompleteCmd())
    36  	return cmd
    37  }
    38  
    39  func handleBootstrapError(cluster *agentpkg.Cluster, err error) {
    40  	logrus.Debug("Printing the event list gathered from the Agent Rest API")
    41  	cluster.PrintInfraEnvRestAPIEventList()
    42  	err2 := cluster.API.OpenShift.LogClusterOperatorConditions()
    43  	if err2 != nil {
    44  		logrus.Error("Attempted to gather ClusterOperator status after wait failure: ", err2)
    45  	}
    46  	logrus.Info("Use the following commands to gather logs from the cluster")
    47  	logrus.Info("openshift-install gather bootstrap --help")
    48  	logrus.Error(errors.Wrap(err, "Bootstrap failed to complete: "))
    49  	logrus.Exit(exitCodeBootstrapFailed)
    50  }
    51  
    52  func newWaitForBootstrapCompleteCmd() *cobra.Command {
    53  	return &cobra.Command{
    54  		Use:   "bootstrap-complete",
    55  		Short: "Wait until the cluster bootstrap is complete",
    56  		Args:  cobra.ExactArgs(0),
    57  		Run: func(cmd *cobra.Command, args []string) {
    58  			cleanup := command.SetupFileHook(command.RootOpts.Dir)
    59  			defer cleanup()
    60  
    61  			assetDir := cmd.Flags().Lookup("dir").Value.String()
    62  			logrus.Debugf("asset directory: %s", assetDir)
    63  			if len(assetDir) == 0 {
    64  				logrus.Fatal("No cluster installation directory found")
    65  			}
    66  
    67  			kubeconfigPath := filepath.Join(assetDir, "auth", "kubeconfig")
    68  
    69  			rendezvousIP, sshKey, err := agentpkg.FindRendezvouIPAndSSHKeyFromAssetStore(assetDir)
    70  			if err != nil {
    71  				logrus.Fatal(err)
    72  			}
    73  
    74  			ctx := context.Background()
    75  			cluster, err := agentpkg.NewCluster(ctx, assetDir, rendezvousIP, kubeconfigPath, sshKey, workflow.AgentWorkflowTypeInstall)
    76  			if err != nil {
    77  				logrus.Exit(exitCodeBootstrapFailed)
    78  			}
    79  
    80  			if err := agentpkg.WaitForBootstrapComplete(cluster); err != nil {
    81  				handleBootstrapError(cluster, err)
    82  			}
    83  		},
    84  	}
    85  }
    86  
    87  func newWaitForInstallCompleteCmd() *cobra.Command {
    88  	return &cobra.Command{
    89  		Use:   "install-complete",
    90  		Short: "Wait until the cluster installation is complete",
    91  		Args:  cobra.ExactArgs(0),
    92  		Run: func(cmd *cobra.Command, args []string) {
    93  			cleanup := command.SetupFileHook(command.RootOpts.Dir)
    94  			defer cleanup()
    95  
    96  			assetDir := cmd.Flags().Lookup("dir").Value.String()
    97  			logrus.Debugf("asset directory: %s", assetDir)
    98  			if len(assetDir) == 0 {
    99  				logrus.Fatal("No cluster installation directory found")
   100  			}
   101  
   102  			kubeconfigPath := filepath.Join(assetDir, "auth", "kubeconfig")
   103  
   104  			rendezvousIP, sshKey, err := agentpkg.FindRendezvouIPAndSSHKeyFromAssetStore(assetDir)
   105  			if err != nil {
   106  				logrus.Fatal(err)
   107  			}
   108  
   109  			ctx := context.Background()
   110  			cluster, err := agentpkg.NewCluster(ctx, assetDir, rendezvousIP, kubeconfigPath, sshKey, workflow.AgentWorkflowTypeInstall)
   111  			if err != nil {
   112  				logrus.Exit(exitCodeBootstrapFailed)
   113  			}
   114  
   115  			if err := agentpkg.WaitForBootstrapComplete(cluster); err != nil {
   116  				handleBootstrapError(cluster, err)
   117  			}
   118  
   119  			if err = agentpkg.WaitForInstallComplete(cluster); err != nil {
   120  				logrus.Error(err)
   121  				err2 := cluster.API.OpenShift.LogClusterOperatorConditions()
   122  				if err2 != nil {
   123  					logrus.Error("Attempted to gather ClusterOperator status after wait failure: ", err2)
   124  				}
   125  				logrus.Error(`Cluster initialization failed because one or more operators are not functioning properly.
   126  				The cluster should be accessible for troubleshooting as detailed in the documentation linked below,
   127  				https://docs.openshift.com/container-platform/latest/support/troubleshooting/troubleshooting-installations.html`)
   128  				logrus.Exit(exitCodeInstallFailed)
   129  			}
   130  			cluster.PrintInstallationComplete()
   131  		},
   132  	}
   133  }