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

Commit 5b847ed

Browse files
committed
Use GetClusterFromMetadata from CAPI
Signed-off-by: Chuck Ha <[email protected]>
1 parent 08dafc3 commit 5b847ed

File tree

3 files changed

+51
-45
lines changed

3 files changed

+51
-45
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 acquried 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
@@ -76,49 +76,24 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
7676
return ctrl.Result{}, nil
7777
}
7878

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

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

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

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

0 commit comments

Comments
 (0)