Merge "[DMAAP] DMaaP ServiceMesh compatibility"
[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 {{- if $dot.Values.metrics.serviceMonitor.labels }}
114 labels: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.labels "context" $dot) | nindent 2 }}
115 {{- else }}
116 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 }}
117 {{- end -}}
118 {{- end -}}
119
120 {{/*
121     Create service monitor template
122 */}}
123 {{- define "common.serviceMonitor" -}}
124 {{-   $dot := default . .dot -}}
125 {{-   $labels := default (dict) .labels -}}
126 apiVersion: monitoring.coreos.com/v1
127 kind: ServiceMonitor
128 metadata:
129 {{- include "common.serviceMonitorMetadata" $dot | nindent 2 }}
130 spec:
131   endpoints:
132   - path: {{ default "/metrics" $dot.Values.metrics.serviceMonitor.path }}
133     {{- if $dot.Values.metrics.serviceMonitor.port }}
134     port: {{ $dot.Values.metrics.serviceMonitor.port }}
135     {{- else if $dot.Values.metrics.serviceMonitor.targetPort }}
136     targetPort: {{ $dot.Values.metrics.serviceMonitor.targetPort }}
137     {{- else }}
138     port: tcp-metrics
139     {{- end }}
140     {{- if $dot.Values.metrics.serviceMonitor.isHttps }}
141     scheme: https
142     {{- if $dot.Values.metrics.serviceMonitor.tlsConfig }}
143     tlsConfig: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.tlsConfig "context" $dot) | nindent 6 }}
144     {{- else }}
145     tlsConfig:
146       insecureSkipVerify: true
147     {{- end }}
148     {{- end }}
149     {{- if $dot.Values.metrics.serviceMonitor.basicAuth.enabled }}
150     basicAuth:
151       username:
152         key: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretUserKey }}
153         {{- if $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretNameSuffix }}
154         name: {{ include "common.release" . }}-{{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretNameSuffix }}
155         {{- else }}
156         name: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretName }}
157         {{- end }}
158       password:
159         key: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretPasswordKey }}
160         {{- if $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretNameSuffix }}
161         name: {{ include "common.release" . }}-{{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretNameSuffix }}
162         {{- else }}
163         name: {{ $dot.Values.metrics.serviceMonitor.basicAuth.externalSecretName }}
164         {{- end }}
165     {{- end }}
166     {{- if $dot.Values.metrics.serviceMonitor.interval }}
167     interval: {{ $dot.Values.metrics.serviceMonitor.interval }}
168     {{- end }}
169     {{- if $dot.Values.metrics.serviceMonitor.scrapeTimeout }}
170     scrapeTimeout: {{ $dot.Values.metrics.serviceMonitor.scrapeTimeout }}
171     {{- end }}
172     {{- if $dot.Values.metrics.serviceMonitor.relabelings }}
173     relabelings: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.relabelings "context" $dot) | nindent 6 }}
174     {{- end }}
175     {{- if $dot.Values.metrics.serviceMonitor.metricRelabelings }}
176     metricRelabelings: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.metricRelabelings "context" $dot) | nindent 6 }}
177     {{- end }}
178   namespaceSelector:
179     matchNames:
180     - {{ include "common.namespace" $dot }}
181   selector:
182     {{- if $dot.Values.metrics.serviceMonitor.selector }}
183     matchLabels: {{- include "common.tplValue" ( dict "value" $dot.Values.metrics.serviceMonitor.selector "context" $dot) | nindent 6 }}
184     {{- else }}
185     matchLabels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 6 }}
186     {{- end }}
187 {{- end -}}