Skip to content

Commit

Permalink
Replace fmt.Print with printer in upgrade plan code and fix configVer…
Browse files Browse the repository at this point in the history
…sions are not printed when output is json or yaml
  • Loading branch information
carlory committed Feb 25, 2024
1 parent 003f4c5 commit 219a4c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/cmd/upgrade/common.go
Expand Up @@ -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")
}

Expand Down
22 changes: 14 additions & 8 deletions cmd/kubeadm/app/cmd/upgrade/plan.go
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:")
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/upgrade/compute.go
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/kubeadm/app/phases/upgrade/health.go
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 219a4c8

Please sign in to comment.