Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions helm/kagent/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ documented contract.
{{- if and .Values.controller.metrics.enabled $port (ne $port "0") -}}1{{- end -}}
{{- end -}}

{{/*
Whether the controller ServiceMonitor (and the RBAC that exists only to
serve its scrape) should render. Requires the metrics endpoint, the
serviceMonitor toggle, and the Prometheus Operator CRDs on the target
cluster; without the CRD the manifest would fail to apply.
*/}}
{{- define "kagent.controller.serviceMonitorEnabled" -}}
{{- if and (include "kagent.controller.metricsEnabled" .) .Values.controller.metrics.serviceMonitor.enabled (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1") -}}1{{- end -}}
{{- end -}}

{{/*
Name of the controller metrics Service port, derived from the scheme the
controller serves. Shared by the metrics Service and the ServiceMonitor
endpoint so the two can never drift apart.
*/}}
{{- define "kagent.controller.metricsPortName" -}}
{{- ternary "https" "http-metrics" .Values.controller.metrics.secureServing -}}
{{- end -}}

{{/*
Controller gRPC observability PrometheusRule name.
*/}}
Expand Down
2 changes: 1 addition & 1 deletion helm/kagent/templates/controller-metrics-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
spec:
type: {{ .Values.controller.metrics.service.type }}
ports:
- name: {{ ternary "https" "http-metrics" .Values.controller.metrics.secureServing }}
- name: {{ include "kagent.controller.metricsPortName" . }}
port: {{ .Values.controller.metrics.service.port }}
targetPort: {{ include "kagent.controller.metricsPort" . | int }}
protocol: TCP
Expand Down
53 changes: 53 additions & 0 deletions helm/kagent/templates/controller-servicemonitor.yaml
Comment thread
younsl marked this conversation as resolved.
Resolved
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 }}
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 }}
90 changes: 90 additions & 0 deletions helm/kagent/tests/controller-metrics-pairing_test.yaml
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 helm/kagent/tests/controller-metrics-reader-binding_test.yaml
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
Loading
Loading