pacoxu / kubeadm-operator

Test work on the design of kubeadm operator. Also you can try https://github.com/chendave/kubeadm-operator
Apache License 2.0
10 stars 2 forks source link

a flag to disable downgrade to a minor version #77

Open github-actions[bot] opened 2 years ago

github-actions[bot] commented 2 years ago

downgrade to a minor version

this is just for test purpose, need to define if it should be supported in the future

https://github.com/pacoxu/kubeadm-operator/blob/9721e44e8e2f00b767972cbd69f857eb3ce153c5/operations/version.go#L59

Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package operations

import (
    "fmt"

    "k8s.io/apimachinery/pkg/util/version"
)

func upgradeCheck(current, target string) (isSupported, isCrossVersion, canSkip bool) {
    currentVer, err := version.ParseSemantic(current)
    if err != nil {
        return false, false, false
    }
    targetVer, err := version.ParseSemantic(target)
    if err != nil {
        return false, false, false
    }
    return upgradeCheckVersion(currentVer, targetVer)
}

func upgradeCheckVersion(current, target *version.Version) (isSupported, isCrossVersion, canSkip bool) {
    // no need to check major as only major 1 is supported
    // if current.Major() != target.Major() {
    //  return false
    // }
    if current.Minor() == target.Minor() {
        if current.Patch() == target.Patch() {
            // just skip as the version is the same
            return true, false, true
        }
        // upgrade to a patched version
        return true, false, false
    } else if current.Minor() < target.Minor() {
        if current.Minor()+1 == target.Minor() {
            // upgrade to a minor version
            return true, false, false
        }
        // upgrade multi-minor version, need to split into multiple upgrade tasks
        return true, true, false
    }
    // downgrade is not supported
    if current.Minor()-1 == target.Minor() {
        // TODO downgrade to a minor version
        // this is just for test purpose, need to define if it should be supported in the future
        return true, false, false
    }
    return false, false, false

}

func isSupported(ver string) bool {
    v, err := version.ParseSemantic(ver)
    if err != nil {
        return false
    }
    return isSupportedVersion(v)
}

func isSupportedVersion(ver *version.Version) bool {
    // TODO a table of supported versions needs to be created in the docs
    if ver.Major() != 1 && ver.Minor() < 17 {
        return false
    }
    return true
}

// before this, we should make sure the version is supported
func getCrossVersions(current, target string) []string {
    versions := []string{}
    cur, err := version.ParseSemantic(current)
    if err != nil {
        return versions
    }
    tar, err := version.ParseSemantic(target)
    if err != nil {
        return versions
    }
    _, isCross, _ := upgradeCheckVersion(cur, tar)
    if !isCross {
        return versions
    }
    tarMinor := tar.Minor()
    for i := cur.Minor() + 1; i < tarMinor; i++ {
        versions = append(versions, fmt.Sprintf("v1.%d.0", i))
    }
    return versions
}
ew file mode 100644
ndex 0000000..3d11a78
++ b/operations/version_test.go

f8d06843b70231dc6828b4811100dc36025cff71

pacoxu commented 2 years ago

There may be some unsupported scenarios.

pacoxu commented 2 years ago

The downgrade support is added for testing.

pacoxu commented 2 years ago

By default, we should not support it. But it is very useful when upgrade fails and user want to restore to the older version.