From e2f87c0cd774f01255e9dd10b34e9d2971ec8d16 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Mon, 26 Oct 2026 16:55:40 +0300 Subject: [PATCH] fix(marketplace): pin a tap digest only from a current-generation artifact The materializer pinned whatever digest status.artifact reported, even when the artifact still described the previous reference. On a re-connect to a new tag, source-controller has not re-resolved yet, so the operator materialized and pinned the old tag's content (issue #4979). Gate materialization and pinning on the artifact reflecting the current spec: status.observedGeneration equals metadata.generation, and for a tag-only source a revision that belongs to that tag (@). A tap connected before digest pinning existed already has its materialized-revision equal to the artifact revision, so Reconcile short-circuited and never pinned, letting it follow the tag one more time. Pin the current digest before that early return. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit 7494d1fad20f8c4b18d52f71f4a3311643fd90d4) Signed-off-by: Myasnikov Daniil --- .../operator/tapmaterializer_reconciler.go | 34 +++++++++ .../tapmaterializer_reconciler_test.go | 70 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/internal/operator/tapmaterializer_reconciler.go b/internal/operator/tapmaterializer_reconciler.go index fd4b4994c..a578862cb 100644 --- a/internal/operator/tapmaterializer_reconciler.go +++ b/internal/operator/tapmaterializer_reconciler.go @@ -105,7 +105,24 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ // fail-closed on the integrity check. return ctrl.Result{RequeueAfter: tapWaitRequeue}, nil } + // Act only on an artifact that reflects the current spec. After a re-connect + // changes spec.ref, source-controller must re-resolve before status.artifact + // describes the new reference; acting early would materialize and pin the + // previous revision (issue #4979). + if !r.artifactReady(&repo, art.Revision) { + return ctrl.Result{RequeueAfter: tapWaitRequeue}, nil + } if repo.Annotations[tapconst.MaterializedRevisionAnnotation] == art.Revision { + // Already materialized at this revision. A tap connected before digest + // pinning existed still follows its tag (no digest on spec.ref); pin the + // current digest now so moving the tag upstream cannot pull new content on + // the next revision. A source that already carries a digest needs nothing. + if ref := repo.Spec.Reference; ref == nil || ref.Digest == "" { + pinTapDigest(&repo, art.Revision) + if err := r.Update(ctx, &repo); err != nil { + return ctrl.Result{}, err + } + } return ctrl.Result{}, nil } @@ -208,6 +225,23 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, nil } +// artifactReady reports whether status.artifact reflects the current spec +// generation and, for a tag-only source, the tag the source now follows. It +// gates materialization and pinning so a re-connect's new spec is never acted on +// with the previous revision's artifact. source-controller formats a tag +// revision as '@', so a tag-only ref requires that prefix; a source +// pinned to a digest reports a bare digest revision and needs only the +// generation check. +func (r *TapMaterializerReconciler) artifactReady(repo *sourcev1.OCIRepository, revision string) bool { + if repo.Status.ObservedGeneration != repo.Generation { + return false + } + if ref := repo.Spec.Reference; ref != nil && ref.Tag != "" && ref.Digest == "" { + return strings.HasPrefix(revision, ref.Tag+"@") + } + return true +} + // manifestDigestRe matches a full OCI manifest digest (sha256:<64 hex>). var manifestDigestRe = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`) diff --git a/internal/operator/tapmaterializer_reconciler_test.go b/internal/operator/tapmaterializer_reconciler_test.go index 404ce6e48..e7c6f7148 100644 --- a/internal/operator/tapmaterializer_reconciler_test.go +++ b/internal/operator/tapmaterializer_reconciler_test.go @@ -640,3 +640,73 @@ func TestReconcilePinIsIdempotent(t *testing.T) { t.Errorf("expected materialized revision stamped to the bare digest, got %q", got.Annotations[tapconst.MaterializedRevisionAnnotation]) } } + +// On a re-connect to a new tag, status.artifact still describes the previous +// tag's content until source-controller re-resolves. The reconciler must not +// materialize and pin that stale revision (issue #4979): spec.ref.Tag is v2 but +// the revision is still v1@, so nothing is pinned until the revision +// belongs to the tag now followed. +func TestReconcileDoesNotPinStaleArtifactOnReconnect(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + oldManifest := "sha256:" + strings.Repeat("a", 64) + repo := tapRepo(true) + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Tag: "v2"} // re-connect cleared the pin, requested v2 + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + setArtifact(t, cl, digest, "v1@"+oldManifest) // still the old tag's revision + + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("reconcile: %v", err) + } + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference.Digest != "" { + t.Errorf("must not pin the previous tag's digest while the artifact is stale, got %q", got.Spec.Reference.Digest) + } + if got.Annotations[tapconst.PinnedDigestAnnotation] != "" { + t.Errorf("must not record a pin from a stale artifact, got %v", got.Annotations) + } + if got.Annotations[tapconst.MaterializedRevisionAnnotation] != "" { + t.Errorf("must not stamp a revision from a stale artifact, got %v", got.Annotations) + } +} + +// A tap materialized before digest pinning existed already has its +// materialized-revision equal to the artifact revision, so Reconcile would +// short-circuit. It must still pin the current digest before that early return, +// so moving the tag upstream does not pull new content on the next revision +// (issue #4979). +func TestReconcilePinsPreUpgradeTapBeforeEarlyReturn(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + manifest := "sha256:" + strings.Repeat("b", 64) + repo := tapRepo(true) + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Tag: "v1"} // tag-only, never pinned + repo.Annotations = map[string]string{tapconst.MaterializedRevisionAnnotation: "v1@" + manifest} + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + setArtifact(t, cl, digest, "v1@"+manifest) + + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("reconcile: %v", err) + } + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference == nil || got.Spec.Reference.Digest != manifest { + t.Fatalf("a pre-upgrade tap must be pinned to the current digest, got %+v", got.Spec.Reference) + } + if got.Annotations[tapconst.PinnedDigestAnnotation] != manifest { + t.Errorf("expected pinned-digest annotation %q, got %v", manifest, got.Annotations) + } +}