Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix version comparison for pre release versions #6649

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bootstrap/kubeadm/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta1"
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta2"
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/upstreamv1beta3"
"sigs.k8s.io/cluster-api/util/version"
)

var (
Expand Down Expand Up @@ -63,14 +64,14 @@ var (
)

// KubeVersionToKubeadmAPIGroupVersion maps a Kubernetes version to the correct Kubeadm API Group supported.
func KubeVersionToKubeadmAPIGroupVersion(version semver.Version) (schema.GroupVersion, error) {
func KubeVersionToKubeadmAPIGroupVersion(v semver.Version) (schema.GroupVersion, error) {
switch {
case version.LT(v1beta1KubeadmVersion):
case version.Compare(v, v1beta1KubeadmVersion, version.WithoutPreReleases()) < 0:
return schema.GroupVersion{}, errors.New("the bootstrap provider for kubeadm doesn't support Kubernetes version lower than v1.13.0")
case version.LT(v1beta2KubeadmVersion):
case version.Compare(v, v1beta2KubeadmVersion, version.WithoutPreReleases()) < 0:
// NOTE: All the Kubernetes version >= v1.13 and < v1.15 should use the kubeadm API version v1beta1
return upstreamv1beta1.GroupVersion, nil
case version.LT(v1beta3KubeadmVersion):
case version.Compare(v, v1beta3KubeadmVersion, version.WithoutPreReleases()) < 0:
// NOTE: All the Kubernetes version >= v1.15 and < v1.22 should use the kubeadm API version v1beta2
return upstreamv1beta2.GroupVersion, nil
default:
Expand Down
24 changes: 24 additions & 0 deletions bootstrap/kubeadm/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func TestKubeVersionToKubeadmAPIGroupVersion(t *testing.T) {
want: schema.GroupVersion{},
wantErr: true,
},
{
name: "pass with minimum kubernetes alpha version for kubeadm API v1beta1",
args: args{
version: semver.MustParse("1.13.0-alpha.0.734+ba502ee555924a"),
},
want: upstreamv1beta1.GroupVersion,
wantErr: false,
},
{
name: "pass with minimum kubernetes version for kubeadm API v1beta1",
args: args{
Expand All @@ -64,6 +72,14 @@ func TestKubeVersionToKubeadmAPIGroupVersion(t *testing.T) {
want: upstreamv1beta1.GroupVersion,
wantErr: false,
},
{
name: "pass with minimum kubernetes alpha version for kubeadm API v1beta2",
args: args{
version: semver.MustParse("1.15.0-alpha.0.734+ba502ee555924a"),
},
want: upstreamv1beta2.GroupVersion,
wantErr: false,
},
{
name: "pass with minimum kubernetes version for kubeadm API v1beta2",
args: args{
Expand All @@ -80,6 +96,14 @@ func TestKubeVersionToKubeadmAPIGroupVersion(t *testing.T) {
want: upstreamv1beta2.GroupVersion,
wantErr: false,
},
{
name: "pass with minimum kubernetes alpha version for kubeadm API v1beta3",
args: args{
version: semver.MustParse("1.22.0-alpha.0.734+ba502ee555924a"),
},
want: upstreamv1beta3.GroupVersion,
wantErr: false,
},
{
name: "pass with minimum kubernetes version for kubeadm API v1beta3",
args: args{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
disallowedUpgrade119Version := before.DeepCopy()
disallowedUpgrade119Version.Spec.Version = "v1.19.0"

disallowedUpgrade120AlphaVersion := before.DeepCopy()
disallowedUpgrade120AlphaVersion.Spec.Version = "v1.20.0-alpha.0.734_ba502ee555924a"

updateNTPServers := before.DeepCopy()
updateNTPServers.Spec.KubeadmConfigSpec.NTP.Servers = []string{"new-server"}

Expand Down Expand Up @@ -900,6 +903,12 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
before: disallowedUpgrade118Prev,
kcp: disallowedUpgrade119Version,
},
{
name: "should return error when trying to upgrade two minor versions",
expectErr: true,
before: disallowedUpgrade118Prev,
kcp: disallowedUpgrade120AlphaVersion,
},
{
name: "should not return an error when maxSurge value is updated to 0",
expectErr: false,
Expand Down
7 changes: 4 additions & 3 deletions controlplane/kubeadm/internal/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"sigs.k8s.io/cluster-api/util/certs"
containerutil "sigs.k8s.io/cluster-api/util/container"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/version"
)

const (
Expand Down Expand Up @@ -293,14 +294,14 @@ func (w *Workload) RemoveMachineFromKubeadmConfigMap(ctx context.Context, machin
}

// RemoveNodeFromKubeadmConfigMap removes the entry for the node from the kubeadm configmap.
func (w *Workload) RemoveNodeFromKubeadmConfigMap(ctx context.Context, name string, version semver.Version) error {
if version.GTE(minKubernetesVersionWithoutClusterStatus) {
func (w *Workload) RemoveNodeFromKubeadmConfigMap(ctx context.Context, name string, v semver.Version) error {
if version.Compare(v, minKubernetesVersionWithoutClusterStatus, version.WithoutPreReleases()) >= 0 {
return nil
}

return w.updateClusterStatus(ctx, func(s *bootstrapv1.ClusterStatus) {
delete(s.APIEndpoints, name)
}, version)
}, v)
}

// updateClusterStatus gets the ClusterStatus kubeadm-config ConfigMap, converts it to the
Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/workload_cluster_coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (w *Workload) updateCoreDNSImageInfoInKubeadmConfigMap(ctx context.Context,
// we have to update the ClusterRole accordingly.
func (w *Workload) updateCoreDNSClusterRole(ctx context.Context, kubernetesVersion semver.Version, info *coreDNSInfo) error {
// Do nothing for Kubernetes < 1.22.
if kubernetesVersion.LT(semver.Version{Major: 1, Minor: 22, Patch: 0}) {
if version.Compare(kubernetesVersion, semver.Version{Major: 1, Minor: 22, Patch: 0}, version.WithoutPreReleases()) < 0 {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ func TestUpdateCoreDNSClusterRole(t *testing.T) {
coreDNSPolicyRules: coreDNS180PolicyRules,
expectCoreDNSPolicyRules: coreDNS180PolicyRules,
},
{
name: "patch ClusterRole: Kubernetes == 1.22 alpha and CoreDNS == 1.8.1",
kubernetesVersion: semver.Version{Major: 1, Minor: 22, Patch: 0, Pre: []semver.PRVersion{{VersionStr: "alpha"}}},
coreDNSVersion: "1.8.1",
coreDNSPolicyRules: coreDNS180PolicyRules,
expectCoreDNSPolicyRules: coreDNS181PolicyRules,
},
{
name: "patch ClusterRole: Kubernetes == 1.22 and CoreDNS == 1.8.1",
kubernetesVersion: semver.Version{Major: 1, Minor: 22, Patch: 0},
Expand Down
7 changes: 7 additions & 0 deletions controlplane/kubeadm/internal/workload_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ func TestRemoveMachineFromKubeadmConfigMap(t *testing.T) {
kind: ClusterStatus
`),
},
{
name: "no op for Kubernetes version 1.22.0 alpha",
kubernetesVersion: semver.MustParse("1.22.0-alpha.0.734+ba502ee555924a"), // Kubernetes version >= 1.22.0 should not manage ClusterStatus
machine: machine,
objs: []client.Object{kubeadmConfigWithoutClusterStatus},
expectErr: false,
},
{
name: "no op for Kubernetes version >= 1.22.0",
kubernetesVersion: minKubernetesVersionWithoutClusterStatus, // Kubernetes version >= 1.22.0 should not manage ClusterStatus
Expand Down