Skip to content

Commit 51bbe57

Browse files
committed
feat(core): make the controller metrics bind address configurable
The manager is constructed with metricsserver.Options{BindAddress: "0"}, so the controller serves no Prometheus metrics and no env or flag can change that. The Helm chart already renders METRICS_BIND_ADDRESS and METRICS_SECURE from controller.metrics, and kagent-dev#2401 added a metrics Service, a ServiceMonitor and the metrics-auth and metrics-reader ClusterRoles for that endpoint, but nothing reads the variables: the scrape target stays at up=0 because the port is never bound. Register both variables in the env package next to LEADER_ELECT, where they are documented and typed like every other controller variable, and read them when building the manager options. METRICS_BIND_ADDRESS defaults to "0", so an installation that sets nothing serves no metrics exactly as before, and controller.metrics.enabled=false renders neither variable. An empty value resolves to "0" as well: controller-runtime reads an empty BindAddress as unset and falls back to :8080, so METRICS_BIND_ADDRESS="" would otherwise open a metrics port nobody asked for. A secure endpoint also sets FilterProvider, so every scrape is authenticated with a TokenReview and authorized with a SubjectAccessReview on the /metrics nonResourceURL. Without it, METRICS_SECURE=true would encrypt the endpoint but serve any caller that reaches the port, and the chart's metrics-reader binding would grant nothing. The tokenreviews and subjectaccessreviews create rules the filter needs are not on the controller ClusterRole: the chart renders them as a separate <fullname>-metrics-auth-role ClusterRole and its ClusterRoleBinding, both gated on controller.metrics.enabled and controller.metrics.secureServing. The filters package adds two indirect requirements to go.mod, both new to the module graph and both reached only through it: pkg/metrics/filters pulls k8s.io/apiserver/pkg/authentication/ authenticatorfactory, which pulls k8s.io/component-base/metrics, which pulls github.com/blang/semver/v4. No package of the main module imports either one directly. Signed-off-by: QuentinBisson <[email protected]>
1 parent 7471879 commit 51bbe57

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎go/core/pkg/app/app.go‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060
"sigs.k8s.io/controller-runtime/pkg/cache"
6161
"sigs.k8s.io/controller-runtime/pkg/client"
6262
"sigs.k8s.io/controller-runtime/pkg/manager"
63+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
6364
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
6465
)
6566

@@ -216,11 +217,21 @@ func Run(ctx context.Context, opts Options) error {
216217
// Forbidden response without a failing Namespace informer blocking startup.
217218
managerClientOptions.Cache = &client.CacheOptions{DisableFor: []client.Object{&corev1.Namespace{}}}
218219
}
220+
metricsOptions := metricsserver.Options{
221+
BindAddress: metricsBindAddress(),
222+
SecureServing: kagentenv.MetricsSecure.Get(),
223+
}
224+
if metricsOptions.SecureServing {
225+
// SecureServing alone only encrypts. The filter authenticates the scraper
226+
// with a TokenReview and authorizes it with a SubjectAccessReview on the
227+
// /metrics nonResourceURL.
228+
metricsOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
229+
}
219230
manager, err := ctrl.NewManager(kubeConfig, ctrl.Options{
220231
Scheme: managerScheme,
221232
Cache: managerCacheOptions,
222233
Client: managerClientOptions,
223-
Metrics: metricsserver.Options{BindAddress: "0"},
234+
Metrics: metricsOptions,
224235
LeaderElection: kagentenv.LeaderElect.Get(),
225236
LeaderElectionID: "0e9f6799.kagent.dev",
226237
LeaderElectionNamespace: env("KAGENT_NAMESPACE", "kagent"),
@@ -369,6 +380,16 @@ func envBool(name string) bool {
369380
return value
370381
}
371382

383+
// metricsBindAddress resolves METRICS_BIND_ADDRESS. controller-runtime reads an
384+
// empty address as "unset" and falls back to :8080, so an empty value would
385+
// serve metrics on a port nobody asked for. "0" disables the metrics server.
386+
func metricsBindAddress() string {
387+
if address := kagentenv.MetricsBindAddress.Get(); address != "" {
388+
return address
389+
}
390+
return "0"
391+
}
392+
372393
func namespaces(value string) []string {
373394
var result []string
374395
for namespace := range strings.SplitSeq(value, ",") {

‎go/core/pkg/app/app_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,38 @@ func TestLeaderElectionDefaultsOnWithLocalOptOut(t *testing.T) {
9797
}
9898
}
9999

100+
func TestMetricsBindAddressNeverFallsBackToTheControllerRuntimeDefault(t *testing.T) {
101+
set := func(value string) *string { return &value }
102+
for name, testCase := range map[string]struct {
103+
value *string
104+
want string
105+
}{
106+
"unset": {value: nil, want: "0"},
107+
"empty": {value: set(""), want: "0"},
108+
"disabled": {value: set("0"), want: "0"},
109+
"port": {value: set(":8443"), want: ":8443"},
110+
} {
111+
t.Run(name, func(t *testing.T) {
112+
if testCase.value != nil {
113+
t.Setenv("METRICS_BIND_ADDRESS", *testCase.value)
114+
}
115+
if got := metricsBindAddress(); got != testCase.want {
116+
t.Fatalf("metricsBindAddress() = %q, want %q", got, testCase.want)
117+
}
118+
})
119+
}
120+
}
121+
122+
func TestMetricsSecureDefaultsOff(t *testing.T) {
123+
if kagentenv.MetricsSecure.Get() {
124+
t.Fatal("METRICS_SECURE must default to false")
125+
}
126+
t.Setenv("METRICS_SECURE", "true")
127+
if !kagentenv.MetricsSecure.Get() {
128+
t.Fatal("METRICS_SECURE=true must enable secure serving")
129+
}
130+
}
131+
100132
func TestNamespaces(t *testing.T) {
101133
want := []string{"one", "two"}
102134
if got := namespaces(" one, ,two,"); !reflect.DeepEqual(got, want) {

‎go/core/pkg/env/kagent.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ var (
99
ComponentController,
1010
)
1111

12+
MetricsBindAddress = RegisterStringVar(
13+
"METRICS_BIND_ADDRESS",
14+
"0",
15+
"Address the controller-runtime metrics server binds to, e.g. :8080. "+
16+
"\"0\" (the default) serves no metrics, so an installation that does not "+
17+
"set this is unchanged. The Helm chart renders this variable, and its "+
18+
"ServiceMonitor, from controller.metrics.",
19+
ComponentController,
20+
)
21+
22+
MetricsSecure = RegisterBoolVar(
23+
"METRICS_SECURE",
24+
false,
25+
"Serve the metrics endpoint over HTTPS with authentication and authorization. "+
26+
"A scraper then needs a token bound to the metrics-reader ClusterRole.",
27+
ComponentController,
28+
)
29+
1230
KagentNamespace = RegisterStringVar(
1331
"KAGENT_NAMESPACE",
1432
"kagent",

‎go/go.mod‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ require (
146146
github.com/bahlo/generic-list-go v0.2.0 // indirect
147147
github.com/beorn7/perks v1.0.1 // indirect
148148
github.com/bkielbasa/cyclop v1.2.3 // indirect
149+
github.com/blang/semver/v4 v4.0.0 // indirect
149150
github.com/blizzy78/varnamelen v0.8.0 // indirect
150151
github.com/bombsimon/wsl/v4 v4.7.0 // indirect
151152
github.com/bombsimon/wsl/v5 v5.8.0 // indirect
@@ -461,6 +462,7 @@ require (
461462
istio.io/api v1.31.0-alpha.1.0.20260819121012-5803fb6accf7 // indirect
462463
istio.io/client-go v1.31.0-alpha.0.0.20260807010324-676a810f2c1f // indirect
463464
k8s.io/apiserver v0.36.3 // indirect
465+
k8s.io/component-base v0.36.3 // indirect
464466
k8s.io/gengo/v2 v2.0.0-20260408192533-25e2208e0dc3 // indirect
465467
k8s.io/klog/v2 v2.140.0 // indirect
466468
k8s.io/kube-openapi v0.0.0-20260721132016-d427ff9ee9ad // indirect

0 commit comments

Comments
 (0)