From f82cd68dca4302ab54c766f0d7c6320fff337ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gomes?= Date: Thu, 12 Nov 2026 13:55:21 +0000 Subject: [PATCH] fix: Scope the host cache to mapped namespaces with mappingsOnly With namespace sync on, the host cache watched every namespace, so the syncer needed cluster-wide RBAC for secrets, pods, exec and workloads. When mappingsOnly is set and every mapping is exact, the syncer only ever works in the mapped host namespaces plus its own, so the cache is limited to those and the RBAC can be namespaced Roles. This needs no more permissions than before, so it runs with either RBAC setup. Host Gateway import keeps its own per-object namespaces as without namespace sync. Additional kubeconfig secret namespaces are added to the scoped set; without it they no longer narrow the cluster-wide cache. Co-Authored-By: Claude Opus 5.5 --- pkg/setup/controller_context.go | 32 +++++ .../controller_context_namespaces_test.go | 127 ++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 pkg/setup/controller_context_namespaces_test.go diff --git a/pkg/setup/controller_context.go b/pkg/setup/controller_context.go index 37a16c40d3..1874886b00 100644 --- a/pkg/setup/controller_context.go +++ b/pkg/setup/controller_context.go @@ -3,7 +3,9 @@ package setup import ( "context" "fmt" + "maps" "os" + "slices" "time" vclusterconfig "github.com/loft-sh/vcluster/config" @@ -19,6 +21,7 @@ import ( "github.com/loft-sh/vcluster/pkg/syncer/synccontext" "github.com/loft-sh/vcluster/pkg/telemetry" "github.com/loft-sh/vcluster/pkg/util/blockingcacheclient" + "github.com/loft-sh/vcluster/pkg/util/namespaces" "github.com/loft-sh/vcluster/pkg/util/pluginhookclient" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -119,6 +122,15 @@ func getLocalCacheOptions(options *config.VirtualClusterConfig) cache.Options { if !options.Sync.ToHost.Namespaces.Enabled { defaultNamespaces[options.HostNamespace] = cache.Config{} gatewayNamespaces = gatewaySourceNamespaces(options) + } else if mapped := mappedHostNamespaces(options); mapped != nil { + // only exact namespace mappings are synced, so the syncer never needs + // objects outside of these namespaces and RBAC can stay namespaced + defaultNamespaces = mapped + gatewayNamespaces = gatewaySourceNamespaces(options) + klog.Infof("Restricting host cache to namespaces %v", slices.Sorted(maps.Keys(mapped))) + } else { + // synced namespaces are not known up front + return cache.Options{DefaultNamespaces: nil} } // do we need access to another namespace to export the kubeconfig ? // we will need access to all the objects that the vcluster usually has access to @@ -146,6 +158,26 @@ func getLocalCacheOptions(options *config.VirtualClusterConfig) cache.Options { return cacheOptions } +// mappedHostNamespaces returns the host namespaces the syncer works in when +// namespace sync only allows exact mappings, or nil if they are not known up +// front. +func mappedHostNamespaces(options *config.VirtualClusterConfig) map[string]cache.Config { + namespaceSync := options.Sync.ToHost.Namespaces + if !namespaceSync.Enabled || !namespaceSync.MappingsOnly || len(namespaceSync.Mappings.ByName) == 0 { + return nil + } + + hostNamespaces := map[string]cache.Config{options.HostNamespace: {}} + for virtualNamespace, hostNamespace := range namespaceSync.Mappings.ByName { + if namespaces.IsPattern(virtualNamespace) || namespaces.IsPattern(hostNamespace) { + return nil + } + hostNamespaces[namespaces.ProcessNamespaceName(hostNamespace, options.Name)] = cache.Config{} + } + + return hostNamespaces +} + func startPlugins(ctx context.Context, virtualConfig *rest.Config, virtualRawConfig *clientcmdapi.Config, options *config.VirtualClusterConfig) error { klog.Infof("Start Plugins Manager...") createKubeConfigOptions := CreateKubeConfigOptions{ diff --git a/pkg/setup/controller_context_namespaces_test.go b/pkg/setup/controller_context_namespaces_test.go new file mode 100644 index 0000000000..868fc45c6c --- /dev/null +++ b/pkg/setup/controller_context_namespaces_test.go @@ -0,0 +1,127 @@ +package setup + +import ( + "maps" + "slices" + "testing" + + rootconfig "github.com/loft-sh/vcluster/config" + pkgconfig "github.com/loft-sh/vcluster/pkg/config" + "sigs.k8s.io/controller-runtime/pkg/cache" +) + +func namespaceSyncOptions() *pkgconfig.VirtualClusterConfig { + options := &pkgconfig.VirtualClusterConfig{} + options.Name = "tenant" + options.HostNamespace = "tenant-control-plane" + options.Sync.ToHost.Namespaces.Enabled = true + options.Sync.ToHost.Namespaces.MappingsOnly = true + options.Sync.ToHost.Namespaces.Mappings.ByName = map[string]string{ + "default": "${name}-ns-default", + "kube-system": "${name}-ns-kube-system", + } + return options +} + +func cacheNamespaces(cacheOptions cache.Options) []string { + return slices.Sorted(maps.Keys(cacheOptions.DefaultNamespaces)) +} + +func TestLocalCacheWatchesHostNamespaceWithoutNamespaceSync(t *testing.T) { + options := &pkgconfig.VirtualClusterConfig{} + options.HostNamespace = "tenant-control-plane" + + got := cacheNamespaces(getLocalCacheOptions(options)) + if !slices.Equal(got, []string{"tenant-control-plane"}) { + t.Fatalf("expected host namespace only, got %v", got) + } +} + +func TestLocalCacheWatchesMappedNamespacesWithMappingsOnly(t *testing.T) { + got := cacheNamespaces(getLocalCacheOptions(namespaceSyncOptions())) + want := []string{"tenant-control-plane", "tenant-ns-default", "tenant-ns-kube-system"} + if !slices.Equal(got, want) { + t.Fatalf("expected %v, got %v", want, got) + } +} + +func TestLocalCacheMergesAdditionalSecretNamespacesWithMappedNamespaces(t *testing.T) { + options := namespaceSyncOptions() + options.ExportKubeConfig.AdditionalSecrets = []rootconfig.ExportKubeConfigAdditionalSecretReference{ + {Namespace: "kubeconfigs", Name: "tenant-kubeconfig"}, + } + + got := cacheNamespaces(getLocalCacheOptions(options)) + want := []string{"kubeconfigs", "tenant-control-plane", "tenant-ns-default", "tenant-ns-kube-system"} + if !slices.Equal(got, want) { + t.Fatalf("expected %v, got %v", want, got) + } +} + +func TestLocalCacheWatchesAllNamespacesWhenNamespaceSyncIsNotRestricted(t *testing.T) { + tests := map[string]func(*pkgconfig.VirtualClusterConfig){ + "mappings only disabled": func(options *pkgconfig.VirtualClusterConfig) { + options.Sync.ToHost.Namespaces.MappingsOnly = false + }, + "no mappings": func(options *pkgconfig.VirtualClusterConfig) { + options.Sync.ToHost.Namespaces.Mappings.ByName = nil + }, + "pattern host mapping": func(options *pkgconfig.VirtualClusterConfig) { + options.Sync.ToHost.Namespaces.Mappings.ByName["team-*"] = "${name}-team-*" + }, + "additional secrets without restriction": func(options *pkgconfig.VirtualClusterConfig) { + options.Sync.ToHost.Namespaces.MappingsOnly = false + options.ExportKubeConfig.AdditionalSecrets = []rootconfig.ExportKubeConfigAdditionalSecretReference{ + {Namespace: "kubeconfigs", Name: "tenant-kubeconfig"}, + } + }, + } + + for name, modify := range tests { + t.Run(name, func(t *testing.T) { + options := namespaceSyncOptions() + modify(options) + + cacheOptions := getLocalCacheOptions(options) + if cacheOptions.DefaultNamespaces != nil { + t.Fatalf("expected cache for all namespaces, got %v", cacheNamespaces(cacheOptions)) + } + }) + } +} + +func TestLocalCacheStaysScopedWithFeaturesUsingOwnCaches(t *testing.T) { + // the virtual scheduler, hybrid scheduling and csi storage capacities + // watch the host through their own caches + options := namespaceSyncOptions() + options.ControlPlane.Advanced.VirtualScheduler.Enabled = true + options.Sync.ToHost.Pods.HybridScheduling.Enabled = true + options.Sync.FromHost.CSIStorageCapacities.Enabled = "true" + + got := cacheNamespaces(getLocalCacheOptions(options)) + want := []string{"tenant-control-plane", "tenant-ns-default", "tenant-ns-kube-system"} + if !slices.Equal(got, want) { + t.Fatalf("expected %v, got %v", want, got) + } +} + +func TestLocalCacheWatchesGatewaySourceNamespacesWithMappedNamespaces(t *testing.T) { + options := namespaceSyncOptions() + options.Sync.FromHost.Gateways.Enabled = true + options.Sync.FromHost.Gateways.Mappings.ByName = map[string]string{ + "platform-gateways/*": "shared-gateways/*", + } + + cacheOptions := getLocalCacheOptions(options) + got := cacheNamespaces(cacheOptions) + want := []string{"tenant-control-plane", "tenant-ns-default", "tenant-ns-kube-system"} + if !slices.Equal(got, want) { + t.Fatalf("expected default local cache %v, got %v", want, got) + } + + gatewayNamespaces := slices.Sorted(maps.Keys(gatewayCacheNamespaces(t, cacheOptions))) + want = []string{"platform-gateways", "tenant-control-plane", "tenant-ns-default", "tenant-ns-kube-system"} + if !slices.Equal(gatewayNamespaces, want) { + t.Fatalf("expected Gateway cache %v, got %v", want, gatewayNamespaces) + } +}