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

{{/*
Create the name of the service account to use
*/}}
{{- define "stackdriver-exporter.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "stackdriver-exporter.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "stackdriver-exporter.fullname" . }}
  labels:
    chart: {{ template "stackdriver-exporter.chart" . }}
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "stackdriver-exporter.name" . }}
      release: "{{ .Release.Name }}"
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ template "stackdriver-exporter.name" . }}
        release: "{{ .Release.Name }}"
{{- if .Values.annotations }}
      annotations:
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
    spec:
{{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
      serviceAccount: {{ template "stackdriver-exporter.serviceAccountName" . }}
      restartPolicy: {{ .Values.restartPolicy }}
      volumes:
      {{- if .Values.stackdriver.serviceAccountSecret }}
        - name: stackdriver-service-account
          secret:
            secretName: {{ .Values.stackdriver.serviceAccountSecret | quote }}
      {{- else if .Values.stackdriver.serviceAccountKey }}
        - name: stackdriver-service-account
          secret:
            secretName: {{ template "stackdriver-exporter.fullname" . }}
      {{- end}}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["stackdriver_exporter"]
          volumeMounts:
          {{- if or .Values.stackdriver.serviceAccountSecret .Values.stackdriver.serviceAccountKey }}
            - name: stackdriver-service-account
              mountPath: /etc/secrets/service-account/
          {{- end}}
          env:
          {{- if or .Values.stackdriver.serviceAccountSecret .Values.stackdriver.serviceAccountKey }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /etc/secrets/service-account/credentials.json
          {{- end }}
            - name: STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID
              value: {{ .Values.stackdriver.projectId | quote }}
            - name: STACKDRIVER_EXPORTER_MONITORING_METRICS_TYPE_PREFIXES
              value: {{ .Values.stackdriver.metrics.typePrefixes | quote }}
            - name: STACKDRIVER_EXPORTER_MONITORING_METRICS_INTERVAL
              value: {{ .Values.stackdriver.metrics.interval | quote }}
            - name: STACKDRIVER_EXPORTER_MONITORING_METRICS_OFFSET
              value: {{ .Values.stackdriver.metrics.offset | quote }}
            - name: STACKDRIVER_EXPORTER_WEB_LISTEN_ADDRESS
              value: {{ .Values.web.listenAddress | quote }}
            - name: STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH
              value: {{ .Values.web.path | quote }}
            - name: STACKDRIVER_EXPORTER_MAX_RETRIES
              value: {{ .Values.stackdriver.maxRetries | quote }}
            - name: STACKDRIVER_EXPORTER_HTTP_TIMEOUT
              value: {{ .Values.stackdriver.httpTimeout | quote }}
            - name: STACKDRIVER_EXPORTER_MAX_BACKOFF_DURATION
              value: {{ .Values.stackdriver.maxBackoff | quote }}
            - name: STACKDRIVER_EXPORTER_BACKODFF_JITTER_BASE
              value: {{ .Values.stackdriver.backoffJitter | quote }}
            - name: STACKDRIVER_EXPORTER_RETRY_STATUSES
              value: {{ .Values.stackdriver.retryStatuses | quote}}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          ports:
            - containerPort: {{ .Values.service.httpPort }}
              name: http
          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 30
            timeoutSeconds: 10
          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            timeoutSeconds: 10
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
{{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}

 # secret.yaml
{{- if .Values.stackdriver.serviceAccountKey -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "stackdriver-exporter.fullname" . }}
  labels:
    chart: {{ template "stackdriver-exporter.chart" . }}
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: Opaque
data:
  credentials.json: {{ .Values.stackdriver.serviceAccountKey | b64enc | quote }}
{{- end }}
 # service.yaml
kind: Service
apiVersion: v1
metadata:
  name: {{ template "stackdriver-exporter.fullname" . }}
  labels:
    chart: {{ template "stackdriver-exporter.chart" . }}
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - name: http
      port: {{ .Values.service.httpPort }}
      protocol: TCP
  selector:
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    chart: {{ template "stackdriver-exporter.chart" . }}
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  name: {{ template "stackdriver-exporter.serviceAccountName" . }}
  {{- if .Values.serviceAccount.annotations }}
  annotations:
{{ toYaml .Values.serviceAccount.annotations | indent 4 }}
  {{- end }}
{{- end -}}
 # servicemonitor.yaml
{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "stackdriver-exporter.name" .  }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
  labels:
    chart: {{ template "stackdriver-exporter.chart" . }}
    app: {{ template "stackdriver-exporter.name" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    {{- if .Values.serviceMonitor.additionalLabels }}
{{ toYaml .Values.serviceMonitor.additionalLabels | indent 4 }}
    {{- end }}
spec:
  endpoints:
  - port: http
    {{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
    {{- end }}
    honorLabels: {{ .Values.serviceMonitor.honorLabels }}
  selector:
    matchLabels:
      chart: {{ template "stackdriver-exporter.chart" . }}
      app: {{ template "stackdriver-exporter.name" . }}
      release: "{{ .Release.Name }}"
      heritage: "{{ .Release.Service }}"
{{- end }}

# Number of exporters to run
replicaCount: 1

# Restart policy for container
restartPolicy: Always

image:
  repository: frodenas/stackdriver-exporter
  tag: v0.6.0
  pullPolicy: IfNotPresent

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

service:
  type: ClusterIP
  httpPort: 9255
  annotations: {}

stackdriver:
  # The Google Project ID to gather metrics for
  projectId: "FALSE"
  # An existing secret which contains credentials.json
  serviceAccountSecret: ""
  # A service account key JSON file. Must be provided when no existing secret is used, in this case a new secret will be created holding this service account
  serviceAccountKey: ""
  # Max number of retries that should be attempted on 503 errors from Stackdriver
  maxRetries: 0
  # How long should Stackdriver_exporter wait for a result from the Stackdriver API
  httpTimeout: 10s
  # Max time between each request in an exp backoff scenario
  maxBackoff: 5s
  # The amount of jitter to introduce in an exp backoff scenario
  backoffJitter: 1s
  # The HTTP statuses that should trigger a retry
  retryStatuses: 503
  metrics:
    # The prefixes to gather metrics for, we default to just CPU metrics.
    typePrefixes: 'compute.googleapis.com/instance/cpu'
    # The frequency to request
    interval: '5m'
    # How far into the past to offset
    offset: '0s'

web:
  # Port to listen on
  listenAddress: ':9255'
  # Path under which to expose metrics.
  path: /metrics

## Pod affinity
##
affinity: {}

annotations: {}

## Node labels for stackdriver-exporter pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
##
nodeSelector: {}

## Node tolerations for stackdriver-exporter scheduling to nodes with taints
## Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
##
tolerations: []
  # - key: "key"
  #   operator: "Equal|Exists"
  #   value: "value"
  #   effect: "NoSchedule|PreferNoSchedule|NoExecute(1.6 only)"


## Service Account
##
serviceAccount:
  # Specifies whether a ServiceAccount should be created
  create: false
  # The name of the ServiceAccount to use.
  # If not set and create is false, 'default' is used
  # If not set and create is true, a name is generated using the fullname template
  name:
  annotations: {}

# Enable this if you're using https://github.com/coreos/prometheus-operator
serviceMonitor:
  enabled: false
  namespace: monitoring
  # additionalLabels is the set of additional labels to add to the ServiceMonitor
  additionalLabels: {}
  # fallback to the prometheus default unless specified
  # interval: 10s
  ## Defaults to what's used if you follow CoreOS [Prometheus Install Instructions](https://github.com/helm/charts/tree/master/stable/prometheus-operator#tldr)
  honorLabels: true
