Skip to content

Commit 22a9531

Browse files
authored
Restrict WVA controller ClusterRole to only WVA ConfigMaps (#985)
* Add shuynh2017 to the OWNERS list * restrict configmaps for cluster-scoped role * revert changes in roles * update resourcesNames * address comments
1 parent e155360 commit 22a9531

7 files changed

Lines changed: 48 additions & 27 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ vet: ## Run go vet against code.
9393

9494
.PHONY: test
9595
test: manifests generate fmt vet setup-envtest helm ## Run tests.
96-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" PATH=$(LOCALBIN):$(PATH) go test $$(go list ./... | grep -v /e2e | grep -v /benchmark) -coverprofile cover.out
96+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" PATH="$(LOCALBIN):$(PATH)" go test $$(go list ./... | grep -v /e2e | grep -v /benchmark) -coverprofile cover.out
9797

9898
# Creates a multi-node Kind cluster
9999
# Adds emulated GPU labels and capacities per node

charts/workload-variant-autoscaler/templates/rbac/leader_election_role.yaml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{- if .Values.controller.enabled }}
22
# permissions to do leader election.
3+
# Leader election uses coordination.k8s.io/v1.Lease (default in controller-runtime v0.12+)
4+
# ConfigMap-based leader election permissions have been removed as they are no longer used.
35
apiVersion: rbac.authorization.k8s.io/v1
46
kind: Role
57
metadata:
@@ -8,18 +10,6 @@ metadata:
810
labels:
911
{{- include "workload-variant-autoscaler.labels" . | nindent 4 }}
1012
rules:
11-
- apiGroups:
12-
- ""
13-
resources:
14-
- configmaps
15-
verbs:
16-
- get
17-
- list
18-
- watch
19-
- create
20-
- update
21-
- patch
22-
- delete
2313
- apiGroups:
2414
- coordination.k8s.io
2515
resources:

charts/workload-variant-autoscaler/templates/rbac/role.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ rules:
1212
resources:
1313
- configmaps
1414
verbs:
15-
- get
1615
- list
17-
- update
1816
- watch
1917
# Note: This broad permission is required for namespace-local ConfigMap overrides.
2018
# The controller filters by well-known names (wva-saturation-scaling-config, wva-model-scale-to-zero-config)
2119
# in its predicate logic, providing effective access control.
20+
- apiGroups:
21+
- ""
22+
resources:
23+
- configmaps
24+
{{- if not .Values.wva.namespaceScoped }}
25+
resourceNames:
26+
- {{ include "workload-variant-autoscaler.fullname" . }}-variantautoscaling-config
27+
- {{ include "workload-variant-autoscaler.fullname" . }}-wva-saturation-scaling-config
28+
- wva-model-scale-to-zero-config
29+
- wva-queueing-model-config
30+
{{- end }}
31+
verbs:
32+
- get
33+
- update
2234
- apiGroups:
2335
- ""
2436
resources:

cmd/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/go-logr/logr"
3535
flag "github.com/spf13/pflag"
36+
"k8s.io/apimachinery/pkg/labels"
3637
"k8s.io/apimachinery/pkg/runtime"
3738
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3839
"k8s.io/client-go/discovery"
@@ -41,6 +42,7 @@ import (
4142
ctrl "sigs.k8s.io/controller-runtime"
4243
"sigs.k8s.io/controller-runtime/pkg/cache"
4344
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
45+
"sigs.k8s.io/controller-runtime/pkg/client"
4446
"sigs.k8s.io/controller-runtime/pkg/healthz"
4547
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
4648
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -65,6 +67,7 @@ import (
6567
promoperator "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
6668
"github.com/prometheus/client_golang/api"
6769
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
70+
corev1 "k8s.io/api/core/v1"
6871
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
6972
inferencePoolV1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
7073
inferencePoolV1alpha2 "sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
@@ -348,6 +351,27 @@ func main() {
348351
watchNS: {},
349352
},
350353
}
354+
} else {
355+
// Multi-namespace mode: Use label selector to filter ConfigMaps in the cache
356+
// This significantly reduces memory usage by only caching WVA-related configmaps
357+
wvaConfigSelector := labels.SelectorFromSet(labels.Set{
358+
"app.kubernetes.io/name": "workload-variant-autoscaler",
359+
})
360+
361+
setupLog.Info("Configuring cache with label selector for ConfigMaps",
362+
"labelSelector", wvaConfigSelector.String())
363+
364+
// Configure cache to only watch configmaps with the WVA labels
365+
// Other resource types are cached normally without filtering
366+
mgrOptions.Cache = cache.Options{
367+
ByObject: map[client.Object]cache.ByObject{
368+
&corev1.ConfigMap{}: {
369+
// Empty map means cache all namespaces, but filter by label
370+
Namespaces: map[string]cache.Config{},
371+
Label: wvaConfigSelector,
372+
},
373+
},
374+
}
351375
}
352376

353377
mgr, err := ctrl.NewManager(restConfig, mgrOptions)

config/rbac/leader_election_role.yaml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# permissions to do leader election.
2+
# Leader election uses coordination.k8s.io/v1.Lease (default in controller-runtime v0.12+)
3+
# ConfigMap-based leader election permissions have been removed as they are no longer used.
24
apiVersion: rbac.authorization.k8s.io/v1
35
kind: Role
46
metadata:
@@ -7,18 +9,6 @@ metadata:
79
app.kubernetes.io/managed-by: kustomize
810
name: leader-election-role
911
rules:
10-
- apiGroups:
11-
- ""
12-
resources:
13-
- configmaps
14-
verbs:
15-
- get
16-
- list
17-
- watch
18-
- create
19-
- update
20-
- patch
21-
- delete
2212
- apiGroups:
2313
- coordination.k8s.io
2414
resources:

config/samples/model-scale-to-zero-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ kind: ConfigMap
2222
metadata:
2323
name: wva-model-scale-to-zero-config
2424
namespace: workload-variant-autoscaler-system
25+
labels:
26+
app.kubernetes.io/name: workload-variant-autoscaler
2527
data:
2628
# Global defaults applied to all models unless overridden
2729
default: |

deploy/configmap-saturation-scaling.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ kind: ConfigMap
1717
metadata:
1818
name: wva-saturation-scaling-config
1919
namespace: workload-variant-autoscaler-system
20+
labels:
21+
app.kubernetes.io/name: workload-variant-autoscaler
22+
app.kubernetes.io/managed-by: kustomize
2023
data:
2124
# Global defaults applied to all variants unless overridden
2225
default: |

0 commit comments

Comments
 (0)