From 81d483944a68f7b084f41453cb99c04468691ecb Mon Sep 17 00:00:00 2001 From: Richard Draycott Date: Tue, 14 May 2024 15:22:37 +0200 Subject: [PATCH] feat: Add more machinefilter test cases and exclude version from check Signed-off-by: Richard Draycott --- pkg/machinefilters/machine_filters.go | 3 + pkg/machinefilters/machine_filters_test.go | 85 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pkg/machinefilters/machine_filters.go b/pkg/machinefilters/machine_filters.go index 9bc8f263..bc7a2aa8 100644 --- a/pkg/machinefilters/machine_filters.go +++ b/pkg/machinefilters/machine_filters.go @@ -114,6 +114,9 @@ func MatchesKThreesBootstrapConfig(machineConfigs map[string]*bootstrapv1.KThree // we are cleaning up from the reflect.DeepEqual comparison. kcpConfig.ServerConfig = bootstrapv1.KThreesServerConfig{} machineConfig.Spec.ServerConfig = bootstrapv1.KThreesServerConfig{} + // KCP version check is handled elsewhere + kcpConfig.Version = "" + machineConfig.Spec.Version = "" return reflect.DeepEqual(&machineConfig.Spec, kcpConfig) } diff --git a/pkg/machinefilters/machine_filters_test.go b/pkg/machinefilters/machine_filters_test.go index df5b8741..e52e4cf4 100644 --- a/pkg/machinefilters/machine_filters_test.go +++ b/pkg/machinefilters/machine_filters_test.go @@ -4,6 +4,7 @@ import ( "testing" . "github.com/onsi/gomega" + "google.golang.org/protobuf/proto" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -109,6 +110,90 @@ func TestMatchesKubeadmBootstrapConfig(t *testing.T) { g.Expect(match).To(BeFalse()) }) + t.Run("Should match on other configurations", func(t *testing.T) { + kThreesConfigSpec := bootstrapv1.KThreesConfigSpec{ + Files: []bootstrapv1.File{}, + PreK3sCommands: []string{"test"}, + PostK3sCommands: []string{"test"}, + AgentConfig: bootstrapv1.KThreesAgentConfig{ + NodeName: "test-node", + NodeTaints: []string{"node-role.kubernetes.io/control-plane:NoSchedule"}, + KubeProxyArgs: []string{"metrics-bind-address=0.0.0.0"}, + }, + ServerConfig: bootstrapv1.KThreesServerConfig{ + DisableComponents: []string{"traefik"}, + KubeControllerManagerArgs: []string{"bind-address=0.0.0.0"}, + KubeSchedulerArgs: []string{"bind-address=0.0.0.0"}, + }, + } + kcp := &controlplanev1.KThreesControlPlane{ + Spec: controlplanev1.KThreesControlPlaneSpec{ + Replicas: proto.Int32(3), + Version: "v1.13.14+k3s1", + MachineTemplate: controlplanev1.KThreesControlPlaneMachineTemplate{ + ObjectMeta: clusterv1.ObjectMeta{ + Labels: map[string]string{"test-label": "test-value"}, + }, + }, + KThreesConfigSpec: kThreesConfigSpec, + }, + } + + m := &clusterv1.Machine{ + TypeMeta: metav1.TypeMeta{ + Kind: "KThreesConfig", + APIVersion: clusterv1.GroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: "default", + Name: "test", + }, + Spec: clusterv1.MachineSpec{ + Bootstrap: clusterv1.Bootstrap{ + ConfigRef: &corev1.ObjectReference{ + Kind: "KThreesConfig", + Namespace: "default", + Name: "test", + APIVersion: bootstrapv1.GroupVersion.String(), + }, + }, + }, + } + machineConfigs := map[string]*bootstrapv1.KThreesConfig{ + m.Name: { + TypeMeta: metav1.TypeMeta{ + Kind: "KThreesConfig", + APIVersion: bootstrapv1.GroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: "default", + Name: "test", + }, + Spec: kThreesConfigSpec, + }, + } + + t.Run("by returning true if all configs match", func(t *testing.T) { + g := NewWithT(t) + match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) + g.Expect(match).To(BeTrue()) + }) + + t.Run("by returning false if post commands don't match", func(t *testing.T) { + g := NewWithT(t) + machineConfigs[m.Name].Spec.PostK3sCommands = []string{"new-test"} + match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) + g.Expect(match).To(BeFalse()) + }) + + t.Run("by returning false if agent configs don't match", func(t *testing.T) { + g := NewWithT(t) + machineConfigs[m.Name].Spec.AgentConfig.KubeletArgs = []string{"test-arg"} + match := MatchesKThreesBootstrapConfig(machineConfigs, kcp)(m) + g.Expect(match).To(BeFalse()) + }) + }) + t.Run("should match on labels and annotations", func(t *testing.T) { kcp := &controlplanev1.KThreesControlPlane{ Spec: controlplanev1.KThreesControlPlaneSpec{