-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanagedcloudprofile_controller.go
More file actions
591 lines (519 loc) · 18.5 KB
/
Copy pathmanagedcloudprofile_controller.go
File metadata and controls
591 lines (519 loc) · 18.5 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0
package controllers
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"strings"
"time"
gardenerv1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/go-logr/logr"
providercfg "github.com/ironcore-dev/gardener-extension-provider-ironcore-metal/pkg/apis/metal/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/cobaltcore-dev/cloud-profile-sync/api/v1alpha1"
"github.com/cobaltcore-dev/cloud-profile-sync/cloudprofilesync"
)
const (
CloudProfileAppliedConditionType string = "CloudProfileApplied"
)
// OCISourceFactory defines an interface for creating OCI sources.
type OCISourceFactory interface {
Create(params cloudprofilesync.OCIParams, insecure bool, log logr.Logger) (cloudprofilesync.Source, error)
}
type RegistryClient interface {
GetTags(ctx context.Context, registry, repository string) (map[string]time.Time, error)
}
type KeppelClient struct{}
func (k *KeppelClient) GetTags(ctx context.Context, registry, repository string) (map[string]time.Time, error) {
return fetchKeppelTags(ctx, registry, repository)
}
// DefaultOCISourceFactory is the default implementation of OCISourceFactory.
type DefaultOCISourceFactory struct{}
func (f *DefaultOCISourceFactory) Create(params cloudprofilesync.OCIParams, insecure bool, log logr.Logger) (cloudprofilesync.Source, error) {
return cloudprofilesync.NewOCI(params, insecure, log)
}
type Reconciler struct {
client.Client
OCISourceFactory OCISourceFactory
RegistryProviderFunc func(registry string) (RegistryClient, error)
EnableCapabilities bool
}
type KeppelTag struct {
Name string `json:"name"`
PushedAt int64 `json:"pushed_at"`
}
type KeppelManifest struct {
Digest string `json:"digest"`
PushedAt int64 `json:"pushed_at"`
Tags []KeppelTag `json:"tags"`
}
type KeppelManifestsResponse struct {
Manifests []KeppelManifest `json:"manifests"`
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
var mcp v1alpha1.ManagedCloudProfile
if err := r.Get(ctx, req.NamespacedName, &mcp); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if err := r.reconcileCloudProfile(ctx, log, &mcp); err != nil {
return ctrl.Result{}, err
}
if err := r.reconcileGarbageCollection(ctx, &mcp); err != nil {
return ctrl.Result{}, err
}
log.Info("reconciled ManagedCloudProfile")
return ctrl.Result{RequeueAfter: 5 * time.Minute}, nil
}
func (r *Reconciler) reconcileCloudProfile(ctx context.Context, log logr.Logger, mcp *v1alpha1.ManagedCloudProfile) error {
var cloudProfile gardenerv1beta1.CloudProfile
cloudProfile.Name = mcp.Name
op, err := controllerutil.CreateOrPatch(ctx, r.Client, &cloudProfile, func() error {
if err := controllerutil.SetControllerReference(mcp, &cloudProfile, r.Scheme()); err != nil {
return err
}
cloudProfile.Spec = CloudProfileSpecToGardener(&mcp.Spec.CloudProfile)
errs := make([]error, 0)
for _, updates := range mcp.Spec.MachineImageUpdates {
if updateErr := r.updateMachineImages(ctx, log, updates, &cloudProfile.Spec); updateErr != nil {
errs = append(errs, updateErr)
}
}
gardenerv1beta1.SetObjectDefaults_CloudProfile(&cloudProfile)
return errors.Join(errs...)
})
if err != nil {
statusErr := r.patchStatusAndCondition(ctx, mcp, v1alpha1.FailedReconcileStatus, metav1.Condition{
Type: CloudProfileAppliedConditionType,
Status: metav1.ConditionFalse,
ObservedGeneration: mcp.Generation,
Reason: "ApplyFailed",
Message: fmt.Sprintf("Failed to apply CloudProfile: %s", err),
})
if statusErr != nil {
return fmt.Errorf("failed to patch ManagedCloudProfile status: %w", statusErr)
}
if apierrors.IsInvalid(err) {
return nil
}
return fmt.Errorf("failed to create or patch CloudProfile: %w", err)
}
if op != controllerutil.OperationResultNone {
statusErr := r.patchStatusAndCondition(ctx, mcp, v1alpha1.SucceededReconcileStatus, metav1.Condition{
Type: CloudProfileAppliedConditionType,
Status: metav1.ConditionTrue,
ObservedGeneration: mcp.Generation,
Reason: "Applied",
Message: "Generated CloudProfile applied successfully",
})
if statusErr != nil {
return fmt.Errorf("failed to patch ManagedCloudProfile status: %w", statusErr)
}
}
return nil
}
func (r *Reconciler) reconcileGarbageCollection(ctx context.Context, mcp *v1alpha1.ManagedCloudProfile) error {
if mcp.Spec.GarbageCollection == nil || !mcp.Spec.GarbageCollection.Enabled {
return nil
}
if mcp.Spec.GarbageCollection.MaxAge.Duration < 0 {
return r.failWithStatusUpdate(ctx, mcp, fmt.Errorf("invalid garbage collection maxAge: %s", mcp.Spec.GarbageCollection.MaxAge.String()))
}
cutoff := time.Now().Add(-mcp.Spec.GarbageCollection.MaxAge.Duration)
for _, updates := range mcp.Spec.MachineImageUpdates {
if updates.Source.OCI == nil {
continue
}
registryClient, err := r.RegistryProviderFunc(updates.Source.OCI.Registry)
if err != nil {
return r.failWithStatusUpdate(ctx, mcp,
fmt.Errorf("no registry provider found for registry %q: %w", updates.Source.OCI.Registry, err))
}
tags, err := registryClient.GetTags(
ctx,
updates.Source.OCI.Registry,
updates.Source.OCI.Repository,
)
if err != nil {
return r.failWithStatusUpdate(ctx, mcp,
fmt.Errorf("failed to fetch tags: %w", err))
}
referencedVersions, err := r.getReferencedVersions(ctx, mcp.Name, updates.ImageName)
if err != nil {
return r.failWithStatusUpdate(ctx, mcp, fmt.Errorf("failed to determine referenced versions for garbage collection: %w", err))
}
versionsToDelete := make(map[string]struct{})
for tag, pushedAt := range tags {
if _, isReferenced := referencedVersions[tag]; isReferenced {
continue
}
if pushedAt.Before(cutoff) {
versionsToDelete[tag] = struct{}{}
}
}
if err := r.deleteVersions(ctx, mcp.Name, updates.ImageName, versionsToDelete); err != nil {
if apierrors.IsInvalid(err) {
continue
}
return r.failWithStatusUpdate(ctx, mcp, fmt.Errorf("failed to delete image versions: %w", err))
}
}
return nil
}
func (r *Reconciler) deleteVersions(ctx context.Context, cloudProfileName, imageName string, versionsToDelete map[string]struct{}) error {
var cp gardenerv1beta1.CloudProfile
if err := r.Get(ctx, types.NamespacedName{Name: cloudProfileName}, &cp); err != nil {
return err
}
// Track which clean versions still have remaining capability flavors after deletion,
// so we can cascade-delete empty clean version entries from spec.machineImages.
// A version present in this map was a clean version entry; true means it still has flavors.
cleanVersionsWithFlavors := make(map[string]bool)
if cp.Spec.ProviderConfig != nil {
var cfg providercfg.CloudProfileConfig
if err := json.Unmarshal(cp.Spec.ProviderConfig.Raw, &cfg); err != nil {
return fmt.Errorf("failed to unmarshal ProviderConfig: %w", err)
}
for i := range cfg.MachineImages {
if cfg.MachineImages[i].Name != imageName {
continue
}
for j := range cfg.MachineImages[i].Versions {
v := &cfg.MachineImages[i].Versions[j]
if v.Image != "" {
// Legacy flat entry — not a clean version, skip.
continue
}
// Mark as a clean version entry; value indicates whether any flavors remain.
cleanVersionsWithFlavors[v.Version] = len(v.CapabilityFlavors) > 0
if len(v.CapabilityFlavors) == 0 {
continue
}
v.CapabilityFlavors = slices.DeleteFunc(v.CapabilityFlavors, func(f providercfg.MachineImageFlavor) bool {
idx := strings.LastIndex(f.Image, ":")
if idx == -1 {
return false
}
_, exists := versionsToDelete[f.Image[idx+1:]]
return exists
})
cleanVersionsWithFlavors[v.Version] = len(v.CapabilityFlavors) > 0
}
// Remove version entries that have no legacy image ref and no remaining flavors.
cfg.MachineImages[i].Versions = slices.DeleteFunc(cfg.MachineImages[i].Versions, func(mv providercfg.MachineImageVersion) bool {
if mv.Image != "" {
// Legacy flat entry — delete if its tag is in versionsToDelete.
idx := strings.LastIndex(mv.Image, ":")
if idx == -1 {
return false
}
_, exists := versionsToDelete[mv.Image[idx+1:]]
return exists
}
// Clean version entry — delete if all flavors were removed.
return !cleanVersionsWithFlavors[mv.Version]
})
}
raw, err := json.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to marshal ProviderConfig: %w", err)
}
cp.Spec.ProviderConfig.Raw = raw
}
for i := range cp.Spec.MachineImages {
if cp.Spec.MachineImages[i].Name != imageName {
continue
}
cp.Spec.MachineImages[i].Versions = slices.DeleteFunc(cp.Spec.MachineImages[i].Versions, func(mv gardenerv1beta1.MachineImageVersion) bool {
if _, exists := versionsToDelete[mv.Version]; exists {
return true
}
// Cascade-delete clean version entry if all its capability flavors were removed.
// Only entries tracked as clean versions (present in the map) are eligible.
hasRemainingFlavors, isCleanVersion := cleanVersionsWithFlavors[mv.Version]
return isCleanVersion && !hasRemainingFlavors
})
}
if err := r.Update(ctx, &cp); err != nil {
return err
}
return nil
}
func (r *Reconciler) getReferencedVersions(ctx context.Context, cloudProfileName, imageName string) (map[string]struct{}, error) {
referenced := make(map[string]struct{})
shootList := &gardenerv1beta1.ShootList{}
if err := r.List(ctx, shootList, client.InNamespace(metav1.NamespaceAll)); err != nil {
return nil, fmt.Errorf("failed to list Shoots: %w", err)
}
for _, shoot := range shootList.Items {
if shoot.Spec.CloudProfile == nil || shoot.Spec.CloudProfile.Name != cloudProfileName {
continue
}
for _, worker := range shoot.Spec.Provider.Workers {
if worker.Machine.Image == nil || worker.Machine.Image.Name != imageName {
continue
}
if worker.Machine.Image.Version != nil {
referenced[*worker.Machine.Image.Version] = struct{}{}
}
}
}
// For any clean version referenced by a Shoot, also protect the raw OCI tags
// that back it via capabilityFlavors — otherwise GC would delete the images
// that the clean version depends on.
if len(referenced) > 0 {
var cp gardenerv1beta1.CloudProfile
if err := r.Get(ctx, types.NamespacedName{Name: cloudProfileName}, &cp); err != nil {
return nil, fmt.Errorf("failed to get CloudProfile: %w", err)
}
if cp.Spec.ProviderConfig != nil {
var cfg providercfg.CloudProfileConfig
if err := json.Unmarshal(cp.Spec.ProviderConfig.Raw, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal ProviderConfig: %w", err)
}
for _, img := range cfg.MachineImages {
if img.Name != imageName {
continue
}
for _, v := range img.Versions {
if _, isReferenced := referenced[v.Version]; !isReferenced {
continue
}
for _, flavor := range v.CapabilityFlavors {
idx := strings.LastIndex(flavor.Image, ":")
if idx == -1 {
continue
}
referenced[flavor.Image[idx+1:]] = struct{}{}
}
}
}
}
}
return referenced, nil
}
func (r *Reconciler) updateMachineImages(ctx context.Context, log logr.Logger, update v1alpha1.MachineImageUpdate, cpSpec *gardenerv1beta1.CloudProfileSpec) error {
var source cloudprofilesync.Source
switch {
case update.Source.OCI != nil:
password, err := r.getCredential(ctx, update.Source.OCI.Password)
if err != nil {
return err
}
src, err := r.OCISourceFactory.Create(cloudprofilesync.OCIParams{
Registry: update.Source.OCI.Registry,
Repository: update.Source.OCI.Repository,
Username: update.Source.OCI.Username,
Password: string(password),
Parallel: 1,
}, update.Source.OCI.Insecure, log)
if err != nil {
return fmt.Errorf("failed to initialize OCI source: %w", err)
}
source = src
default:
return errors.New("no machine images source configured")
}
var provider cloudprofilesync.Provider
if update.Provider.IroncoreMetal != nil {
provider = &cloudprofilesync.IroncoreProvider{
Registry: update.Provider.IroncoreMetal.Registry,
Repository: update.Provider.IroncoreMetal.Repository,
ImageName: update.ImageName,
EnableCapabilities: r.EnableCapabilities,
}
}
imageUpdater := cloudprofilesync.ImageUpdater{
Log: log,
Source: source,
Provider: provider,
ImageName: update.ImageName,
EnableCapabilities: r.EnableCapabilities,
}
if err := imageUpdater.Update(ctx, cpSpec); err != nil {
return fmt.Errorf("updating machine images failed: %w", err)
}
return nil
}
func (r *Reconciler) getCredential(ctx context.Context, ref v1alpha1.SecretReference) ([]byte, error) {
if ref.Name == "" {
return nil, nil
}
var secret corev1.Secret
if err := r.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: ref.Namespace}, &secret); err != nil {
return nil, fmt.Errorf("failed to get secret: %w", err)
}
data, ok := secret.Data[ref.Key]
if !ok {
return nil, fmt.Errorf("secret %s/%s does not have key %s", ref.Namespace, ref.Name, ref.Key)
}
return data, nil
}
func (r *Reconciler) patchStatusAndCondition(ctx context.Context, mcp *v1alpha1.ManagedCloudProfile, status v1alpha1.ReconcileStatus, cond metav1.Condition) error {
original := mcp.DeepCopy()
mcp.Status.Status = status
if cond.Type != "" {
mcp.Status.Conditions = applyCondition(mcp.Status.Conditions, cond)
}
return r.Status().Patch(ctx, mcp, client.MergeFrom(original))
}
func applyCondition(conditions []metav1.Condition, cond metav1.Condition) []metav1.Condition {
idx := slices.IndexFunc(conditions, func(c metav1.Condition) bool {
return c.Type == cond.Type
})
if idx == -1 {
idx = len(conditions)
conditions = append(conditions, metav1.Condition{})
}
conditions[idx] = metav1.Condition{
Type: cond.Type,
Status: cond.Status,
ObservedGeneration: cond.ObservedGeneration,
LastTransitionTime: metav1.Now(),
Reason: cond.Reason,
Message: cond.Message,
}
return conditions
}
func CloudProfileSpecToGardener(spec *v1alpha1.CloudProfileSpec) gardenerv1beta1.CloudProfileSpec {
cpu := spec.DeepCopy()
return gardenerv1beta1.CloudProfileSpec{
CABundle: cpu.CABundle,
Kubernetes: cpu.Kubernetes,
MachineImages: cpu.MachineImages,
MachineTypes: cpu.MachineTypes,
ProviderConfig: cpu.ProviderConfig,
Regions: cpu.Regions,
SeedSelector: cpu.SeedSelector,
Type: cpu.Type,
VolumeTypes: cpu.VolumeTypes,
Bastion: cpu.Bastion,
Limits: cpu.Limits,
MachineCapabilities: cpu.MachineCapabilities,
}
}
func (r *Reconciler) failWithStatusUpdate(ctx context.Context, mcp *v1alpha1.ManagedCloudProfile, returnErr error) error {
if patchErr := r.patchStatusAndCondition(ctx, mcp, v1alpha1.FailedReconcileStatus, metav1.Condition{
Type: CloudProfileAppliedConditionType,
Status: metav1.ConditionFalse,
ObservedGeneration: mcp.Generation,
Reason: "GarbageCollectionFailed",
Message: returnErr.Error(),
}); patchErr != nil {
return fmt.Errorf("failed to patch ManagedCloudProfile status: %w (original error: %w)", patchErr, returnErr)
}
return returnErr
}
func fetchKeppelTags(ctx context.Context, registry, repository string) (map[string]time.Time, error) {
baseURL := registryBaseURL(registry, false)
keppelURL, err := keppelURL(baseURL, repository)
if err != nil {
return nil, fmt.Errorf("failed to build keppel URL: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, keppelURL, http.NoBody)
if err != nil {
return nil, fmt.Errorf("failed to create keppel request: %w", err)
}
tr := &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
httpClient := &http.Client{
Timeout: 30 * time.Second,
Transport: tr,
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("keppel API returned status %d", resp.StatusCode)
return nil, err
}
var result KeppelManifestsResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
tagMap := make(map[string]time.Time)
for _, m := range result.Manifests {
for _, t := range m.Tags {
if t.PushedAt == 0 {
continue
}
tagMap[t.Name] = time.Unix(t.PushedAt, 0)
}
}
return tagMap, nil
}
func keppelURL(baseURL, repository string) (string, error) {
account, repo, err := splitKeppelRepository(repository)
if err != nil {
return "", err
}
keppelURL := fmt.Sprintf(
"%s/keppel/v1/accounts/%s/repositories/%s/_manifests",
baseURL,
account,
repo,
)
return keppelURL, nil
}
func registryBaseURL(registryHost string, insecure bool) string {
scheme := "https"
if insecure {
scheme = "http"
}
u := &url.URL{
Scheme: scheme,
Host: registryHost,
}
base := u.String()
return base
}
func splitKeppelRepository(repository string) (account, repo string, err error) {
parts := strings.SplitN(repository, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
err := fmt.Errorf("invalid repository format %q, must be <account>/<repository-path>", repository)
return "", "", err
}
account = parts[0]
repo = parts[1]
return account, repo, nil
}
func (r *Reconciler) getRegistryProvider(registry string) (registryClient RegistryClient, err error) {
if registry == "" {
return nil, errors.New("registry cannot be empty")
}
if strings.Contains(strings.ToLower(registry), "keppel") {
return &KeppelClient{}, nil
}
return nil, errors.New("no registry provider found for registry")
}
// SetupWithManager attaches the controller to the given manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
if r.OCISourceFactory == nil {
r.OCISourceFactory = &DefaultOCISourceFactory{}
}
if r.RegistryProviderFunc == nil {
r.RegistryProviderFunc = r.getRegistryProvider
}
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ManagedCloudProfile{}).
Owns(&gardenerv1beta1.CloudProfile{}).
Complete(r)
}