Skip to content

Commit caa59bc

Browse files
committed
Implement secure diagnostics (metrics, pprof, log level changes)
Signed-off-by: Stefan Büringer [email protected]
1 parent 58557b7 commit caa59bc

File tree

27 files changed

+489
-200
lines changed

27 files changed

+489
-200
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ generate-manifests: $(addprefix generate-manifests-,$(ALL_GENERATE_MODULES)) ##
273273
generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.g. CRD, RBAC etc. for core
274274
$(MAKE) clean-generated-yaml SRC_DIRS="./config/crd/bases"
275275
$(CONTROLLER_GEN) \
276+
paths=./ \
276277
paths=./api/... \
277278
paths=./internal/controllers/... \
278279
paths=./internal/webhooks/... \
@@ -299,6 +300,7 @@ generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.
299300
generate-manifests-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for kubeadm bootstrap
300301
$(MAKE) clean-generated-yaml SRC_DIRS="./bootstrap/kubeadm/config/crd/bases"
301302
$(CONTROLLER_GEN) \
303+
paths=./bootstrap/kubeadm \
302304
paths=./bootstrap/kubeadm/api/... \
303305
paths=./bootstrap/kubeadm/internal/controllers/... \
304306
crd:crdVersions=v1 \
@@ -312,6 +314,7 @@ generate-manifests-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate manifests e.
312314
generate-manifests-kubeadm-control-plane: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for kubeadm control plane
313315
$(MAKE) clean-generated-yaml SRC_DIRS="./controlplane/kubeadm/config/crd/bases"
314316
$(CONTROLLER_GEN) \
317+
paths=./controlplane/kubeadm \
315318
paths=./controlplane/kubeadm/api/... \
316319
paths=./controlplane/kubeadm/internal/controllers/... \
317320
paths=./controlplane/kubeadm/internal/webhooks/... \
@@ -326,6 +329,7 @@ generate-manifests-kubeadm-control-plane: $(CONTROLLER_GEN) ## Generate manifest
326329
generate-manifests-docker-infrastructure: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for docker infrastructure provider
327330
$(MAKE) clean-generated-yaml SRC_DIRS="$(CAPD_DIR)/config/crd/bases"
328331
cd $(CAPD_DIR); $(CONTROLLER_GEN) \
332+
paths=./ \
329333
paths=./api/... \
330334
paths=./$(EXP_DIR)/api/... \
331335
paths=./$(EXP_DIR)/internal/controllers/... \
@@ -341,6 +345,7 @@ generate-manifests-docker-infrastructure: $(CONTROLLER_GEN) ## Generate manifest
341345
generate-manifests-in-memory-infrastructure: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for in-memory infrastructure provider
342346
$(MAKE) clean-generated-yaml SRC_DIRS="$(CAPIM_DIR)/config/crd/bases"
343347
cd $(CAPIM_DIR); $(CONTROLLER_GEN) \
348+
paths=./ \
344349
paths=./api/... \
345350
paths=./internal/controllers/... \
346351
crd:crdVersions=v1 \

bootstrap/kubeadm/config/manager/manager.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
- /manager
2121
args:
2222
- "--leader-elect"
23-
- "--metrics-bind-addr=localhost:8080"
23+
- "--diagnostics-address=:8443"
2424
- "--feature-gates=MachinePool=${EXP_MACHINE_POOL:=false},KubeadmBootstrapFormatIgnition=${EXP_KUBEADM_BOOTSTRAP_FORMAT_IGNITION:=false}"
2525
- "--bootstrap-token-ttl=${KUBEADM_BOOTSTRAP_TOKEN_TTL:=15m}"
2626
image: controller:latest
@@ -29,6 +29,9 @@ spec:
2929
- containerPort: 9440
3030
name: healthz
3131
protocol: TCP
32+
- containerPort: 8443
33+
name: metrics
34+
protocol: TCP
3235
readinessProbe:
3336
httpGet:
3437
path: /readyz

bootstrap/kubeadm/config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ rules:
1818
- patch
1919
- update
2020
- watch
21+
- apiGroups:
22+
- authentication.k8s.io
23+
resources:
24+
- tokenreviews
25+
verbs:
26+
- create
27+
- apiGroups:
28+
- authorization.k8s.io
29+
resources:
30+
- subjectaccessreviews
31+
verbs:
32+
- create
2133
- apiGroups:
2234
- bootstrap.cluster.x-k8s.io
2335
resources:

bootstrap/kubeadm/main.go

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141
"sigs.k8s.io/controller-runtime/pkg/cache"
4242
"sigs.k8s.io/controller-runtime/pkg/client"
4343
"sigs.k8s.io/controller-runtime/pkg/controller"
44-
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4544
"sigs.k8s.io/controller-runtime/pkg/webhook"
4645

4746
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -59,49 +58,44 @@ var (
5958
scheme = runtime.NewScheme()
6059
setupLog = ctrl.Log.WithName("setup")
6160
controllerName = "cluster-api-kubeadm-bootstrap-manager"
61+
62+
// flags.
63+
enableLeaderElection bool
64+
leaderElectionLeaseDuration time.Duration
65+
leaderElectionRenewDeadline time.Duration
66+
leaderElectionRetryPeriod time.Duration
67+
watchFilterValue string
68+
watchNamespace string
69+
profilerAddress string
70+
enableContentionProfiling bool
71+
syncPeriod time.Duration
72+
restConfigQPS float32
73+
restConfigBurst int
74+
webhookPort int
75+
webhookCertDir string
76+
healthAddr string
77+
tlsOptions = flags.TLSOptions{}
78+
diagnosticsOptions = flags.DiagnosticsOptions{}
79+
logOptions = logs.NewOptions()
80+
// CABPK specific flags.
81+
clusterConcurrency int
82+
clusterCacheTrackerConcurrency int
83+
kubeadmConfigConcurrency int
84+
tokenTTL time.Duration
6285
)
6386

6487
func init() {
65-
klog.InitFlags(nil)
66-
6788
_ = clientgoscheme.AddToScheme(scheme)
6889
_ = clusterv1.AddToScheme(scheme)
6990
_ = expv1.AddToScheme(scheme)
7091
_ = bootstrapv1alpha4.AddToScheme(scheme)
7192
_ = bootstrapv1.AddToScheme(scheme)
7293
}
7394

74-
var (
75-
metricsBindAddr string
76-
enableLeaderElection bool
77-
leaderElectionLeaseDuration time.Duration
78-
leaderElectionRenewDeadline time.Duration
79-
leaderElectionRetryPeriod time.Duration
80-
watchFilterValue string
81-
watchNamespace string
82-
profilerAddress string
83-
enableContentionProfiling bool
84-
clusterConcurrency int
85-
clusterCacheTrackerConcurrency int
86-
kubeadmConfigConcurrency int
87-
syncPeriod time.Duration
88-
restConfigQPS float32
89-
restConfigBurst int
90-
webhookPort int
91-
webhookCertDir string
92-
healthAddr string
93-
tokenTTL time.Duration
94-
tlsOptions = flags.TLSOptions{}
95-
logOptions = logs.NewOptions()
96-
)
97-
98-
// InitFlags initializes this manager's flags.
95+
// InitFlags initializes the flags.
9996
func InitFlags(fs *pflag.FlagSet) {
10097
logsv1.AddFlags(logOptions, fs)
10198

102-
fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080",
103-
"The address the metric endpoint binds to.")
104-
10599
fs.BoolVar(&enableLeaderElection, "leader-elect", false,
106100
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
107101

@@ -117,11 +111,14 @@ func InitFlags(fs *pflag.FlagSet) {
117111
fs.StringVar(&watchNamespace, "namespace", "",
118112
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
119113

114+
fs.StringVar(&watchFilterValue, "watch-filter", "",
115+
fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))
116+
120117
fs.StringVar(&profilerAddress, "profiler-address", "",
121118
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
122119

123120
fs.BoolVar(&enableContentionProfiling, "contention-profiling", false,
124-
"Enable block profiling, if profiler-address is set.")
121+
"Enable block profiling")
125122

126123
fs.IntVar(&clusterConcurrency, "cluster-concurrency", 10,
127124
"Number of clusters to process simultaneously")
@@ -145,9 +142,6 @@ func InitFlags(fs *pflag.FlagSet) {
145142
fs.DurationVar(&tokenTTL, "bootstrap-token-ttl", kubeadmbootstrapcontrollers.DefaultTokenTTL,
146143
"The amount of time the bootstrap token will be valid")
147144

148-
fs.StringVar(&watchFilterValue, "watch-filter", "",
149-
fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))
150-
151145
fs.IntVar(&webhookPort, "webhook-port", 9443,
152146
"Webhook Server port")
153147

@@ -157,11 +151,16 @@ func InitFlags(fs *pflag.FlagSet) {
157151
fs.StringVar(&healthAddr, "health-addr", ":9440",
158152
"The address the health endpoint binds to.")
159153

154+
flags.AddDiagnosticsOptions(fs, &diagnosticsOptions)
160155
flags.AddTLSOptions(fs, &tlsOptions)
161156

162157
feature.MutableGates.AddFlag(fs)
163158
}
164159

160+
// Add RBAC for the authorized diagnostics endpoint.
161+
// +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create
162+
// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create
163+
165164
func main() {
166165
InitFlags(pflag.CommandLine)
167166
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
@@ -192,14 +191,16 @@ func main() {
192191
os.Exit(1)
193192
}
194193

194+
diagnosticsOpts := flags.GetDiagnosticsOptions(diagnosticsOptions)
195+
195196
var watchNamespaces map[string]cache.Config
196197
if watchNamespace != "" {
197198
watchNamespaces = map[string]cache.Config{
198199
watchNamespace: {},
199200
}
200201
}
201202

202-
if profilerAddress != "" && enableContentionProfiling {
203+
if enableContentionProfiling {
203204
goruntime.SetBlockProfileRate(1)
204205
}
205206

@@ -216,9 +217,7 @@ func main() {
216217
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
217218
HealthProbeBindAddress: healthAddr,
218219
PprofBindAddress: profilerAddress,
219-
Metrics: metricsserver.Options{
220-
BindAddress: metricsBindAddr,
221-
},
220+
Metrics: diagnosticsOpts,
222221
Cache: cache.Options{
223222
DefaultNamespaces: watchNamespaces,
224223
SyncPeriod: &syncPeriod,

config/manager/manager.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ spec:
2121
- /manager
2222
args:
2323
- "--leader-elect"
24-
- "--metrics-bind-addr=localhost:8080"
24+
- "--diagnostics-address=:8443"
2525
- "--feature-gates=MachinePool=${EXP_MACHINE_POOL:=false},ClusterResourceSet=${EXP_CLUSTER_RESOURCE_SET:=false},ClusterTopology=${CLUSTER_TOPOLOGY:=false},RuntimeSDK=${EXP_RUNTIME_SDK:=false},MachineSetPreflightChecks=${EXP_MACHINE_SET_PREFLIGHT_CHECKS:=false}"
2626
image: controller:latest
2727
name: manager
@@ -42,6 +42,9 @@ spec:
4242
- containerPort: 9440
4343
name: healthz
4444
protocol: TCP
45+
- containerPort: 8443
46+
name: metrics
47+
protocol: TCP
4548
readinessProbe:
4649
httpGet:
4750
path: /readyz

config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ rules:
4141
- get
4242
- list
4343
- watch
44+
- apiGroups:
45+
- authentication.k8s.io
46+
resources:
47+
- tokenreviews
48+
verbs:
49+
- create
50+
- apiGroups:
51+
- authorization.k8s.io
52+
resources:
53+
- subjectaccessreviews
54+
verbs:
55+
- create
4456
- apiGroups:
4557
- bootstrap.cluster.x-k8s.io
4658
- controlplane.cluster.x-k8s.io

controlplane/kubeadm/config/manager/manager.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
- /manager
2121
args:
2222
- "--leader-elect"
23-
- "--metrics-bind-addr=localhost:8080"
23+
- "--diagnostics-address=:8443"
2424
- "--feature-gates=ClusterTopology=${CLUSTER_TOPOLOGY:=false},KubeadmBootstrapFormatIgnition=${EXP_KUBEADM_BOOTSTRAP_FORMAT_IGNITION:=false}"
2525
image: controller:latest
2626
name: manager
@@ -41,6 +41,9 @@ spec:
4141
- containerPort: 9440
4242
name: healthz
4343
protocol: TCP
44+
- containerPort: 8443
45+
name: metrics
46+
protocol: TCP
4447
readinessProbe:
4548
httpGet:
4649
path: /readyz

controlplane/kubeadm/config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ rules:
1212
- get
1313
- list
1414
- watch
15+
- apiGroups:
16+
- authentication.k8s.io
17+
resources:
18+
- tokenreviews
19+
verbs:
20+
- create
21+
- apiGroups:
22+
- authorization.k8s.io
23+
resources:
24+
- subjectaccessreviews
25+
verbs:
26+
- create
1527
- apiGroups:
1628
- bootstrap.cluster.x-k8s.io
1729
- controlplane.cluster.x-k8s.io

0 commit comments

Comments
 (0)