Skip to content

Commit 13a3729

Browse files
refactor: enable metrics manifests (#79)
Signed-off-by: AvineshTripathi <avineshtripathi1@gmail.com>
1 parent 5c179b9 commit 13a3729

22 files changed

+442
-47
lines changed

Makefile

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ CONTROLLER_GEN_PKG := sigs.k8s.io/controller-tools/cmd/controller-gen
6262
IMG_PREFIX ?= controller
6363
IMG_TAG ?= latest
6464

65+
# ENABLE_METRICS: If set to true, includes Prometheus Service and ServiceMonitor resources.
66+
ENABLE_METRICS ?= false
67+
# ENABLE_TLS: If set to true (and ENABLE_METRICS is true), configures metrics to use HTTPS with CertManager.
68+
ENABLE_TLS ?= false
6569

6670
# Default value for ignore-not-found flag in undeploy target
6771
ignore-not-found ?= true
@@ -208,14 +212,13 @@ docker-buildx-reporter: ## Build and push docker image for the reporter for cros
208212
- $(CONTAINER_TOOL) buildx rm reporter-builder
209213

210214
.PHONY: build-installer
211-
build-installer: manifests generate $(KUSTOMIZE) ## Generate CRDs and deployment manifests for release.
215+
build-installer: build-manifests-temp ## Generate CRDs and deployment manifests for release.
212216
mkdir -p dist
213217
# Generate CRDs only
214218
$(KUSTOMIZE) build config/crd > dist/crds.yaml
215219
@echo "Generated dist/crds.yaml"
216220
# Generate controller deployment without CRDs
217-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_PREFIX}:${IMG_TAG}
218-
$(KUSTOMIZE) build config/default > dist/install.yaml
221+
cp $(BUILD_DIR)/manifests.yaml dist/install.yaml
219222
@echo "Generated dist/install.yaml with image ${IMG_PREFIX}:${IMG_TAG}"
220223
@echo "NOTE: Install crds.yaml first, then install.yaml. Deployment runs on any available node by default."
221224

@@ -229,6 +232,32 @@ ifndef ignore-not-found
229232
ignore-not-found = false
230233
endif
231234

235+
# Temporary directory for building manifests
236+
BUILD_DIR := $(ROOT_DIR)/bin/build
237+
238+
# Internal target to build manifests in a temporary directory to keep the source config clean.
239+
# This prevents 'kustomize edit' from modifying your local git state.
240+
# Features (Metrics, TLS) are enabled by adding Kustomize Components to the temporary copy.
241+
# TODO: we can do better for prometheus metrics ports that are added by manager_prometheus_metrics.yaml
242+
.PHONY: build-manifests-temp
243+
build-manifests-temp: manifests $(KUSTOMIZE)
244+
@mkdir -p $(BUILD_DIR)
245+
@rm -rf $(BUILD_DIR)/config
246+
@cp -r config $(BUILD_DIR)/
247+
@cd $(BUILD_DIR)/config/manager && $(KUSTOMIZE) edit set image controller=${IMG_PREFIX}:${IMG_TAG}
248+
@if [ "$(ENABLE_METRICS)" = "true" ]; then \
249+
cd $(BUILD_DIR)/config/default && $(KUSTOMIZE) edit add component ../prometheus; \
250+
if [ "$(ENABLE_TLS)" = "true" ]; then \
251+
cd $(BUILD_DIR)/config/default && $(KUSTOMIZE) edit add component ../certmanager && \
252+
$(KUSTOMIZE) edit add component ../prometheus/tls; \
253+
else \
254+
cd $(BUILD_DIR)/config/prometheus && $(KUSTOMIZE) edit add patch --path manager_prometheus_metrics.yaml --kind Deployment --name controller-manager; \
255+
fi; \
256+
fi
257+
@$(KUSTOMIZE) build $(BUILD_DIR)/config/default > $(BUILD_DIR)/manifests.yaml
258+
@rm -rf $(BUILD_DIR)/config
259+
260+
232261
.PHONY: install
233262
install: manifests $(KUSTOMIZE) ## Install CRDs into the K8s cluster specified in ~/.kube/config.
234263
@out="$$( $(KUSTOMIZE) build config/crd 2>/dev/null || true )"; \
@@ -240,13 +269,30 @@ uninstall: manifests $(KUSTOMIZE) ## Uninstall CRDs from the K8s cluster specifi
240269
if [ -n "$$out" ]; then echo "$$out" | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -; else echo "No CRDs to delete; skipping."; fi
241270

242271
.PHONY: deploy
243-
deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
244-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_PREFIX}:${IMG_TAG}
245-
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
272+
deploy: build-manifests-temp ## Deploy controller to the K8s cluster. Use ENABLE_METRICS=true and ENABLE_TLS=true to enable features.
273+
$(KUBECTL) apply -f $(BUILD_DIR)/manifests.yaml
246274

247275
.PHONY: undeploy
248-
undeploy: $(KUSTOMIZE) ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
249-
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
276+
undeploy: build-manifests-temp ## Undeploy controller from the K8s cluster. Use ENABLE_METRICS=true and ENABLE_TLS=true if they were enabled during deploy.
277+
$(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f $(BUILD_DIR)/manifests.yaml
278+
279+
.PHONY: deploy-with-metrics
280+
deploy-with-metrics: ENABLE_METRICS=true
281+
deploy-with-metrics: deploy ## Deploy with metrics enabled.
282+
283+
.PHONY: undeploy-with-metrics
284+
undeploy-with-metrics: ENABLE_METRICS=true
285+
undeploy-with-metrics: undeploy ## Undeploy with metrics enabled.
286+
287+
.PHONY: deploy-with-metrics-tls-enabled
288+
deploy-with-metrics-tls-enabled: ENABLE_TLS=true
289+
deploy-with-metrics-tls-enabled: ENABLE_METRICS=true
290+
deploy-with-metrics-tls-enabled: deploy ## Deploy with metrics and TLS enabled.
291+
292+
.PHONY: undeploy-with-metrics-tls-enabled
293+
undeploy-with-metrics-tls-enabled: ENABLE_TLS=true
294+
undeploy-with-metrics-tls-enabled: ENABLE_METRICS=true
295+
undeploy-with-metrics-tls-enabled: undeploy ## Undeploy with metrics and TLS enabled.
250296

251297
## --------------------------------------
252298
## Testing

cmd/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package main
1919
import (
2020
"flag"
2121
"fmt"
22+
"net/http"
2223
"os"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
2627
"go.uber.org/zap/zapcore"
2728
_ "k8s.io/client-go/plugin/pkg/client/auth"
29+
"k8s.io/client-go/rest"
2830

2931
"k8s.io/apimachinery/pkg/runtime"
3032
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -33,6 +35,7 @@ import (
3335
ctrl "sigs.k8s.io/controller-runtime"
3436
"sigs.k8s.io/controller-runtime/pkg/healthz"
3537
"sigs.k8s.io/controller-runtime/pkg/log/zap"
38+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3639
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3740

3841
nodereadinessiov1alpha1 "sigs.k8s.io/node-readiness-controller/api/v1alpha1"
@@ -60,8 +63,15 @@ func main() {
6063
var enableLeaderElection bool
6164
var probeAddr string
6265
var enableWebhook bool
66+
var metricsSecure bool
67+
var metricsCertDir string
6368
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
6469
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
70+
flag.BoolVar(&metricsSecure, "metrics-secure", false,
71+
"If set, the metrics endpoint is served securely via HTTPS. "+
72+
"Requires certificate and key.")
73+
flag.StringVar(&metricsCertDir, "metrics-cert-dir", "",
74+
"The directory where the certificates for metrics are located.")
6575
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6676
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6777
"Enable leader election for controller manager. "+
@@ -80,7 +90,15 @@ func main() {
8090
ctrl.Log.Info(fmt.Sprintf("version: %s", info.GetVersionString()))
8191

8292
metricsServerOptions := metricsserver.Options{
83-
BindAddress: metricsAddr,
93+
BindAddress: metricsAddr,
94+
CertDir: metricsCertDir,
95+
SecureServing: metricsSecure,
96+
FilterProvider: func() func(c *rest.Config, httpClient *http.Client) (metricsserver.Filter, error) {
97+
if metricsSecure {
98+
return filters.WithAuthenticationAndAuthorization
99+
}
100+
return nil
101+
}(),
84102
}
85103

86104
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The following manifests contain a self-signed issuer CR and a certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Issuer
5+
metadata:
6+
name: selfsigned-issuer
7+
namespace: system
8+
spec:
9+
selfSigned: {}
10+
---
11+
apiVersion: cert-manager.io/v1
12+
kind: Certificate
13+
metadata:
14+
name: metrics-certs
15+
namespace: system
16+
spec:
17+
commonName: nrr-metrics
18+
dnsNames:
19+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
20+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
21+
issuerRef:
22+
kind: Issuer
23+
name: selfsigned-issuer
24+
secretName: metrics-server-cert
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: kustomize.config.k8s.io/v1alpha1
2+
kind: Component
3+
resources:
4+
- certificate.yaml
5+
6+
configurations:
7+
- kustomizeconfig.yaml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
nameReference:
2+
- kind: Issuer
3+
group: cert-manager.io
4+
fieldSpecs:
5+
- kind: Certificate
6+
group: cert-manager.io
7+
path: spec/issuerRef/name
8+
9+
- kind: Secret
10+
version: v1
11+
fieldSpecs:
12+
- kind: Certificate
13+
group: cert-manager.io
14+
path: spec/secretName
15+
16+
varReference:
17+
- kind: Certificate
18+
group: cert-manager.io
19+
path: spec/dnsNames

config/default/kustomization.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,3 @@ namePrefix: nrr-
1717
resources:
1818
- ../rbac
1919
- ../manager
20-
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
21-
#- ../prometheus
22-
# [METRICS] Expose the controller manager metrics service.
23-
# - metrics_service.yaml
24-
# [NETWORK POLICY] Protect the /metrics endpoint and Webhook Server with NetworkPolicy.
25-
# Only Pod(s) running a namespace labeled with 'metrics: enabled' will be able to gather the metrics.
26-
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
27-
# be able to communicate with the Webhook Server.
28-
#- ../network-policy
29-
30-
# Uncomment the patches line if you enable Metrics
31-
# patches:
32-
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
33-
# More info: https://book.kubebuilder.io/reference/metrics
34-
# - path: manager_metrics_patch.yaml
35-
# target:
36-
# kind: Deployment
37-

config/default/manager_metrics_patch.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
apiVersion: kustomize.config.k8s.io/v1alpha1
2+
kind: Component
13
resources:
24
- monitor.yaml
5+
- metrics_service.yaml
36

4-
# [PROMETHEUS-WITH-CERTS] The following patch configures the ServiceMonitor in ../prometheus
5-
# to securely reference certificates created and managed by cert-manager.
6-
# Additionally, ensure that you uncomment the [METRICS WITH CERTMANAGER] patch under config/default/kustomization.yaml
7-
# to mount the "metrics-server-cert" secret in the Manager Deployment.
8-
#patches:
9-
# - path: monitor_tls_patch.yaml
10-
# target:
11-
# kind: ServiceMonitor
7+
patches:
8+
# Bind metrics to port 8080 for HTTP.
9+
# This matches the Service and ServiceMonitor configuration in this directory.
10+
# - path: manager_prometheus_metrics.yaml
11+
# target:
12+
# kind: Deployment
13+
# name: controller-manager
14+
15+
# By default, metrics are disabled in the manager (default : "0").
16+
# This component adds the Service and ServiceMonitor for Prometheus,
17+
# and applies the patch to bind the manager to port :8080(it is done in Makefile for now).
18+
19+
# Patches for TLS are in the 'tls' component which will:
20+
# 1. Overlay the HTTPS args (:8443) and security flags
21+
# 2. Add ServiceMonitor TLS config
22+
# 3. Mount CertManager secrets
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 HTTP
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/-
4+
value: --metrics-bind-address=:8080
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ metadata:
99
namespace: system
1010
spec:
1111
ports:
12-
- name: https
13-
port: 8443
12+
- name: http
13+
port: 8080
1414
protocol: TCP
15-
targetPort: 8443
15+
targetPort: 8080
1616
selector:
1717
control-plane: controller-manager
1818
app.kubernetes.io/name: nrrcontroller

0 commit comments

Comments
 (0)