Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#4638 from vincepri/cr090b4
Browse files Browse the repository at this point in the history
🌱 Update controller runtime to v0.9.0-beta.5
  • Loading branch information
k8s-ci-robot authored and enxebre committed May 24, 2021
2 parents a10e659 + 493fb80 commit 2844d93
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 147 deletions.
12 changes: 4 additions & 8 deletions controllers/machine_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
},
Expand All @@ -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",
Expand Down
41 changes: 10 additions & 31 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
102 changes: 0 additions & 102 deletions controllers/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion test/infrastructure/docker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions test/infrastructure/docker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 2844d93

Please sign in to comment.