This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
baremetalmachine_controller.go
324 lines (275 loc) · 11.5 KB
/
baremetalmachine_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
"github.com/go-logr/logr"
bmh "github.com/metal3-io/baremetal-operator/pkg/apis/metal3/v1alpha1"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
capbm "github.com/metal3-io/cluster-api-provider-baremetal/api/v1alpha2"
"github.com/metal3-io/cluster-api-provider-baremetal/baremetal"
capi "sigs.k8s.io/cluster-api/api/v1alpha2"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
machineControllerName = "BareMetalMachine-controller"
)
// BareMetalMachineReconciler reconciles a BareMetalMachine object
type BareMetalMachineReconciler struct {
Client client.Client
ManagerFactory baremetal.ManagerFactoryInterface
Log logr.Logger
CapiClientGetter baremetal.ClientGetter
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=baremetalmachines,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=baremetalmachines/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// Add RBAC rules to access cluster-api resources
// +kubebuilder:rbac:groups=metal3.io,resources=baremetalhosts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=metal3.io,resources=baremetalhosts/status,verbs=get;update;patch
// Reconcile handles BareMetalMachine events
func (r *BareMetalMachineReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr error) {
ctx := context.Background()
machineLog := r.Log.WithName(machineControllerName).WithValues("baremetal-machine", req.NamespacedName)
// Fetch the BareMetalMachine instance.
capbmMachine := &capbm.BareMetalMachine{}
if err := r.Client.Get(ctx, req.NamespacedName, capbmMachine); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
helper, err := patch.NewHelper(capbmMachine, r.Client)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to init patch helper")
}
// Always patch capbMachine exiting this function so we can persist any BareMetalMachine changes.
defer func() {
err := helper.Patch(ctx, capbmMachine)
if err != nil {
machineLog.Info("failed to Patch capbmMachine")
}
}()
//clear an error if one was previously set
clearErrorBMMachine(capbmMachine)
// Fetch the Machine.
capiMachine, err := util.GetOwnerMachine(ctx, r.Client, capbmMachine.ObjectMeta)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "BareMetalMachine's owner Machine could not be retrieved")
}
if capiMachine == nil {
machineLog.Info("Waiting for Machine Controller to set OwnerRef on BareMetalMachine")
return ctrl.Result{}, nil
}
machineLog = machineLog.WithValues("machine", capiMachine.Name)
// Fetch the Cluster.
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, capiMachine.ObjectMeta)
if err != nil {
machineLog.Info("BareMetalMachine's owner Machine is missing cluster label or cluster does not exist")
setErrorBMMachine(capbmMachine, "BareMetalMachine's owner Machine is missing cluster label or cluster does not exist", capierrors.InvalidConfigurationMachineError)
return ctrl.Result{}, errors.Wrapf(err, "BareMetalMachine's owner Machine is missing label or the cluster does not exist")
}
if cluster == nil {
setErrorBMMachine(capbmMachine, fmt.Sprintf(
"The machine is NOT associated with a cluster using the label %s: <name of cluster>",
capi.MachineClusterLabelName,
), capierrors.InvalidConfigurationMachineError)
machineLog.Info(fmt.Sprintf("The machine is NOT associated with a cluster using the label %s: <name of cluster>", capi.MachineClusterLabelName))
return ctrl.Result{}, nil
}
machineLog = machineLog.WithValues("cluster", cluster.Name)
// Make sure infrastructure is ready
if !cluster.Status.InfrastructureReady {
machineLog.Info("Waiting for BareMetalCluster Controller to create cluster infrastructure")
return ctrl.Result{}, nil
}
// Fetch the BareMetal Cluster.
baremetalCluster := &capbm.BareMetalCluster{}
baremetalClusterName := types.NamespacedName{
Namespace: capbmMachine.Namespace,
Name: cluster.Spec.InfrastructureRef.Name,
}
if err := r.Client.Get(ctx, baremetalClusterName, baremetalCluster); err != nil {
machineLog.Info("Waiting for BareMetalCluster Controller to create the BareMetalCluster")
return ctrl.Result{}, nil
}
machineLog = machineLog.WithValues("baremetal-cluster", baremetalCluster.Name)
// Create a helper for managing the baremetal container hosting the machine.
machineMgr, err := r.ManagerFactory.NewMachineManager(cluster, baremetalCluster, capiMachine, capbmMachine, machineLog)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to create helper for managing the machineMgr")
}
// Handle deleted machines
if !capbmMachine.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, machineMgr)
}
// Handle non-deleted machines
return r.reconcileNormal(ctx, machineMgr)
}
func (r *BareMetalMachineReconciler) reconcileNormal(ctx context.Context,
machineMgr baremetal.MachineManagerInterface,
) (ctrl.Result, error) {
// If the BareMetalMachine doesn't have finalizer, add it.
machineMgr.SetFinalizer()
// if the machine is already provisioned, return
if machineMgr.IsProvisioned() {
err := machineMgr.Update(ctx)
return ctrl.Result{}, err
}
// Make sure bootstrap data is available and populated.
if !machineMgr.IsBootstrapReady() {
return ctrl.Result{}, nil
}
// Check if the baremetalmachine was associated with a baremetalhost
if !machineMgr.HasAnnotation() {
//Associate the baremetalhost hosting the machine
err := machineMgr.Associate(ctx)
if err != nil {
return checkError(err, "failed to associate the BareMetalMachine to a BaremetalHost")
}
}
bmhID, err := machineMgr.GetBaremetalHostID(ctx)
if err != nil {
return checkError(err, "failed to get the providerID for the BaremetalMachine")
}
if bmhID != nil {
providerID := fmt.Sprintf("metal3://%s", *bmhID)
// Set the providerID on the node if no Cloud provider
err = machineMgr.SetNodeProviderID(*bmhID, providerID, r.CapiClientGetter)
if err != nil {
return checkError(err, "failed to get the providerID for the BaremetalMachine")
}
// Make sure Spec.ProviderID is set and mark the capbmMachine ready
machineMgr.SetProviderID(providerID)
}
err = machineMgr.Update(ctx)
return ctrl.Result{}, err
}
func (r *BareMetalMachineReconciler) reconcileDelete(ctx context.Context,
machineMgr baremetal.MachineManagerInterface,
) (ctrl.Result, error) {
// delete the machine
if err := machineMgr.Delete(ctx); err != nil {
return checkError(err, "failed to delete BareMetalMachine")
}
// BaremetalMachine is marked for deletion and ready to be deleted,
// so remove the finalizer.
machineMgr.UnsetFinalizer()
return ctrl.Result{}, nil
}
// SetupWithManager will add watches for this controller
func (r *BareMetalMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&capbm.BareMetalMachine{}).
Watches(
&source.Kind{Type: &capi.Machine{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: util.MachineToInfrastructureMapFunc(capbm.GroupVersion.WithKind("BareMetalMachine")),
},
).
Watches(
&source.Kind{Type: &capbm.BareMetalCluster{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(r.BareMetalClusterToBareMetalMachines),
},
).
Watches(
&source.Kind{Type: &bmh.BareMetalHost{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(r.BareMetalHostToBareMetalMachines),
},
).
Complete(r)
}
// BareMetalClusterToBareMetalMachines is a handler.ToRequestsFunc to be used to enqeue
// requests for reconciliation of BareMetalMachines.
func (r *BareMetalMachineReconciler) BareMetalClusterToBareMetalMachines(o handler.MapObject) []ctrl.Request {
result := []ctrl.Request{}
c, ok := o.Object.(*capbm.BareMetalCluster)
if !ok {
r.Log.Error(errors.Errorf("expected a BareMetalCluster but got a %T", o.Object), "failed to get BareMetalMachine for BareMetalCluster")
return nil
}
log := r.Log.WithValues("BareMetalCluster", c.Name, "Namespace", c.Namespace)
cluster, err := util.GetOwnerCluster(context.TODO(), r.Client, c.ObjectMeta)
switch {
case apierrors.IsNotFound(err) || cluster == nil:
return result
case err != nil:
log.Error(err, "failed to get owning cluster")
return result
}
labels := map[string]string{capi.MachineClusterLabelName: cluster.Name}
capiMachineList := &capi.MachineList{}
if err := r.Client.List(context.TODO(), capiMachineList, client.InNamespace(c.Namespace), client.MatchingLabels(labels)); err != nil {
log.Error(err, "failed to list BareMetalMachines")
return nil
}
for _, m := range capiMachineList.Items {
if m.Spec.InfrastructureRef.Name == "" {
continue
}
name := client.ObjectKey{Namespace: m.Namespace, Name: m.Name}
result = append(result, ctrl.Request{NamespacedName: name})
}
return result
}
// BareMetalHostToBareMetalMachines will return a reconcile request for a BareMetalMachine if the event is for a
// BareMetalHost and that BareMetalHost references a BareMetalMachine.
func (r *BareMetalMachineReconciler) BareMetalHostToBareMetalMachines(obj handler.MapObject) []ctrl.Request {
if host, ok := obj.Object.(*bmh.BareMetalHost); ok {
if host.Spec.ConsumerRef != nil &&
host.Spec.ConsumerRef.Kind == "BareMetalMachine" &&
host.Spec.ConsumerRef.APIVersion == capbm.GroupVersion.String() {
return []ctrl.Request{
ctrl.Request{
NamespacedName: types.NamespacedName{
Name: host.Spec.ConsumerRef.Name,
Namespace: host.Spec.ConsumerRef.Namespace,
},
},
}
}
}
return []ctrl.Request{}
}
// setError sets the ErrorMessage and ErrorReason fields on the baremetalmachine
func setErrorBMMachine(bmm *capbm.BareMetalMachine, message string, reason capierrors.MachineStatusError) {
bmm.Status.ErrorMessage = pointer.StringPtr(message)
bmm.Status.ErrorReason = &reason
}
// clearError removes the ErrorMessage from the baremetalmachine's Status if set.
func clearErrorBMMachine(bmm *capbm.BareMetalMachine) {
if bmm.Status.ErrorMessage != nil || bmm.Status.ErrorReason != nil {
bmm.Status.ErrorMessage = nil
bmm.Status.ErrorReason = nil
}
}
func checkError(err error, errMessage string) (ctrl.Result, error) {
if requeueErr, ok := errors.Cause(err).(baremetal.HasRequeueAfterError); ok {
return ctrl.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil
}
return ctrl.Result{}, errors.Wrap(err, errMessage)
}