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

Commit ec28869

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

File tree

1 file changed

+6
-35
lines changed

1 file changed

+6
-35
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,9 @@ 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
}
10484

@@ -107,18 +87,9 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
10787
return ctrl.Result{}, nil
10888
}
10989

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")
90+
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
91+
if err != nil {
92+
log.Error(err, "could not get cluster")
12293
return ctrl.Result{}, err
12394
}
12495

0 commit comments

Comments
 (0)