From 219a4c8e5994df2f56eadd332716740ca22e8667 Mon Sep 17 00:00:00 2001 From: carlory Date: Fri, 23 Feb 2024 14:18:48 +0800 Subject: [PATCH] Replace fmt.Print with printer in upgrade plan code and fix configVersions are not printed when output is json or yaml --- cmd/kubeadm/app/cmd/upgrade/common.go | 2 +- cmd/kubeadm/app/cmd/upgrade/plan.go | 22 ++++++++++++++-------- cmd/kubeadm/app/phases/upgrade/compute.go | 4 ++-- cmd/kubeadm/app/phases/upgrade/health.go | 5 +++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/cmd/kubeadm/app/cmd/upgrade/common.go b/cmd/kubeadm/app/cmd/upgrade/common.go index ee3c59f29e615..434c067d58f9e 100644 --- a/cmd/kubeadm/app/cmd/upgrade/common.go +++ b/cmd/kubeadm/app/cmd/upgrade/common.go @@ -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") } diff --git a/cmd/kubeadm/app/cmd/upgrade/plan.go b/cmd/kubeadm/app/cmd/upgrade/plan.go index f910530a8a089..d135287bdbc8f 100644 --- a/cmd/kubeadm/app/cmd/upgrade/plan.go +++ b/cmd/kubeadm/app/cmd/upgrade/plan.go @@ -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:") diff --git a/cmd/kubeadm/app/phases/upgrade/compute.go b/cmd/kubeadm/app/phases/upgrade/compute.go index d60e7f8f3f58c..0f11b346085d9 100644 --- a/cmd/kubeadm/app/phases/upgrade/compute.go +++ b/cmd/kubeadm/app/phases/upgrade/compute.go @@ -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) { diff --git a/cmd/kubeadm/app/phases/upgrade/health.go b/cmd/kubeadm/app/phases/upgrade/health.go index 3c11729e4d37c..3dd3c0404129e 100644 --- a/cmd/kubeadm/app/phases/upgrade/health.go +++ b/cmd/kubeadm/app/phases/upgrade/health.go @@ -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{