Skip to content

Commit

Permalink
Add additional version check
Browse files Browse the repository at this point in the history
  • Loading branch information
xmudrii committed Feb 21, 2019
1 parent 7666a9c commit b4ca039
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
41 changes: 39 additions & 2 deletions pkg/upgrader/upgrade/preflight_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func runPreflightChecks(ctx *util.Context) error {
if err := verifyVersion(ctx, nodes, ctx.Verbose); err != nil {
return fmt.Errorf("unable to verify components version: %v", err)
}
if err := verifyVersionSkew(ctx, nodes, ctx.Verbose); err != nil {
if ctx.ForceUpgrade {
ctx.Logger.Warningf("version skew check failed: %v", err)
} else {
return fmt.Errorf("version skew check failed: %v", err)
}
}

return nil
}
Expand Down Expand Up @@ -147,13 +154,43 @@ func verifyEndpoints(nodes *corev1.NodeList, hosts []*config.HostConfig, verbose
return nil
}

// verifyVersion ensures it's possible to upgrade to the requested version and that the version skew policy is fulfilled
// verifyVersion verifies is it possible to upgrade to the requested version
func verifyVersion(ctx *util.Context, nodes *corev1.NodeList, verbose bool) error {
reqVer, err := semver.NewVersion(ctx.Cluster.Versions.Kubernetes)
if err != nil {
return fmt.Errorf("provided version is invalid: %v", err)
}

var highestKubelet *semver.Version
for _, n := range nodes.Items {
kubelet, err := semver.NewVersion(n.Status.NodeInfo.KubeletVersion)
if err != nil {
return err
}
if highestKubelet == nil || highestKubelet.Compare(kubelet) < 0 {
highestKubelet = kubelet
}
}

if reqVer.Equal(highestKubelet) {
return fmt.Errorf("requested version is same as current")
}
// Check are we upgrading to newer minor or patch release
if reqVer.Minor()-highestKubelet.Minor() < 0 ||
(reqVer.Minor() == highestKubelet.Minor() && reqVer.Patch() < highestKubelet.Patch()) {
return fmt.Errorf("requested version can't be lower than current")
}

return nil
}

// verifyVersionSkew ensures the requested version matches the version skew policy
func verifyVersionSkew(ctx *util.Context, nodes *corev1.NodeList, verbose bool) error {
reqVer, err := semver.NewVersion(ctx.Cluster.Versions.Kubernetes)
if err != nil {
return fmt.Errorf("provided version is invalid: %v", err)
}

// Check API server version
var apiserverVersion *semver.Version
apiserverPods, err := ctx.Clientset.CoreV1().Pods(metav1.NamespaceSystem).List(metav1.ListOptions{
Expand Down Expand Up @@ -220,7 +257,7 @@ func checkVersionSkew(reqVer, currVer *semver.Version, diff int64) error {
}
// Check are we upgrading to newer minor or patch release
if reqVer.Minor()-currVer.Minor() < 0 ||
(reqVer.Minor() == currVer.Minor() && reqVer.Patch() < reqVer.Patch()) {
(reqVer.Minor() == currVer.Minor() && reqVer.Patch() < currVer.Patch()) {
return fmt.Errorf("requested version can't be lower than current")
}
// Ensure the version skew policy
Expand Down
14 changes: 10 additions & 4 deletions pkg/upgrader/upgrade/preflight_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,35 @@ func TestCheckVersionSkewInvalid(t *testing.T) {
diff: 2,
},
{
name: "upgrade 1.13.0 to 1.12.1 with diff of 1",
name: "downgrade 1.13.0 to 1.12.1 with diff of 1",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.12.1"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.1 with diff of 1",
name: "downgrade 1.13.3 to 1.12.1 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.1"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.3 with diff of 1",
name: "downgrade 1.13.3 to 1.12.3 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.3"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.4 with diff of 1",
name: "downgrade 1.13.3 to 1.12.4 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.4"),
diff: 1,
},
{
name: "downgrade 1.13.3 to 1.13.2 with diff of 1",
currentVersion: semver.MustParse("v1.13.3"),
desiredVersion: semver.MustParse("v1.13.2"),
diff: 1,
},
}
for _, tc := range testcases {
tc := tc
Expand Down

0 comments on commit b4ca039

Please sign in to comment.