Skip to content

Commit

Permalink
Ignore kubernetes version prefix for machine filter (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 committed Jul 12, 2024
1 parent 7d278a5 commit 8f64ad0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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")
}
}

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))
})
}
}

0 comments on commit 8f64ad0

Please sign in to comment.