-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce support for Kubernetes version compatibility checks
This allows to enable/disable features based on Kubernetes version. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
5 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 compatibility provides some a way to enable/disable features based on Kubernetes version. | ||
package compatibility | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/blang/semver/v4" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
// Version is the Kubernetes version to have running. | ||
type Version semver.Version | ||
|
||
// latest is used if the version can't be parsed. | ||
var latest = Version{ | ||
Major: 1, | ||
Minor: 99, | ||
} | ||
|
||
// VersionFromImageRef parses container image ref to return just Kubernetes version. | ||
// | ||
// If the version can't be parsed, assume latest version. | ||
func VersionFromImageRef(imageRef string) Version { | ||
// try to parse as tagged | ||
ref, err := name.NewTag(imageRef) | ||
if err != nil { | ||
// try to cut digest part | ||
var ok bool | ||
|
||
imageRef, _, ok = strings.Cut(imageRef, "@") | ||
|
||
if ok { | ||
ref, err = name.NewTag(imageRef) | ||
} | ||
|
||
if err != nil { | ||
return latest | ||
} | ||
} | ||
|
||
v, err := semver.ParseTolerant(ref.TagStr()) | ||
if err != nil { | ||
return latest | ||
} | ||
|
||
return Version(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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 compatibility_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/siderolabs/go-kubernetes/kubernetes/compatibility" | ||
) | ||
|
||
func TestVersionFromImageRef(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
imageRef string | ||
|
||
expectedVersion compatibility.Version | ||
}{ | ||
{ | ||
name: "just tag", | ||
imageRef: "k8s.gcr.io/kube-apiserver:v1.18.0", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 18}, | ||
}, | ||
{ | ||
name: "just tag 2", | ||
imageRef: "ghcr.io/siderolabs/kubelet:v1.27.9", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 27, Patch: 9}, | ||
}, | ||
{ | ||
name: "tag and digest", | ||
imageRef: "ghcr.io/siderolabs/kubelet:v1.27.9@sha256:3f226f5b385960e311f19d6c9c3ea1778e86e6ad2e98a7bbbf1b1a63fe963916", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 27, Patch: 9}, | ||
}, | ||
{ | ||
name: "only digest", | ||
imageRef: "ghcr.io/siderolabs/kubelet@sha256:3f226f5b385960e311f19d6c9c3ea1778e86e6ad2e98a7bbbf1b1a63fe963916", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 99}, | ||
}, | ||
{ | ||
name: "no tag or digest", | ||
imageRef: "ghcr.io/siderolabs/kubelet", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 99}, | ||
}, | ||
{ | ||
name: "invalid version", | ||
imageRef: "ghcr.io/siderolabs/kubelet:alpha", | ||
|
||
expectedVersion: compatibility.Version{Major: 1, Minor: 99}, | ||
}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
actualVersion := compatibility.VersionFromImageRef(test.imageRef) | ||
assert.Equal(t, test.expectedVersion, actualVersion) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 compatibility | ||
|
||
import "github.com/blang/semver/v4" | ||
|
||
// SupportsKubeletConfigContainerRuntimeEndpoint returns true if kubelet supports ContainerRuntimEndpoint in kubelet config. | ||
func (v Version) SupportsKubeletConfigContainerRuntimeEndpoint() bool { | ||
// see https://github.com/kubernetes/kubernetes/pull/112136 | ||
return semver.Version(v).GTE(semver.Version{Major: 1, Minor: 27}) | ||
} | ||
|
||
// FeatureFlagSeccompDefaultEnabledByDefault returns true if a SeccompDefault feature flag is enabled by default. | ||
func (v Version) FeatureFlagSeccompDefaultEnabledByDefault() bool { | ||
// see https://github.com/kubernetes/kubernetes/pull/110805 | ||
return semver.Version(v).GTE(semver.Version{Major: 1, Minor: 25}) | ||
} |