@@ -76,29 +76,9 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
76
76
return ctrl.Result {}, nil
77
77
}
78
78
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" )
102
82
return ctrl.Result {}, err
103
83
}
104
84
@@ -107,18 +87,9 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
107
87
return ctrl.Result {}, nil
108
88
}
109
89
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" )
122
93
return ctrl.Result {}, err
123
94
}
124
95
0 commit comments