eb6c047c2f4f6c1ab4fa4ee215f886ad42f52f85
[oom.git] / kubernetes / common / common / templates / _serviceMonitor.tpl
1 {{/*
2 # Copyright © 2021 Bell Canada
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16 {{/*
17   Resolve the name of a chart's serviceMonitor.
18
19   The default will be the chart name (or $dot.Values.nameOverride if set).
20   And the use of .Values.metrics.serviceMonitor.name overrides all.
21
22   - .Values.metrics.serviceMonitor.name: override default serviceMonitor (ie. chart) name
23   Example values file addition:
24   metrics:
25     serviceMonitor:
26       enabled: true
27       port: blueprints-processor-http
28       ## specify target port if name is not given to the port in the service definition
29       ##
30       # targetPort: 8080
31       path: /metrics
32       basicAuth:
33         enabled: false
34         externalSecretName: mysecretname
35         externalSecretUserKey: login
36         externalSecretPasswordKey: password
37
38       ## Namespace in which Prometheus is running
39       ##
40       # namespace: monitoring
41
42       ## Interval at which metrics should be scraped.
43       ## ref: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#endpoint
44       ##
45       # interval: 60s
46
47       ## Timeout after which the scrape is ended
48       ## ref: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#endpoint
49       ##
50       # scrapeTimeout: 10s
51
52       ## ServiceMonitor selector labels
53       ## ref: https://github.com/bitnami/charts/tree/master/bitnami/prometheus-operator#prometheus-configuration
54       ##
55       selector:
56         app: '{{ include "common.name" . }}'
57         chart: '{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}'
58         release: '{{ include "common.release" . }}'
59         heritage: '{{ .Release.Service }}'
60
61       ## RelabelConfigs to apply to samples before scraping
62       ## ref: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#relabelconfig
63       ## Value is evalued as a template
64       ##
65       relabelings: []
66
67       ## MetricRelabelConfigs to apply to samples before ingestion
68       ## ref: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#relabelconfig
69       ## Value is evalued as a template
70       ##
71       metricRelabelings: []
72       #  - sourceLabels:
73       #      - "__name__"
74       #    targetLabel: "__name__"
75       #    action: replace
76       #    regex: '(.*)'
77       #    replacement: 'example_prefix_$1'
78
79 */}}
80 {{/*
81   Expand the serviceMonitor name for a chart.
82 */}}
83 {{- define "common.serviceMonitorName" -}}
84   {{- $name := default .Chart.Name .Values.nameOverride -}}
85   {{- default $name .Values.metrics.serviceMonitor.name | trunc 63 | trimSuffix "-" -}}
86 {{- end -}}
87
88 {{/* Define the metadata of serviceMonitor
89      The function takes from one to four arguments (inside a dictionary):
90      - .dot : environment (.)
91      - .suffix : a string which will be added at the end of the name (with a '-').
92      - .annotations: the annotations to add
93      - .labels : labels to add
94      Usage example:
95       {{ include "common.serviceMonitorMetadata" ( dict "suffix" "myService" "dot" .) }}
96       {{ include "common.serviceMonitorMetadata" ( dict "annotations" .Values.metrics.serviceMonitor.annotation "dot" .) }}
97 */}}
98
99 {{- define "common.serviceMonitorMetadata" -}}
100 {{-   $dot := default . .dot -}}
101 {{-   $annotations := default "" .annotations -}}
102 {{-   $labels := default (dict) .labels -}}
103 {{- if $annotations -}}
104 annotations:
105 {{    include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
106 {{- end }}
107 name: {{ include "common.serviceMonitorName" $dot }}
108 {{- if $dot.Values.metrics.serviceMonitor.namespace }}
109 namespace: {{ $dot.Values.metrics.serviceMonitor.namespace }}
110 {{- else }}
111 namespace: {{ include "common.namespace" $dot }}
112 {{- end }}
113 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 }}
114 {{- end -}}
115
116 {{/*
117     Create service monitor template
118 */}}
119 {{- define "common.serviceMonitor" -}}
120 {{-   $dot := default . .dot -}}
121 {{-   $labels := default (dict) .labels -}}
122 apiVersion: monitoring.coreos.com/v1
123 kind: ServiceMonitor
124 metadata:
125 {{- include "common.serviceMonitorMetadata" $dot | nindent 2 }}
126 spec:
127   endpoints:
128   - path: {{ default "/metrics" $dot.Values.metrics.serviceMonitor.path }}
129     {{- if $dot.Values.metrics.serviceMonitor.port }}
130     port: {{ $dot.Values.metrics.serviceMonitor.port }}
131     {{- else if $dot.Values.metrics.serviceMonitor.targetPort }}
132     targetPort: {{ $dot.Values.metrics.serviceMonitor.targetPort }}
133     {{- else }}
134     port: metrics
135     {{- end }}
136     {{- if $dot.Values.metrics.serviceMonitor.basicAuth.enabled }}
137     basicAuth:
138       username:
139         key: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretUserKey }}
140         name: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretName }}
141       password:
142         key: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretPasswordKey }}
143         name: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretName }}
144     {{- end }}
145     {{- if $dot.Values.metrics.serviceMonitor.interval }}
146     interval: {{ $dot.Values.metrics.serviceMonitor.interval }}
147     {{- end }}
148     {{- if $dot.Values.metrics.serviceMonitor.scrapeTimeout }}
149     scrapeTimeout: {{ $dot.Values.metrics.serviceMonitor.scrapeTimeout }}
150     {{- end }}
151     {{- if $dot.Values.metrics.serviceMonitor.relabelings }}
152     relabelings: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.relabelings "context" $dot) | nindent 6 }}
153     {{- end }}
154     {{- if $dot.Values.metrics.serviceMonitor.metricRelabelings }}
155     metricRelabelings: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.metricRelabelings "context" $dot) | nindent 6 }}
156     {{- end }}
157   namespaceSelector:
158     matchNames:
159     - {{ include "common.namespace" $dot }}
160   selector:
161     {{- if $dot.Values.metrics.serviceMonitor.selector }}
162     matchLabels: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.selector "context" $dot) | nindent 6 }}
163     {{- else }}
164     matchLabels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 6 }}
165     {{- end }}
166 {{- end -}}