-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathironic_controller.go
More file actions
349 lines (302 loc) · 12.9 KB
/
ironic_controller.go
File metadata and controls
349 lines (302 loc) · 12.9 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Copyright 2023.
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 controller
import (
"context"
"fmt"
"slices"
"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
metal3api "github.com/metal3-io/ironic-standalone-operator/api/v1alpha1"
"github.com/metal3-io/ironic-standalone-operator/pkg/ironic"
"github.com/metal3-io/ironic-standalone-operator/pkg/secretutils"
)
// IronicReconciler reconciles a Ironic object.
type IronicReconciler struct {
client.Client
KubeClient kubernetes.Interface
APIReader client.Reader
Scheme *runtime.Scheme
Log logr.Logger
Domain string
VersionInfo ironic.VersionInfo
}
//+kubebuilder:rbac:groups=ironic.metal3.io,resources=ironics,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=ironic.metal3.io,resources=ironics/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=ironic.metal3.io,resources=ironics/finalizers,verbs=update
//+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch
//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;update
//+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;delete
//+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *IronicReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Log.WithValues("Ironic", req.NamespacedName)
logger.Info("starting reconcile")
cctx := ironic.ControllerContext{
Context: ctx,
Client: r.Client,
KubeClient: r.KubeClient,
Scheme: r.Scheme,
Logger: logger,
Domain: r.Domain,
VersionInfo: r.VersionInfo,
}
ironicConf, err := getIronic(cctx, req.NamespacedName)
if ironicConf == nil || err != nil {
return ctrl.Result{}, err
}
changed, err := r.handleIronic(cctx, ironicConf)
if err != nil {
cctx.Logger.Error(err, "reconcile failed, will retry")
return ctrl.Result{}, err
}
if changed {
return ctrl.Result{Requeue: true}, nil
}
logger.Info("object has been fully reconciled")
return ctrl.Result{}, nil
}
func (r *IronicReconciler) setNotReady(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic, reason, message string) error {
setCondition(&ironicConf.Status.Conditions, ironicConf.Generation,
false, reason, message)
err := cctx.Client.Status().Update(cctx.Context, ironicConf)
if err != nil {
cctx.Logger.Error(err, "potentially transient error when updating conditions")
}
return err
}
func (r *IronicReconciler) handleIronic(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic) (requeue bool, err error) {
if ironicConf.DeletionTimestamp.IsZero() {
requeue, err = ensureFinalizer(cctx, ironicConf)
if requeue || err != nil {
return requeue, err
}
} else {
return r.cleanUp(cctx, ironicConf)
}
versionInfo, err := cctx.VersionInfo.WithIronicOverrides(ironicConf)
if err != nil {
// This condition requires a user's intervention
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, err.Error())
return true, err
}
cctx.VersionInfo = versionInfo
actuallyRequestedVersion := cctx.VersionInfo.InstalledVersion.String()
if actuallyRequestedVersion != ironicConf.Status.InstalledVersion && actuallyRequestedVersion != ironicConf.Status.RequestedVersion {
// Ironic does not support downgrades when a real external database is used.
if ironicConf.Status.InstalledVersion != "" && ironicConf.Spec.Database != nil {
var parsedVersion metal3api.Version
parsedVersion, err = metal3api.ParseVersion(ironicConf.Status.InstalledVersion)
if err != nil {
return false, err
}
// NOTE(dtantsur): allow upgrades from latest to a numeric version.
// This requires a user to ensure that the new version is actually newer, but it's a valid scenario.
if !parsedVersion.IsLatest() && cctx.VersionInfo.InstalledVersion.Compare(parsedVersion) < 0 {
cctx.Logger.Info("refusing to downgrade Ironic", "InstalledVersion", ironicConf.Status.InstalledVersion, "RequestedVersion", actuallyRequestedVersion)
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, "Ironic does not support downgrades with an external database")
return false, nil
}
}
cctx.Logger.Info("new version requested", "InstalledVersion", ironicConf.Status.InstalledVersion, "RequestedVersion", actuallyRequestedVersion)
ironicConf.Status.RequestedVersion = actuallyRequestedVersion
err = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonInProgress, "new version requested")
if err != nil {
return requeue, err
}
}
apiSecret, requeue, err := r.ensureAPISecret(cctx, ironicConf)
if requeue || err != nil {
return requeue, err
}
var tlsSecret *corev1.Secret
if tlsSecretName := ironicConf.Spec.TLS.CertificateName; tlsSecretName != "" {
tlsSecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, tlsSecretName)
if requeue || err != nil {
return requeue, err
}
}
var bmcCASecret *corev1.Secret
var bmcCAConfigMap *corev1.ConfigMap
if bmcCARef := ironic.GetBMCCA(&ironicConf.Spec.TLS); bmcCARef != nil {
switch bmcCARef.Kind {
case metal3api.ResourceKindSecret:
bmcCASecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, bmcCARef.Name)
case metal3api.ResourceKindConfigMap:
bmcCAConfigMap, requeue, err = r.getConfigMap(cctx, ironicConf, bmcCARef.Name)
}
if requeue || err != nil {
return requeue, err
}
}
var trustedCASecret *corev1.Secret
var trustedCAConfigMap *corev1.ConfigMap
if trustedCARef := ironic.GetTrustedCA(&ironicConf.Spec.TLS); trustedCARef != nil {
switch trustedCARef.Kind {
case metal3api.ResourceKindSecret:
trustedCASecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, trustedCARef.Name)
case metal3api.ResourceKindConfigMap:
trustedCAConfigMap, requeue, err = r.getConfigMap(cctx, ironicConf, trustedCARef.Name)
}
if requeue || err != nil {
return requeue, err
}
}
resources := ironic.Resources{
Ironic: ironicConf,
APISecret: apiSecret,
TLSSecret: tlsSecret,
BMCCASecret: bmcCASecret,
BMCCAConfigMap: bmcCAConfigMap,
TrustedCASecret: trustedCASecret,
TrustedCAConfigMap: trustedCAConfigMap,
}
status, err := ironic.EnsureIronic(cctx, resources)
if err != nil {
cctx.Logger.Error(err, "potentially transient error, will retry")
return requeue, err
}
newStatus := ironicConf.Status.DeepCopy()
setConditionsFromStatus(cctx, status, &newStatus.Conditions, ironicConf.Generation, "ironic")
requeue = status.NeedsRequeue()
if status.IsReady() {
newStatus.InstalledVersion = actuallyRequestedVersion
}
if !apiequality.Semantic.DeepEqual(newStatus, &ironicConf.Status) {
cctx.Logger.Info("updating status", "Status", newStatus)
ironicConf.Status = *newStatus
err = cctx.Client.Status().Update(cctx.Context, ironicConf)
}
return requeue, err
}
// Get a secret and update its owner references using SecretManager.
// This ensures the secret is labeled for cache filtering and has owner references set.
// Only returns a valid pointer if requeue is false and err is nil.
func (r *IronicReconciler) getAndUpdateSecret(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic, secretName string) (secret *corev1.Secret, requeue bool, err error) {
namespacedName := types.NamespacedName{
Namespace: ironicConf.Namespace,
Name: secretName,
}
secretManager := secretutils.NewSecretManager(cctx.Context, cctx.Logger, cctx.Client, r.APIReader)
secret, err = secretManager.AcquireSecret(namespacedName, ironicConf, cctx.Scheme)
if err != nil {
// NotFound requires a user's intervention, so reporting it in the conditions.
// Everything else is reported up for a retry.
if k8serrors.IsNotFound(err) {
message := fmt.Sprintf("secret %s/%s not found", ironicConf.Namespace, secretName)
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, message)
}
return nil, true, fmt.Errorf("cannot load secret %s/%s: %w", ironicConf.Namespace, secretName, err)
}
return secret, false, nil
}
// Get a user-provided configmap.
// Only returns a valid pointer if requeue is false and err is nil.
func (r *IronicReconciler) getConfigMap(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic, configMapName string) (configMap *corev1.ConfigMap, requeue bool, err error) {
namespacedName := types.NamespacedName{
Namespace: ironicConf.Namespace,
Name: configMapName,
}
secretManager := secretutils.NewSecretManager(cctx.Context, cctx.Logger, cctx.Client, r.APIReader)
configMap, err = secretManager.ObtainConfigMap(namespacedName)
if err != nil {
// NotFound requires a user's intervention, so reporting it in the conditions.
// Everything else is reported up for a retry.
if k8serrors.IsNotFound(err) {
message := fmt.Sprintf("configmap %s/%s not found", ironicConf.Namespace, configMapName)
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, message)
}
return nil, true, fmt.Errorf("cannot load configmap %s/%s: %w", ironicConf.Namespace, configMapName, err)
}
return configMap, false, nil
}
func (r *IronicReconciler) ensureAPISecret(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic) (apiSecret *corev1.Secret, requeue bool, err error) {
if ironicConf.Spec.APICredentialsName == "" {
apiSecret, err = generateSecret(cctx, ironicConf, &ironicConf.ObjectMeta, "service", true)
if err != nil {
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, err.Error())
return nil, true, err
}
cctx.Logger.Info("updating Ironic to use the newly generated secret", "Secret", apiSecret.Name)
ironicConf.Spec.APICredentialsName = apiSecret.Name
err = cctx.Client.Update(cctx.Context, ironicConf)
if err != nil {
// Considering this a transient error
return nil, true, fmt.Errorf("cannot update the new API credentials secret: %w", err)
}
requeue = true
return apiSecret, requeue, err
}
apiSecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, ironicConf.Spec.APICredentialsName)
if requeue || err != nil {
return nil, requeue, err
}
requeue, err = ironic.UpdateSecret(apiSecret, cctx.Logger)
if err != nil {
_ = r.setNotReady(cctx, ironicConf, metal3api.IronicReasonFailed, err.Error())
return nil, true, err
}
if requeue {
cctx.Logger.Info("updating htpasswd", "Secret", apiSecret.Name)
err = cctx.Client.Update(cctx.Context, apiSecret)
}
return apiSecret, requeue, err
}
func (r *IronicReconciler) cleanUp(cctx ironic.ControllerContext, ironicConf *metal3api.Ironic) (bool, error) {
if !slices.Contains(ironicConf.Finalizers, IronicFinalizer) {
return false, nil
}
err := ironic.RemoveIronic(cctx, ironicConf)
if err != nil {
return false, err
}
// This must be the last action.
return removeFinalizer(cctx, ironicConf)
}
// SetupWithManager sets up the controller with the Manager.
func (r *IronicReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder := ctrl.NewControllerManagedBy(mgr).
For(&metal3api.Ironic{}).
Owns(&corev1.Secret{}, builder.MatchEveryOwner).
Owns(&corev1.Service{}).
Owns(&appsv1.DaemonSet{}).
Owns(&appsv1.Deployment{}).
Owns(&batchv1.Job{})
hasServiceMonitor, err := clusterHasCRD(mgr, &monitoringv1.ServiceMonitor{})
if err != nil {
return err
}
if hasServiceMonitor {
builder = builder.Owns(&monitoringv1.ServiceMonitor{})
} else {
r.Log.Info("WARNING: ServiceMonitor resources are not available and will not be reconciled")
}
return builder.Complete(r)
}