Skip to content

Commit df3e813

Browse files
authored
Merge pull request #154 from leelavg/remove-proxy
remove kube-rbac-proxy container from deployment
2 parents 6e1c39c + 56bfee3 commit df3e813

File tree

1,056 files changed

+253518
-225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,056 files changed

+253518
-225
lines changed

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ WATCH_NAMESPACE ?= ""
1212

1313
IMG ?= $(IMAGE_REGISTRY)/$(REGISTRY_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG)
1414

15-
KUBE_RBAC_PROXY_IMG ?= gcr.io/kubebuilder/kube-rbac-proxy:v0.16.0
16-
1715
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
1816
ENVTEST_K8S_VERSION = 1.29.0
1917

@@ -45,12 +43,12 @@ namePrefix: $(NAME_PREFIX)
4543
patches:
4644
- patch: |-
4745
- op: add
48-
path: /spec/template/spec/containers/1/env/-
46+
path: /spec/template/spec/containers/0/env/-
4947
value:
5048
name: CSI_SERVICE_ACCOUNT_PREFIX
5149
value: $(NAME_PREFIX)
5250
- op: add
53-
path: /spec/template/spec/containers/1/env/-
51+
path: /spec/template/spec/containers/0/env/-
5452
value:
5553
name: WATCH_NAMESPACE
5654
value: $(WATCH_NAMESPACE)
@@ -60,8 +58,6 @@ patches:
6058
images:
6159
- name: controller
6260
newName: ${IMG}
63-
- name: kube-rbac-proxy
64-
newName: ${KUBE_RBAC_PROXY_IMG}
6561
endef
6662
export BUILD_INSTALLER_OVERLAY
6763

cmd/main.go

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"flag"
2222
"fmt"
2323
"os"
24+
"path/filepath"
2425
"strings"
2526

2627
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -32,10 +33,11 @@ import (
3233
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3334
ctrl "sigs.k8s.io/controller-runtime"
3435
"sigs.k8s.io/controller-runtime/pkg/cache"
36+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3537
"sigs.k8s.io/controller-runtime/pkg/healthz"
3638
"sigs.k8s.io/controller-runtime/pkg/log/zap"
39+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3740
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
38-
"sigs.k8s.io/controller-runtime/pkg/webhook"
3941

4042
csiv1alpha1 "github.com/ceph/ceph-csi-operator/api/v1alpha1"
4143
"github.com/ceph/ceph-csi-operator/internal/controller"
@@ -57,27 +59,34 @@ func init() {
5759

5860
func main() {
5961
var metricsAddr string
62+
var metricsCertPath, metricsCertName, metricsCertKey string
6063
var enableLeaderElection bool
64+
var enableHTTP2 bool
6165
var probeAddr string
6266
var secureMetrics bool
63-
var enableHTTP2 bool
64-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
67+
var tlsOpts []func(*tls.Config)
68+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
69+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
6570
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6671
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6772
"Enable leader election for controller manager. "+
6873
"Enabling this will ensure there is only one active controller manager.")
69-
flag.BoolVar(&secureMetrics, "metrics-secure", false,
70-
"If set the metrics endpoint is served securely")
74+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
75+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
76+
flag.StringVar(&metricsCertPath, "metrics-cert-path", "",
77+
"The directory that contains the metrics server certificate.")
78+
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
79+
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
7180
flag.BoolVar(&enableHTTP2, "enable-http2", false,
72-
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
81+
"If set, HTTP/2 will be enabled for the metrics")
82+
7383
opts := zap.Options{
7484
Development: true,
7585
}
7686
opts.BindFlags(flag.CommandLine)
7787
flag.Parse()
7888

7989
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
80-
8190
// if the enable-http2 flag is false (the default), http/2 should be disabled
8291
// due to its vulnerabilities. More specifically, disabling http/2 will
8392
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
@@ -89,14 +98,55 @@ func main() {
8998
c.NextProtos = []string{"http/1.1"}
9099
}
91100

92-
tlsOpts := []func(*tls.Config){}
93101
if !enableHTTP2 {
94102
tlsOpts = append(tlsOpts, disableHTTP2)
95103
}
104+
// Create watchers for metrics certificates
105+
var metricsCertWatcher *certwatcher.CertWatcher
106+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
107+
// More info:
108+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.0/pkg/metrics/server
109+
// - https://book.kubebuilder.io/reference/metrics.html
110+
metricsServerOptions := metricsserver.Options{
111+
BindAddress: metricsAddr,
112+
SecureServing: secureMetrics,
113+
TLSOpts: tlsOpts,
114+
}
96115

97-
webhookServer := webhook.NewServer(webhook.Options{
98-
TLSOpts: tlsOpts,
99-
})
116+
if secureMetrics {
117+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
118+
// These configurations ensure that only authorized users and service accounts
119+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
120+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
121+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
122+
}
123+
124+
// If the certificate is not specified, controller-runtime will automatically
125+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
126+
// this setup is not recommended for production.
127+
//
128+
// TODO(user): If you enable certManager, uncomment the following lines:
129+
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
130+
// managed by cert-manager for the metrics server.
131+
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
132+
if len(metricsCertPath) > 0 {
133+
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
134+
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
135+
136+
var err error
137+
metricsCertWatcher, err = certwatcher.New(
138+
filepath.Join(metricsCertPath, metricsCertName),
139+
filepath.Join(metricsCertPath, metricsCertKey),
140+
)
141+
if err != nil {
142+
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err)
143+
os.Exit(1)
144+
}
145+
146+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
147+
config.GetCertificate = metricsCertWatcher.GetCertificate
148+
})
149+
}
100150

101151
defaultNamespaces := map[string]cache.Config{}
102152
operatorNamespace, err := utils.GetOperatorNamespace()
@@ -116,13 +166,8 @@ func main() {
116166
}
117167
}
118168
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
119-
Scheme: scheme,
120-
Metrics: metricsserver.Options{
121-
BindAddress: metricsAddr,
122-
SecureServing: secureMetrics,
123-
TLSOpts: tlsOpts,
124-
},
125-
WebhookServer: webhookServer,
169+
Scheme: scheme,
170+
Metrics: metricsServerOptions,
126171
HealthProbeBindAddress: probeAddr,
127172
LeaderElection: enableLeaderElection,
128173
LeaderElectionID: "0a62cc8a.ceph.io",
@@ -167,6 +212,14 @@ func main() {
167212
}
168213
//+kubebuilder:scaffold:builder
169214

215+
if metricsCertWatcher != nil {
216+
setupLog.Info("Adding metrics certificate watcher to manager")
217+
if err := mgr.Add(metricsCertWatcher); err != nil {
218+
setupLog.Error(err, "unable to add metrics certificate watcher to manager")
219+
os.Exit(1)
220+
}
221+
}
222+
170223
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
171224
setupLog.Error(err, "unable to set up health check")
172225
os.Exit(1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This patch adds the args, volumes, and ports to allow the manager to use the metrics-server certs.
2+
3+
# Add the volumeMount for the metrics-server certs
4+
- op: add
5+
path: /spec/template/spec/containers/0/volumeMounts/-
6+
value:
7+
mountPath: /tmp/k8s-metrics-server/metrics-certs
8+
name: metrics-certs
9+
readOnly: true
10+
11+
# Add the --metrics-cert-path argument for the metrics server
12+
- op: add
13+
path: /spec/template/spec/containers/0/args/-
14+
value: --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs
15+
16+
# Add the metrics-server certs volume configuration
17+
- op: add
18+
path: /spec/template/spec/volumes/-
19+
value:
20+
name: metrics-certs
21+
secret:
22+
secretName: metrics-server-cert
23+
optional: false
24+
items:
25+
- key: ca.crt
26+
path: ca.crt
27+
- key: tls.crt
28+
path: tls.crt
29+
- key: tls.key
30+
path: tls.key

config/default/kustomization.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,29 @@ resources:
1616
#- ../certmanager
1717
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
1818
#- ../prometheus
19+
# [METRICS] Expose the controller manager metrics service.
20+
# - metrics_service.yaml
21+
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
22+
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
23+
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
24+
# be able to communicate with the Webhook Server.
25+
#- ../network-policy
1926

2027
#patches:
28+
# Uncomment the patches line if you enable Metrics
29+
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
30+
# More info: https://book.kubebuilder.io/reference/metrics
31+
# - path: manager_metrics_patch.yaml
32+
# target:
33+
# kind: Deployment
34+
35+
# Uncomment the patches line if you enable Metrics and CertManager
36+
# [METRICS-WITH-CERTS] To enable metrics protected with certManager, uncomment the following line.
37+
# This patch will protect the metrics with certManager self-signed certs.
38+
#- path: cert_metrics_manager_patch.yaml
39+
# target:
40+
# kind: Deployment
41+
2142
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2243
# crd/kustomization.yaml
2344
#- path: manager_webhook_patch.yaml
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
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ spec:
1212
- name: https
1313
port: 8443
1414
protocol: TCP
15-
targetPort: https
15+
targetPort: 8443
1616
selector:
1717
control-plane: controller-manager
18+
app.kubernetes.io/name: ceph-csi-operator

config/manager/kustomization.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
resources:
22
- manager.yaml
3-
apiVersion: kustomize.config.k8s.io/v1beta1
4-
kind: Kustomization
5-
patches:
6-
# Protect the /metrics endpoint by putting it behind auth.
7-
# If you want your controller-manager to expose the /metrics
8-
# endpoint w/o any authn/z, please comment the following line.
9-
- path: manager_auth_proxy_patch.yaml

config/manager/manager_auth_proxy_patch.yaml

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This NetworkPolicy allows ingress traffic
2+
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those
3+
# namespaces are able to gather data from the metrics endpoint.
4+
apiVersion: networking.k8s.io/v1
5+
kind: NetworkPolicy
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: kube
9+
app.kubernetes.io/managed-by: kustomize
10+
name: allow-metrics-traffic
11+
namespace: system
12+
spec:
13+
podSelector:
14+
matchLabels:
15+
control-plane: controller-manager
16+
app.kubernetes.io/name: kube
17+
policyTypes:
18+
- Ingress
19+
ingress:
20+
# This allows ingress traffic from any namespace with the label metrics: enabled
21+
- from:
22+
- namespaceSelector:
23+
matchLabels:
24+
metrics: enabled # Only from namespaces with this label
25+
ports:
26+
- port: 8443
27+
protocol: TCP
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resources:
2+
- allow-metrics-traffic.yaml

0 commit comments

Comments
 (0)