Skip to content

Commit 44207f4

Browse files
committed
Avoid event logging GC failure
We try to avoid affecting the source reconciliation when there's a garbage collection related failure. The event logging was resulting in events and notifications related to GC failure when the artifact directory isn't created in the first reconciliation of an object. Signed-off-by: Sunny <[email protected]>
1 parent 741033e commit 44207f4

File tree

5 files changed

+11
-22
lines changed

5 files changed

+11
-22
lines changed

controllers/bucket_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,12 +634,10 @@ func (r *BucketReconciler) garbageCollect(ctx context.Context, obj *sourcev1.Buc
634634
if obj.GetArtifact() != nil {
635635
delFiles, err := r.Storage.GarbageCollect(ctx, *obj.GetArtifact(), time.Second*5)
636636
if err != nil {
637-
e := &serror.Event{
637+
return &serror.Event{
638638
Err: fmt.Errorf("garbage collection of artifacts failed: %w", err),
639639
Reason: "GarbageCollectionFailed",
640640
}
641-
r.eventLogf(ctx, obj, corev1.EventTypeWarning, e.Reason, e.Err.Error())
642-
return e
643641
}
644642
if len(delFiles) > 0 {
645643
r.eventLogf(ctx, obj, events.EventTypeTrace, "GarbageCollectionSucceeded",

controllers/gitrepository_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,10 @@ func (r *GitRepositoryReconciler) garbageCollect(ctx context.Context, obj *sourc
710710
if obj.GetArtifact() != nil {
711711
delFiles, err := r.Storage.GarbageCollect(ctx, *obj.GetArtifact(), time.Second*5)
712712
if err != nil {
713-
e := &serror.Event{
713+
return &serror.Event{
714714
Err: fmt.Errorf("garbage collection of artifacts failed: %w", err),
715715
Reason: "GarbageCollectionFailed",
716716
}
717-
r.eventLogf(ctx, obj, corev1.EventTypeWarning, e.Reason, e.Err.Error())
718-
return e
719717
}
720718
if len(delFiles) > 0 {
721719
r.eventLogf(ctx, obj, events.EventTypeTrace, "GarbageCollectionSucceeded",

controllers/helmchart_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,10 @@ func (r *HelmChartReconciler) garbageCollect(ctx context.Context, obj *sourcev1.
806806
if obj.GetArtifact() != nil {
807807
delFiles, err := r.Storage.GarbageCollect(ctx, *obj.GetArtifact(), time.Second*5)
808808
if err != nil {
809-
e := &serror.Event{
809+
return &serror.Event{
810810
Err: fmt.Errorf("garbage collection of artifacts failed: %w", err),
811811
Reason: "GarbageCollectionFailed",
812812
}
813-
r.eventLogf(ctx, obj, corev1.EventTypeWarning, e.Reason, e.Err.Error())
814-
return e
815813
}
816814
if len(delFiles) > 0 {
817815
r.eventLogf(ctx, obj, events.EventTypeTrace, "GarbageCollectionSucceeded",

controllers/helmrepository_controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,10 @@ func (r *HelmRepositoryReconciler) garbageCollect(ctx context.Context, obj *sour
520520
if obj.GetArtifact() != nil {
521521
delFiles, err := r.Storage.GarbageCollect(ctx, *obj.GetArtifact(), time.Second*5)
522522
if err != nil {
523-
e := &serror.Event{
523+
return &serror.Event{
524524
Err: fmt.Errorf("garbage collection of artifacts failed: %w", err),
525525
Reason: "GarbageCollectionFailed",
526526
}
527-
r.eventLogf(ctx, obj, corev1.EventTypeWarning, e.Reason, e.Err.Error())
528-
return e
529527
}
530528
if len(delFiles) > 0 {
531529
r.eventLogf(ctx, obj, events.EventTypeTrace, "GarbageCollectionSucceeded",

controllers/storage.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"hash"
2626
"io"
27+
"io/fs"
2728
"net/url"
2829
"os"
2930
"path/filepath"
@@ -32,16 +33,12 @@ import (
3233
"time"
3334

3435
securejoin "github.com/cyphar/filepath-securejoin"
36+
"github.com/fluxcd/pkg/lockedfile"
37+
"github.com/fluxcd/pkg/untar"
3538
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
36-
kerrors "k8s.io/apimachinery/pkg/util/errors"
37-
3839
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
40+
kerrors "k8s.io/apimachinery/pkg/util/errors"
3941

40-
"github.com/fluxcd/pkg/lockedfile"
41-
42-
"io/fs"
43-
44-
"github.com/fluxcd/pkg/untar"
4542
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
4643
sourcefs "github.com/fluxcd/source-controller/internal/fs"
4744
"github.com/fluxcd/source-controller/pkg/sourceignore"
@@ -181,7 +178,7 @@ func (s *Storage) getGarbageFiles(artifact sourcev1.Artifact, totalCountLimit, m
181178
return nil
182179
}
183180
if totalFiles >= totalCountLimit {
184-
return fmt.Errorf("Reached file walking limit, already walked over: %d", totalFiles)
181+
return fmt.Errorf("reached file walking limit, already walked over: %d", totalFiles)
185182
}
186183
info, err := d.Info()
187184
if err != nil {
@@ -190,8 +187,8 @@ func (s *Storage) getGarbageFiles(artifact sourcev1.Artifact, totalCountLimit, m
190187
}
191188
createdAt := info.ModTime().UTC()
192189
diff := now.Sub(createdAt)
193-
// compare the time difference between now and the time at which the file was created
194-
// with the provided ttl. delete if difference is greater than the ttl.
190+
// Compare the time difference between now and the time at which the file was created
191+
// with the provided TTL. Delete if the difference is greater than the TTL.
195192
expired := diff > ttl
196193
if !info.IsDir() && info.Mode()&os.ModeSymlink != os.ModeSymlink {
197194
if path != localPath && expired {

0 commit comments

Comments
 (0)