Skip to content

Commit

Permalink
Helm: Add servicemonitor support (#4094)
Browse files Browse the repository at this point in the history
* Add servicemonitor support

* Add prometheus operator crd to helm test

Signed-off-by: QuentinBisson <[email protected]>

---------

Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson authored Sep 21, 2023
1 parent 0b0b4a5 commit 6d9ae6b
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/helm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,13 @@ jobs:
uses: helm/[email protected]
if: steps.list-changed.outputs.changed == 'true'

- name: Add dependency chart repos
run: |
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
- name: Install Prometheus Operator CRDs
run: |
helm install my-prometheus-operator-crds prometheus-community/prometheus-operator-crds --version 6.0.0
- name: Test charts
run: ct install --config ./operations/helm/ct.yaml
4 changes: 2 additions & 2 deletions operations/helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ These manifests are never run directly, but are instead used to validate the
correctness of the templates emitted by the Helm chart. To regenerate this
folder, call `make rebuild-tests` from the root of the repository.

`make rebuild-tests` will iterate through the value.yaml files in
`charts/grafana-agent/tests` and generate each one as a separate directory.
`make generate-helm-tests` will iterate through the value.yaml files in
`charts/grafana-agent/ci` and generate each one as a separate directory under `charts/grafana-agent/tests`.

When modifying the Helm charts, `make rebuild-tests` must be run before
submitting a PR, as a linter check will ensure that this directory is
Expand Down
2 changes: 2 additions & 0 deletions operations/helm/charts/grafana-agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Unreleased

- An image's digest can now be used in place of a tag. (@hainenber)

- Add ServiceMonitor support. (@QuentinBisson)

0.24.0 (2023-09-08)
-------------------

Expand Down
5 changes: 5 additions & 0 deletions operations/helm/charts/grafana-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ use the older mode (called "static mode"), set the `agent.mode` value to
| serviceAccount.annotations | object | `{}` | Annotations to add to the created service account. |
| serviceAccount.create | bool | `true` | Whether to create a service account for the Grafana Agent deployment. |
| serviceAccount.name | string | `nil` | The name of the existing service account to use when serviceAccount.create is false. |
| serviceMonitor.additionalLabels | object | `{}` | Additional labels for the service monitor. |
| serviceMonitor.enabled | bool | `false` | |
| serviceMonitor.interval | string | `""` | Scrape interval. If not set, the Prometheus default scrape interval is used. |
| serviceMonitor.metricRelabelings | list | `[]` | MetricRelabelConfigs to apply to samples after scraping, but before ingestion. ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#relabelconfig |
| serviceMonitor.relabelings | list | `[]` | RelabelConfigs to apply to samples before scraping ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#relabelconfig |

### agent.extraArgs

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Test rendering of the chart with the service monitor enabled
service:
enabled: true
serviceMonitor:
enabled: true
29 changes: 29 additions & 0 deletions operations/helm/charts/grafana-agent/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{- if and .Values.service.enabled .Values.serviceMonitor.enabled -}}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "grafana-agent.fullname" . }}
labels:
{{- include "grafana-agent.labels" . | nindent 4 }}
{{- with .Values.serviceMonitor.additionalLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
endpoints:
- port: http-metrics
honorLabels: true
{{- if .Values.serviceMonitor.interval }}
interval: {{ .Values.serviceMonitor.interval }}
{{- end }}
{{- if .Values.serviceMonitor.metricRelabelings }}
metricRelabelings:
{{ tpl (toYaml .Values.serviceMonitor.metricRelabelings | nindent 6) . }}
{{- end }}
{{- if .Values.serviceMonitor.relabelings }}
relabelings:
{{ tpl (toYaml .Values.serviceMonitor.relabelings | nindent 6) . }}
{{- end }}
selector:
matchLabels:
{{- include "grafana-agent.selectorLabels" . | nindent 6 }}
{{- end }}
23 changes: 23 additions & 0 deletions operations/helm/charts/grafana-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,29 @@ service:
annotations: {}
# cloud.google.com/load-balancer-type: Internal

serviceMonitor:
enabled: false
# -- Additional labels for the service monitor.
additionalLabels: {}
# -- Scrape interval. If not set, the Prometheus default scrape interval is used.
interval: ""
# -- MetricRelabelConfigs to apply to samples after scraping, but before ingestion.
# ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#relabelconfig
metricRelabelings: []
# - action: keep
# regex: 'kube_(daemonset|deployment|pod|namespace|node|statefulset).+'
# sourceLabels: [__name__]

# -- RelabelConfigs to apply to samples before scraping
# ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#relabelconfig
relabelings: []
# - sourceLabels: [__meta_kubernetes_pod_node_name]
# separator: ;
# regex: ^(.*)$
# targetLabel: nodename
# replacement: $1
# action: replace

ingress:
# -- Enables ingress for the agent (faro port)
enabled: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# Source: grafana-agent/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
data:
config.river: |-
logging {
level = "info"
format = "logfmt"
}
discovery.kubernetes "pods" {
role = "pod"
}
discovery.kubernetes "nodes" {
role = "node"
}
discovery.kubernetes "services" {
role = "service"
}
discovery.kubernetes "endpoints" {
role = "endpoints"
}
discovery.kubernetes "endpointslices" {
role = "endpointslice"
}
discovery.kubernetes "ingresses" {
role = "ingress"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# Source: grafana-agent/templates/controllers/daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
spec:
minReadySeconds: 10
selector:
matchLabels:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
template:
metadata:
labels:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
spec:
serviceAccountName: grafana-agent
containers:
- name: grafana-agent
image: docker.io/grafana/agent:v0.36.1
imagePullPolicy: IfNotPresent
args:
- run
- /etc/agent/config.river
- --storage.path=/tmp/agent
- --server.http.listen-addr=0.0.0.0:80
env:
- name: AGENT_MODE
value: flow
- name: HOSTNAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
ports:
- containerPort: 80
name: http-metrics
readinessProbe:
httpGet:
path: /-/ready
port: 80
initialDelaySeconds: 10
timeoutSeconds: 1
volumeMounts:
- name: config
mountPath: /etc/agent
- name: config-reloader
image: docker.io/jimmidyson/configmap-reload:v0.8.0
args:
- --volume-dir=/etc/agent
- --webhook-url=http://localhost:80/-/reload
volumeMounts:
- name: config
mountPath: /etc/agent
resources:
requests:
cpu: 1m
memory: 5Mi
dnsPolicy: ClusterFirst
volumes:
- name: config
configMap:
name: grafana-agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
# Source: grafana-agent/templates/rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
rules:
# Rules which allow discovery.kubernetes to function.
- apiGroups:
- ""
- "discovery.k8s.io"
- "networking.k8s.io"
resources:
- endpoints
- endpointslices
- ingresses
- nodes
- nodes/proxy
- nodes/metrics
- pods
- services
verbs:
- get
- list
- watch
# Rules which allow loki.source.kubernetes and loki.source.podlogs to work.
- apiGroups:
- ""
resources:
- pods
- pods/log
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- "monitoring.grafana.com"
resources:
- podlogs
verbs:
- get
- list
- watch
# Rules which allow mimir.rules.kubernetes to work.
- apiGroups: ["monitoring.coreos.com"]
resources:
- prometheusrules
verbs:
- get
- list
- watch
- nonResourceURLs:
- /metrics
verbs:
- get
# Rules for prometheus.kubernetes.*
- apiGroups: ["monitoring.coreos.com"]
resources:
- podmonitors
- servicemonitors
- probes
verbs:
- get
- list
- watch
# Rules which allow eventhandler to work.
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
---
# Source: grafana-agent/templates/rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: grafana-agent
subjects:
- kind: ServiceAccount
name: grafana-agent
namespace: default
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
# Source: grafana-agent/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
spec:
type: ClusterIP
selector:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
ports:
- name: http-metrics
port: 80
targetPort: 80
protocol: "TCP"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
# Source: grafana-agent/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# Source: grafana-agent/templates/servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: grafana-agent
labels:
helm.sh/chart: grafana-agent
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent
app.kubernetes.io/version: "vX.Y.Z"
app.kubernetes.io/managed-by: Helm
spec:
endpoints:
- port: http-metrics
honorLabels: true
selector:
matchLabels:
app.kubernetes.io/name: grafana-agent
app.kubernetes.io/instance: grafana-agent

0 comments on commit 6d9ae6b

Please sign in to comment.