Skip to content

Commit 3fa534f

Browse files
committed
fix(marketplace): do not install a connected repository's components on connect
Connecting a marketplace repository materialized its PackageSources and then auto-created a tap-managed Package per source. The Package reconciler installed a HelmRelease for every install-marked component of the default variant, which Flux runs under its cluster-admin service account with no impersonation. A publisher's install chart therefore ran as cluster-admin on connect, with no operator step, contradicting the documented behaviour that tapping installs nothing. Remove the on-connect auto-registration. Connecting now only materializes PackageSources; installing an app is an explicit cozypkg add (or dashboard action), which is the sole remaining creator of a Package. This also stops a Package the admin removed with cozypkg del from being re-created on the next source revision. Catalog population from PackageSource metadata without an install is left to the long-term design in #4359, and the privileged-confirmation gaps #4358 and #4359 stay open. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 215e582 commit 3fa534f

2 files changed

Lines changed: 67 additions & 131 deletions

File tree

‎internal/operator/tapmaterializer_reconciler.go‎

Lines changed: 9 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
sourcev1 "github.com/fluxcd/source-controller/api/v1"
3131
corev1 "k8s.io/api/core/v1"
3232
apierrors "k8s.io/apimachinery/pkg/api/errors"
33-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"k8s.io/apimachinery/pkg/runtime"
3534
"k8s.io/apimachinery/pkg/types"
3635
"k8s.io/client-go/tools/record"
@@ -165,22 +164,15 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
165164
if err := r.Patch(ctx, ps, client.Apply, client.FieldOwner(tapFieldOwner), client.ForceOwnership); err != nil {
166165
return ctrl.Result{}, fmt.Errorf("materialize PackageSource %s: %w", ps.GetName(), err)
167166
}
168-
// Register the repository's apps on connect (not on a later `cozypkg
169-
// add`) via a tap-managed Package whose install-marked components (the
170-
// ApplicationDefinition registrations) deploy so the apps appear in the
171-
// catalog, mirroring how the platform ships a Package per built-in app.
172-
// The app components carry no install block and are instantiated per user
173-
// resource. Privileged install components are refused for a tap-labelled
174-
// (unconfirmed) Package on several best-effort layers: the Package
175-
// reconciler skips their HelmRelease and prunes a lingering one at the
176-
// install site, and the PackageSource reconciler withholds their
177-
// ExternalArtifact (collision.PrivilegedConfirmed). A benign component
178-
// flipped to privileged in a new revision can still be materialised by
179-
// source-watcher before these observe the flip; fully closing that race
180-
// needs the catalog registration decoupled from install (issue #4359).
181-
if err := r.ensureRegistration(ctx, ps, &repo); err != nil {
182-
return ctrl.Result{}, fmt.Errorf("register apps for %s: %w", ps.GetName(), err)
183-
}
167+
// Connecting a repository materializes its PackageSources and nothing
168+
// else: no Package, HelmRelease or ApplicationDefinition is created here.
169+
// Auto-registering on connect (removed) created a tap-managed Package whose
170+
// install-marked components Flux deployed under its cluster-admin service
171+
// account, so a publisher's install chart ran on connect with no operator
172+
// step (issue #4978). Installing is now explicit: `cozypkg add` (or a
173+
// dashboard action) creates the Package. Populating the catalog from
174+
// PackageSource metadata without an install is the long-term shape in
175+
// issue #4359.
184176
applied[ps.GetName()] = true
185177
logger.Info("materialized PackageSource from tap", "name", ps.GetName(), "tap", repo.Name)
186178
}
@@ -230,52 +222,6 @@ func (r *TapMaterializerReconciler) failMaterialize(ctx context.Context, repo *s
230222
return ctrl.Result{}, nil
231223
}
232224

233-
// registrationOwnerRef builds the ownerReference from a registration Package to
234-
// its PackageSource, so the Package is garbage-collected if the PackageSource is
235-
// deleted directly.
236-
func registrationOwnerRef(ps *cozyv1alpha1.PackageSource) metav1.OwnerReference {
237-
return metav1.OwnerReference{
238-
APIVersion: cozyv1alpha1.GroupVersion.String(),
239-
Kind: "PackageSource",
240-
Name: ps.GetName(),
241-
UID: ps.GetUID(),
242-
}
243-
}
244-
245-
// autoRegisterSkipReason returns why a PackageSource's apps cannot be
246-
// auto-registered on connect, or "" when they can. Only a source with no
247-
// "default" variant is skipped (nothing to register by default). Privileged
248-
// components are NOT a reason to skip: the source is registered and its
249-
// privileged components are refused at install time (collision.PrivilegedConfirmed)
250-
// until the operator confirms with 'cozypkg add --allow-privileged'.
251-
func autoRegisterSkipReason(ps *cozyv1alpha1.PackageSource) string {
252-
if !hasDefaultVariant(ps) {
253-
return `it declares no "default" variant; register it manually with 'cozypkg add'`
254-
}
255-
return ""
256-
}
257-
258-
// warnNotAutoRegistered records on the source, as a Warning Event, that a
259-
// materialized PackageSource's apps were not auto-registered, with the reason.
260-
// This Event is the immediate, human-facing signal.
261-
func (r *TapMaterializerReconciler) warnNotAutoRegistered(repo *sourcev1.OCIRepository, ps *cozyv1alpha1.PackageSource, reason string) {
262-
if r.Recorder != nil {
263-
r.Recorder.Event(repo, corev1.EventTypeWarning, "AppsNotAutoRegistered",
264-
fmt.Sprintf("PackageSource %s: %s", ps.GetName(), reason))
265-
}
266-
}
267-
268-
// hasDefaultVariant reports whether the PackageSource declares a variant named
269-
// "default".
270-
func hasDefaultVariant(ps *cozyv1alpha1.PackageSource) bool {
271-
for i := range ps.Spec.Variants {
272-
if ps.Spec.Variants[i].Name == "default" {
273-
return true
274-
}
275-
}
276-
return false
277-
}
278-
279225
// deleteMaterialized removes every PackageSource materialized from the given tap
280226
// source, and each one's tap-managed registration Package. A user's own Package
281227
// (no tap label) is left in place.
@@ -445,29 +391,3 @@ func (r *TapMaterializerReconciler) SetupWithManager(mgr ctrl.Manager) error {
445391
For(&sourcev1.OCIRepository{}, builder.WithPredicates(isTap)).
446392
Complete(r)
447393
}
448-
449-
// ensureRegistration creates the tap-managed registration Package for a
450-
// materialized PackageSource so its apps appear in the catalog, unless the source
451-
// has no "default" variant (nothing to register), in which case a Warning Event
452-
// says why. Privileged components are not gated here: the Package reconciler
453-
// refuses them for an unconfirmed tap Package at the install site. The Package is
454-
// created only when absent; an existing registration, or a user's own or pinned
455-
// Package, is left as is.
456-
func (r *TapMaterializerReconciler) ensureRegistration(ctx context.Context, ps *cozyv1alpha1.PackageSource, repo *sourcev1.OCIRepository) error {
457-
if skip := autoRegisterSkipReason(ps); skip != "" {
458-
r.warnNotAutoRegistered(repo, ps, "not auto-registered because "+skip)
459-
return nil
460-
}
461-
pkg := &cozyv1alpha1.Package{
462-
ObjectMeta: metav1.ObjectMeta{
463-
Name: ps.GetName(),
464-
Labels: map[string]string{tapconst.Label: "true"},
465-
Annotations: map[string]string{tapconst.SourceAnnotation: repo.Name},
466-
OwnerReferences: []metav1.OwnerReference{registrationOwnerRef(ps)},
467-
},
468-
}
469-
if err := r.Create(ctx, pkg); err != nil && !apierrors.IsAlreadyExists(err) {
470-
return err
471-
}
472-
return nil
473-
}

‎internal/operator/tapmaterializer_reconciler_test.go‎

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,6 @@ func TestDeleteRegistrationPackageLosesRaceToHandover(t *testing.T) {
299299
}
300300
}
301301

302-
func TestHasDefaultVariant(t *testing.T) {
303-
with := &cozyv1alpha1.PackageSource{Spec: cozyv1alpha1.PackageSourceSpec{
304-
Variants: []cozyv1alpha1.Variant{{Name: "big"}, {Name: "default"}},
305-
}}
306-
if !hasDefaultVariant(with) {
307-
t.Error("expected a default variant to be detected")
308-
}
309-
without := &cozyv1alpha1.PackageSource{Spec: cozyv1alpha1.PackageSourceSpec{
310-
Variants: []cozyv1alpha1.Variant{{Name: "only"}, {Name: "second"}},
311-
}}
312-
if hasDefaultVariant(without) {
313-
t.Error("expected no default variant")
314-
}
315-
if hasDefaultVariant(&cozyv1alpha1.PackageSource{}) {
316-
t.Error("expected no default variant for an empty source")
317-
}
318-
}
319-
320302
func TestPrivilegedInstallComponents(t *testing.T) {
321303
ps := &cozyv1alpha1.PackageSource{Spec: cozyv1alpha1.PackageSourceSpec{
322304
Variants: []cozyv1alpha1.Variant{{
@@ -337,18 +319,20 @@ func TestPrivilegedInstallComponents(t *testing.T) {
337319
}
338320
}
339321

340-
// TestReconcileRegistersApps asserts the happy path: a materialized tap creates
341-
// a tap-managed registration Package so the repository's apps register in the
342-
// catalog on connect, without a manual `cozypkg add`.
343-
func TestReconcileRegistersApps(t *testing.T) {
322+
// TestReconcileCreatesNoPackageOnConnect asserts that connecting a repository
323+
// materializes its PackageSource and nothing else: no Package (and so no
324+
// HelmRelease or ApplicationDefinition) is created on connect. Auto-registering
325+
// on connect ran a publisher's install chart under Flux's cluster-admin account
326+
// with no operator step (issue #4978); installing is now explicit via
327+
// `cozypkg add`.
328+
func TestReconcileCreatesNoPackageOnConnect(t *testing.T) {
344329
scheme := tapScheme(t)
345330
data, digest := tarGz(t, map[string]string{
346331
// samplePS declares PackageSource "example.hello" with a default variant.
347332
"packages/core/platform/sources/hello.yaml": samplePS,
348333
})
349-
repo := tapRepo(true)
350334
cl := fake.NewClientBuilder().WithScheme(scheme).
351-
WithObjects(repo).
335+
WithObjects(tapRepo(true)).
352336
WithStatusSubresource(&sourcev1.OCIRepository{}).
353337
Build()
354338

@@ -374,25 +358,57 @@ func TestReconcileRegistersApps(t *testing.T) {
374358
if err := cl.Get(context.Background(), client.ObjectKey{Name: "example.hello"}, &cozyv1alpha1.PackageSource{}); err != nil {
375359
t.Fatalf("PackageSource not materialized: %v", err)
376360
}
377-
// A tap-managed registration Package was created for it.
361+
// No Package is created: connect installs nothing.
378362
var pkg cozyv1alpha1.Package
379-
if err := cl.Get(context.Background(), client.ObjectKey{Name: "example.hello"}, &pkg); err != nil {
380-
t.Fatalf("registration Package not created: %v", err)
363+
if err := cl.Get(context.Background(), client.ObjectKey{Name: "example.hello"}, &pkg); !apierrors.IsNotFound(err) {
364+
t.Fatalf("connect must create no Package, got err=%v pkg=%+v", err, pkg)
381365
}
382-
if pkg.GetLabels()[tapconst.Label] != "true" {
383-
t.Errorf("registration Package must be tap-managed (labelled), got %v", pkg.GetLabels())
366+
}
367+
368+
// TestReconcileDoesNotRecreateDeletedPackage asserts that a Package the admin
369+
// removed (e.g. with `cozypkg del`) stays removed across a new source revision.
370+
// Auto-registration used to re-create it on every revision (the second effect in
371+
// issue #4978); with it gone the materializer never creates a Package at all.
372+
func TestReconcileDoesNotRecreateDeletedPackage(t *testing.T) {
373+
scheme := tapScheme(t)
374+
data, digest := tarGz(t, map[string]string{
375+
"packages/core/platform/sources/hello.yaml": samplePS,
376+
})
377+
cur := data
378+
cl := fake.NewClientBuilder().WithScheme(scheme).
379+
WithObjects(tapRepo(true)).
380+
WithStatusSubresource(&sourcev1.OCIRepository{}).
381+
Build()
382+
r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &cur)
383+
384+
// Connect (rev1): materializes the PackageSource, creates no Package.
385+
setArtifact(t, cl, digest, "rev1")
386+
if _, err := r.Reconcile(context.Background(), req()); err != nil {
387+
t.Fatalf("reconcile rev1: %v", err)
384388
}
385-
if pkg.GetAnnotations()[tapconst.SourceAnnotation] != repo.Name {
386-
t.Errorf("registration Package must record its source, got %v", pkg.GetAnnotations())
389+
390+
// Admin installs then removes the app: a plain user Package appears and is
391+
// then deleted with `cozypkg del`.
392+
userPkg := &cozyv1alpha1.Package{ObjectMeta: metav1.ObjectMeta{Name: "example.hello"}}
393+
if err := cl.Create(context.Background(), userPkg); err != nil {
394+
t.Fatalf("create user Package: %v", err)
387395
}
388-
// Variant is left empty so the Package reconciler resolves it to "default"
389-
// deterministically, rather than pinning whichever variant was listed first.
390-
if pkg.Spec.Variant != "" {
391-
t.Errorf("registration Package must leave Variant empty, got %q", pkg.Spec.Variant)
396+
if err := cl.Delete(context.Background(), userPkg); err != nil {
397+
t.Fatalf("delete user Package: %v", err)
392398
}
393-
// Owned by its PackageSource so a direct delete of the PackageSource GCs it.
394-
if len(pkg.OwnerReferences) != 1 || pkg.OwnerReferences[0].Kind != "PackageSource" || pkg.OwnerReferences[0].Name != "example.hello" {
395-
t.Errorf("registration Package must own-ref its PackageSource, got %+v", pkg.OwnerReferences)
399+
400+
// A new revision must not resurrect the deleted Package.
401+
data2, digest2 := tarGz(t, map[string]string{
402+
"packages/core/platform/sources/hello.yaml": samplePS,
403+
})
404+
cur = data2
405+
setArtifact(t, cl, digest2, "rev2")
406+
if _, err := r.Reconcile(context.Background(), req()); err != nil {
407+
t.Fatalf("reconcile rev2: %v", err)
408+
}
409+
var pkg cozyv1alpha1.Package
410+
if err := cl.Get(context.Background(), client.ObjectKey{Name: "example.hello"}, &pkg); !apierrors.IsNotFound(err) {
411+
t.Fatalf("a removed Package must stay removed across revisions, got err=%v pkg=%+v", err, pkg)
396412
}
397413
}
398414

@@ -422,10 +438,10 @@ func setArtifact(t *testing.T, cl client.Client, digest, rev string) {
422438
}
423439

424440
// TestReconcileLeavesUnmarkedPackage: an unmarked Package of the same name (a
425-
// user's `cozypkg add`, or one from before this version) means registration is
426-
// already satisfied. The reconcile must NOT error (which would hot-loop the whole
427-
// tap and re-pull the artifact every backoff), NOT adopt it, and NOT delete it;
428-
// the revision is stamped so the loop does not recur.
441+
// user's `cozypkg add`, or one from before this version) must be left untouched
442+
// by materialization. The reconcile must NOT error (which would hot-loop the
443+
// whole tap and re-pull the artifact every backoff), NOT adopt it, and NOT delete
444+
// it; the revision is stamped so the loop does not recur.
429445
func TestReconcileLeavesUnmarkedPackage(t *testing.T) {
430446
scheme := tapScheme(t)
431447
data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS})

0 commit comments

Comments
 (0)