cli: add "Connecting" spinner state for "constellation init"

This commit is contained in:
Nils Hanke 2023-03-20 12:42:48 +01:00 committed by Nils Hanke
parent f2ce9518a3
commit 77d19eb896
3 changed files with 22 additions and 16 deletions

View File

@ -65,8 +65,9 @@ func NewInitCmd() *cobra.Command {
}
type initCmd struct {
log debugLog
merger configMerger
log debugLog
merger configMerger
spinner spinnerInterf
}
// runInitialize runs the initialize command.
@ -90,13 +91,13 @@ func runInitialize(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), time.Hour)
defer cancel()
cmd.SetContext(ctx)
i := &initCmd{log: log, merger: &kubeconfigMerger{log: log}}
return i.initialize(cmd, newDialer, fileHandler, license.NewClient(), spinner)
i := &initCmd{log: log, spinner: spinner, merger: &kubeconfigMerger{log: log}}
return i.initialize(cmd, newDialer, fileHandler, license.NewClient())
}
// initialize initializes a Constellation.
func (i *initCmd) initialize(cmd *cobra.Command, newDialer func(validator *cloudcmd.Validator) *dialer.Dialer,
fileHandler file.Handler, quotaChecker license.QuotaChecker, spinner spinnerInterf,
fileHandler file.Handler, quotaChecker license.QuotaChecker,
) error {
flags, err := i.evalFlagArgs(cmd)
if err != nil {
@ -160,7 +161,8 @@ func (i *initCmd) initialize(cmd *cobra.Command, newDialer func(validator *cloud
clusterName := conf.Name + "-" + idFile.UID
i.log.Debugf("Setting cluster name to %s", clusterName)
spinner.Start("Initializing cluster ", false)
cmd.PrintErrln("Note: If you just created the cluster, it can take a few minutes to connect.")
i.spinner.Start("Connecting ", false)
req := &initproto.InitRequest{
KmsUri: masterSecret.EncodeToURI(),
StorageUri: uri.NoStoreURI,
@ -176,7 +178,7 @@ func (i *initCmd) initialize(cmd *cobra.Command, newDialer func(validator *cloud
}
i.log.Debugf("Sending initialization request")
resp, err := i.initCall(cmd.Context(), newDialer(validator), idFile.IP, req)
spinner.Stop()
i.spinner.Stop()
if err != nil {
var nonRetriable *nonRetriableError
if errors.As(err, &nonRetriable) {
@ -201,6 +203,7 @@ func (i *initCmd) initCall(ctx context.Context, dialer grpcDialer, ip string, re
endpoint: net.JoinHostPort(ip, strconv.Itoa(constants.BootstrapperPort)),
req: req,
log: i.log,
spinner: i.spinner,
}
// Create a wrapper function that allows logging any returned error from the retrier before checking if it's the expected retriable one.
@ -224,6 +227,7 @@ type initDoer struct {
req *initproto.InitRequest
resp *initproto.InitResponse
log debugLog
spinner spinnerInterf
}
func (d *initDoer) Do(ctx context.Context) error {
@ -239,8 +243,7 @@ func (d *initDoer) Do(ctx context.Context) error {
grpcStateLogCtx, grpcStateLogCancel := context.WithCancel(ctx)
defer grpcStateLogCancel()
wg.Add(1)
d.logGRPCStateChanges(grpcStateLogCtx, &wg, conn)
d.handleGRPCStateChanges(grpcStateLogCtx, &wg, conn)
protoClient := initproto.NewAPIClient(conn)
d.log.Debugf("Created protoClient")
@ -252,7 +255,8 @@ func (d *initDoer) Do(ctx context.Context) error {
return nil
}
func (d *initDoer) logGRPCStateChanges(ctx context.Context, wg *sync.WaitGroup, conn *grpc.ClientConn) {
func (d *initDoer) handleGRPCStateChanges(ctx context.Context, wg *sync.WaitGroup, conn *grpc.ClientConn) {
wg.Add(1)
go func() {
defer wg.Done()
state := conn.GetState()
@ -262,6 +266,8 @@ func (d *initDoer) logGRPCStateChanges(ctx context.Context, wg *sync.WaitGroup,
}
if state == connectivity.Ready {
d.log.Debugf("Connection ready")
d.spinner.Stop()
d.spinner.Start("Initializing cluster ", false)
} else {
d.log.Debugf("Connection state ended with %s", state)
}

View File

@ -173,8 +173,8 @@ func TestInitialize(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel()
cmd.SetContext(ctx)
i := &initCmd{log: logger.NewTest(t)}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, &nopSpinner{})
i := &initCmd{log: logger.NewTest(t), spinner: &nopSpinner{}}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{})
if tc.wantErr {
assert.Error(err)
@ -452,8 +452,8 @@ func TestAttestation(t *testing.T) {
defer cancel()
cmd.SetContext(ctx)
i := &initCmd{log: logger.NewTest(t)}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, &nopSpinner{})
i := &initCmd{log: logger.NewTest(t), spinner: &nopSpinner{}}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{})
assert.Error(err)
// make sure the error is actually a TLS handshake error
assert.Contains(err.Error(), "transport: authentication handshake failed")

View File

@ -265,8 +265,8 @@ func (m *miniUpCmd) initializeMiniCluster(cmd *cobra.Command, fileHandler file.H
}
m.log.Debugf("Created new logger")
defer log.Sync()
i := &initCmd{log: log, merger: &kubeconfigMerger{log: log}}
if err := i.initialize(cmd, newDialer, fileHandler, license.NewClient(), spinner); err != nil {
i := &initCmd{log: log, merger: &kubeconfigMerger{log: log}, spinner: spinner}
if err := i.initialize(cmd, newDialer, fileHandler, license.NewClient()); err != nil {
return err
}
m.log.Debugf("Initialized mini cluster")