Skip to content

Commit a5c3a84

Browse files
patrykw-splunkPatryk Wasielewski
andauthored
Fix kustomization templates after removing kube-rbac-proxy (#1570)
* fix kustomization templates --------- Co-authored-by: Patryk Wasielewski <pwasiele@splunk.com>
1 parent 0c2e582 commit a5c3a84

8 files changed

Lines changed: 81 additions & 44 deletions

cmd/main.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"crypto/tls"
2021
"flag"
2122
"fmt"
2223
"os"
@@ -65,6 +66,7 @@ func init() {
6566

6667
func main() {
6768
var metricsAddr string
69+
var secureMetrics bool
6870
var enableLeaderElection bool
6971
var probeAddr string
7072
var pprofActive bool
@@ -76,8 +78,9 @@ func main() {
7678
var leaseDurationSecond int
7779
var renewDeadlineSecond int
7880

81+
var tlsOpts []func(*tls.Config)
82+
7983
flag.StringVar(&logEncoder, "log-encoder", "json", "log encoding ('json' or 'console')")
80-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
8184
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
8285
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
8386
"Enable leader election for controller manager. "+
@@ -86,6 +89,36 @@ func main() {
8689
flag.IntVar(&logLevel, "log-level", int(zapcore.InfoLevel), "set log level")
8790
flag.IntVar(&leaseDurationSecond, "lease-duration", int(leaseDurationSecond), "manager lease duration in seconds")
8891
flag.IntVar(&renewDeadlineSecond, "renew-duration", int(renewDeadlineSecond), "manager renew duration in seconds")
92+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metrics endpoint binds to. "+
93+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
94+
flag.BoolVar(&secureMetrics, "metrics-secure", false,
95+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
96+
97+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
98+
// More info:
99+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/server
100+
// - https://book.kubebuilder.io/reference/metrics.html
101+
metricsServerOptions := metricsserver.Options{
102+
BindAddress: metricsAddr,
103+
SecureServing: secureMetrics,
104+
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
105+
// not provided, self-signed certificates will be generated by default. This option is not recommended for
106+
// production environments as self-signed certificates do not offer the same level of trust and security
107+
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
108+
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
109+
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
110+
TLSOpts: tlsOpts,
111+
FilterProvider: filters.WithAuthenticationAndAuthorization,
112+
}
113+
114+
// TODO: enable https for /metrics endpoint by default
115+
// if secureMetrics {
116+
// // FilterProvider is used to protect the metrics endpoint with authn/authz.
117+
// // These configurations ensure that only authorized users and service accounts
118+
// // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
119+
// // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization
120+
// metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
121+
// }
89122

90123
// see https://github.com/operator-framework/operator-sdk/issues/1813
91124
if leaseDurationSecond < 30 {
@@ -111,10 +144,7 @@ func main() {
111144
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
112145

113146
baseOptions := ctrl.Options{
114-
Metrics: metricsserver.Options{
115-
BindAddress: metricsAddr,
116-
FilterProvider: filters.WithAuthenticationAndAuthorization,
117-
},
147+
Metrics: metricsServerOptions,
118148
Scheme: scheme,
119149
HealthProbeBindAddress: probeAddr,
120150
LeaderElection: enableLeaderElection,

config/debug/kustomization-cluster.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ bases:
2727
#- ../prometheus
2828

2929
patchesStrategicMerge:
30-
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your controller-manager to expose the /metrics
32-
# endpoint w/o any authn/z, please comment the following line.
33-
- manager_auth_proxy_patch.yaml
34-
3530
# Mount the controller config file for loading manager configurations
3631
# through a ComponentConfig type
3732
#- manager_config_patch.yaml
@@ -132,4 +127,9 @@ patches:
132127
- name: POD_NAME
133128
valueFrom:
134129
fieldRef:
135-
fieldPath: metadata.name
130+
fieldPath: metadata.name
131+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
132+
# More info: https://book.kubebuilder.io/reference/metrics
133+
- path: manager_metrics_patch.yaml
134+
target:
135+
kind: Deployment

config/debug/kustomization-namespace.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ bases:
2727
#- ../prometheus
2828

2929
patchesStrategicMerge:
30-
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your controller-manager to expose the /metrics
32-
# endpoint w/o any authn/z, please comment the following line.
33-
- manager_auth_proxy_patch.yaml
34-
3530
# Mount the controller config file for loading manager configurations
3631
# through a ComponentConfig type
3732
#- manager_config_patch.yaml
@@ -134,4 +129,9 @@ patches:
134129
- name: POD_NAME
135130
valueFrom:
136131
fieldRef:
137-
fieldPath: metadata.name
132+
fieldPath: metadata.name
133+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
134+
# More info: https://book.kubebuilder.io/reference/metrics
135+
- path: manager_metrics_patch.yaml
136+
target:
137+
kind: Deployment

config/debug/kustomization.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ bases:
3131
patchesStrategicMerge:
3232
- debug-sidecar-patch.yaml
3333

34-
3534
# Mount the controller config file for loading manager configurations
3635
# through a ComponentConfig type
3736
#- manager_config_patch.yaml

config/default/kustomization-cluster.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ bases:
2525
#- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
#- ../prometheus
28+
# [METRICS] Expose the controller manager metrics service.
29+
- metrics_service.yaml
2830

2931
patchesStrategicMerge:
30-
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your controller-manager to expose the /metrics
32-
# endpoint w/o any authn/z, please comment the following line.
33-
- manager_auth_proxy_patch.yaml
34-
3532
# Mount the controller config file for loading manager configurations
3633
# through a ComponentConfig type
3734
#- manager_config_patch.yaml
@@ -132,4 +129,9 @@ patches:
132129
- name: POD_NAME
133130
valueFrom:
134131
fieldRef:
135-
fieldPath: metadata.name
132+
fieldPath: metadata.name
133+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
134+
# More info: https://book.kubebuilder.io/reference/metrics
135+
- path: manager_metrics_patch.yaml
136+
target:
137+
kind: Deployment

config/default/kustomization-namespace.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ bases:
2525
#- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
#- ../prometheus
28+
# [METRICS] Expose the controller manager metrics service.
29+
- metrics_service.yaml
2830

2931
patchesStrategicMerge:
30-
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your controller-manager to expose the /metrics
32-
# endpoint w/o any authn/z, please comment the following line.
33-
- manager_auth_proxy_patch.yaml
34-
3532
# Mount the controller config file for loading manager configurations
3633
# through a ComponentConfig type
3734
#- manager_config_patch.yaml
@@ -134,4 +131,9 @@ patches:
134131
- name: POD_NAME
135132
valueFrom:
136133
fieldRef:
137-
fieldPath: metadata.name
134+
fieldPath: metadata.name
135+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
136+
# More info: https://book.kubebuilder.io/reference/metrics
137+
- path: manager_metrics_patch.yaml
138+
target:
139+
kind: Deployment

config/rbac/kustomization-cluster.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ resources:
99
- role_binding.yaml
1010
- leader_election_role.yaml
1111
- leader_election_role_binding.yaml
12-
# Comment the following 4 lines if you want to disable
13-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
14-
# which protects your /metrics endpoint.
15-
- auth_proxy_service.yaml
16-
- auth_proxy_role.yaml
17-
- auth_proxy_role_binding.yaml
18-
- auth_proxy_client_clusterrole.yaml
12+
# The following RBAC configurations are used to protect
13+
# the metrics endpoint with authn/authz. These configurations
14+
# ensure that only authorized users and service accounts
15+
# can access the metrics endpoint. Comment the following
16+
# permissions if you want to disable this protection.
17+
# More info: https://book.kubebuilder.io/reference/metrics.html
18+
- metrics_auth_role.yaml
19+
- metrics_auth_role_binding.yaml
20+
- metrics_reader_role.yaml

config/rbac/kustomization-namespace.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ resources:
99
- role_binding.yaml
1010
- leader_election_role.yaml
1111
- leader_election_role_binding.yaml
12-
# Comment the following 4 lines if you want to disable
13-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
14-
# which protects your /metrics endpoint.
15-
#- auth_proxy_service.yaml
16-
#- auth_proxy_role.yaml
17-
#- auth_proxy_role_binding.yaml
18-
#- auth_proxy_client_clusterrole.yaml
12+
# The following RBAC configurations are used to protect
13+
# the metrics endpoint with authn/authz. These configurations
14+
# ensure that only authorized users and service accounts
15+
# can access the metrics endpoint. Comment the following
16+
# permissions if you want to disable this protection.
17+
# More info: https://book.kubebuilder.io/reference/metrics.html
18+
# - metrics_auth_role.yaml
19+
# - metrics_auth_role_binding.yaml
20+
# - metrics_reader_role.yaml

0 commit comments

Comments
 (0)