Merge pull request #123492 from carlory/fix-kubeadm-494-1

Replace fmt.Print with printer in upgrade plan code and fix configVersions are not printed when output is json or yaml
This commit is contained in:
Kubernetes Prow Robot 2024-02-25 13:47:31 -08:00 committed by GitHub
commit a58221c104
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 13 deletions

View File

@ -176,7 +176,7 @@ func enforceRequirements(flags *applyPlanFlags, args []string, dryRun bool, upgr
}
// Run healthchecks against the cluster
if err := upgrade.CheckClusterHealth(client, &cfg.ClusterConfiguration, ignorePreflightErrorsSet); err != nil {
if err := upgrade.CheckClusterHealth(client, &cfg.ClusterConfiguration, ignorePreflightErrorsSet, printer); err != nil {
return nil, nil, nil, errors.Wrap(err, "[upgrade/health] FATAL")
}

View File

@ -158,7 +158,8 @@ func (pf *upgradePlanJSONYamlPrintFlags) AllowedFormats() []string {
// upgradePlanJSONYAMLPrinter prints upgrade plan in a JSON or YAML format
type upgradePlanJSONYAMLPrinter struct {
output.ResourcePrinterWrapper
Buffer []outputapiv1alpha2.ComponentUpgradePlan
Components []outputapiv1alpha2.ComponentUpgradePlan
ConfigVersions []outputapiv1alpha2.ComponentConfigVersionState
}
// newUpgradePlanJSONYAMLPrinter creates a new upgradePlanJSONYAMLPrinter object
@ -175,7 +176,7 @@ func (p *upgradePlanJSONYAMLPrinter) PrintObj(obj runtime.Object, writer io.Writ
if !ok {
return errors.Errorf("expected ComponentUpgradePlan, but got %+v", obj)
}
p.Buffer = append(p.Buffer, *item)
p.Components = append(p.Components, *item)
return nil
}
@ -184,19 +185,18 @@ func (p *upgradePlanJSONYAMLPrinter) Flush(writer io.Writer, last bool) {
if !last {
return
}
if len(p.Buffer) == 0 {
if len(p.Components) == 0 && len(p.ConfigVersions) == 0 {
return
}
plan := &outputapiv1alpha2.UpgradePlan{Components: p.Buffer}
plan := &outputapiv1alpha2.UpgradePlan{Components: p.Components, ConfigVersions: p.ConfigVersions}
if err := p.Printer.PrintObj(plan, writer); err != nil {
fmt.Fprintf(os.Stderr, "could not flush output buffer: %v\n", err)
}
p.Components = p.Components[:0]
}
// Close empties the list of buffered components
func (p *upgradePlanJSONYAMLPrinter) Close(writer io.Writer) {
p.Buffer = p.Buffer[:0]
}
// Close does nothing.
func (p *upgradePlanJSONYAMLPrinter) Close(writer io.Writer) {}
// upgradePlanTextPrinter prints upgrade plan in a text form
type upgradePlanTextPrinter struct {
@ -284,6 +284,11 @@ func runPlan(flags *planFlags, args []string, printer output.Printer) error {
return nil
}
// A workaround to set the configVersionStates in the printer
if p, ok := printer.(*upgradePlanJSONYAMLPrinter); ok {
p.ConfigVersions = configVersionStates
}
// Generate and print upgrade plans
for _, up := range availUpgrades {
plan, unstableVersionFlag, err := genUpgradePlan(&up, isExternalEtcd)
@ -387,6 +392,7 @@ func printUpgradePlan(up *upgrade.Upgrade, plan *outputapiv1alpha2.UpgradePlan,
printer.PrintObj(&plan, writer)
}
}
printer.Flush(writer, true)
printer.Fprintln(writer, "You can now apply the upgrade by executing the following command:")

View File

@ -216,7 +216,7 @@ func GetAvailableUpgrades(versionGetterImpl VersionGetter, experimentalUpgradesA
if err != nil {
return upgrades, err
}
fmt.Printf("[upgrade/versions] Latest %s: %s\n", "experimental version", latestVersionStr)
_, _ = printer.Printf("[upgrade/versions] Latest %s: %s\n", "experimental version", latestVersionStr)
minorUnstable := latestVersion.Components()[1]
// Get and output the current latest unstable version
@ -225,7 +225,7 @@ func GetAvailableUpgrades(versionGetterImpl VersionGetter, experimentalUpgradesA
if err != nil {
return upgrades, err
}
fmt.Printf("[upgrade/versions] Latest %s: %s\n", "previous version", previousBranchLatestVersionStr)
_, _ = printer.Printf("[upgrade/versions] Latest %s: %s\n", "previous version", previousBranchLatestVersionStr)
// If that previous latest version is an RC, RCs are allowed and the cluster version is lower than the RC version, show the upgrade
if rcUpgradesAllowed && rcUpgradePossible(clusterVersion, previousBranchLatestVersion) {

View File

@ -38,6 +38,7 @@ import (
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/images"
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
)
// healthCheck is a helper struct for easily performing healthchecks against the cluster and printing the output
@ -66,8 +67,8 @@ func (c *healthCheck) Name() string {
// - the API /healthz endpoint is healthy
// - all control-plane Nodes are Ready
// - (if static pod-hosted) that all required Static Pod manifests exist on disk
func CheckClusterHealth(client clientset.Interface, cfg *kubeadmapi.ClusterConfiguration, ignoreChecksErrors sets.Set[string]) error {
fmt.Println("[upgrade] Running cluster health checks")
func CheckClusterHealth(client clientset.Interface, cfg *kubeadmapi.ClusterConfiguration, ignoreChecksErrors sets.Set[string], printer output.Printer) error {
_, _ = printer.Println("[upgrade] Running cluster health checks")
healthChecks := []preflight.Checker{
&healthCheck{