@@ -27,6 +27,7 @@ import (
27
27
28
28
securejoin "github.com/cyphar/filepath-securejoin"
29
29
celtypes "github.com/google/cel-go/common/types"
30
+ "github.com/opencontainers/go-digest"
30
31
corev1 "k8s.io/api/core/v1"
31
32
apierrors "k8s.io/apimachinery/pkg/api/errors"
32
33
apimeta "k8s.io/apimachinery/pkg/api/meta"
@@ -281,6 +282,7 @@ func (r *KustomizationReconciler) reconcile(
281
282
src sourcev1.Source ,
282
283
patcher * patch.SerialPatcher ,
283
284
statusReaders []func (apimeta.RESTMapper ) engine.StatusReader ) error {
285
+ reconcileStart := time .Now ()
284
286
log := ctrl .LoggerFrom (ctx )
285
287
286
288
// Update status with the reconciliation progress.
@@ -314,13 +316,14 @@ func (r *KustomizationReconciler) reconcile(
314
316
}(tmpDir )
315
317
316
318
// Download artifact and extract files to the tmp dir.
317
- if err = fetch .NewArchiveFetcherWithLogger (
318
- r .artifactFetchRetries ,
319
- tar .UnlimitedUntarSize ,
320
- tar .UnlimitedUntarSize ,
321
- os .Getenv ("SOURCE_CONTROLLER_LOCALHOST" ),
322
- ctrl .LoggerFrom (ctx ),
323
- ).Fetch (src .GetArtifact ().URL , src .GetArtifact ().Digest , tmpDir ); err != nil {
319
+ fetcher := fetch .New (
320
+ fetch .WithLogger (ctrl .LoggerFrom (ctx )),
321
+ fetch .WithRetries (r .artifactFetchRetries ),
322
+ fetch .WithMaxDownloadSize (tar .UnlimitedUntarSize ),
323
+ fetch .WithUntar (tar .WithMaxUntarSize (tar .UnlimitedUntarSize )),
324
+ fetch .WithHostnameOverwrite (os .Getenv ("SOURCE_CONTROLLER_LOCALHOST" )),
325
+ )
326
+ if err = fetcher .Fetch (src .GetArtifact ().URL , src .GetArtifact ().Digest , tmpDir ); err != nil {
324
327
conditions .MarkFalse (obj , meta .ReadyCondition , meta .ArtifactFailedReason , "%s" , err )
325
328
return err
326
329
}
@@ -398,6 +401,13 @@ func (r *KustomizationReconciler) reconcile(
398
401
return err
399
402
}
400
403
404
+ // Calculate the digest of the built resources for history tracking.
405
+ checksum := digest .FromBytes (resources ).String ()
406
+ historyMeta := map [string ]string {"revision" : revision }
407
+ if originRevision != "" {
408
+ historyMeta ["originRevision" ] = originRevision
409
+ }
410
+
401
411
// Convert the build result into Kubernetes unstructured objects.
402
412
objects , err := ssautil .ReadObjects (bytes .NewReader (resources ))
403
413
if err != nil {
@@ -423,6 +433,7 @@ func (r *KustomizationReconciler) reconcile(
423
433
// Validate and apply resources in stages.
424
434
drifted , changeSet , err := r .apply (ctx , resourceManager , obj , revision , originRevision , objects )
425
435
if err != nil {
436
+ obj .Status .History .Upsert (checksum , time .Now (), time .Since (reconcileStart ), meta .ReconciliationFailedReason , historyMeta )
426
437
conditions .MarkFalse (obj , meta .ReadyCondition , meta .ReconciliationFailedReason , "%s" , err )
427
438
return err
428
439
}
@@ -431,6 +442,7 @@ func (r *KustomizationReconciler) reconcile(
431
442
newInventory := inventory .New ()
432
443
err = inventory .AddChangeSet (newInventory , changeSet )
433
444
if err != nil {
445
+ obj .Status .History .Upsert (checksum , time .Now (), time .Since (reconcileStart ), meta .ReconciliationFailedReason , historyMeta )
434
446
conditions .MarkFalse (obj , meta .ReadyCondition , meta .ReconciliationFailedReason , "%s" , err )
435
447
return err
436
448
}
@@ -441,12 +453,14 @@ func (r *KustomizationReconciler) reconcile(
441
453
// Detect stale resources which are subject to garbage collection.
442
454
staleObjects , err := inventory .Diff (oldInventory , newInventory )
443
455
if err != nil {
456
+ obj .Status .History .Upsert (checksum , time .Now (), time .Since (reconcileStart ), meta .ReconciliationFailedReason , historyMeta )
444
457
conditions .MarkFalse (obj , meta .ReadyCondition , meta .ReconciliationFailedReason , "%s" , err )
445
458
return err
446
459
}
447
460
448
461
// Run garbage collection for stale resources that do not have pruning disabled.
449
462
if _ , err := r .prune (ctx , resourceManager , obj , revision , originRevision , staleObjects ); err != nil {
463
+ obj .Status .History .Upsert (checksum , time .Now (), time .Since (reconcileStart ), meta .PruneFailedReason , historyMeta )
450
464
conditions .MarkFalse (obj , meta .ReadyCondition , meta .PruneFailedReason , "%s" , err )
451
465
return err
452
466
}
@@ -462,6 +476,7 @@ func (r *KustomizationReconciler) reconcile(
462
476
isNewRevision ,
463
477
drifted ,
464
478
changeSet .ToObjMetadataSet ()); err != nil {
479
+ obj .Status .History .Upsert (checksum , time .Now (), time .Since (reconcileStart ), meta .HealthCheckFailedReason , historyMeta )
465
480
conditions .MarkFalse (obj , meta .ReadyCondition , meta .HealthCheckFailedReason , "%s" , err )
466
481
return err
467
482
}
@@ -475,6 +490,11 @@ func (r *KustomizationReconciler) reconcile(
475
490
meta .ReadyCondition ,
476
491
meta .ReconciliationSucceededReason ,
477
492
"Applied revision: %s" , revision )
493
+ obj .Status .History .Upsert (checksum ,
494
+ time .Now (),
495
+ time .Since (reconcileStart ),
496
+ meta .ReconciliationSucceededReason ,
497
+ historyMeta )
478
498
479
499
return nil
480
500
}
0 commit comments