talos
80 строк · 3.2 Кб
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package compatibility
6
7import (
8"fmt"
9
10"github.com/blang/semver/v4"
11"github.com/siderolabs/gen/pair/ordered"
12
13"github.com/siderolabs/talos/pkg/machinery/compatibility/talos12"
14"github.com/siderolabs/talos/pkg/machinery/compatibility/talos13"
15"github.com/siderolabs/talos/pkg/machinery/compatibility/talos14"
16"github.com/siderolabs/talos/pkg/machinery/compatibility/talos15"
17"github.com/siderolabs/talos/pkg/machinery/compatibility/talos16"
18"github.com/siderolabs/talos/pkg/machinery/compatibility/talos17"
19"github.com/siderolabs/talos/pkg/machinery/compatibility/talos18"
20)
21
22// KubernetesVersion embeds Kubernetes version.
23type KubernetesVersion struct {
24vers semver.Version
25}
26
27// ParseKubernetesVersion parses Kubernetes version.
28func ParseKubernetesVersion(v string) (*KubernetesVersion, error) {
29parsed, err := semver.ParseTolerant(v)
30if err != nil {
31return nil, err
32}
33
34return &KubernetesVersion{
35vers: parsed,
36}, nil
37}
38
39func (v *KubernetesVersion) String() string {
40return v.vers.String()
41}
42
43// SupportedWith checks if the Kubernetes version is supported with specified version of Talos.
44func (v *KubernetesVersion) SupportedWith(target *TalosVersion) error {
45var minK8sVersion, maxK8sVersion semver.Version
46
47switch target.majorMinor {
48case talos12.MajorMinor: // upgrades to 1.2.x
49minK8sVersion, maxK8sVersion = talos12.MinimumKubernetesVersion, talos12.MaximumKubernetesVersion
50case talos13.MajorMinor: // upgrades to 1.3.x
51minK8sVersion, maxK8sVersion = talos13.MinimumKubernetesVersion, talos13.MaximumKubernetesVersion
52case talos14.MajorMinor: // upgrades to 1.4.x
53minK8sVersion, maxK8sVersion = talos14.MinimumKubernetesVersion, talos14.MaximumKubernetesVersion
54case talos15.MajorMinor: // upgrades to 1.5.x
55minK8sVersion, maxK8sVersion = talos15.MinimumKubernetesVersion, talos15.MaximumKubernetesVersion
56case talos16.MajorMinor: // upgrades to 1.6.x
57minK8sVersion, maxK8sVersion = talos16.MinimumKubernetesVersion, talos16.MaximumKubernetesVersion
58case talos17.MajorMinor: // upgrades to 1.7.x
59minK8sVersion, maxK8sVersion = talos17.MinimumKubernetesVersion, talos17.MaximumKubernetesVersion
60case talos18.MajorMinor: // upgrades to 1.8.x
61minK8sVersion, maxK8sVersion = talos18.MinimumKubernetesVersion, talos18.MaximumKubernetesVersion
62default:
63return fmt.Errorf("compatibility with version %s is not supported", target.String())
64}
65
66core := ordered.MakeTriple(v.vers.Major, v.vers.Minor, v.vers.Patch)
67minK8sVersionCore := ordered.MakeTriple(minK8sVersion.Major, minK8sVersion.Minor, minK8sVersion.Patch)
68
69if core.LessThan(minK8sVersionCore) {
70return fmt.Errorf("version of Kubernetes %s is too old to be used with Talos %s", v.vers.String(), target.version.String())
71}
72
73maxK8sVersionCore := ordered.MakeTriple(maxK8sVersion.Major, maxK8sVersion.Minor, maxK8sVersion.Patch)
74
75if core.Compare(maxK8sVersionCore) >= 0 {
76return fmt.Errorf("version of Kubernetes %s is too new to be used with Talos %s", v.vers.String(), target.version.String())
77}
78
79return nil
80}
81