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

Ignore kubernetes version prefix for machine filter #26

Merged
merged 2 commits into from
Jul 12, 2024
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
3 changes: 2 additions & 1 deletion pkg/machinefilters/machine_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package machinefilters

import (
"reflect"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -78,7 +79,7 @@ func MatchesKubernetesVersion(kubernetesVersion string) Func {
if machine.Spec.Version == nil {
return false
}
return *machine.Spec.Version == kubernetesVersion
return strings.TrimPrefix(*machine.Spec.Version, "v") == strings.TrimPrefix(kubernetesVersion, "v")
bschimke95 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
47 changes: 47 additions & 0 deletions pkg/machinefilters/machine_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"google.golang.org/protobuf/proto"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

bootstrapv1 "github.com/canonical/cluster-api-k8s/bootstrap/api/v1beta2"
Expand Down Expand Up @@ -283,3 +284,49 @@ func TestMatchesCK8sBootstrapConfig(t *testing.T) {
})
})
}
func TestMatchesKubernetesVersion(t *testing.T) {
tests := []struct {
name string
kubernetesVersion string
expectedMatch bool
machine *clusterv1.Machine
}{
{
name: "returns true if machine's version matches",
kubernetesVersion: "v1.30.0",
expectedMatch: true,
machine: &clusterv1.Machine{Spec: clusterv1.MachineSpec{Version: ptr.To("v1.30.0")}},
},
{
name: "ignores 'v' prefix",
kubernetesVersion: "1.30.0",
expectedMatch: true,
machine: &clusterv1.Machine{Spec: clusterv1.MachineSpec{Version: ptr.To("v1.30.0")}},
},
{
name: "returns false if machine's version does not match",
kubernetesVersion: "v1.30.0",
expectedMatch: false,
machine: &clusterv1.Machine{Spec: clusterv1.MachineSpec{Version: ptr.To("v1.29.0")}},
},
{
name: "returns false if machine's version is nil",
kubernetesVersion: "v1.30.0",
expectedMatch: false,
machine: &clusterv1.Machine{Spec: clusterv1.MachineSpec{Version: nil}},
},
{
name: "returns false if machine is nil",
expectedMatch: false,
machine: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)
match := MatchesKubernetesVersion(test.kubernetesVersion)(test.machine)
g.Expect(match).To(Equal(test.expectedMatch))
})
}
}