-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserverbootconfiguration_pxe_controller.go
More file actions
314 lines (268 loc) · 12.4 KB
/
Copy pathserverbootconfiguration_pxe_controller.go
File metadata and controls
314 lines (268 loc) · 12.4 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
/*
Copyright 2024.
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"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/ironcore-dev/boot-operator/api/v1alpha1"
"github.com/ironcore-dev/boot-operator/internal/oci"
"github.com/ironcore-dev/boot-operator/internal/registry"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/go-logr/logr"
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/containerd/containerd/remotes/docker"
)
const (
MediaTypeKernel = "application/vnd.ironcore.image.kernel"
MediaTypeInitrd = "application/vnd.ironcore.image.initramfs"
MediaTypeSquashFS = "application/vnd.ironcore.image.squashfs"
MediaTypeKernelOld = "application/io.gardenlinux.kernel"
MediaTypeInitrdOld = "application/io.gardenlinux.initrd"
MediaTypeSquashFSOld = "application/io.gardenlinux.squashfs"
CNAMEPrefixMetalPXE = "metal_pxe"
)
type ServerBootConfigurationPXEReconciler struct {
client.Client
Scheme *runtime.Scheme
IPXEServiceURL string
Architecture string
RegistryValidator *registry.Validator
}
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=serverbootconfigurations,verbs=get;list;watch
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=serverbootconfigurations/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=serverbootconfigurations/finalizers,verbs=update
//+kubebuilder:rbac:groups=boot.ironcore.dev,resources=ipxebootconfig,verbs=get;list;watch;create;delete;patch
//+kubebuilder:rbac:groups=boot.ironcore.dev,resources=ipxebootconfig/status,verbs=get
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=servers,verbs=get;list;watch
// 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 *ServerBootConfigurationPXEReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
bootConfig := &metalv1alpha1.ServerBootConfiguration{}
if err := r.Get(ctx, req.NamespacedName, bootConfig); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
return r.reconcileExists(ctx, log, bootConfig)
}
func (r *ServerBootConfigurationPXEReconciler) reconcileExists(ctx context.Context, log logr.Logger, config *metalv1alpha1.ServerBootConfiguration) (ctrl.Result, error) {
if !config.DeletionTimestamp.IsZero() {
return r.delete(ctx, log, config)
}
return r.reconcile(ctx, log, config)
}
func (r *ServerBootConfigurationPXEReconciler) delete(_ context.Context, _ logr.Logger, _ *metalv1alpha1.ServerBootConfiguration) (ctrl.Result, error) {
return ctrl.Result{}, nil
}
func (r *ServerBootConfigurationPXEReconciler) reconcile(ctx context.Context, log logr.Logger, bootConfig *metalv1alpha1.ServerBootConfiguration) (ctrl.Result, error) {
log.V(1).Info("Reconciling ServerBootConfiguration")
systemUUID, err := r.getSystemUUIDFromBootConfig(ctx, bootConfig)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get system UUID from BootConfig: %w", err)
}
log.V(1).Info("Got system UUID from BootConfig", "systemUUID", systemUUID)
systemIPs, err := r.getSystemIPFromBootConfig(ctx, bootConfig)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get system IP from BootConfig: %w", err)
}
log.V(1).Info("Got system IP from BootConfig", "systemIPs", systemIPs)
kernelURL, initrdURL, squashFSURL, err := r.getImageDetailsFromConfig(ctx, bootConfig)
if err != nil {
if patchErr := PatchServerBootConfigCondition(ctx, r.Client,
types.NamespacedName{Name: bootConfig.Name, Namespace: bootConfig.Namespace},
metav1.Condition{
Type: IPXEBootReadyConditionType,
Status: metav1.ConditionFalse,
Reason: "ImageDetailsFailed",
Message: err.Error(),
ObservedGeneration: bootConfig.Generation,
}); patchErr != nil {
return ctrl.Result{}, fmt.Errorf("failed to patch %s condition: %w (original error: %w)", IPXEBootReadyConditionType, patchErr, err)
}
return ctrl.Result{}, err
}
log.V(1).Info("Extracted OS image layer details")
config := &v1alpha1.IPXEBootConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "boot.ironcore.dev/v1alpha1",
Kind: "IPXEBootConfig",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: bootConfig.Namespace,
Name: bootConfig.Name,
},
Spec: v1alpha1.IPXEBootConfigSpec{
SystemUUID: systemUUID,
SystemIPs: systemIPs,
KernelURL: kernelURL,
InitrdURL: initrdURL,
SquashfsURL: squashFSURL,
},
}
if bootConfig.Spec.IgnitionSecretRef != nil {
config.Spec.IgnitionSecretRef = bootConfig.Spec.IgnitionSecretRef
}
if err := controllerutil.SetControllerReference(bootConfig, config, r.Scheme); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to set controller reference: %w", err)
}
log.V(1).Info("Set controller reference")
applyData, err := json.Marshal(config)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to marshal IPXE config apply data: %w", err)
}
if err := r.Patch(ctx, config, client.RawPatch(types.ApplyPatchType, applyData), fieldOwner, client.ForceOwnership); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply IPXE config: %w", err)
}
log.V(1).Info("Applied IPXE config for server boot config")
if err := r.Get(ctx, client.ObjectKey{Namespace: bootConfig.Namespace, Name: bootConfig.Name}, config); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get IPXE config: %w", err)
}
if err := r.patchIPXEBootReadyConditionFromIPXEState(ctx, config, bootConfig); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to patch %s condition from IPXEBootConfig state %s: %w", IPXEBootReadyConditionType, config.Status.State, err)
}
log.V(1).Info("Patched server boot config condition", "condition", IPXEBootReadyConditionType)
log.V(1).Info("Reconciled ServerBootConfiguration")
return ctrl.Result{}, nil
}
func (r *ServerBootConfigurationPXEReconciler) patchIPXEBootReadyConditionFromIPXEState(ctx context.Context, config *v1alpha1.IPXEBootConfig, bootConfig *metalv1alpha1.ServerBootConfiguration) error {
key := types.NamespacedName{Name: bootConfig.Name, Namespace: bootConfig.Namespace}
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
var cur metalv1alpha1.ServerBootConfiguration
if err := r.Get(ctx, key, &cur); err != nil {
return err
}
base := cur.DeepCopy()
if cur.Generation != bootConfig.Generation {
// The SBC has been updated since this reconcile started; a newer reconcile
// will handle the fresh generation. Avoid stamping stale data on it.
return nil
}
cond := metav1.Condition{
Type: IPXEBootReadyConditionType,
// Use bootConfig.Generation, not cur.Generation: the condition content was
// derived from bootConfig's IPXEBootConfig, so it reflects that generation.
ObservedGeneration: bootConfig.Generation,
}
switch {
case config.Status.ObservedGeneration < config.Generation:
// Child controller hasn't reconciled the new spec yet; don't write anything.
// The Owns() watch will re-trigger this reconcile once the child updates its status.
return nil
case config.Status.State == v1alpha1.IPXEBootConfigStateReady:
cond.Status = metav1.ConditionTrue
cond.Reason = "BootConfigReady"
cond.Message = "IPXE boot configuration is ready."
case config.Status.State == v1alpha1.IPXEBootConfigStateError:
cond.Status = metav1.ConditionFalse
cond.Reason = "BootConfigError"
cond.Message = "IPXEBootConfig reported an error."
default:
cond.Status = metav1.ConditionUnknown
cond.Reason = BootConfigPendingReason
cond.Message = "Waiting for IPXEBootConfig to become Ready."
}
apimeta.SetStatusCondition(&cur.Status.Conditions, cond)
return r.Status().Patch(ctx, &cur, client.MergeFrom(base))
})
}
func (r *ServerBootConfigurationPXEReconciler) getSystemUUIDFromBootConfig(ctx context.Context, config *metalv1alpha1.ServerBootConfiguration) (string, error) {
server := &metalv1alpha1.Server{}
if err := r.Get(ctx, client.ObjectKey{Name: config.Spec.ServerRef.Name}, server); err != nil {
return "", err
}
return server.Spec.SystemUUID, nil
}
func (r *ServerBootConfigurationPXEReconciler) getSystemIPFromBootConfig(ctx context.Context, config *metalv1alpha1.ServerBootConfiguration) ([]string, error) {
server := &metalv1alpha1.Server{}
if err := r.Get(ctx, client.ObjectKey{Name: config.Spec.ServerRef.Name}, server); err != nil {
return nil, err
}
return ExtractServerNetworkIDs(server, false), nil
}
func (r *ServerBootConfigurationPXEReconciler) getImageDetailsFromConfig(ctx context.Context, config *metalv1alpha1.ServerBootConfiguration) (string, string, string, error) {
imageName, imageVersion, err := ParseImageReference(config.Spec.Image)
if err != nil {
return "", "", "", err
}
kernelDigest, initrdDigest, squashFSDigest, err := r.getLayerDigestsFromNestedManifest(ctx, imageName, imageVersion)
if err != nil {
return "", "", "", fmt.Errorf("failed to fetch layer digests: %w", err)
}
kernelURL := fmt.Sprintf("%s/image?imageName=%s&version=%s&layerDigest=%s", r.IPXEServiceURL, imageName, imageVersion, kernelDigest)
initrdURL := fmt.Sprintf("%s/image?imageName=%s&version=%s&layerDigest=%s", r.IPXEServiceURL, imageName, imageVersion, initrdDigest)
squashFSURL := fmt.Sprintf("%s/image?imageName=%s&version=%s&layerDigest=%s", r.IPXEServiceURL, imageName, imageVersion, squashFSDigest)
return kernelURL, initrdURL, squashFSURL, nil
}
func (r *ServerBootConfigurationPXEReconciler) getLayerDigestsFromNestedManifest(ctx context.Context, imageName, imageVersion string) (string, string, string, error) {
imageRef := BuildImageReference(imageName, imageVersion)
if err := r.RegistryValidator.ValidateImageRegistry(imageRef); err != nil {
return "", "", "", fmt.Errorf("registry validation failed: %w", err)
}
resolver := docker.NewResolver(docker.ResolverOptions{})
name, desc, err := resolver.Resolve(ctx, imageRef)
if err != nil {
return "", "", "", fmt.Errorf("failed to resolve image reference: %w", err)
}
manifest, err := oci.FindManifestByArchitecture(ctx, resolver, name, desc, r.Architecture, oci.FindManifestOptions{
EnableCNAMECompat: true,
CNAMEPrefix: CNAMEPrefixMetalPXE,
})
if err != nil {
return "", "", "", err
}
var kernelDigest, initrdDigest, squashFSDigest string
for _, layer := range manifest.Layers {
switch layer.MediaType {
case MediaTypeKernel:
kernelDigest = layer.Digest.String()
case MediaTypeInitrd:
initrdDigest = layer.Digest.String()
case MediaTypeSquashFS:
squashFSDigest = layer.Digest.String()
case MediaTypeKernelOld:
kernelDigest = layer.Digest.String()
case MediaTypeInitrdOld:
initrdDigest = layer.Digest.String()
case MediaTypeSquashFSOld:
squashFSDigest = layer.Digest.String()
}
}
if kernelDigest == "" || initrdDigest == "" || squashFSDigest == "" {
return "", "", "", fmt.Errorf("failed to find all required layer digests")
}
return kernelDigest, initrdDigest, squashFSDigest, nil
}
func (r *ServerBootConfigurationPXEReconciler) enqueueServerBootConfigFromIgnitionSecret(ctx context.Context, secret client.Object) []reconcile.Request {
return EnqueueServerBootConfigsReferencingSecret(ctx, r.Client, secret)
}
// SetupWithManager sets up the controller with the Manager.
func (r *ServerBootConfigurationPXEReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.ServerBootConfiguration{}).
Owns(&v1alpha1.IPXEBootConfig{}).
Watches(
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.enqueueServerBootConfigFromIgnitionSecret),
).
Complete(r)
}