Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit c3315b0

Browse files
authored
Merge pull request #106 from chuckha/util-fn
Use CAPI utils to reduce boilerplate
2 parents 8e3a5ce + 6bebe9e commit c3315b0

File tree

5 files changed

+62
-52
lines changed

5 files changed

+62
-52
lines changed

controllers/control_plane_init_locker_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ import (
3232
)
3333

3434
func TestControlPlaneInitLockerAcquire(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
getError error
38+
}{
39+
{
40+
name: "create succeeds",
41+
getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"),
42+
},
43+
}
44+
45+
for _, tc := range tests {
46+
t.Run(tc.name, func(t *testing.T) {
47+
l := &controlPlaneInitLocker{
48+
log: log.ZapLogger(true),
49+
configMapClient: &configMapsGetter{
50+
getError: tc.getError,
51+
},
52+
}
53+
54+
cluster := &clusterv2.Cluster{
55+
ObjectMeta: metav1.ObjectMeta{
56+
Namespace: "ns1",
57+
Name: "name1",
58+
UID: types.UID("uid1"),
59+
},
60+
}
61+
62+
acquired := l.Acquire(cluster)
63+
if !acquired {
64+
t.Fatal("acquired was false but it should have been true")
65+
}
66+
})
67+
}
68+
}
69+
70+
func TestControlPlaneInitLockerAcquireErrors(t *testing.T) {
3571
tests := []struct {
3672
name string
3773
configMap *v1.ConfigMap
@@ -49,11 +85,6 @@ func TestControlPlaneInitLockerAcquire(t *testing.T) {
4985
getError: errors.New("get error"),
5086
expectAcquire: false,
5187
},
52-
{
53-
name: "create succeeds",
54-
getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"),
55-
expectAcquire: true,
56-
},
5788
{
5889
name: "create fails",
5990
getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"),
@@ -82,8 +113,8 @@ func TestControlPlaneInitLockerAcquire(t *testing.T) {
82113
}
83114

84115
acquired := l.Acquire(cluster)
85-
if tc.expectAcquire != acquired {
86-
t.Errorf("expected %t, got %t", tc.expectAcquire, acquired)
116+
if acquired {
117+
t.Fatal("expected acquired to be false but it is true")
87118
}
88119
})
89120
}

controllers/kubeadmconfig_controller.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,24 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
7777
return ctrl.Result{}, nil
7878
}
7979

80-
// Find the owner reference
81-
// The cluster-api machine controller set this value.
82-
var machineRef *v1.OwnerReference
83-
for _, ref := range config.OwnerReferences {
84-
if ref.Kind == machineKind.Kind && ref.APIVersion == machineKind.GroupVersion().String() {
85-
machineRef = &ref
86-
break
87-
}
88-
}
89-
if machineRef == nil {
90-
log.Info("did not find matching machine reference")
91-
return ctrl.Result{}, nil
92-
}
93-
94-
// Get the machine
95-
machine := &capiv1alpha2.Machine{}
96-
machineKey := client.ObjectKey{
97-
Namespace: req.Namespace,
98-
Name: machineRef.Name,
99-
}
100-
101-
if err := r.Get(ctx, machineKey, machine); err != nil {
102-
log.Error(err, "failed to get machine")
80+
machine, err := util.GetOwnerMachine(ctx, r.Client, config.ObjectMeta)
81+
if err != nil {
82+
log.Error(err, "could not get owner machine")
10383
return ctrl.Result{}, err
10484
}
85+
if machine == nil {
86+
log.Info("Waiting for Machine Controller to set OwnerRef on the KubeadmConfig")
87+
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
88+
}
10589

10690
// Ignore machines that already have bootstrap data
10791
if machine.Spec.Bootstrap.Data != nil {
10892
return ctrl.Result{}, nil
10993
}
11094

111-
if machine.Labels[capiv1alpha2.MachineClusterLabelName] == "" {
112-
return ctrl.Result{}, errors.New("machine has no associated cluster")
113-
}
114-
115-
// Get the cluster
116-
cluster := &capiv1alpha2.Cluster{}
117-
clusterKey := client.ObjectKey{
118-
Namespace: req.Namespace,
119-
Name: machine.Labels[capiv1alpha2.MachineClusterLabelName],
120-
}
121-
if err := r.Get(ctx, clusterKey, cluster); err != nil {
122-
log.Error(err, "failed to get cluster")
95+
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
96+
if err != nil {
97+
log.Error(err, "could not get cluster by machine metadata")
12398
return ctrl.Result{}, err
12499
}
125100

controllers/kubeadmconfig_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestBailIfKubeadmConfigStatusReady(t *testing.T) {
7474
}
7575
}
7676

77-
func TestBailIfNoMachineRefIsSet(t *testing.T) {
77+
func TestRequeueIfNoMachineRefIsSet(t *testing.T) {
7878
config := newKubeadmConfig(nil, "cfg") // intentionally omitting machine
7979
objects := []runtime.Object{
8080
config,
@@ -99,8 +99,8 @@ func TestBailIfNoMachineRefIsSet(t *testing.T) {
9999
if result.Requeue == true {
100100
t.Fatal("did not expected to requeue")
101101
}
102-
if result.RequeueAfter != time.Duration(0) {
103-
t.Fatal("did not expected to requeue after")
102+
if result.RequeueAfter == time.Duration(0) {
103+
t.Fatal("Expected a requeue but did not get one")
104104
}
105105
}
106106

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
1414
k8s.io/cluster-bootstrap v0.0.0-20190516232516-d7d78ab2cfe7
1515
k8s.io/klog v0.3.3
16-
sigs.k8s.io/cluster-api v0.0.0-20190725170330-835ee872f98d
17-
sigs.k8s.io/controller-runtime v0.2.0-beta.4
16+
k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a // indirect
17+
sigs.k8s.io/cluster-api v0.0.0-20190809134526-b8af96f0a42a
18+
sigs.k8s.io/controller-runtime v0.2.0-beta.5
1819
)

go.sum

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
5252
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5353
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
5454
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
55+
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
5556
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
5657
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
5758
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
@@ -248,11 +249,13 @@ k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c h1:3KSCztE7gPitlZmWbNwue/
248249
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
249250
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 h1:VBM/0P5TWxwk+Nw6Z+lAw3DKgO76g90ETOiA6rfLV1Y=
250251
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
251-
sigs.k8s.io/cluster-api v0.0.0-20190725170330-835ee872f98d h1:DjeaMyqyw5tpps/prbudaojzp8gRMPV6L6MU4C8M8Eg=
252-
sigs.k8s.io/cluster-api v0.0.0-20190725170330-835ee872f98d/go.mod h1:DIiQEP2FjsAiEgBTgT5EEUAVM7XRy7aLgeERFXvZHaQ=
253-
sigs.k8s.io/controller-runtime v0.2.0-beta.4 h1:S1XVfRWR1MuIXZdkYx3jN8JDw+bbQxmWZroy0i87z/A=
254-
sigs.k8s.io/controller-runtime v0.2.0-beta.4/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
255-
sigs.k8s.io/controller-tools v0.2.0-beta.4/go.mod h1:8t/X+FVWvk6TaBcsa+UKUBbn7GMtvyBKX30SGl4em6Y=
252+
k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a h1:2jUDc9gJja832Ftp+QbDV0tVhQHMISFn01els+2ZAcw=
253+
k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
254+
sigs.k8s.io/cluster-api v0.0.0-20190809134526-b8af96f0a42a h1:aDiczQxkpWlxe5yzqm6rJNe5orMuHTt7Q0MqDJOJ/bg=
255+
sigs.k8s.io/cluster-api v0.0.0-20190809134526-b8af96f0a42a/go.mod h1:CgxuxJy6W8onO8duP2ED+s0kwNxsNWIGEXMnSUsxGcw=
256+
sigs.k8s.io/controller-runtime v0.2.0-beta.5 h1:W2jTb239QEwQ+HejhTCF9GriFPy2zmo1I6pPmJTeEy8=
257+
sigs.k8s.io/controller-runtime v0.2.0-beta.5/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
258+
sigs.k8s.io/controller-tools v0.2.0-beta.5/go.mod h1:8t/X+FVWvk6TaBcsa+UKUBbn7GMtvyBKX30SGl4em6Y=
256259
sigs.k8s.io/testing_frameworks v0.1.1/go.mod h1:VVBKrHmJ6Ekkfz284YKhQePcdycOzNH9qL6ht1zEr/U=
257260
sigs.k8s.io/testing_frameworks v0.1.2-0.20190130140139-57f07443c2d4 h1:GtDhkj3cF4A4IW+A9LScsuxvJqA9DE7G7PGH1f8B07U=
258261
sigs.k8s.io/testing_frameworks v0.1.2-0.20190130140139-57f07443c2d4/go.mod h1:VVBKrHmJ6Ekkfz284YKhQePcdycOzNH9qL6ht1zEr/U=

0 commit comments

Comments
 (0)