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

Commit 7f96bf3

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

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

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")
12297
return ctrl.Result{}, err
12398
}
12499

0 commit comments

Comments
 (0)