Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/controller/artifactgenerator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context,

// Garbage collect old artifacts in storage according to the retention policy.
for _, eaRef := range eaRefs {
storagePath := gotkstroage.ArtifactPath(sourcev1.ExternalArtifactKind, eaRef.Namespace, eaRef.Name, "*")
storagePath := gotkstroage.ArtifactPath(sourcev1.ExternalArtifactKind, eaRef.Namespace, eaRef.Name, eaRef.Filename)
delFiles, err := r.Storage.GarbageCollect(ctx, gotkmeta.Artifact{Path: storagePath}, 5*time.Minute)
if err != nil {
log.Error(err, "failed to garbage collect artifacts", "path", storagePath)
Expand Down
68 changes: 68 additions & 0 deletions internal/controller/artifactgenerator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

gotkmeta "github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -291,6 +292,73 @@ func TestArtifactGeneratorReconciler_Reconcile(t *testing.T) {
g.Expect(a).To(HaveLen(0))
}

// TestArtifactGeneratorReconciler_RetainsCurrentArtifacts exercises retention
// when both the published archives and an older revision have expired.
func TestArtifactGeneratorReconciler_RetainsCurrentArtifacts(t *testing.T) {
g := NewWithT(t)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

ns, err := testEnv.CreateNamespace(ctx, "retention")
g.Expect(err).NotTo(HaveOccurred())
key := client.ObjectKey{Namespace: ns.Name, Name: "retention"}
req := reconcile.Request{NamespacedName: key}
obj := getArtifactGenerator(key)
g.Expect(testClient.Create(ctx, obj)).To(Succeed())
g.Expect(applyGitRepository(key, "main@sha256:abc123", []gotktestsrv.File{
{Name: "app.yaml", Body: "version: 1"},
})).To(Succeed())
g.Expect(applyOCIRepository(key, digest.FromString("initial").String(), []gotktestsrv.File{
{Name: "config.yaml", Body: "ociVersion: 1"},
})).To(Succeed())

reconciler := getArtifactGeneratorReconciler()
_, err = reconciler.Reconcile(ctx, req)
g.Expect(err).NotTo(HaveOccurred())
_, err = reconciler.Reconcile(ctx, req)
g.Expect(err).NotTo(HaveOccurred())

gitKey := client.ObjectKey{Namespace: ns.Name, Name: obj.Spec.OutputArtifacts[0].Name}
previous := &sourcev1.ExternalArtifact{}
g.Expect(testClient.Get(ctx, gitKey, previous)).To(Succeed())
g.Expect(previous.Status.Artifact).NotTo(BeNil())
expired := time.Now().Add(-2 * retentionTTL)
previousPath := testStorage.LocalPath(*previous.Status.Artifact)
previousTime := expired.Add(-retentionTTL)
g.Expect(os.Chtimes(previousPath, previousTime, previousTime)).To(Succeed())

// Age the newly built archives before garbage collection runs.
watchClient, err := client.NewWithWatch(testEnv.Config, client.Options{Scheme: testEnv.Scheme()})
g.Expect(err).NotTo(HaveOccurred())
reconciler.Client = interceptor.NewClient(watchClient, interceptor.Funcs{
SubResourcePatch: func(ctx context.Context, c client.Client, subresource string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
if ea, ok := obj.(*sourcev1.ExternalArtifact); ok && subresource == "status" {
path := testStorage.LocalPath(*ea.Status.Artifact)
if err := os.Chtimes(path, expired, expired); err != nil {
return err
}
}
return c.SubResource(subresource).Patch(ctx, obj, patch, opts...)
},
})
g.Expect(applyGitRepository(key, "main@sha256:def456", []gotktestsrv.File{
{Name: "app.yaml", Body: "version: 2"},
})).To(Succeed())
_, err = reconciler.Reconcile(ctx, req)
g.Expect(err).NotTo(HaveOccurred())

fetcher := gotkfetch.New(gotkfetch.WithMaxDownloadSize(1 << 20))
for _, output := range obj.Spec.OutputArtifacts {
ea := &sourcev1.ExternalArtifact{}
g.Expect(testClient.Get(ctx, client.ObjectKey{Namespace: ns.Name, Name: output.Name}, ea)).To(Succeed())
g.Expect(gotkconditions.IsReady(ea)).To(BeTrue())
artifact := ea.Status.Artifact
g.Expect(artifact).NotTo(BeNil())
g.Expect(fetcher.FetchWithContext(ctx, artifact.URL, artifact.Digest, t.TempDir())).To(Succeed())
}
g.Expect(previousPath).NotTo(BeAnExistingFile())
}

func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {
reconciler := getArtifactGeneratorReconciler()

Expand Down