mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-13 21:25:09 +00:00
Merge pull request #56029 from wackxu/errmsg
Automatic merge from submit-queue (batch tested with PRs 55112, 56029, 55740, 56095, 55845). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Improve kubeadm upgrade apply error logging style **What this PR does / why we need it**: Improve kubeadm upgrade apply error logging style **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # https://github.com/kubernetes/kubeadm/issues/549 **Special notes for your reviewer**: /assign @luxas **Release note**: ```release-note NONE ```
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user