diff --git a/cmd/kubeadm/app/cmd/upgrade/apply.go b/cmd/kubeadm/app/cmd/upgrade/apply.go index b36ddf23c10..75668e36ade 100644 --- a/cmd/kubeadm/app/cmd/upgrade/apply.go +++ b/cmd/kubeadm/app/cmd/upgrade/apply.go @@ -202,16 +202,16 @@ func EnforceVersionPolicies(flags *applyFlags, versionGetter upgrade.VersionGett if versionSkewErrs != nil { if len(versionSkewErrs.Mandatory) > 0 { - return fmt.Errorf("The --version argument is invalid due to these fatal errors: %v", versionSkewErrs.Mandatory) + return fmt.Errorf("The --version argument is invalid due to these fatal errors:\n\n%v\nPlease fix the misalignments highlighted above and try upgrading again", kubeadmutil.FormatErrMsg(versionSkewErrs.Mandatory)) } if len(versionSkewErrs.Skippable) > 0 { // Return the error if the user hasn't specified the --force flag if !flags.force { - return fmt.Errorf("The --version argument is invalid due to these errors: %v. Can be bypassed if you pass the --force flag", versionSkewErrs.Skippable) + return fmt.Errorf("The --version argument is invalid due to these errors:\n\n%v\nCan be bypassed if you pass the --force flag", kubeadmutil.FormatErrMsg(versionSkewErrs.Skippable)) } // Soft errors found, but --force was specified - fmt.Printf("[upgrade/version] Found %d potential version compatibility errors but skipping since the --force flag is set: %v\n", len(versionSkewErrs.Skippable), versionSkewErrs.Skippable) + fmt.Printf("[upgrade/version] Found %d potential version compatibility errors but skipping since the --force flag is set: \n\n%v", len(versionSkewErrs.Skippable), kubeadmutil.FormatErrMsg(versionSkewErrs.Skippable)) } } return nil diff --git a/cmd/kubeadm/app/util/error.go b/cmd/kubeadm/app/util/error.go index 32cb6150197..c27ab860d7e 100644 --- a/cmd/kubeadm/app/util/error.go +++ b/cmd/kubeadm/app/util/error.go @@ -75,3 +75,12 @@ func checkErr(prefix string, err error, handleErr func(string, int)) { handleErr(err.Error(), DefaultErrorExitCode) } } + +// FormatErrMsg returns a human-readable string describing the slice of errors passed to the function +func FormatErrMsg(errs []error) string { + var errMsg string + for _, err := range errs { + errMsg = fmt.Sprintf("%s\t-%s\n", errMsg, err.Error()) + } + return errMsg +} diff --git a/cmd/kubeadm/app/util/error_test.go b/cmd/kubeadm/app/util/error_test.go index 07aae6c3357..831007550ad 100644 --- a/cmd/kubeadm/app/util/error_test.go +++ b/cmd/kubeadm/app/util/error_test.go @@ -50,3 +50,34 @@ func TestCheckErr(t *testing.T) { } } } + +func TestFormatErrMsg(t *testing.T) { + errMsg1 := "specified version to upgrade to v1.9.0-alpha.3 is equal to or lower than the cluster version v1.10.0-alpha.0.69+638add6ddfb6d2. Downgrades are not supported yet" + errMsg2 := "specified version to upgrade to v1.9.0-alpha.3 is higher than the kubeadm version v1.9.0-alpha.1.3121+84178212527295-dirty. Upgrade kubeadm first using the tool you used to install kubeadm" + + testCases := []struct { + errs []error + expect string + }{ + { + errs: []error{ + fmt.Errorf(errMsg1), + fmt.Errorf(errMsg2), + }, + expect: "\t-" + errMsg1 + "\n" + "\t-" + errMsg2 + "\n", + }, + { + errs: []error{ + fmt.Errorf(errMsg1), + }, + expect: "\t-" + errMsg1 + "\n", + }, + } + + for _, testCase := range testCases { + got := FormatErrMsg(testCase.errs) + if got != testCase.expect { + t.Errorf("FormatErrMsg error, expect: %v, got: %v", testCase.expect, got) + } + } +}