mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-20 13:06:51 +00:00
fix: allowing replication of cluster scoped objects via gtr (#2090)
Signed-off-by: Dario Tranchitella <dario@tranchitella.eu>
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user