diff --git a/internal/controllers/resources/collect.go b/internal/controllers/resources/collect.go index 78abd709..66496d6c 100644 --- a/internal/controllers/resources/collect.go +++ b/internal/controllers/resources/collect.go @@ -217,7 +217,7 @@ func (co *Collector) AddToAccumulation( return err } - if err := co.validateClusterScopedObjectAllowed(opts, obj, ns); err != nil { + if err := co.validateClusterScopedObjectAllowed(opts, obj); err != nil { return err } @@ -302,7 +302,7 @@ func (co *Collector) CollectForNamespace( // Rejected upfront, before imposing the target Namespace on a copy which could never // be applied as such. - if err := co.validateClusterScopedObjectAllowed(opts, obj, target); err != nil { + if err := co.validateClusterScopedObjectAllowed(opts, obj); err != nil { return err } @@ -444,12 +444,8 @@ func GatherAdditionalMetadata( // // Cluster-scoped objects remain replicable through the None and Tenant scopes, which impose // no target Namespace at all. -func (co *Collector) validateClusterScopedObjectAllowed( - opts CollectorOptions, - obj *unstructured.Unstructured, - ns *corev1.Namespace, -) error { - if opts.AllowClusterScopedObjects && ns == nil { +func (co *Collector) validateClusterScopedObjectAllowed(opts CollectorOptions, obj *unstructured.Unstructured) error { + if opts.AllowClusterScopedObjects { return nil } @@ -458,18 +454,11 @@ func (co *Collector) validateClusterScopedObjectAllowed( return err } - if isNamespaced { - return nil - } - - if !opts.AllowClusterScopedObjects { + if !isNamespaced { return fmt.Errorf("cluster-scoped kind %s/%s is not allowed", obj.GetAPIVersion(), obj.GetKind()) } - return fmt.Errorf( - "cluster-scoped kind %s/%s cannot be replicated into the Namespace %s", - obj.GetAPIVersion(), obj.GetKind(), ns.GetName(), - ) + return nil } // Handles a single generator item. diff --git a/internal/controllers/resources/collect_test.go b/internal/controllers/resources/collect_test.go index 9c96563f..4fc020d4 100644 --- a/internal/controllers/resources/collect_test.go +++ b/internal/controllers/resources/collect_test.go @@ -81,33 +81,6 @@ func TestCollectorAddToAccumulationClusterScopedObjects(t *testing.T) { } }) - t.Run("rejects cluster scoped object targeting a namespace", func(t *testing.T) { - t.Parallel() - - acc := processor.Accumulator{} - obj := newUnstructured("v1", "Namespace", "", "example") - - opts := CollectorOptions{ - Accumulator: acc, - AllowClusterScopedObjects: true, - } - - target := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "tenant-a"}} - - err := collector.AddToAccumulation(nil, target, opts, capsuleResourceSpec(), obj, "test", true) - if err == nil { - t.Fatal("expected error, got nil") - } - - if !strings.Contains(err.Error(), "cannot be replicated into the Namespace tenant-a") { - t.Fatalf("expected a namespaced replication error, got %v", err) - } - - if len(acc) != 0 { - t.Fatalf("expected object not to be accumulated, got %d items", len(acc)) - } - }) - t.Run("keeps allowing namespaced object targeting a namespace", func(t *testing.T) { t.Parallel()