Skip to content

Commit 6d42223

Browse files
committed
fix(registry): preserve dry-run in tap storage
Tap Create and Delete bound the options parameter to _ and built their own metav1.CreateOptions and DeleteOptions for the dynamic client, so the caller's dry-run directive never reached the backing write. A --dry-run=server connect created the Flux OCIRepository and a dry-run disconnect deleted the PackageSource and its source for real, which is the opposite of what a server-side dry-run promises. Forward the directive to all five backing writes, including the orphan-source fallback that had no access to the options at all. Field manager stays as the storage sets it: the objects are synthesized here rather than supplied by the caller, so who owns them is a separate question from whether the write happens. The fake dynamic client's tracker ignores DryRun and persists regardless, so the tests assert the options recorded on the backing action instead of the absence of an object; reverting any one of the five turns the suite red. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 37e22dc commit 6d42223

2 files changed

Lines changed: 168 additions & 9 deletions

File tree

‎pkg/registry/core/tap/rest.go‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,26 @@ func (r *REST) pendingTapByName(ctx context.Context, name string) (*corev1alpha1
179179
// materialization is pending.
180180
// -----------------------------------------------------------------------------
181181

182-
func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, _ *metav1.CreateOptions) (runtime.Object, error) {
182+
// createDryRun and deleteDryRun carry the caller's dry-run directive down to
183+
// the backing write. The dynamic client takes the directive by value, so a
184+
// request that reaches it without one performs the write for real: a
185+
// --dry-run=server connect would create the Flux source and a dry-run
186+
// disconnect would delete the PackageSource.
187+
func createDryRun(opts *metav1.CreateOptions) []string {
188+
if opts == nil {
189+
return nil
190+
}
191+
return opts.DryRun
192+
}
193+
194+
func deleteDryRun(opts *metav1.DeleteOptions) []string {
195+
if opts == nil {
196+
return nil
197+
}
198+
return opts.DryRun
199+
}
200+
201+
func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, opts *metav1.CreateOptions) (runtime.Object, error) {
183202
in, ok := obj.(*corev1alpha1.Tap)
184203
if !ok {
185204
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a Tap object, got %T", obj))
@@ -219,7 +238,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
219238
// Create the Flux source, idempotently: a repeat connect updates the
220239
// existing source (new tag/secret) rather than erroring.
221240
src := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system")
222-
if _, err := src.Create(ctx, repo, metav1.CreateOptions{FieldManager: "cozystack-api"}); err != nil {
241+
if _, err := src.Create(ctx, repo, metav1.CreateOptions{FieldManager: "cozystack-api", DryRun: createDryRun(opts)}); err != nil {
223242
if !apierrors.IsAlreadyExists(err) {
224243
return nil, apierrors.NewInternalError(fmt.Errorf("create Flux source for tap %s: %w", target.FluxSourceName, err))
225244
}
@@ -251,7 +270,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
251270
}
252271
ann[tapconst.NameAnnotation] = target.FluxSourceName
253272
cur.SetAnnotations(ann)
254-
if _, err := src.Update(ctx, cur, metav1.UpdateOptions{FieldManager: "cozystack-api"}); err != nil {
273+
if _, err := src.Update(ctx, cur, metav1.UpdateOptions{FieldManager: "cozystack-api", DryRun: createDryRun(opts)}); err != nil {
255274
return nil, apierrors.NewInternalError(fmt.Errorf("update Flux source for tap %s: %w", target.FluxSourceName, err))
256275
}
257276
}
@@ -273,7 +292,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
273292
// GracefulDeleter — disconnect a community tap (mirrors `cozypkg untap`)
274293
// -----------------------------------------------------------------------------
275294

276-
func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, _ *metav1.DeleteOptions) (runtime.Object, bool, error) {
295+
func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, opts *metav1.DeleteOptions) (runtime.Object, bool, error) {
277296
u, err := r.dyn.Resource(gvrPackageSources).Get(ctx, name, metav1.GetOptions{})
278297
if err != nil {
279298
if apierrors.IsNotFound(err) {
@@ -282,7 +301,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va
282301
// name). Fall back to removing that source so a pending or
283302
// collision-blocked connect is still recoverable from the dashboard
284303
// rather than orphaning a finalized OCIRepository.
285-
return r.deleteOrphanTapSource(ctx, name)
304+
return r.deleteOrphanTapSource(ctx, name, deleteDryRun(opts))
286305
}
287306
return nil, false, apierrors.NewInternalError(fmt.Errorf("get PackageSource %q: %w", name, err))
288307
}
@@ -304,7 +323,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va
304323
}
305324
}
306325

307-
if err := r.dyn.Resource(gvrPackageSources).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
326+
if err := r.dyn.Resource(gvrPackageSources).Delete(ctx, name, metav1.DeleteOptions{DryRun: deleteDryRun(opts)}); err != nil {
308327
return nil, false, apierrors.NewInternalError(fmt.Errorf("delete PackageSource %q: %w", name, err))
309328
}
310329

@@ -317,7 +336,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va
317336
if ns == "" {
318337
ns = "cozy-system"
319338
}
320-
if err := r.dyn.Resource(gvrOCIRepos).Namespace(ns).Delete(ctx, ref.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
339+
if err := r.dyn.Resource(gvrOCIRepos).Namespace(ns).Delete(ctx, ref.Name, metav1.DeleteOptions{DryRun: deleteDryRun(opts)}); err != nil && !apierrors.IsNotFound(err) {
321340
klog.V(2).InfoS("tap disconnected but its OCIRepository could not be deleted", "source", ref.Name, "err", err)
322341
}
323342
}
@@ -329,7 +348,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va
329348
// deleteOrphanTapSource removes a tap whose OCIRepository exists but whose
330349
// PackageSource has not been materialized. It matches the labeled OCIRepository
331350
// carrying the tap-name annotation and deletes it.
332-
func (r *REST) deleteOrphanTapSource(ctx context.Context, name string) (runtime.Object, bool, error) {
351+
func (r *REST) deleteOrphanTapSource(ctx context.Context, name string, dryRun []string) (runtime.Object, bool, error) {
333352
list, err := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system").List(ctx, metav1.ListOptions{
334353
LabelSelector: tapconst.Label + "=true",
335354
})
@@ -341,7 +360,7 @@ func (r *REST) deleteOrphanTapSource(ctx context.Context, name string) (runtime.
341360
if item.GetAnnotations()[tapconst.NameAnnotation] != name {
342361
continue
343362
}
344-
if err := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system").Delete(ctx, item.GetName(), metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
363+
if err := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system").Delete(ctx, item.GetName(), metav1.DeleteOptions{DryRun: dryRun}); err != nil && !apierrors.IsNotFound(err) {
345364
return nil, false, apierrors.NewInternalError(fmt.Errorf("delete tap source %q: %w", item.GetName(), err))
346365
}
347366
tap := &corev1alpha1.Tap{
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2025 The Cozystack Authors.
3+
4+
package tap
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
13+
dynamicfake "k8s.io/client-go/dynamic/fake"
14+
k8stesting "k8s.io/client-go/testing"
15+
16+
"github.com/cozystack/cozystack/internal/marketplace/tapconst"
17+
corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1"
18+
)
19+
20+
// The fake dynamic client's tracker ignores DryRun and persists regardless, so
21+
// asserting that an object is absent afterwards would pass with or without the
22+
// fix. What distinguishes them is the options the storage hands to the backing
23+
// client, which the fake records on the action.
24+
25+
func recordedActions(t *testing.T, r *REST) []k8stesting.Action {
26+
t.Helper()
27+
f, ok := r.dyn.(*dynamicfake.FakeDynamicClient)
28+
if !ok {
29+
t.Fatalf("expected a fake dynamic client, got %T", r.dyn)
30+
}
31+
return f.Actions()
32+
}
33+
34+
func createDryRunFor(t *testing.T, r *REST, gvr schema.GroupVersionResource) []string {
35+
t.Helper()
36+
for _, a := range recordedActions(t, r) {
37+
c, ok := a.(k8stesting.CreateActionImpl)
38+
if ok && a.GetVerb() == "create" && a.GetResource() == gvr {
39+
return c.CreateOptions.DryRun
40+
}
41+
}
42+
t.Fatalf("no create action recorded for %q", gvr.Resource)
43+
return nil
44+
}
45+
46+
func updateDryRunFor(t *testing.T, r *REST, gvr schema.GroupVersionResource) []string {
47+
t.Helper()
48+
for _, a := range recordedActions(t, r) {
49+
u, ok := a.(k8stesting.UpdateActionImpl)
50+
if ok && a.GetVerb() == "update" && a.GetResource() == gvr {
51+
return u.UpdateOptions.DryRun
52+
}
53+
}
54+
t.Fatalf("no update action recorded for %q", gvr.Resource)
55+
return nil
56+
}
57+
58+
func deleteDryRunFor(t *testing.T, r *REST, gvr schema.GroupVersionResource) []string {
59+
t.Helper()
60+
for _, a := range recordedActions(t, r) {
61+
d, ok := a.(k8stesting.DeleteActionImpl)
62+
if ok && a.GetVerb() == "delete" && a.GetResource() == gvr {
63+
return d.DeleteOptions.DryRun
64+
}
65+
}
66+
t.Fatalf("no delete action recorded for %q", gvr.Resource)
67+
return nil
68+
}
69+
70+
func wantDryRunAll(t *testing.T, what string, got []string) {
71+
t.Helper()
72+
if len(got) != 1 || got[0] != metav1.DryRunAll {
73+
t.Errorf("%s dropped the caller's dry-run: got %v, want [%s]", what, got, metav1.DryRunAll)
74+
}
75+
}
76+
77+
func connectTap() *corev1alpha1.Tap {
78+
return &corev1alpha1.Tap{
79+
ObjectMeta: metav1.ObjectMeta{Name: "a.b"},
80+
Spec: corev1alpha1.TapSpec{URL: "oci://ghcr.io/a/b", Tag: "v1"},
81+
}
82+
}
83+
84+
// orphanOciRepoObj is a tap whose OCIRepository exists but whose PackageSource
85+
// was never materialized, which is the fallback branch of Delete.
86+
func orphanOciRepoObj(srcName, tapName string) *unstructured.Unstructured {
87+
u := ociRepoObj(srcName)
88+
u.SetLabels(map[string]string{tapconst.Label: "true"})
89+
u.SetAnnotations(map[string]string{tapconst.NameAnnotation: tapName})
90+
return u
91+
}
92+
93+
func TestCreateForwardsDryRunToBackingSource(t *testing.T) {
94+
r := fakeREST()
95+
opts := &metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}}
96+
if _, err := r.Create(context.Background(), connectTap(), nil, opts); err != nil {
97+
t.Fatalf("create: %v", err)
98+
}
99+
wantDryRunAll(t, "the backing OCIRepository create", createDryRunFor(t, r, gvrOCIRepos))
100+
}
101+
102+
func TestCreateForwardsDryRunWhenSourceAlreadyExists(t *testing.T) {
103+
// A repeat connect at the same URL takes the update branch.
104+
r := fakeREST(ociRepoObj("tap-a-b"))
105+
opts := &metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}}
106+
if _, err := r.Create(context.Background(), connectTap(), nil, opts); err != nil {
107+
t.Fatalf("create: %v", err)
108+
}
109+
wantDryRunAll(t, "the backing OCIRepository update", updateDryRunFor(t, r, gvrOCIRepos))
110+
}
111+
112+
func TestDeleteForwardsDryRunToBackingDeletes(t *testing.T) {
113+
r := fakeREST(tapPsObj("a.b", "tap-a-b"), ociRepoObj("tap-a-b"))
114+
opts := &metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}}
115+
if _, _, err := r.Delete(context.Background(), "a.b", nil, opts); err != nil {
116+
t.Fatalf("delete: %v", err)
117+
}
118+
wantDryRunAll(t, "the backing PackageSource delete", deleteDryRunFor(t, r, gvrPackageSources))
119+
wantDryRunAll(t, "the backing OCIRepository delete", deleteDryRunFor(t, r, gvrOCIRepos))
120+
}
121+
122+
func TestDeleteOrphanTapSourceForwardsDryRun(t *testing.T) {
123+
r := fakeREST(orphanOciRepoObj("tap-a-b", "a.b"))
124+
opts := &metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}}
125+
if _, _, err := r.Delete(context.Background(), "a.b", nil, opts); err != nil {
126+
t.Fatalf("delete: %v", err)
127+
}
128+
wantDryRunAll(t, "the orphan OCIRepository delete", deleteDryRunFor(t, r, gvrOCIRepos))
129+
}
130+
131+
func TestWritesWithoutOptionsDoNotSynthesiseDryRun(t *testing.T) {
132+
// Callers that pass no options at all must keep writing for real.
133+
r := fakeREST(tapPsObj("a.b", "tap-a-b"), ociRepoObj("tap-a-b"))
134+
if _, _, err := r.Delete(context.Background(), "a.b", nil, nil); err != nil {
135+
t.Fatalf("delete with nil options: %v", err)
136+
}
137+
if got := deleteDryRunFor(t, r, gvrPackageSources); len(got) != 0 {
138+
t.Errorf("nil options must not produce a dry-run: got %v", got)
139+
}
140+
}

0 commit comments

Comments
 (0)