diff --git a/cmd/cozypkg/cmd/tap.go b/cmd/cozypkg/cmd/tap.go index 6260e14d0..3f844bfc7 100644 --- a/cmd/cozypkg/cmd/tap.go +++ b/cmd/cozypkg/cmd/tap.go @@ -209,21 +209,38 @@ func rewritePackageSourceForTap(ps *cozyv1alpha1.PackageSource, sourceName, sour } } -// clearMaterializedRevision removes the operator's materialized-revision -// annotation from the tap's OCIRepository, so the next reconcile does not -// short-circuit on the unchanged artifact revision and re-materializes its -// PackageSources (recovering one removed out-of-band). A no-op when the source or -// the annotation is absent. -func clearMaterializedRevision(ctx context.Context, k8sClient client.Client, srcName string) error { +// resetTapPinState prepares a tap's OCIRepository for a re-tap. It clears the +// materialized-revision annotation so the next reconcile does not short-circuit +// on the unchanged revision and re-materializes its PackageSources (recovering +// one removed out-of-band), and the pinned-digest annotation so the operator +// re-pins the newly requested reference. For a tag-based re-tap (explicitDigest +// false) it also drops the operator's prior digest pin from spec.ref: a +// server-side apply under field owner cozypkg cannot clear a field the operator +// owns, so a moved tag would otherwise stay pinned to the old digest. An +// explicit-digest re-tap keeps the digest the apply itself set. A no-op (and no +// error) when the source is absent or there is nothing to clear. +func resetTapPinState(ctx context.Context, k8sClient client.Client, srcName string, explicitDigest bool) error { reg := &sourcev1.OCIRepository{} if err := k8sClient.Get(ctx, client.ObjectKey{Name: srcName, Namespace: cozySystemNamespace}, reg); err != nil { return client.IgnoreNotFound(err) } - if _, ok := reg.Annotations[tapconst.MaterializedRevisionAnnotation]; !ok { + base := reg.DeepCopy() + changed := false + if _, ok := reg.Annotations[tapconst.MaterializedRevisionAnnotation]; ok { + delete(reg.Annotations, tapconst.MaterializedRevisionAnnotation) + changed = true + } + if _, ok := reg.Annotations[tapconst.PinnedDigestAnnotation]; ok { + delete(reg.Annotations, tapconst.PinnedDigestAnnotation) + changed = true + } + if !explicitDigest && reg.Spec.Reference != nil && reg.Spec.Reference.Digest != "" { + reg.Spec.Reference.Digest = "" + changed = true + } + if !changed { return nil } - base := reg.DeepCopy() - delete(reg.Annotations, tapconst.MaterializedRevisionAnnotation) return k8sClient.Patch(ctx, reg, client.MergeFrom(base)) } @@ -378,8 +395,9 @@ runs its charts in your management cluster: tap only sources you trust.`, // PackageSource removed out-of-band since the last materialization. Tapping // no longer installs anything, so a re-tap does NOT re-install an app removed // with `cozypkg del`; re-install it with `cozypkg add`. A first tap has no - // such annotation, so this is a no-op then. - if err := clearMaterializedRevision(ctx, k8sClient, srcName); err != nil { + // such annotation, so this is a no-op then. resetTapPinState also drops the + // operator's prior digest pin so the newly requested tag re-resolves. + if err := resetTapPinState(ctx, k8sClient, srcName, ref.Digest != ""); err != nil { _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not reset materialization state on OCIRepository/%s: %v\n", srcName, err) } diff --git a/cmd/cozypkg/cmd/tap_test.go b/cmd/cozypkg/cmd/tap_test.go index c6a6a9309..d57e75b2e 100644 --- a/cmd/cozypkg/cmd/tap_test.go +++ b/cmd/cozypkg/cmd/tap_test.go @@ -317,16 +317,23 @@ func TestCreatePackageIdempotent(t *testing.T) { } } -func TestClearMaterializedRevision(t *testing.T) { +func TestResetTapPinState(t *testing.T) { s := untapScheme(t) + const oldDigest = "sha256:2222222222222222222222222222222222222222222222222222222222222222" oci := &sourcev1.OCIRepository{ObjectMeta: metav1.ObjectMeta{ Name: "tap-acme-hello", Namespace: "cozy-system", - Annotations: map[string]string{tapconst.MaterializedRevisionAnnotation: "rev-1", "keep": "me"}, - }} + Annotations: map[string]string{ + tapconst.MaterializedRevisionAnnotation: "v1@" + oldDigest, + tapconst.PinnedDigestAnnotation: oldDigest, + "keep": "me", + }, + }, Spec: sourcev1.OCIRepositorySpec{Reference: &sourcev1.OCIRepositoryRef{Tag: "v1", Digest: oldDigest}}} cl := fake.NewClientBuilder().WithScheme(s).WithObjects(oci).Build() - if err := clearMaterializedRevision(context.Background(), cl, "tap-acme-hello"); err != nil { - t.Fatalf("clear: %v", err) + // A tag-based re-tap must drop the operator's digest pin (cozypkg cannot + // clear a field the operator owns via server-side apply) and both annotations. + if err := resetTapPinState(context.Background(), cl, "tap-acme-hello", false); err != nil { + t.Fatalf("reset: %v", err) } var got sourcev1.OCIRepository if err := cl.Get(context.Background(), client.ObjectKey{Name: "tap-acme-hello", Namespace: "cozy-system"}, &got); err != nil { @@ -335,11 +342,38 @@ func TestClearMaterializedRevision(t *testing.T) { if _, ok := got.Annotations[tapconst.MaterializedRevisionAnnotation]; ok { t.Error("materialized-revision annotation must be cleared to force re-materialization") } + if _, ok := got.Annotations[tapconst.PinnedDigestAnnotation]; ok { + t.Error("pinned-digest annotation must be cleared so the operator re-pins") + } + if got.Spec.Reference != nil && got.Spec.Reference.Digest != "" { + t.Errorf("a tag re-tap must drop the operator digest pin, got %q", got.Spec.Reference.Digest) + } + if got.Spec.Reference == nil || got.Spec.Reference.Tag != "v1" { + t.Errorf("the tag must be left in place, got %+v", got.Spec.Reference) + } if got.Annotations["keep"] != "me" { t.Error("unrelated annotations must be preserved") } + + // An explicit-digest re-tap must NOT drop the digest the CLI itself set. + oci2 := &sourcev1.OCIRepository{ObjectMeta: metav1.ObjectMeta{ + Name: "tap-acme-two", Namespace: "cozy-system", + Annotations: map[string]string{tapconst.MaterializedRevisionAnnotation: oldDigest}, + }, Spec: sourcev1.OCIRepositorySpec{Reference: &sourcev1.OCIRepositoryRef{Digest: oldDigest}}} + cl2 := fake.NewClientBuilder().WithScheme(s).WithObjects(oci2).Build() + if err := resetTapPinState(context.Background(), cl2, "tap-acme-two", true); err != nil { + t.Fatalf("reset explicit: %v", err) + } + var got2 sourcev1.OCIRepository + if err := cl2.Get(context.Background(), client.ObjectKey{Name: "tap-acme-two", Namespace: "cozy-system"}, &got2); err != nil { + t.Fatal(err) + } + if got2.Spec.Reference == nil || got2.Spec.Reference.Digest != oldDigest { + t.Errorf("an explicit-digest re-tap must keep its digest, got %+v", got2.Spec.Reference) + } + // Absent source: no error. - if err := clearMaterializedRevision(context.Background(), cl, "missing"); err != nil { + if err := resetTapPinState(context.Background(), cl, "missing", false); err != nil { t.Errorf("a missing source must not error, got %v", err) } }