Skip to content

Commit

Permalink
Changes from PR comments
Browse files Browse the repository at this point in the history
* Use tabwriter to log preflight check results
* DRY code
  • Loading branch information
banjoh committed Sep 26, 2023
1 parent 2924058 commit c1f3cfe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
8 changes: 5 additions & 3 deletions cmd/kots/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,14 @@ func InstallCmd() *cobra.Command {
case storetypes.VersionPendingPreflight:
log.ActionWithSpinner("Waiting for preflight checks to complete")
if err := ValidatePreflightStatus(deployOptions, authSlug, apiEndpoint); err != nil {
log.FinishSpinnerWithError()
log.Errorf("Preflight checks contain warnings or errors. The application was not deployed")
perr := preflightError{}
if errors.As(err, &perr) {
log.FinishSpinner() // We succeeded waiting for the results. Don't finish with an error
log.Errorf("Preflight checks contain warnings or errors. The application was not deployed")
print.PreflightErrors(log, perr.Results)
cmd.SilenceErrors = true // Stop Cobra from printing the error, we print it ourselves
cmd.SilenceErrors = true // Stop Cobra from printing the error, we format the message ourselves
} else {
log.FinishSpinnerWithError()
}
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/preflight/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func execute(appID string, sequence int64, preflightSpec *troubleshootv1beta2.Pr
logger.Errorf("error while running preflights: %v", err)
} else {
switch m := msg.(type) {
case preflight.CollectProgress:
logger.Infof("preflight progress: %s", m.String())
default:
logger.Infof("preflight progress: %+v", msg)
case preflight.CollectProgress:
logger.Infof("preflight progress: %s", m.String())
default:
logger.Infof("preflight progress: %+v", msg)
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
if result == nil {
continue
}
logger.Infof("preflight state=%s title=%q message=%q", preflightState(*result), result.Title, result.Message)
logger.Infof("preflight state=%s title=%q message=%q", GetPreflightCheckState(result), result.Title, result.Message)
}
} else {
logger.Info("preflight checks completed")
Expand Down Expand Up @@ -229,7 +229,12 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
return nil
}

func preflightState(p troubleshootpreflight.UploadPreflightResult) string {
// GetPreflightCheckState returns the state of a single preflight check result
func GetPreflightCheckState(p *troubleshootpreflight.UploadPreflightResult) string {
if p == nil {
return "unknown"
}

if p.IsFail {
return "fail"
}
Expand Down Expand Up @@ -275,6 +280,10 @@ func maybeDeployFirstVersion(appID string, sequence int64, preflightResults *typ
return true, nil
}

// GetPreflightState returns a single state based on checking all
// preflight checks results. If there are any errors, the state is fail.
// If there are no errors and any warnings, the state is warn.
// Otherwise, the state is pass.
func GetPreflightState(preflightResults *types.PreflightResults) string {
if len(preflightResults.Errors) > 0 {
return "fail"
Expand Down
31 changes: 11 additions & 20 deletions pkg/print/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

configtypes "github.com/replicatedhq/kots/pkg/kotsadmconfig/types"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/troubleshoot/pkg/preflight"
"github.com/replicatedhq/kots/pkg/preflight"
tsPreflight "github.com/replicatedhq/troubleshoot/pkg/preflight"
)

func ConfigValidationErrors(log *logger.CLILogger, groupValidationErrors []configtypes.ConfigGroupValidationError) {
Expand All @@ -28,25 +29,15 @@ func ConfigValidationErrors(log *logger.CLILogger, groupValidationErrors []confi
log.Errorf(sb.String())
}

func PreflightErrors(log *logger.CLILogger, results []*preflight.UploadPreflightResult) {
var s strings.Builder
s.WriteString("\nPreflight check results (state - title - message)")
for _, result := range results {
s.WriteString(fmt.Sprintf("\n%s - %q - %q", preflightState(result), result.Title, result.Message))
}
log.Info(s.String())
}
func PreflightErrors(log *logger.CLILogger, results []*tsPreflight.UploadPreflightResult) {
w := NewTabWriter()
defer w.Flush()

func preflightState(r *preflight.UploadPreflightResult) string {
if r.IsFail {
return "FAIL"
}
if r.IsWarn {
return "WARN"
}
if r.IsPass {
return "PASS"
fmt.Fprintf(w, "\n")
fmtColumns := "%s\t%s\t%s\n"
fmt.Fprintf(w, fmtColumns, "STATE", "TITLE", "MESSAGE")
for _, result := range results {
fmt.Fprintf(w, fmtColumns, strings.ToUpper(preflight.GetPreflightCheckState(result)), result.Title, result.Message)
}
// We should never get here
return "UNKNOWN"
fmt.Fprintf(w, "\n")
}

0 comments on commit c1f3cfe

Please sign in to comment.