Skip to content

Commit

Permalink
print nodename
Browse files Browse the repository at this point in the history
  • Loading branch information
carlory committed Mar 3, 2024
1 parent 210378e commit 22adf22
Show file tree
Hide file tree
Showing 12 changed files with 708 additions and 414 deletions.
1 change: 1 addition & 0 deletions cmd/kubeadm/app/apis/output/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ComponentUpgradePlan struct {
Name string
CurrentVersion string
NewVersion string
NodeName string
}

// ComponentConfigVersionState describes the current and desired version of a component config
Expand Down
5 changes: 5 additions & 0 deletions cmd/kubeadm/app/apis/output/v1alpha2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ import (
func Convert_output_UpgradePlan_To_v1alpha2_UpgradePlan(in *output.UpgradePlan, out *UpgradePlan, s conversion.Scope) error {
return autoConvert_output_UpgradePlan_To_v1alpha2_UpgradePlan(in, out, s)
}

// Convert_output_ComponentUpgradePlan_To_v1alpha2_ComponentUpgradePlan converts a private ComponentUpgradePlan to public ComponentUpgradePlan.
func Convert_output_ComponentUpgradePlan_To_v1alpha2_ComponentUpgradePlan(in *output.ComponentUpgradePlan, out *ComponentUpgradePlan, s conversion.Scope) error {
return autoConvert_output_ComponentUpgradePlan_To_v1alpha2_ComponentUpgradePlan(in, out, s)
}
40 changes: 28 additions & 12 deletions cmd/kubeadm/app/apis/output/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/kubeadm/app/apis/output/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ComponentUpgradePlan struct {
Name string `json:"name"`
CurrentVersion string `json:"currentVersion"`
NewVersion string `json:"newVersion"`
NodeName string `json:"nodeName,omitempty"`
}

// ComponentConfigVersionState describes the current and desired version of a component config
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 50 additions & 36 deletions cmd/kubeadm/app/cmd/upgrade/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package upgrade

import (
"fmt"
"io"
"os"
"sort"
Expand Down Expand Up @@ -88,11 +87,12 @@ func newCmdPlan(apf *applyPlanFlags) *cobra.Command {
}

// newComponentUpgradePlan helper creates outputapiv1alpha3.ComponentUpgradePlan object
func newComponentUpgradePlan(name, currentVersion, newVersion string) outputapiv1alpha3.ComponentUpgradePlan {
func newComponentUpgradePlan(name, currentVersion, newVersion, nodeName string) outputapiv1alpha3.ComponentUpgradePlan {
return outputapiv1alpha3.ComponentUpgradePlan{
Name: name,
CurrentVersion: currentVersion,
NewVersion: newVersion,
NodeName: nodeName,
}
}

Expand All @@ -106,10 +106,6 @@ func runPlan(flagSet *pflag.FlagSet, flags *planFlags, args []string, printer ou
return err
}

// Currently this is the only method we have for distinguishing
// external etcd vs static pod etcd
isExternalEtcd := initCfg.Etcd.External != nil

// Compute which upgrade possibilities there are
klog.V(1).Infoln("[upgrade/plan] computing upgrade possibilities")

Expand All @@ -124,7 +120,7 @@ func runPlan(flagSet *pflag.FlagSet, flags *planFlags, args []string, printer ou
return cmdutil.TypeMismatchErr("allowExperimentalUpgrades", "bool")
}

availUpgrades, err := upgrade.GetAvailableUpgrades(versionGetter, *allowExperimentalUpgrades, *allowRCUpgrades, isExternalEtcd, client, constants.GetStaticPodDirectory(), printer)
availUpgrades, err := upgrade.GetAvailableUpgrades(versionGetter, *allowExperimentalUpgrades, *allowRCUpgrades, client, printer)
if err != nil {
return errors.Wrap(err, "[upgrade/versions] FATAL")
}
Expand All @@ -143,16 +139,15 @@ func runPlan(flagSet *pflag.FlagSet, flags *planFlags, args []string, printer ou
}

// Generate and print the upgrade plan
plan := genUpgradePlan(availUpgrades, configVersionStates, isExternalEtcd)
plan := genUpgradePlan(availUpgrades, configVersionStates)
return printer.PrintObj(plan, os.Stdout)
}

// genUpgradePlan generates upgrade plan from available upgrades and component config version states
func genUpgradePlan(availUpgrades []upgrade.Upgrade, configVersions []outputapiv1alpha3.ComponentConfigVersionState, isExternalEtcd bool) *outputapiv1alpha3.UpgradePlan {
func genUpgradePlan(availUpgrades []upgrade.Upgrade, configVersions []outputapiv1alpha3.ComponentConfigVersionState) *outputapiv1alpha3.UpgradePlan {
plan := &outputapiv1alpha3.UpgradePlan{ConfigVersions: configVersions}
for _, up := range availUpgrades {
au := genAvailableUpgrade(&up, isExternalEtcd)
plan.AvailableUpgrades = append(plan.AvailableUpgrades, au)
plan.AvailableUpgrades = append(plan.AvailableUpgrades, genAvailableUpgrade(&up))
}
return plan
}
Expand All @@ -164,7 +159,7 @@ func appendDNSComponent(components []outputapiv1alpha3.ComponentUpgradePlan, up
afterVersion := up.After.DNSVersion

if beforeVersion != "" || afterVersion != "" {
components = append(components, newComponentUpgradePlan(name, beforeVersion, afterVersion))
components = append(components, newComponentUpgradePlan(name, beforeVersion, afterVersion, ""))
}
return components
}
Expand All @@ -175,41 +170,64 @@ func appendKubeadmComponent(components []outputapiv1alpha3.ComponentUpgradePlan,
afterVersion := up.After.KubeadmVersion

if beforeVersion != "" || afterVersion != "" {
components = append(components, newComponentUpgradePlan(name, beforeVersion, afterVersion))
components = append(components, newComponentUpgradePlan(name, beforeVersion, afterVersion, ""))
}
return components
}

// genAvailableUpgrade generates available upgrade from upgrade object and external etcd boolean
func genAvailableUpgrade(up *upgrade.Upgrade, isExternalEtcd bool) outputapiv1alpha3.AvailableUpgrade {
// genAvailableUpgrade generates available upgrade from upgrade object.
func genAvailableUpgrade(up *upgrade.Upgrade) outputapiv1alpha3.AvailableUpgrade {
components := []outputapiv1alpha3.ComponentUpgradePlan{}

if up.CanUpgradeKubelets() {
// The map is of the form <old-version>:<node-count>. Here all the keys are put into a slice and sorted
// The map is of the form <old-version>:<node-names>. Here all the keys are put into a slice and sorted
// in order to always get the right order. Then the map value is extracted separately
for _, oldVersion := range sortedSliceFromStringIntMap(up.Before.KubeletVersions) {
nodeCount := up.Before.KubeletVersions[oldVersion]
components = append(components, newComponentUpgradePlan(constants.Kubelet, fmt.Sprintf("%d x %s", nodeCount, oldVersion), up.After.KubeVersion))
for _, oldVersion := range sortedSliceFromStringStringArrayMap(up.Before.KubeletVersions) {
nodeNames := up.Before.KubeletVersions[oldVersion]
for _, nodeName := range nodeNames {
components = append(components, newComponentUpgradePlan(constants.Kubelet, oldVersion, up.After.KubeVersion, nodeName))
}
}
}

for _, oldVersion := range sortedSliceFromStringStringArrayMap(up.Before.KubeAPIServerVersions) {
nodeNames := up.Before.KubeAPIServerVersions[oldVersion]
for _, nodeName := range nodeNames {
components = append(components, newComponentUpgradePlan(constants.KubeAPIServer, oldVersion, up.After.KubeVersion, nodeName))
}
}

for _, oldVersion := range sortedSliceFromStringStringArrayMap(up.Before.KubeControllerManagerVersions) {
nodeNames := up.Before.KubeControllerManagerVersions[oldVersion]
for _, nodeName := range nodeNames {
components = append(components, newComponentUpgradePlan(constants.KubeControllerManager, oldVersion, up.After.KubeVersion, nodeName))
}
}

components = append(components, newComponentUpgradePlan(constants.KubeAPIServer, up.Before.KubeVersion, up.After.KubeVersion))
components = append(components, newComponentUpgradePlan(constants.KubeControllerManager, up.Before.KubeVersion, up.After.KubeVersion))
components = append(components, newComponentUpgradePlan(constants.KubeScheduler, up.Before.KubeVersion, up.After.KubeVersion))
components = append(components, newComponentUpgradePlan(constants.KubeProxy, up.Before.KubeVersion, up.After.KubeVersion))
for _, oldVersion := range sortedSliceFromStringStringArrayMap(up.Before.KubeSchedulerVersions) {
nodeNames := up.Before.KubeSchedulerVersions[oldVersion]
for _, nodeName := range nodeNames {
components = append(components, newComponentUpgradePlan(constants.KubeScheduler, oldVersion, up.After.KubeVersion, nodeName))
}
}

components = append(components, newComponentUpgradePlan(constants.KubeProxy, up.Before.KubeVersion, up.After.KubeVersion, ""))
components = appendDNSComponent(components, up, constants.CoreDNS)
components = appendKubeadmComponent(components, up, constants.Kubeadm)

if !isExternalEtcd {
components = append(components, newComponentUpgradePlan(constants.Etcd, up.Before.EtcdVersion, up.After.EtcdVersion))
// If etcd is not external, we should include it in the upgrade plan
for _, oldVersion := range sortedSliceFromStringStringArrayMap(up.Before.EtcdVersions) {
nodeNames := up.Before.EtcdVersions[oldVersion]
for _, nodeName := range nodeNames {
components = append(components, newComponentUpgradePlan(constants.Etcd, oldVersion, up.After.EtcdVersion, nodeName))
}
}

return outputapiv1alpha3.AvailableUpgrade{Description: up.Description, Components: components}
}

// sortedSliceFromStringIntMap returns a slice of the keys in the map sorted alphabetically
func sortedSliceFromStringIntMap(strMap map[string]uint16) []string {
// sortedSliceFromStringStringArrayMap returns a slice of the keys in the map sorted alphabetically
func sortedSliceFromStringStringArrayMap(strMap map[string][]string) []string {
strSlice := []string{}
for k := range strMap {
strSlice = append(strSlice, k)
Expand Down Expand Up @@ -280,28 +298,24 @@ func (printer *upgradePlanTextPrinter) printAvailableUpgrade(writer io.Writer, a

_, _ = printer.Fprintln(writer, "Components that must be upgraded manually after you have upgraded the control plane with 'kubeadm upgrade apply':")
tabw := tabwriter.NewWriter(writer, 10, 4, 3, ' ', 0)
_, _ = printer.Fprintln(tabw, strings.Join([]string{"COMPONENT", "CURRENT", "TARGET"}, "\t"))
for i, component := range au.Components {
_, _ = printer.Fprintln(tabw, strings.Join([]string{"COMPONENT", "NODE", "CURRENT", "TARGET"}, "\t"))
for _, component := range au.Components {
if component.Name != constants.Kubelet {
continue
}
if i == 0 {
_, _ = printer.Fprintf(tabw, "%s\t%s\t%s\n", component.Name, component.CurrentVersion, component.NewVersion)
} else {
_, _ = printer.Fprintf(tabw, "%s\t%s\t%s\n", "", component.CurrentVersion, component.NewVersion)
}
_, _ = printer.Fprintf(tabw, "%s\t%s\t%s\t%s\n", component.Name, component.NodeName, component.CurrentVersion, component.NewVersion)
}
_ = tabw.Flush()

_, _ = printer.Fprintln(writer, "")
_, _ = printer.Fprintf(writer, "Upgrade to the latest %s:\n", au.Description)
_, _ = printer.Fprintln(writer, "")
_, _ = printer.Fprintln(tabw, strings.Join([]string{"COMPONENT", "CURRENT", "TARGET"}, "\t"))
_, _ = printer.Fprintln(tabw, strings.Join([]string{"COMPONENT", "NODE", "CURRENT", "TARGET"}, "\t"))
for _, component := range au.Components {
if component.Name == constants.Kubelet || component.Name == constants.Kubeadm {
continue
}
_, _ = printer.Fprintf(tabw, "%s\t%s\t%s\n", component.Name, component.CurrentVersion, component.NewVersion)
_, _ = printer.Fprintf(tabw, "%s\t%s\t%s\t%s\n", component.Name, component.NodeName, component.CurrentVersion, component.NewVersion)
}
_ = tabw.Flush()

Expand Down
Loading

0 comments on commit 22adf22

Please sign in to comment.