Repository navigation
feat(helm): add a ServiceMonitor for the controller metrics endpoint #2401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EItanya
merged 3 commits into
kagent-dev:main
from
younsl:feat/controller-servicemonitor
Sep 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| {{- if include "kagent.controller.serviceMonitorEnabled" . }} | ||
| apiVersion: monitoring.coreos.com/v1 | ||
| kind: ServiceMonitor | ||
| metadata: | ||
| name: {{ include "kagent.fullname" . }}-controller-metrics | ||
| namespace: {{ default (include "kagent.namespace" .) .Values.controller.metrics.serviceMonitor.namespace }} | ||
| labels: | ||
| {{- include "kagent.controller.labels" . | nindent 4 }} | ||
| {{- with .Values.controller.metrics.serviceMonitor.labels }} | ||
| {{- toYaml . | nindent 4 }} | ||
| {{- end }} | ||
| {{- with .Values.controller.metrics.serviceMonitor.annotations }} | ||
| annotations: | ||
| {{- toYaml . | nindent 4 }} | ||
| {{- end }} | ||
| spec: | ||
| selector: | ||
| matchLabels: | ||
| {{- include "kagent.controller.selectorLabels" . | nindent 6 }} | ||
| namespaceSelector: | ||
| matchNames: | ||
| - {{ include "kagent.namespace" . }} | ||
| endpoints: | ||
| - port: {{ include "kagent.controller.metricsPortName" . }} | ||
| path: /metrics | ||
| {{- with .Values.controller.metrics.serviceMonitor.interval }} | ||
| interval: {{ . }} | ||
| {{- end }} | ||
| {{- with .Values.controller.metrics.serviceMonitor.scrapeTimeout }} | ||
| scrapeTimeout: {{ . }} | ||
| {{- end }} | ||
| honorLabels: {{ .Values.controller.metrics.serviceMonitor.honorLabels }} | ||
| {{- if .Values.controller.metrics.secureServing }} | ||
| scheme: https | ||
| {{- with .Values.controller.metrics.serviceMonitor.bearerTokenFile }} | ||
| bearerTokenFile: {{ . | quote }} | ||
| {{- end }} | ||
| tlsConfig: | ||
| {{- with .Values.controller.metrics.serviceMonitor.tlsConfig }} | ||
| {{- toYaml . | nindent 8 }} | ||
| {{- else }} | ||
| insecureSkipVerify: true | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- with .Values.controller.metrics.serviceMonitor.relabelings }} | ||
| relabelings: | ||
| {{- toYaml . | nindent 8 }} | ||
| {{- end }} | ||
| {{- with .Values.controller.metrics.serviceMonitor.metricRelabelings }} | ||
| metricRelabelings: | ||
| {{- toYaml . | nindent 8 }} | ||
| {{- end }} | ||
| {{- end }} |
16 changes: 16 additions & 0 deletions
16
helm/kagent/templates/rbac/metrics-reader-clusterrolebinding.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| {{- if and (include "kagent.controller.serviceMonitorEnabled" .) .Values.controller.metrics.secureServing .Values.controller.metrics.serviceMonitor.prometheusServiceAccount.name }} | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRoleBinding | ||
| metadata: | ||
| name: {{ include "kagent.fullname" . }}-metrics-reader-rolebinding | ||
| labels: | ||
| {{- include "kagent.controller.labels" . | nindent 4 }} | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: {{ include "kagent.fullname" . }}-metrics-reader | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: {{ .Values.controller.metrics.serviceMonitor.prometheusServiceAccount.name }} | ||
| namespace: {{ .Values.controller.metrics.serviceMonitor.prometheusServiceAccount.namespace | default .Values.controller.metrics.serviceMonitor.namespace | default (include "kagent.namespace" .) }} | ||
| {{- end }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| suite: test controller metrics secure serving pairing | ||
| templates: | ||
| - controller-metrics-service.yaml | ||
| - controller-servicemonitor.yaml | ||
| - rbac/metrics-auth-clusterrolebinding.yaml | ||
| - rbac/metrics-reader-clusterrolebinding.yaml | ||
| tests: | ||
| - it: should pair the service and service monitor on the https port when secure serving is enabled | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - equal: | ||
| path: spec.ports[0].name | ||
| value: https | ||
| template: controller-metrics-service.yaml | ||
| - equal: | ||
| path: spec.endpoints[0].port | ||
| value: https | ||
| template: controller-servicemonitor.yaml | ||
| - equal: | ||
| path: spec.endpoints[0].scheme | ||
| value: https | ||
| template: controller-servicemonitor.yaml | ||
| - exists: | ||
| path: spec.endpoints[0].bearerTokenFile | ||
| template: controller-servicemonitor.yaml | ||
| - hasDocuments: | ||
| count: 1 | ||
| template: rbac/metrics-auth-clusterrolebinding.yaml | ||
| - hasDocuments: | ||
| count: 1 | ||
| template: rbac/metrics-reader-clusterrolebinding.yaml | ||
|
|
||
| - it: should pair the service and service monitor on the plaintext port when secure serving is disabled | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.secureServing: false | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - equal: | ||
| path: spec.ports[0].name | ||
| value: http-metrics | ||
| template: controller-metrics-service.yaml | ||
| - equal: | ||
| path: spec.endpoints[0].port | ||
| value: http-metrics | ||
| template: controller-servicemonitor.yaml | ||
| - notExists: | ||
| path: spec.endpoints[0].scheme | ||
| template: controller-servicemonitor.yaml | ||
| - notExists: | ||
| path: spec.endpoints[0].bearerTokenFile | ||
| template: controller-servicemonitor.yaml | ||
| - notExists: | ||
| path: spec.endpoints[0].tlsConfig | ||
| template: controller-servicemonitor.yaml | ||
| - hasDocuments: | ||
| count: 0 | ||
| template: rbac/metrics-auth-clusterrolebinding.yaml | ||
| - hasDocuments: | ||
| count: 0 | ||
| template: rbac/metrics-reader-clusterrolebinding.yaml | ||
|
|
||
| - it: should keep the service and the auth binding but skip the service monitor and reader binding when the monitoring api is absent | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 1 | ||
| template: controller-metrics-service.yaml | ||
| - hasDocuments: | ||
| count: 1 | ||
| template: rbac/metrics-auth-clusterrolebinding.yaml | ||
| - hasDocuments: | ||
| count: 0 | ||
| template: controller-servicemonitor.yaml | ||
| - hasDocuments: | ||
| count: 0 | ||
| template: rbac/metrics-reader-clusterrolebinding.yaml |
126 changes: 126 additions & 0 deletions
126
helm/kagent/tests/controller-metrics-reader-binding_test.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| suite: test controller metrics reader binding | ||
| templates: | ||
| - rbac/metrics-reader-clusterrolebinding.yaml | ||
| tests: | ||
| - it: should not render by default | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 | ||
|
|
||
| - it: should not render without a prometheus service account | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 | ||
|
|
||
| - it: should not render when the service monitor is disabled | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 | ||
|
|
||
| - it: should not render when secure serving is disabled | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.secureServing: false | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 | ||
|
|
||
| - it: should not render when bindAddress disables metrics | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.bindAddress: "0" | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 | ||
|
|
||
| - it: should bind metrics-reader to the prometheus service account | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.namespace: monitoring | ||
| asserts: | ||
| - isKind: | ||
| of: ClusterRoleBinding | ||
| - equal: | ||
| path: metadata.name | ||
| value: RELEASE-NAME-metrics-reader-rolebinding | ||
| - equal: | ||
| path: roleRef.kind | ||
| value: ClusterRole | ||
| - equal: | ||
| path: roleRef.name | ||
| value: RELEASE-NAME-metrics-reader | ||
| - equal: | ||
| path: subjects[0].kind | ||
| value: ServiceAccount | ||
| - equal: | ||
| path: subjects[0].name | ||
| value: prometheus-k8s | ||
| - equal: | ||
| path: subjects[0].namespace | ||
| value: monitoring | ||
|
|
||
| - it: should default the subject namespace to the service monitor namespace | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.namespace: monitoring | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - equal: | ||
| path: subjects[0].namespace | ||
| value: monitoring | ||
|
|
||
| - it: should default the subject namespace to the release namespace | ||
| capabilities: | ||
| apiVersions: | ||
| - monitoring.coreos.com/v1 | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - equal: | ||
| path: subjects[0].namespace | ||
| value: NAMESPACE | ||
|
|
||
| - it: should not render when the monitoring api is absent | ||
| set: | ||
| controller.metrics.enabled: true | ||
| controller.metrics.serviceMonitor.enabled: true | ||
| controller.metrics.serviceMonitor.prometheusServiceAccount.name: prometheus-k8s | ||
| asserts: | ||
| - hasDocuments: | ||
| count: 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.