Skip to content

Commit 45a0abb

Browse files
authored
Merge pull request #4652 from enxebre/hasMatchingLabels
🌱 Reuse `hasMatchingLabels` in MachineSet controller
2 parents 798007a + 9ad4ea8 commit 45a0abb

File tree

3 files changed

+18
-145
lines changed

3 files changed

+18
-145
lines changed

controllers/machine_helpers_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
)
2626

27-
func TestMachineHealthCheckHasMatchingLabels(t *testing.T) {
27+
func TestHasMatchingLabels(t *testing.T) {
2828
testCases := []struct {
2929
name string
3030
selector metav1.LabelSelector
@@ -33,28 +33,24 @@ func TestMachineHealthCheckHasMatchingLabels(t *testing.T) {
3333
}{
3434
{
3535
name: "selector matches labels",
36-
3736
selector: metav1.LabelSelector{
3837
MatchLabels: map[string]string{
3938
"foo": "bar",
4039
},
4140
},
42-
4341
labels: map[string]string{
44-
"foo": "bar",
42+
"foo": "bar",
43+
"more": "labels",
4544
},
46-
4745
expected: true,
4846
},
4947
{
5048
name: "selector does not match labels",
51-
5249
selector: metav1.LabelSelector{
5350
MatchLabels: map[string]string{
5451
"foo": "bar",
5552
},
5653
},
57-
5854
labels: map[string]string{
5955
"no": "match",
6056
},
@@ -67,7 +63,7 @@ func TestMachineHealthCheckHasMatchingLabels(t *testing.T) {
6763
expected: false,
6864
},
6965
{
70-
name: "seelctor is invalid",
66+
name: "selector is invalid",
7167
selector: metav1.LabelSelector{
7268
MatchLabels: map[string]string{
7369
"foo": "bar",

controllers/machineset_controller.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ func (r *MachineSetReconciler) waitForMachineDeletion(ctx context.Context, machi
519519
// MachineToMachineSets is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
520520
// for MachineSets that might adopt an orphaned Machine.
521521
func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Request {
522+
ctx := context.Background()
523+
// This won't log unless the global logger is set
524+
log := ctrl.LoggerFrom(ctx, "object", client.ObjectKeyFromObject(o))
522525
result := []ctrl.Request{}
523526

524527
m, ok := o.(*clusterv1.Machine)
@@ -534,7 +537,11 @@ func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Requ
534537
}
535538
}
536539

537-
mss := r.getMachineSetsForMachine(context.TODO(), m)
540+
mss, err := r.getMachineSetsForMachine(ctx, m)
541+
if err != nil {
542+
log.Error(err, "Failed getting MachineSets for Machine")
543+
return nil
544+
}
538545
if len(mss) == 0 {
539546
return nil
540547
}
@@ -547,53 +554,25 @@ func (r *MachineSetReconciler) MachineToMachineSets(o client.Object) []ctrl.Requ
547554
return result
548555
}
549556

550-
func (r *MachineSetReconciler) getMachineSetsForMachine(ctx context.Context, m *clusterv1.Machine) []*clusterv1.MachineSet {
551-
log := ctrl.LoggerFrom(ctx, "machine", m.Name)
552-
557+
func (r *MachineSetReconciler) getMachineSetsForMachine(ctx context.Context, m *clusterv1.Machine) ([]*clusterv1.MachineSet, error) {
553558
if len(m.Labels) == 0 {
554-
log.Info("No machine sets found because it has no labels")
555-
return nil
559+
return nil, fmt.Errorf("machine %v has no labels, this is unexpected", client.ObjectKeyFromObject(m))
556560
}
557561

558562
msList := &clusterv1.MachineSetList{}
559-
err := r.Client.List(ctx, msList, client.InNamespace(m.Namespace))
560-
if err != nil {
561-
log.Error(err, "Failed to list machine sets")
562-
return nil
563+
if err := r.Client.List(ctx, msList, client.InNamespace(m.Namespace)); err != nil {
564+
return nil, errors.Wrapf(err, "failed to list MachineSets")
563565
}
564566

565567
var mss []*clusterv1.MachineSet
566568
for idx := range msList.Items {
567569
ms := &msList.Items[idx]
568-
if r.hasMatchingLabels(ctx, ms, m) {
570+
if hasMatchingLabels(ms.Spec.Selector, m.Labels) {
569571
mss = append(mss, ms)
570572
}
571573
}
572574

573-
return mss
574-
}
575-
576-
func (r *MachineSetReconciler) hasMatchingLabels(ctx context.Context, machineSet *clusterv1.MachineSet, machine *clusterv1.Machine) bool {
577-
log := ctrl.LoggerFrom(ctx, "machine", machine.Name)
578-
579-
selector, err := metav1.LabelSelectorAsSelector(&machineSet.Spec.Selector)
580-
if err != nil {
581-
log.Error(err, "Unable to convert selector")
582-
return false
583-
}
584-
585-
// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
586-
if selector.Empty() {
587-
log.V(2).Info("Machineset has empty selector")
588-
return false
589-
}
590-
591-
if !selector.Matches(labels.Set(machine.Labels)) {
592-
log.V(4).Info("Machine has mismatch labels")
593-
return false
594-
}
595-
596-
return true
575+
return mss, nil
597576
}
598577

599578
func (r *MachineSetReconciler) shouldAdopt(ms *clusterv1.MachineSet) bool {

controllers/machineset_controller_test.go

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -675,108 +675,6 @@ func TestAdoptOrphan(t *testing.T) {
675675
}
676676
}
677677

678-
func TestHasMatchingLabels(t *testing.T) {
679-
r := &MachineSetReconciler{}
680-
681-
testCases := []struct {
682-
name string
683-
machineSet clusterv1.MachineSet
684-
machine clusterv1.Machine
685-
expected bool
686-
}{
687-
{
688-
name: "machine set and machine have matching labels",
689-
machineSet: clusterv1.MachineSet{
690-
Spec: clusterv1.MachineSetSpec{
691-
Selector: metav1.LabelSelector{
692-
MatchLabels: map[string]string{
693-
"foo": "bar",
694-
},
695-
},
696-
},
697-
},
698-
machine: clusterv1.Machine{
699-
ObjectMeta: metav1.ObjectMeta{
700-
Name: "matchSelector",
701-
Labels: map[string]string{
702-
"foo": "bar",
703-
},
704-
},
705-
},
706-
expected: true,
707-
},
708-
{
709-
name: "machine set and machine do not have matching labels",
710-
machineSet: clusterv1.MachineSet{
711-
Spec: clusterv1.MachineSetSpec{
712-
Selector: metav1.LabelSelector{
713-
MatchLabels: map[string]string{
714-
"foo": "bar",
715-
},
716-
},
717-
},
718-
},
719-
machine: clusterv1.Machine{
720-
ObjectMeta: metav1.ObjectMeta{
721-
Name: "doesNotMatchSelector",
722-
Labels: map[string]string{
723-
"no": "match",
724-
},
725-
},
726-
},
727-
expected: false,
728-
},
729-
{
730-
name: "machine set has empty selector",
731-
machineSet: clusterv1.MachineSet{
732-
Spec: clusterv1.MachineSetSpec{
733-
Selector: metav1.LabelSelector{},
734-
},
735-
},
736-
machine: clusterv1.Machine{
737-
ObjectMeta: metav1.ObjectMeta{
738-
Name: "doesNotMatter",
739-
},
740-
},
741-
expected: false,
742-
},
743-
{
744-
name: "machine set has bad selector",
745-
machineSet: clusterv1.MachineSet{
746-
Spec: clusterv1.MachineSetSpec{
747-
Selector: metav1.LabelSelector{
748-
MatchLabels: map[string]string{
749-
"foo": "bar",
750-
},
751-
MatchExpressions: []metav1.LabelSelectorRequirement{
752-
{
753-
Operator: "bad-operator",
754-
},
755-
},
756-
},
757-
},
758-
},
759-
machine: clusterv1.Machine{
760-
ObjectMeta: metav1.ObjectMeta{
761-
Name: "match",
762-
Labels: map[string]string{
763-
"foo": "bar",
764-
},
765-
},
766-
},
767-
expected: false,
768-
},
769-
}
770-
771-
for _, tc := range testCases {
772-
t.Run(tc.name, func(t *testing.T) {
773-
g := NewWithT(t)
774-
got := r.hasMatchingLabels(ctx, &tc.machineSet, &tc.machine)
775-
g.Expect(got).To(Equal(tc.expected))
776-
})
777-
}
778-
}
779-
780678
func newMachineSet(name, cluster string) *clusterv1.MachineSet {
781679
var replicas int32
782680
return &clusterv1.MachineSet{

0 commit comments

Comments
 (0)