Skip to content

Commit 2581021

Browse files
Use built-in auth protection instead of kube-rbac-proxy
Signed-off-by: Varad Ahirwadkar <[email protected]>
1 parent 9cb6545 commit 2581021

18 files changed

+186
-100
lines changed

bundle/manifests/rsct-operator-controller-manager-metrics-service_v1_service.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ kind: Service
33
metadata:
44
creationTimestamp: null
55
labels:
6-
app.kubernetes.io/component: kube-rbac-proxy
76
app.kubernetes.io/created-by: rsct-operator
87
app.kubernetes.io/instance: controller-manager-metrics-service
98
app.kubernetes.io/managed-by: kustomize
@@ -16,7 +15,7 @@ spec:
1615
- name: https
1716
port: 8443
1817
protocol: TCP
19-
targetPort: https
18+
targetPort: 8443
2019
selector:
2120
control-plane: controller-manager
2221
status:

bundle/manifests/rsct-operator-manager-config_v1_configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ data:
66
health:
77
healthProbeBindAddress: :8081
88
metrics:
9-
bindAddress: 127.0.0.1:8080
9+
bindAddress: 127.0.0.1:8443
1010
webhook:
1111
port: 9443
1212
leaderElection:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
app.kubernetes.io/created-by: rsct-operator
7+
app.kubernetes.io/instance: metrics-auth-rolebinding
8+
app.kubernetes.io/managed-by: kustomize
9+
app.kubernetes.io/name: clusterrolebinding
10+
app.kubernetes.io/part-of: rsct-operator
11+
name: rsct-operator-metrics-auth-rolebinding
12+
roleRef:
13+
apiGroup: rbac.authorization.k8s.io
14+
kind: ClusterRole
15+
name: metrics-auth-role
16+
subjects:
17+
- kind: ServiceAccount
18+
name: rsct-operator-controller-manager
19+
namespace: rsct-operator-system

bundle/manifests/rsct-operator-metrics-reader_rbac.authorization.k8s.io_v1_clusterrole.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ kind: ClusterRole
33
metadata:
44
creationTimestamp: null
55
labels:
6-
app.kubernetes.io/component: kube-rbac-proxy
76
app.kubernetes.io/created-by: rsct-operator
87
app.kubernetes.io/instance: metrics-reader
98
app.kubernetes.io/managed-by: kustomize
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
app.kubernetes.io/created-by: rsct-operator
7+
app.kubernetes.io/instance: metrics-auth-role
8+
app.kubernetes.io/managed-by: kustomize
9+
app.kubernetes.io/name: clusterrole
10+
app.kubernetes.io/part-of: rsct-operator
11+
name: rsct-operator-metrics-auth-role
12+
rules:
13+
- apiGroups:
14+
- authentication.k8s.io
15+
resources:
16+
- tokenreviews
17+
verbs:
18+
- create
19+
- apiGroups:
20+
- authorization.k8s.io
21+
resources:
22+
- subjectaccessreviews
23+
verbs:
24+
- create

bundle/manifests/rsct-operator.clusterserviceversion.yaml

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ metadata:
2424
capabilities: Basic Install
2525
categories: Monitoring
2626
containerImage: ghcr.io/ocp-power-automation/rsct-operator:0.0.1-alpha2
27-
createdAt: "2024-12-10T05:23:35Z"
27+
createdAt: "2025-01-09T11:53:38Z"
2828
description: Deploys the RSCT DaemonSet on all ppc64le architecture nodes of Kubernetes
2929
and OpenShift clusters.
3030
operators.operatorframework.io/builder: operator-sdk-v1.34.1
@@ -172,31 +172,7 @@ spec:
172172
- linux
173173
containers:
174174
- args:
175-
- --secure-listen-address=0.0.0.0:8443
176-
- --upstream=http://127.0.0.1:8080/
177-
- --logtostderr=true
178-
- --v=0
179-
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.15.0
180-
name: kube-rbac-proxy
181-
ports:
182-
- containerPort: 8443
183-
name: https
184-
protocol: TCP
185-
resources:
186-
limits:
187-
cpu: 500m
188-
memory: 128Mi
189-
requests:
190-
cpu: 5m
191-
memory: 64Mi
192-
securityContext:
193-
allowPrivilegeEscalation: false
194-
capabilities:
195-
drop:
196-
- ALL
197-
- args:
198-
- --health-probe-bind-address=:8081
199-
- --metrics-bind-address=127.0.0.1:8080
175+
- --metrics-bind-address=:8443
200176
- --leader-elect
201177
command:
202178
- /manager

cmd/main.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3435
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3536
"sigs.k8s.io/controller-runtime/pkg/webhook"
3637

@@ -57,12 +58,12 @@ func main() {
5758
var probeAddr string
5859
var secureMetrics bool
5960
var enableHTTP2 bool
60-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8443", "The address the metric endpoint binds to.")
6162
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6263
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6364
"Enable leader election for controller manager. "+
6465
"Enabling this will ensure there is only one active controller manager.")
65-
flag.BoolVar(&secureMetrics, "metrics-secure", false,
66+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
6667
"If set the metrics endpoint is served securely")
6768
flag.BoolVar(&enableHTTP2, "enable-http2", false,
6869
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
@@ -94,13 +95,31 @@ func main() {
9495
TLSOpts: tlsOpts,
9596
})
9697

98+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
99+
// More info:
100+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server
101+
// - https://book.kubebuilder.io/reference/metrics.html
102+
metricsServerOptions := metricsserver.Options{
103+
BindAddress: metricsAddr,
104+
SecureServing: secureMetrics,
105+
TLSOpts: tlsOpts,
106+
}
107+
108+
if secureMetrics {
109+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
110+
// These configurations ensure that only authorized users and service accounts
111+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
112+
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
113+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
114+
115+
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
116+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
117+
// this setup is not recommended for production.
118+
}
119+
97120
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
98-
Scheme: scheme,
99-
Metrics: metricsserver.Options{
100-
BindAddress: metricsAddr,
101-
SecureServing: secureMetrics,
102-
TLSOpts: tlsOpts,
103-
},
121+
Scheme: scheme,
122+
Metrics: metricsServerOptions,
104123
WebhookServer: webhookServer,
105124
HealthProbeBindAddress: probeAddr,
106125
LeaderElection: enableLeaderElection,

config/default/kustomization.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ resources:
2727
#- ../certmanager
2828
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2929
#- ../prometheus
30+
# [METRICS] Expose the controller manager metrics service.
31+
- metrics_service.yaml
3032

3133
patches:
3234
# Protect the /metrics endpoint by putting it behind auth.
3335
# If you want your controller-manager to expose the /metrics
3436
# endpoint w/o any authn/z, please comment the following line.
35-
- path: manager_auth_proxy_patch.yaml
37+
- path: manager_metrics_patch.yaml
38+
target:
39+
kind: Deployment
3640

3741
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3842
# crd/kustomization.yaml

config/default/manager_auth_proxy_patch.yaml

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This patch adds the args to allow exposing the metrics endpoint using HTTPS
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8443

0 commit comments

Comments
 (0)