Skip to content

Commit 0a924b4

Browse files
kempsponineinchnick
authored andcommitted
Add ServiceMonitor support
1 parent 75c517f commit 0a924b4

File tree

7 files changed

+112
-9
lines changed

7 files changed

+112
-9
lines changed

charts/trino/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,15 @@ Fast distributed SQL query engine for big data analytics that helps you explore
640640
value: '$2'
641641
help: 'ThreadCount (java.lang<type=Threading><>ThreadCount)'
642642
type: UNTYPED
643+
* `serviceMonitor.enabled` - bool, default: `false`
644+
645+
Set to true to create resources for the [prometheus-operator](https://github.com/prometheus-operator/prometheus-operator).
646+
* `serviceMonitor.labels` - object, default: `{"prometheus":"kube-prometheus"}`
647+
648+
Labels for serviceMonitor, so that Prometheus can select it
649+
* `serviceMonitor.interval` - string, default: `"30s"`
650+
651+
The serviceMonitor web endpoint interval
643652
* `commonLabels` - object, default: `{}`
644653

645654
Labels that get applied to every resource's metadata
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{- if .Values.serviceMonitor.enabled -}}
2+
apiVersion: monitoring.coreos.com/v1
3+
kind: ServiceMonitor
4+
metadata:
5+
name: {{ template "trino.fullname" . }}
6+
namespace: {{ .Release.Namespace }}
7+
labels:
8+
{{- include "trino.labels" . | nindent 4 }}
9+
{{- if .Values.serviceMonitor.labels }}
10+
{{- toYaml .Values.serviceMonitor.labels | nindent 4 }}
11+
{{- end }}
12+
spec:
13+
selector:
14+
matchLabels:
15+
{{- include "trino.selectorLabels" . | nindent 6 }}
16+
namespaceSelector:
17+
matchNames:
18+
- {{ .Release.Namespace }}
19+
endpoints:
20+
- port: jmx-exporter
21+
interval: {{ .Values.serviceMonitor.interval }}
22+
{{- end }}

charts/trino/templates/tests/test-connection.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ metadata:
77
app.kubernetes.io/component: test
88
test: connection
99
annotations:
10-
"helm.sh/hook": test-success
10+
"helm.sh/hook": test
1111
spec:
1212
containers:
1313
- name: cli
@@ -25,8 +25,8 @@ spec:
2525
- --password
2626
{{- end }}
2727
- --debug
28-
- --network-logging=BODY
2928
- --execute=SELECT 1
29+
- --no-progress
3030
{{- if eq .Values.server.config.authenticationType "PASSWORD" }}
3131
env:
3232
- name: TRINO_PASSWORD

charts/trino/templates/tests/test-jmx.yaml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.jmx.exporter.enabled }}
1+
{{- if or .Values.jmx.exporter.enabled .Values.serviceMonitor.enabled }}
22
apiVersion: v1
33
kind: Pod
44
metadata:
@@ -8,7 +8,7 @@ metadata:
88
app.kubernetes.io/component: test
99
test: jmx
1010
annotations:
11-
"helm.sh/hook": test-success
11+
"helm.sh/hook": test
1212
spec:
1313
containers:
1414
{{- if .Values.jmx.exporter.enabled }}
@@ -18,5 +18,50 @@ spec:
1818
args:
1919
- curl -s {{ include "trino.fullname" . }}.{{ .Release.Namespace }}:5556 | grep -q trino
2020
{{- end }}
21+
{{- if .Values.serviceMonitor.enabled }}
22+
- name: service-monitor
23+
image: python:3-slim
24+
command: ["python", "/tests/test.py"]
25+
volumeMounts:
26+
- name: tests
27+
mountPath: /tests
28+
{{- end }}
29+
volumes:
30+
- name: tests
31+
configMap:
32+
name: {{ include "trino.fullname" . }}-test-jmx
2133
restartPolicy: Never
34+
---
35+
apiVersion: v1
36+
kind: ConfigMap
37+
metadata:
38+
name: {{ include "trino.fullname" . }}-test-jmx
39+
labels:
40+
{{- include "trino.labels" . | nindent 4 }}
41+
app.kubernetes.io/component: test
42+
test: jmx
43+
data:
44+
test.py: |
45+
from urllib.request import urlopen
46+
from urllib.error import URLError, HTTPError
47+
import json
48+
49+
url = "http://prometheus-operator-kube-p-prometheus:9090/api/v1/targets?scrapePool=serviceMonitor/{{ .Release.Namespace }}/{{ include "trino.fullname" . }}/0&state=active"
50+
while True:
51+
try:
52+
with urlopen(url) as response:
53+
data = json.load(response)
54+
except (URLError, HTTPError) as e:
55+
print("Error fetching targets, Prometheus service might not be ready: ", e)
56+
continue
57+
58+
try:
59+
service_name = data["data"]["activeTargets"][0]["discoveredLabels"]["__meta_kubernetes_service_name"]
60+
except (KeyError, IndexError) as e:
61+
print("Invalid Prometheus response: ", e)
62+
continue
63+
64+
if service_name == "{{ include "trino.fullname" . }}":
65+
print("Found expected service in Prometheus targets")
66+
break
2267
{{- end }}

charts/trino/values.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,15 @@ jmx:
763763
# help: 'ThreadCount (java.lang<type=Threading><>ThreadCount)'
764764
# type: UNTYPED
765765

766+
serviceMonitor:
767+
# serviceMonitor.enabled -- Set to true to create resources for the [prometheus-operator](https://github.com/prometheus-operator/prometheus-operator).
768+
enabled: false
769+
# serviceMonitor.labels -- Labels for serviceMonitor, so that Prometheus can select it
770+
labels:
771+
prometheus: kube-prometheus
772+
# serviceMonitor.interval -- The serviceMonitor web endpoint interval
773+
interval: "30s"
774+
766775
# -- Labels that get applied to every resource's metadata
767776
commonLabels: {}
768777

test-values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ jmx:
156156
- pattern: 'trino.memory*'
157157
- pattern: 'trino.execution<name=QueryManager>*'
158158
159+
serviceMonitor:
160+
enabled: true
161+
selector:
162+
prometheus: default
163+
interval: "30s"
164+
159165
ingress:
160166
enabled: true
161167
hosts:

test.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function join_by {
1919
# default to randomly generated namespace, same as chart-testing would do, but we need to load secrets into the same namespace
2020
NAMESPACE=trino-$(LC_ALL=C tr -dc 'a-z0-9' </dev/urandom | head -c 6 || true)
2121
HELM_EXTRA_SET_ARGS=
22-
CT_ARGS=(--charts=charts/trino --skip-clean-up)
22+
CT_ARGS=(--charts=charts/trino --skip-clean-up --helm-extra-args="--timeout 2m")
2323
CLEANUP_NAMESPACE=true
2424
TEST_NAMES=(default single_node complete_values)
2525

@@ -71,8 +71,18 @@ openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
7171
-addext "subjectAltName=DNS:localhost,DNS:*.$NAMESPACE,DNS:*.$NAMESPACE.svc,DNS:*.$NAMESPACE.svc.cluster.local,IP:127.0.0.1" \
7272
-keyout cert.key -out cert.crt
7373

74-
kubectl create namespace "$NAMESPACE"
75-
kubectl -n "$NAMESPACE" create secret tls certificates --cert=cert.crt --key=cert.key
74+
kubectl create namespace "$NAMESPACE" --dry-run=client --output yaml | kubectl apply --filename -
75+
kubectl -n "$NAMESPACE" create secret tls certificates --cert=cert.crt --key=cert.key --dry-run=client --output yaml | kubectl apply --filename -
76+
77+
# only install the Prometheus Helm chart when running the `complete_values` test
78+
if printf '%s\0' "${TEST_NAMES[@]}" | grep -qwz complete_values; then
79+
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
80+
helm upgrade --install prometheus-operator prometheus-community/kube-prometheus-stack -n "$NAMESPACE" \
81+
--version "60.0.2" \
82+
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false \
83+
--set grafana.enabled=false
84+
kubectl rollout status --watch deployments -l release=prometheus-operator -n "$NAMESPACE"
85+
fi
7686

7787
CT_ARGS+=(--namespace "$NAMESPACE")
7888

@@ -84,21 +94,23 @@ for test_name in "${TEST_NAMES[@]}"; do
8494
if ! time ct install "${CT_ARGS[@]}" --helm-extra-set-args "$HELM_EXTRA_SET_ARGS ${testCases[$test_name]}"; then
8595
echo 1>&2 "❌ Test $test_name failed"
8696
echo 1>&2 "Test logs:"
87-
kubectl --namespace "$NAMESPACE" logs --tail=-1 --selector app.kubernetes.io/component=test
97+
kubectl --namespace "$NAMESPACE" logs --tail=-1 --selector app.kubernetes.io/component=test --all-containers=true
8898
result=1
8999
else
90100
echo 1>&2 "✅ Test $test_name completed"
91101
fi
92102
if [ "$CLEANUP_NAMESPACE" == "true" ]; then
93-
for release in $(helm --namespace "$NAMESPACE" ls --all --short); do
103+
for release in $(helm --namespace "$NAMESPACE" ls --all --short | grep -v 'prometheus-operator'); do
94104
echo 1>&2 "Cleaning up Helm release $release"
95105
helm --namespace "$NAMESPACE" delete "$release"
96106
done
97107
fi
98108
done
99109

100110
if [ "$CLEANUP_NAMESPACE" == "true" ]; then
111+
helm -n "$NAMESPACE" uninstall prometheus-operator --ignore-not-found
101112
kubectl delete namespace "$NAMESPACE"
113+
kubectl delete crd $(kubectl api-resources --api-group=monitoring.coreos.com --output name)
102114
fi
103115

104116
exit $result

0 commit comments

Comments
 (0)