# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "helm-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 "helm-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 "helm-exporter.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "helm-exporter.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "helm-exporter.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "helm-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    helm.sh/chart: {{ include "helm-exporter.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.annotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
{{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.tillerNamespaces }}
          args: ["-tiller-namespaces", {{ .Values.tillerNamespaces | quote }}]
          {{- end }}
          ports:
            - name: http
              containerPort: 9571
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: http
          readinessProbe:
            httpGet:
              path: /healthz
              port: http
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.rbac.create }}
      serviceAccountName: {{ template "helm-exporter.fullname" . }}
    {{- else }}
      serviceAccountName: "{{ .Values.rbac.serviceAccountName }}"
    {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled }}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ include "helm-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    helm.sh/chart: {{ include "helm-exporter.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  rules:
    - host: {{ .Values.ingress.host }}
      http:
        paths:
          - path: {{ .Values.ingress.path }}
            backend:
              serviceName: {{ include "helm-exporter.name" . }}
              servicePort: 9571
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "helm-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    helm.sh/chart: {{ include "helm-exporter.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- if not .Values.serviceMonitor.create }}
  annotations:
    prometheus.io/scrape: "true"
  {{- end }}
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 9571
      targetPort: http
      protocol: TCP
      name: metrics
  selector:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    helm.sh/chart: {{ include "helm-exporter.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  name: {{ template "helm-exporter.serviceAccountName" . }}
{{- end }}
 # servicemonitor.yaml
{{ if .Values.serviceMonitor.create }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "helm-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "helm-exporter.chart" . }}
    {{- range $key, $value := .Values.serviceMonitor.additionalLabels }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
  {{- with .Values.serviceMonitor.namespace }}
  namespace: {{ . }}
  {{- end }}
spec:
  endpoints:
  - port: metrics
    honorLabels: true
    {{- with .Values.serviceMonitor.interval }}
    interval: {{ . }}
    {{- end }}
    {{- with .Values.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ . }}
    {{- end }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "helm-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

replicaCount: 1

image:
  repository: sstarcher/helm-exporter
  tag: 0.4.2
  pullPolicy: Always

# To override default tiller namespace name or to provide the multiple tiller namespaces like "kube-system,dev"
tillerNamespaces: ""

nameOverride: ""
fullnameOverride: ""

resources: {}

nodeSelector: {}

tolerations: []

affinity: {}

imagePullSecrets:
# - name: "image-pull-secret"

rbac:
  # Specifies whether RBAC resources should be created
  create: true

serviceAccount:
  # Specifies whether a ServiceAccount should be created
  create: true
  # The name of the ServiceAccount to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

serviceMonitor:
  # Specifies whether a ServiceMonitor should be created
  create: false
  interval:
  scrapeTimeout:
  namespace:
  additionalLabels: {}

ingress:
  # Specifies whether an Ingress should be created
  enabled: false

  annotations: {}
  # kubernetes.io/ingress.class: nginx
  path: /
  # Name of host e.g example.com
  host:
