diff --git a/controllers/machine_helpers_test.go b/controllers/machine_helpers_test.go index ed6c50f0a096..de5b7e4c9f26 100644 --- a/controllers/machine_helpers_test.go +++ b/controllers/machine_helpers_test.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func TestMachineHealthCheckHasMatchingLabels(t *testing.T) { +func TestHasMatchingLabels(t *testing.T) { testCases := []struct { name string selector metav1.LabelSelector @@ -33,28 +33,24 @@ func TestMachineHealthCheckHasMatchingLabels(t *testing.T) { }{ { name: "selector matches labels", - selector: metav1.LabelSelector{ MatchLabels: map[string]string{ "foo": "bar", }, }, - labels: map[string]string{ - "foo": "bar", + "foo": "bar", + "more": "labels", }, - expected: true, }, { name: "selector does not match labels", - selector: metav1.LabelSelector{ MatchLabels: map[string]string{ "foo": "bar", }, }, - labels: map[string]string{ "no": "match", }, @@ -67,7 +63,7 @@ func TestMachineHealthCheckHasMatchingLabels(t *testing.T) { expected: false, }, { - name: "seelctor is invalid", + name: "selector is invalid", selector: metav1.LabelSelector{ MatchLabels: map[string]string{ "foo": "bar", diff --git a/controllers/machineset_controller.go b/controllers/machineset_controller.go index 290281bb335a..eab385b0f05a 100644 --- a/controllers/machineset_controller.go +++ b/controllers/machineset_controller.go @@ -534,8 +534,8 @@ func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Requ } } - mss := r.getMachineSetsForMachine(context.TODO(), m) - if len(mss) == 0 { + mss, err := r.getMachineSetsForMachine(context.TODO(), m) + if len(mss) == 0 || err != nil { return nil } @@ -547,53 +547,32 @@ func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Requ return result } -func (r *MachineSetReconciler) getMachineSetsForMachine(ctx context.Context, m *clusterv1.Machine) []*clusterv1.MachineSet { +func (r *MachineSetReconciler) getMachineSetsForMachine(ctx context.Context, m *clusterv1.Machine) ([]*clusterv1.MachineSet, error) { log := ctrl.LoggerFrom(ctx, "machine", m.Name) if len(m.Labels) == 0 { log.Info("No machine sets found because it has no labels") - return nil + return nil, nil } msList := &clusterv1.MachineSetList{} - err := r.Client.List(ctx, msList, client.InNamespace(m.Namespace)) - if err != nil { - log.Error(err, "Failed to list machine sets") - return nil + if err := r.Client.List(ctx, msList, client.InNamespace(m.Namespace)); err != nil { + return nil, fmt.Errorf("failed to list machine sets: %w", err) } var mss []*clusterv1.MachineSet for idx := range msList.Items { ms := &msList.Items[idx] - if r.hasMatchingLabels(ctx, ms, m) { + if r.hasMatchingLabels(ms, m) { mss = append(mss, ms) } } - return mss + return mss, nil } -func (r *MachineSetReconciler) hasMatchingLabels(ctx context.Context, machineSet *clusterv1.MachineSet, machine *clusterv1.Machine) bool { - log := ctrl.LoggerFrom(ctx, "machine", machine.Name) - - selector, err := metav1.LabelSelectorAsSelector(&machineSet.Spec.Selector) - if err != nil { - log.Error(err, "Unable to convert selector") - return false - } - - // If a deployment with a nil or empty selector creeps in, it should match nothing, not everything. - if selector.Empty() { - log.V(2).Info("Machineset has empty selector") - return false - } - - if !selector.Matches(labels.Set(machine.Labels)) { - log.V(4).Info("Machine has mismatch labels") - return false - } - - return true +func (r *MachineSetReconciler) hasMatchingLabels(machineSet *clusterv1.MachineSet, machine *clusterv1.Machine) bool { + return hasMatchingLabels(machineSet.Spec.Selector, machine.Labels) } func (r *MachineSetReconciler) shouldAdopt(ms *clusterv1.MachineSet) bool { diff --git a/controllers/machineset_controller_test.go b/controllers/machineset_controller_test.go index 18c0f87fdecc..eadab1d92810 100644 --- a/controllers/machineset_controller_test.go +++ b/controllers/machineset_controller_test.go @@ -690,108 +690,6 @@ func TestAdoptOrphan(t *testing.T) { } } -func TestHasMatchingLabels(t *testing.T) { - r := &MachineSetReconciler{} - - testCases := []struct { - name string - machineSet clusterv1.MachineSet - machine clusterv1.Machine - expected bool - }{ - { - name: "machine set and machine have matching labels", - machineSet: clusterv1.MachineSet{ - Spec: clusterv1.MachineSetSpec{ - Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "foo": "bar", - }, - }, - }, - }, - machine: clusterv1.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "matchSelector", - Labels: map[string]string{ - "foo": "bar", - }, - }, - }, - expected: true, - }, - { - name: "machine set and machine do not have matching labels", - machineSet: clusterv1.MachineSet{ - Spec: clusterv1.MachineSetSpec{ - Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "foo": "bar", - }, - }, - }, - }, - machine: clusterv1.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "doesNotMatchSelector", - Labels: map[string]string{ - "no": "match", - }, - }, - }, - expected: false, - }, - { - name: "machine set has empty selector", - machineSet: clusterv1.MachineSet{ - Spec: clusterv1.MachineSetSpec{ - Selector: metav1.LabelSelector{}, - }, - }, - machine: clusterv1.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "doesNotMatter", - }, - }, - expected: false, - }, - { - name: "machine set has bad selector", - machineSet: clusterv1.MachineSet{ - Spec: clusterv1.MachineSetSpec{ - Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "foo": "bar", - }, - MatchExpressions: []metav1.LabelSelectorRequirement{ - { - Operator: "bad-operator", - }, - }, - }, - }, - }, - machine: clusterv1.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Name: "match", - Labels: map[string]string{ - "foo": "bar", - }, - }, - }, - expected: false, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - g := NewWithT(t) - got := r.hasMatchingLabels(ctx, &tc.machineSet, &tc.machine) - g.Expect(got).To(Equal(tc.expected)) - }) - } -} - func newMachineSet(name, cluster string) *clusterv1.MachineSet { var replicas int32 return &clusterv1.MachineSet{ diff --git a/go.mod b/go.mod index 18012fb36f97..07fd1d59c642 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( k8s.io/klog/v2 v2.8.0 k8s.io/kubectl v0.21.0 k8s.io/utils v0.0.0-20210305010621-2afb4311ab10 - sigs.k8s.io/controller-runtime v0.9.0-beta.1 + sigs.k8s.io/controller-runtime v0.9.0-beta.5 sigs.k8s.io/kind v0.9.0 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index 0b523486f2d2..e05728d2c608 100644 --- a/go.sum +++ b/go.sum @@ -1070,8 +1070,8 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8 rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg= -sigs.k8s.io/controller-runtime v0.9.0-beta.1 h1:pmJVlNQeVMtkszjrRP59XT55Ny7+1it7ri3yyukwYTE= -sigs.k8s.io/controller-runtime v0.9.0-beta.1/go.mod h1:ufPDuvefw2Y1KnBgHQrLdOjueYlj+XJV2AszbT+WTxs= +sigs.k8s.io/controller-runtime v0.9.0-beta.5 h1:yteq8am8lUFvlI/bfIgySMp/MqAPXiAghxX+XrMCifE= +sigs.k8s.io/controller-runtime v0.9.0-beta.5/go.mod h1:ufPDuvefw2Y1KnBgHQrLdOjueYlj+XJV2AszbT+WTxs= sigs.k8s.io/kind v0.9.0 h1:SoDlXq6pEc7dGagHULNRCCBYrLH6xOi7lqXTRXeAlg4= sigs.k8s.io/kind v0.9.0/go.mod h1:cxKQWwmbtRDzQ+RNKnR6gZG6fjbeTtItp5cGf+ww+1Y= sigs.k8s.io/kustomize/api v0.8.5/go.mod h1:M377apnKT5ZHJS++6H4rQoCHmWtt6qTpp3mbe7p6OLY= diff --git a/test/infrastructure/docker/go.mod b/test/infrastructure/docker/go.mod index bc859131779a..bcd9e610340a 100644 --- a/test/infrastructure/docker/go.mod +++ b/test/infrastructure/docker/go.mod @@ -14,7 +14,7 @@ require ( k8s.io/klog/v2 v2.8.0 k8s.io/utils v0.0.0-20210305010621-2afb4311ab10 sigs.k8s.io/cluster-api v0.3.3 - sigs.k8s.io/controller-runtime v0.9.0-beta.1 + sigs.k8s.io/controller-runtime v0.9.0-beta.5 sigs.k8s.io/kind v0.9.0 sigs.k8s.io/yaml v1.2.0 ) diff --git a/test/infrastructure/docker/go.sum b/test/infrastructure/docker/go.sum index b7b4bdc90d87..60e10975afdc 100644 --- a/test/infrastructure/docker/go.sum +++ b/test/infrastructure/docker/go.sum @@ -999,8 +999,8 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8 rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg= -sigs.k8s.io/controller-runtime v0.9.0-beta.1 h1:pmJVlNQeVMtkszjrRP59XT55Ny7+1it7ri3yyukwYTE= -sigs.k8s.io/controller-runtime v0.9.0-beta.1/go.mod h1:ufPDuvefw2Y1KnBgHQrLdOjueYlj+XJV2AszbT+WTxs= +sigs.k8s.io/controller-runtime v0.9.0-beta.5 h1:yteq8am8lUFvlI/bfIgySMp/MqAPXiAghxX+XrMCifE= +sigs.k8s.io/controller-runtime v0.9.0-beta.5/go.mod h1:ufPDuvefw2Y1KnBgHQrLdOjueYlj+XJV2AszbT+WTxs= sigs.k8s.io/kind v0.9.0 h1:SoDlXq6pEc7dGagHULNRCCBYrLH6xOi7lqXTRXeAlg4= sigs.k8s.io/kind v0.9.0/go.mod h1:cxKQWwmbtRDzQ+RNKnR6gZG6fjbeTtItp5cGf+ww+1Y= sigs.k8s.io/kustomize/api v0.8.5/go.mod h1:M377apnKT5ZHJS++6H4rQoCHmWtt6qTpp3mbe7p6OLY=