@@ -76,49 +76,24 @@ 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
}
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
+ }
104
88
105
89
// Ignore machines that already have bootstrap data
106
90
if machine .Spec .Bootstrap .Data != nil {
107
91
return ctrl.Result {}, nil
108
92
}
109
93
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" )
122
97
return ctrl.Result {}, err
123
98
}
124
99
0 commit comments