-
Notifications
You must be signed in to change notification settings - Fork 241
/
preprovisioningimage_controller.go
361 lines (308 loc) · 11.7 KB
/
preprovisioningimage_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
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
350
351
352
353
354
355
356
357
358
359
360
361
/*
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"
"time"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
metal3 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/imageprovider"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/utils"
)
const (
minRetryDelay = time.Second * 10
maxRetryDelay = time.Minute * 10
)
// PreprovisioningImageReconciler reconciles a PreprovisioningImage object
type PreprovisioningImageReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
APIReader client.Reader
ImageProvider imageprovider.ImageProvider
}
type imageConditionReason string
const (
reasonImageSuccess imageConditionReason = "ImageSuccess"
reasonImageConfigurationError imageConditionReason = "ConfigurationError"
reasonImageMissingNetworkData imageConditionReason = "MissingNetworkData"
reasonImageBuildInvalid imageConditionReason = "ImageBuildInvalid"
)
// +kubebuilder:rbac:groups=metal3.io,resources=preprovisioningimages,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=metal3.io,resources=preprovisioningimages/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;update
func (r *PreprovisioningImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("preprovisioningimage", req.NamespacedName)
result := ctrl.Result{}
img := metal3.PreprovisioningImage{}
err := r.Get(ctx, req.NamespacedName, &img)
if err != nil {
if k8serrors.IsNotFound(err) {
log.Info("PreprovisioningImage not found")
err = nil
}
return ctrl.Result{}, err
}
if !img.DeletionTimestamp.IsZero() {
log.Info("cleaning up deleted resource")
if err := r.discardExistingImage(&img, log); err != nil {
return ctrl.Result{}, err
}
img.Finalizers = utils.FilterStringFromList(
img.Finalizers, metal3.PreprovisioningImageFinalizer)
err := r.Update(ctx, &img)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to remove finalizer")
}
return ctrl.Result{}, nil
}
if !utils.StringInList(img.Finalizers, metal3.PreprovisioningImageFinalizer) {
log.Info("adding finalizer")
img.Finalizers = append(img.Finalizers, metal3.PreprovisioningImageFinalizer)
err := r.Update(ctx, &img)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to add finalizer")
}
return ctrl.Result{}, nil
}
changed, err := r.update(&img, log)
if k8serrors.IsNotFound(err) {
delay := getErrorRetryDelay(img.Status)
log.Info("requeuing to check for secret", "after", delay)
result.RequeueAfter = delay
}
notReady := imageprovider.ImageNotReady{}
if errors.As(err, ¬Ready) {
log.Info("image is not ready yet, requeuing", "after", minRetryDelay)
if setUnready(img.GetGeneration(), &img.Status, err.Error()) {
changed = true
}
result.RequeueAfter = minRetryDelay
}
if changed {
log.Info("updating status")
err = r.Status().Update(ctx, &img)
}
return result, err
}
func configChanged(img *metal3.PreprovisioningImage, format metal3.ImageFormat, networkDataStatus metal3.SecretStatus) bool {
return !(img.Status.Format == format &&
img.Status.Architecture == img.Spec.Architecture &&
img.Status.NetworkData == networkDataStatus)
}
func (r *PreprovisioningImageReconciler) update(img *metal3.PreprovisioningImage, log logr.Logger) (bool, error) {
generation := img.GetGeneration()
if !r.ImageProvider.SupportsArchitecture(img.Spec.Architecture) {
log.Info("image architecture not supported", "architecture", img.Spec.Architecture)
return setError(generation, &img.Status, reasonImageConfigurationError, "Architecture not supported"), nil
}
format := r.getImageFormat(img.Spec, log)
if format == "" {
return setError(generation, &img.Status, reasonImageConfigurationError, "No acceptable image format supported"), nil
}
secretManager := secretutils.NewSecretManager(log, r.Client, r.APIReader)
networkData, secretStatus, err := getNetworkData(secretManager, img)
if err != nil {
if k8serrors.IsNotFound(err) {
log.Info("network data Secret does not exist")
return setError(generation, &img.Status, reasonImageMissingNetworkData, "NetworkData secret not found"), err
}
return false, err
}
if configChanged(img, format, secretStatus) {
reason := "Config changed"
if meta.IsStatusConditionTrue(img.Status.Conditions, string(metal3.ConditionImageReady)) {
// Ensure we mark the status as not ready before we remove the build
// from the image cache.
setUnready(generation, &img.Status, reason)
} else {
if err := r.discardExistingImage(img, log); err != nil {
return false, err
}
// Set up all the data before building the image and adding the URL,
// so that even if we fail to write the built image status and the
// config subsequently changes, the image cache cannot leak.
setImage(generation, &img.Status, imageprovider.GeneratedImage{},
format, secretStatus, img.Spec.Architecture,
reason)
}
return true, nil
}
var networkDataContent imageprovider.NetworkData
if networkData != nil {
networkDataContent = networkData.Data
}
image, err := r.ImageProvider.BuildImage(imageprovider.ImageData{
ImageMetadata: img.ObjectMeta.DeepCopy(),
Format: format,
Architecture: img.Spec.Architecture,
NetworkDataStatus: secretStatus,
}, networkDataContent, log)
if err != nil {
failure := imageprovider.ImageBuildInvalid{}
if errors.As(err, &failure) {
log.Info("image build failed", "error", "err")
return setError(generation, &img.Status, reasonImageBuildInvalid, failure.Error()), nil
}
return false, err
}
log.Info("image URL available", "url", image, "format", format)
return setImage(generation, &img.Status, image, format,
secretStatus, img.Spec.Architecture,
"Generated image"), nil
}
func (r *PreprovisioningImageReconciler) getImageFormat(spec metal3.PreprovisioningImageSpec, log logr.Logger) (format metal3.ImageFormat) {
for _, acceptableFormat := range spec.AcceptFormats {
if r.ImageProvider.SupportsFormat(acceptableFormat) {
return acceptableFormat
}
}
if len(spec.AcceptFormats) > 0 {
log = log.WithValues("preferredFormat", spec.AcceptFormats[0])
}
log.Info("no acceptable image format supported")
return
}
func (r *PreprovisioningImageReconciler) discardExistingImage(img *metal3.PreprovisioningImage, log logr.Logger) error {
if img.Status.Format == "" {
return nil
}
log.Info("discarding existing image", "image_url", img.Status.ImageUrl)
return r.ImageProvider.DiscardImage(imageprovider.ImageData{
ImageMetadata: img.ObjectMeta.DeepCopy(),
Format: img.Status.Format,
Architecture: img.Status.Architecture,
NetworkDataStatus: img.Status.NetworkData,
})
}
func getErrorRetryDelay(status metal3.PreprovisioningImageStatus) time.Duration {
errorCond := meta.FindStatusCondition(status.Conditions, string(metal3.ConditionImageError))
if errorCond == nil || errorCond.Status != metav1.ConditionTrue {
return 0
}
// exponential delay
delay := time.Since(errorCond.LastTransitionTime.Time) + minRetryDelay
if delay > maxRetryDelay {
return maxRetryDelay
}
return delay
}
func getNetworkData(secretManager secretutils.SecretManager, img *metal3.PreprovisioningImage) (*corev1.Secret, metal3.SecretStatus, error) {
networkDataSecret := img.Spec.NetworkDataName
if networkDataSecret == "" {
return nil, metal3.SecretStatus{}, nil
}
secretKey := client.ObjectKey{
Name: networkDataSecret,
Namespace: img.ObjectMeta.Namespace,
}
secret, err := secretManager.AcquireSecret(secretKey, img, false)
if err != nil {
return nil, metal3.SecretStatus{}, err
}
return secret, metal3.SecretStatus{
Name: networkDataSecret,
Version: secret.GetResourceVersion(),
}, nil
}
func setImageCondition(generation int64, status *metal3.PreprovisioningImageStatus,
cond metal3.ImageStatusConditionType, newStatus metav1.ConditionStatus,
time metav1.Time, reason imageConditionReason, message string) {
newCondition := metav1.Condition{
Type: string(cond),
Status: newStatus,
LastTransitionTime: time,
ObservedGeneration: generation,
Reason: string(reason),
Message: message,
}
meta.SetStatusCondition(&status.Conditions, newCondition)
}
func setImage(generation int64, status *metal3.PreprovisioningImageStatus, image imageprovider.GeneratedImage,
format metal3.ImageFormat, networkData metal3.SecretStatus, arch string,
message string) bool {
newStatus := status.DeepCopy()
newStatus.ImageUrl = image.ImageURL
newStatus.KernelUrl = image.KernelURL
newStatus.ExtraKernelParams = image.ExtraKernelParams
newStatus.Format = format
newStatus.Architecture = arch
newStatus.NetworkData = networkData
time := metav1.Now()
reason := reasonImageSuccess
ready := metav1.ConditionFalse
if newStatus.ImageUrl != "" {
ready = metav1.ConditionTrue
}
setImageCondition(generation, newStatus,
metal3.ConditionImageReady, ready,
time, reason, message)
setImageCondition(generation, newStatus,
metal3.ConditionImageError, metav1.ConditionFalse,
time, reason, "")
changed := !apiequality.Semantic.DeepEqual(status, &newStatus)
*status = *newStatus
return changed
}
func setUnready(generation int64, status *metal3.PreprovisioningImageStatus, message string) bool {
newStatus := status.DeepCopy()
time := metav1.Now()
reason := reasonImageSuccess
setImageCondition(generation, newStatus,
metal3.ConditionImageReady, metav1.ConditionFalse,
time, reason, message)
changed := !apiequality.Semantic.DeepEqual(status, &newStatus)
*status = *newStatus
return changed
}
func setError(generation int64, status *metal3.PreprovisioningImageStatus, reason imageConditionReason, message string) bool {
newStatus := status.DeepCopy()
newStatus.ImageUrl = ""
time := metav1.Now()
setImageCondition(generation, newStatus,
metal3.ConditionImageReady, metav1.ConditionFalse,
time, reason, "")
setImageCondition(generation, newStatus,
metal3.ConditionImageError, metav1.ConditionTrue,
time, reason, message)
changed := !apiequality.Semantic.DeepEqual(status, &newStatus)
*status = *newStatus
return changed
}
func (r *PreprovisioningImageReconciler) CanStart() bool {
for _, fmt := range []metal3.ImageFormat{metal3.ImageFormatISO, metal3.ImageFormatInitRD} {
if r.ImageProvider.SupportsFormat(fmt) {
return true
}
}
r.Log.Info("not starting preprovisioning image controller; no image data available")
return false
}
func (r *PreprovisioningImageReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconcile int) error {
return ctrl.NewControllerManagedBy(mgr).
For(&metal3.PreprovisioningImage{}).
WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconcile}).
Owns(&corev1.Secret{}, builder.MatchEveryOwner).
Complete(r)
}