fix: resolve the issue with Kubernetes upgrade

Add missing cases, refactoring.

Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
This commit is contained in:
Alexey Palazhchenko 2021-03-25 21:29:11 +03:00 committed by talos-bot
parent 74b2b5578c
commit 1f5a0c4065
5 changed files with 82 additions and 15 deletions

1
go.mod
View File

@ -25,6 +25,7 @@ require (
github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.9.1
github.com/coreos/go-iptables v0.5.0
github.com/coreos/go-semver v0.3.0
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.4+incompatible
github.com/docker/go-connections v0.4.0

View File

@ -24,15 +24,14 @@ import (
)
// UpgradeSelfHosted the Kubernetes control plane.
//
//nolint:gocyclo
func UpgradeSelfHosted(ctx context.Context, cluster cluster.K8sProvider, options UpgradeOptions) error {
switch {
case strings.HasPrefix(options.FromVersion, "1.18.") && strings.HasPrefix(options.ToVersion, "1.19."):
switch path := options.Path(); path {
case "1.18->1.19":
fallthrough
case "1.19->1.19":
return hyperkubeUpgrade(ctx, cluster, options)
case strings.HasPrefix(options.FromVersion, "1.19.") && strings.HasPrefix(options.ToVersion, "1.19."):
return hyperkubeUpgrade(ctx, cluster, options)
case strings.HasPrefix(options.FromVersion, "1.19.") && strings.HasPrefix(options.ToVersion, "1.20."):
case "1.19->1.20":
options.extraUpdaters = append(options.extraUpdaters, addControlPlaneToleration())
options.podCheckpointerExtraUpdaters = append(options.podCheckpointerExtraUpdaters, addControlPlaneToleration())
@ -48,10 +47,16 @@ func UpgradeSelfHosted(ctx context.Context, cluster cluster.K8sProvider, options
}
return hyperkubeUpgrade(ctx, cluster, options)
case strings.HasPrefix(options.FromVersion, "1.20.") && strings.HasPrefix(options.ToVersion, "1.20."):
case "1.20->1.20":
fallthrough
case "1.20->1.21":
fallthrough
case "1.21->1.21":
return hyperkubeUpgrade(ctx, cluster, options)
default:
return fmt.Errorf("unsupported upgrade from %q to %q", options.FromVersion, options.ToVersion)
return fmt.Errorf("unsupported upgrade path %q (from %q to %q)", path, options.FromVersion, options.ToVersion)
}
}

View File

@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/talos-systems/go-retry/retry"
@ -34,13 +33,20 @@ type UpgradeProvider interface {
//
//nolint:gocyclo
func UpgradeTalosManaged(ctx context.Context, cluster UpgradeProvider, options UpgradeOptions) error {
switch {
case strings.HasPrefix(options.FromVersion, "1.19.") && strings.HasPrefix(options.ToVersion, "1.19."):
case strings.HasPrefix(options.FromVersion, "1.19.") && strings.HasPrefix(options.ToVersion, "1.20."):
switch path := options.Path(); path {
case "1.19->1.19":
// nothing
case "1.19->1.20":
options.extraUpdaters = append(options.extraUpdaters, addControlPlaneToleration())
case strings.HasPrefix(options.FromVersion, "1.20.") && strings.HasPrefix(options.ToVersion, "1.20."):
// nothing for all those
case "1.20->1.20":
case "1.20->1.21":
case "1.21->1.21":
default:
return fmt.Errorf("unsupported upgrade from %q to %q", options.FromVersion, options.ToVersion)
return fmt.Errorf("unsupported upgrade path %q (from %q to %q)", path, options.FromVersion, options.ToVersion)
}
k8sClient, err := cluster.K8sHelper(ctx)

View File

@ -5,6 +5,9 @@
package kubernetes
import (
"fmt"
"github.com/coreos/go-semver/semver"
appsv1 "k8s.io/api/apps/v1"
)
@ -31,4 +34,17 @@ type UpgradeOptions struct {
masterNodes []string
}
// Path returns upgrade path in a form "FromMajor.FromMinor->ToMajor.ToMinor" (e.g. "1.20->1.21"),
// or empty string, if one or both versions can't be parsed.
func (options *UpgradeOptions) Path() string {
from, fromErr := semver.NewVersion(options.FromVersion)
to, toErr := semver.NewVersion(options.ToVersion)
if fromErr != nil || toErr != nil {
return ""
}
return fmt.Sprintf("%d.%d->%d.%d", from.Major, from.Minor, to.Major, to.Minor)
}
type daemonsetUpdater func(ds string, daemonset *appsv1.DaemonSet) error

View File

@ -0,0 +1,39 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package kubernetes_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/talos-systems/talos/pkg/cluster/kubernetes"
)
func TestPath(t *testing.T) {
t.Parallel()
t.Run("Valid", func(t *testing.T) {
t.Parallel()
options := &kubernetes.UpgradeOptions{
FromVersion: "1.20.5",
ToVersion: "1.21.0-beta.0",
}
assert.Equal(t, "1.20->1.21", options.Path())
})
t.Run("Invalid", func(t *testing.T) {
t.Parallel()
options := &kubernetes.UpgradeOptions{
FromVersion: "foo",
ToVersion: "bar",
}
assert.Equal(t, "", options.Path())
})
}