Skip to content

Commit 047cfc1

Browse files
igoramfclaude
andauthored
feat(metrics): expose metrics via HTTP on :8080 for vmagent scraping (#8)
* feat(metrics): expose metrics via HTTP on :8080 for vmagent scraping Switch metrics endpoint from HTTPS :8443 to HTTP :8080 to enable automatic scraping via the kubernetes-pods vmagent job using prometheus.io pod annotations. Webhook TLS on :9443 is unchanged. * fix(metrics): add --metrics-secure=false to expose HTTP without TLS * feat(metrics): expose HTTP metrics on :8080 with podAnnotations support - Switch metrics endpoint from :8443 (TLS) to :8080 (HTTP) for vmagent scraping - Disable cert_metrics_manager_patch to remove metrics-certs volume dependency - Add podAnnotations injection in helm-generator for vmagent scrape annotations Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(crd): revert controller-gen version annotation to v0.18.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7022b14 commit 047cfc1

5 files changed

Lines changed: 55 additions & 22 deletions

File tree

chart/templates/deployment-operator-controller-manager.yaml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ spec:
1717
metadata:
1818
annotations:
1919
kubectl.kubernetes.io/default-container: manager
20+
{{- with .Values.podAnnotations }}
21+
{{- toYaml . | nindent 8 }}
22+
{{- end }}
2023
labels:
2124
app.kubernetes.io/name: operator
2225
control-plane: controller-manager
2326
spec:
2427
containers:
2528
- args:
26-
- --metrics-bind-address=:8443
29+
- --metrics-bind-address=:8080
30+
- --metrics-secure=false
2731
- --leader-elect
2832
- --health-probe-bind-address=:8081
29-
- --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs
3033
- --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs
3134
command:
3235
- /manager
@@ -124,6 +127,9 @@ spec:
124127
periodSeconds: 20
125128
name: manager
126129
ports:
130+
- containerPort: 8080
131+
name: metrics
132+
protocol: TCP
127133
- containerPort: 9443
128134
name: webhook-server
129135
protocol: TCP
@@ -146,9 +152,6 @@ spec:
146152
drop:
147153
- ALL
148154
volumeMounts:
149-
- mountPath: /tmp/k8s-metrics-server/metrics-certs
150-
name: metrics-certs
151-
readOnly: true
152155
- mountPath: /tmp/k8s-webhook-server/serving-certs
153156
name: webhook-certs
154157
readOnly: true
@@ -159,17 +162,6 @@ spec:
159162
serviceAccountName: {{ .Release.Name }}-controller-manager
160163
terminationGracePeriodSeconds: 10
161164
volumes:
162-
- name: metrics-certs
163-
secret:
164-
items:
165-
- key: ca.crt
166-
path: ca.crt
167-
- key: tls.crt
168-
path: tls.crt
169-
- key: tls.key
170-
path: tls.key
171-
optional: false
172-
secretName: metrics-server-cert
173165
- name: webhook-certs
174166
secret:
175167
secretName: webhook-server-cert

chart/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ healthProbe:
5858
# Metrics configuration
5959
metrics:
6060
enabled: true
61-
port: 8443
61+
port: 8080
6262

6363
# Leader election
6464
leaderElection:

config/default/kustomization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ patches:
4444
# Uncomment the patches line if you enable Metrics and CertManager
4545
# [METRICS-WITH-CERTS] To enable metrics protected with certManager, uncomment the following line.
4646
# This patch will protect the metrics with certManager self-signed certs.
47-
- path: cert_metrics_manager_patch.yaml
48-
target:
49-
kind: Deployment
47+
#- path: cert_metrics_manager_patch.yaml
48+
# target:
49+
# kind: Deployment
5050

5151
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
5252
# crd/kustomization.yaml
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# This patch adds the args to allow exposing the metrics endpoint using HTTPS
1+
# This patch adds the args to expose the metrics endpoint via HTTP
22
- op: add
33
path: /spec/template/spec/containers/0/args/0
4-
value: --metrics-bind-address=:8443
4+
value: --metrics-bind-address=:8080
5+
- op: add
6+
path: /spec/template/spec/containers/0/args/1
7+
value: --metrics-secure=false
8+
- op: add
9+
path: /spec/template/spec/containers/0/ports/-
10+
value:
11+
containerPort: 8080
12+
name: metrics
13+
protocol: TCP

hack/helm-generator/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func main() {
9393
fmt.Fprintf(os.Stderr, "Warning: Could not add builder service account: %v\n", err)
9494
}
9595

96+
// Add podAnnotations to deployment pod template
97+
if err := addPodAnnotations(templatesDir); err != nil {
98+
fmt.Fprintf(os.Stderr, "Warning: Could not add pod annotations to deployment: %v\n", err)
99+
}
100+
96101
fmt.Printf("✓ Generated %d Helm templates\n\n", fileCount)
97102
fmt.Println("Test with:")
98103
fmt.Println(" make helm-lint")
@@ -285,6 +290,33 @@ func addEnvVarsToDeployment(templatesDir string) error {
285290
return os.WriteFile(deploymentFile, []byte(contentStr), 0644)
286291
}
287292

293+
func addPodAnnotations(templatesDir string) error {
294+
files, err := filepath.Glob(filepath.Join(templatesDir, "deployment-*.yaml"))
295+
if err != nil || len(files) == 0 {
296+
return fmt.Errorf("no deployment file found")
297+
}
298+
299+
deploymentFile := files[0]
300+
content, err := os.ReadFile(deploymentFile)
301+
if err != nil {
302+
return err
303+
}
304+
305+
contentStr := string(content)
306+
307+
annotationsBlock := ` annotations:
308+
kubectl.kubernetes.io/default-container: manager
309+
{{- with .Values.podAnnotations }}
310+
{{- toYaml . | nindent 8 }}
311+
{{- end }}`
312+
313+
contentStr = strings.ReplaceAll(contentStr,
314+
" annotations:\n kubectl.kubernetes.io/default-container: manager",
315+
annotationsBlock)
316+
317+
return os.WriteFile(deploymentFile, []byte(contentStr), 0644)
318+
}
319+
288320
func addBuilderServiceAccount(templatesDir string) error {
289321
content := `{{- if .Values.build.serviceAccount }}
290322
apiVersion: v1

0 commit comments

Comments
 (0)