# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "prometheus-mongodb-exporter.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "prometheus-mongodb-exporter.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "prometheus-mongodb-exporter.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "prometheus-mongodb-exporter.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "prometheus-mongodb-exporter.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Determine secret name, can either be the self-created of an existing one
*/}}
{{- define "prometheus-mongodb-exporter.secretName" -}}
{{- if .Values.existingSecret.name -}}
    {{- .Values.existingSecret.name -}}
{{- else -}}
    {{ include "prometheus-mongodb-exporter.fullname" . }}
{{- end -}}
{{- end -}} # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "prometheus-mongodb-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
  annotations:
    {{- toYaml .Values.annotations | nindent 4 }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
          {{- toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
      labels:
        app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "prometheus-mongodb-exporter.serviceAccountName" . }}
      containers:
      - name: mongodb-exporter
        env:
          - name: MONGODB_URI
            valueFrom:
              secretKeyRef:
                name: {{ include "prometheus-mongodb-exporter.secretName" . }}
                key: {{ .Values.existingSecret.key }}
        {{- if .Values.env }}
        {{- range $key, $value := .Values.env }}
          - name: "{{ $key }}"
            value: "{{ $value }}"
        {{- end }}
        {{- end }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - --web.listen-address={{ printf ":%s" .Values.port }}
        {{- toYaml .Values.extraArgs | nindent 8 }}
        ports:
        - name: metrics
          containerPort: {{ .Values.port }}
          protocol: TCP
        livenessProbe:
          {{- toYaml .Values.livenessProbe | nindent 10 }}
        readinessProbe:
          {{- toYaml .Values.readinessProbe | nindent 10 }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 10 }}
      affinity:
        {{- toYaml .Values.affinity | nindent 8 }}
      imagePullSecrets:
        {{- toYaml .Values.imagePullSecrets | nindent 8 }}
      nodeSelector:
        {{- toYaml .Values.nodeSelector | nindent 8 }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      terminationGracePeriodSeconds: 30
      tolerations:
        {{- toYaml .Values.tolerations | nindent 8 }}
 # secret.yaml
{{- if not .Values.existingSecret.name -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "prometheus-mongodb-exporter.secretName" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
type: Opaque
data:
  {{ .Values.existingSecret.key }} : {{ required "A MongoDB URI is required" .Values.mongodb.uri | b64enc }}
{{- end -}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "prometheus-mongodb-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
{{- if .Values.service.labels }}
{{ toYaml .Values.service.labels | indent 4 }}
{{- end }}
  annotations:
    {{- toYaml .Values.service.annotations | nindent 4 }}
spec:
  ports:
    - port: {{ .Values.service.port }}
      targetPort: metrics
      protocol: TCP
      name: metrics
  selector:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
  type: {{ .Values.service.type }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "prometheus-mongodb-exporter.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
{{- end -}}
 # servicemonitor.yaml
{{ if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "prometheus-mongodb-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
    {{- range $key, $value := .Values.serviceMonitor.additionalLabels }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
spec:
  endpoints:
  - port: metrics
    interval: {{ .Values.serviceMonitor.interval }}
    scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
    {{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings: {{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 4 }}
    {{- end }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{- range .Values.serviceMonitor.targetLabels }}
    - {{ . }}
{{- end }}
{{- end }}
{{- end }}

 # test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "{{ include "prometheus-mongodb-exporter.fullname" . }}-test-connection"
  labels:
    app.kubernetes.io/name: {{ include "prometheus-mongodb-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-mongodb-exporter.chart" . }}
  annotations:
    "helm.sh/hook": test-success
spec:
  containers:
  - name: wget
    image: busybox
    command: ['wget']
    args:  ['-qO-', '{{ include "prometheus-mongodb-exporter.fullname" . }}:{{ .Values.service.port }}/metrics']
  restartPolicy: Never

affinity: {}

annotations: {}

extraArgs:
- --collect.collection
- --collect.database
- --collect.indexusage
- --collect.topmetrics
- --collect.connpoolstats

fullnameOverride: ""

image:
  pullPolicy: IfNotPresent
  repository: ssheehy/mongodb-exporter
  tag: 0.10.0

imagePullSecrets: []

livenessProbe:
  httpGet:
    path: /
    port: metrics
  initialDelaySeconds: 10

# [mongodb[+srv]://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
mongodb:
  uri: ""

# Name of an externally managed secret (in the same namespace) containing the connection uri as key `mongodb-uri`.
# If this is provided, the value mongodb.uri is ignored.
existingSecret:
  name: ""
  key: "mongodb-uri"

nameOverride: ""

nodeSelector: {}

podAnnotations: {}
#  prometheus.io/scrape: "true"
#  prometheus.io/port: "metrics"

port: "9216"

priorityClassName: ""

readinessProbe:
  httpGet:
    path: /
    port: metrics
  initialDelaySeconds: 10

replicas: 1

resources: {}
# limits:
#   cpu: 250m
#   memory: 192Mi
# requests:
#   cpu: 100m
#   memory: 128Mi

# Extra environment variables that will be passed into the exporter pod
env: {}

securityContext:
  allowPrivilegeEscalation: false
  capabilities:
    drop: ["all"]
  readOnlyRootFilesystem: true
  runAsGroup: 10000
  runAsNonRoot: true
  runAsUser: 10000

service:
  labels: {}
  annotations: {}
  port: 9216
  type: ClusterIP

serviceAccount:
  create: true
  # If create is true and name is not set, then a name is generated using the
  # fullname template.
  name:

serviceMonitor:
  enabled: true
  interval: 30s
  scrapeTimeout: 10s
  namespace:
  additionalLabels: {}
  targetLabels: []
  metricRelabelings: []

tolerations: []
