# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "chronograf.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).
*/}}
{{- define "chronograf.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "chronograf.fullname" . }}
  labels:
    app: {{ template "chronograf.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.service.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "chronograf.fullname" . }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
{{- range $key, $value := .Values.env }}
        - name: "{{ $key }}"
          value: "{{ $value }}"
{{- end }}
{{- if .Values.oauth.enabled }}
        - name: TOKEN_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: token_secret
{{- if .Values.oauth.github.enabled }}
        - name: GH_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: gh_client_id
        - name: GH_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: gh_client_secret
        - name: GH_ORGS
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: gh_orgs
{{- end }}
{{- if .Values.oauth.heroku.enabled }}
        - name: HEROKU_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: he_client_id
        - name: HEROKU_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: he_client_secret
        - name: HEROKU_ORGS
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: he_orgs
{{- end }}
{{- if .Values.oauth.google.enabled }}
        - name: GOOGLE_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: go_client_id
        - name: GOOGLE_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: go_client_secret
        - name: GOOGLE_DOMAINS
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: go_domains
        - name: PUBLIC_URL
          valueFrom:
            secretKeyRef:
              name: {{ template "chronograf.fullname" . }}
              key: go_public_url
{{- end }}
{{- end }}
{{- if .Values.envFromSecret }}
        envFrom:
        - secretRef:
            name: {{ .Values.envFromSecret }}
{{- end }}
        ports:
        - containerPort: 8888
          name: api
        livenessProbe:
          httpGet:
            path: /ping
            port: api
        readinessProbe:
          httpGet:
            path: /ping
            port: api
        volumeMounts:
        - name: data
          mountPath: /var/lib/chronograf
        resources:
{{ toYaml .Values.resources | indent 10 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "chronograf.fullname" . }}
        {{ else }}
          emptyDir: {}
        {{ end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ template "chronograf.fullname" . }}
  labels:
    app: {{ template "chronograf.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  annotations:
{{ toYaml .Values.ingress.annotations | indent 4 }}
spec:
{{- if .Values.ingress.tls }}
  tls:
    - hosts:
      - {{ .Values.ingress.hostname | quote }}
      secretName: {{ .Values.ingress.secretName | default (printf "%s-tls" (include "chronograf.fullname" .)) }}
{{- end }}
  rules:
  - host: {{ .Values.ingress.hostname }}
    http:
      paths:
      - path: /
        backend:
          serviceName: {{ template "chronograf.fullname" . }}
          servicePort: 80
{{- end -}} # pvc.yaml
{{- if .Values.persistence.enabled -}}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ template "chronograf.fullname" . }}
  labels:
    app: {{ template "chronograf.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  accessModes:
  - {{ .Values.persistence.accessMode | quote }}
  resources:
    requests:
      storage: {{ .Values.persistence.size | quote }}
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- end }}
 # secret.yaml
{{- if .Values.oauth.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "chronograf.fullname" . }}
type: Opaque
data:
  token_secret: {{ .Values.oauth.token_secret | b64enc | quote }}
{{- if .Values.oauth.github.enabled }}
  gh_client_id: {{ .Values.oauth.github.client_id | b64enc | quote }}
  gh_client_secret: {{ .Values.oauth.github.client_secret | b64enc | quote }}
  gh_orgs: {{ .Values.oauth.github.gh_orgs | b64enc | quote }}
{{- end }}
{{- if .Values.oauth.google.enabled }}
  go_client_id: {{ .Values.oauth.google.client_id | b64enc | quote }}
  go_client_secret: {{ .Values.oauth.google.client_secret | b64enc | quote }}
  go_domains: {{ .Values.oauth.google.domains | b64enc | quote }}
  go_public_url: {{ .Values.oauth.google.public_url | b64enc | quote }}
{{- end }}
{{- if .Values.oauth.heroku.enabled }}
  he_client_id: {{ .Values.oauth.heroku.client_id | b64enc | quote }}
  he_client_secret: {{ .Values.oauth.heroku.client_secret | b64enc | quote }}
  he_orgs: {{ .Values.oauth.heroku.heroku_orgs | b64enc | quote }}
{{- end }}
{{- end }} # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "chronograf.fullname" . }}
  labels:
    app: {{ template "chronograf.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  type: {{ .Values.service.type }}
  ports:
  - port: 80
    targetPort: 8888
  selector:
    app: {{ template "chronograf.fullname" . }}

## Image Settings
##
image:
  repository: quay.io/influxdb/chronograf
  tag: 1.7.12
  pullPolicy: IfNotPresent

## Specify a service type
## ClusterIP is default
## ref: http://kubernetes.io/docs/user-guide/services/
##
service:
  replicas: 1
  type: ClusterIP

## Persist data to a persistent volume
##
persistence:
  enabled: false
  ## chronograf data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # storageClass: "-"
  accessMode: ReadWriteOnce
  size: 8Gi

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    memory: 256Mi
    cpu: 0.1
  limits:
    memory: 2Gi
    cpu: 2

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

 ## Tolerations for pod assignment
## ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
##
tolerations: []

 ## Affinity for pod assignment
## ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
##
affinity: {}

## Configure the ingress object to hook into existing infastructure
## ref : http://kubernetes.io/docs/user-guide/ingress/
## OPTIONALLY you can set .Values.ingress.secretName to set which secret to use
##
ingress:
  enabled: false
  tls: false
  hostname: chronograf.foobar.com
  annotations:
    # kubernetes.io/ingress.class: "nginx"
    # secretName: my-tls-cert
    # kubernetes.io/tls-acme: "true"

## OAuth Settings for OAuth Providers
## More information -> https://github.com/influxdata/chronograf/blob/master/docs/auth.md
##
oauth:
  # Need to set to true to use any of the oauth options
  enabled: false
  # Used for JWT to support running multiple copies of Chronograf
  token_secret: CHANGE_ME
  github:
    enabled: false
    client_id: CHANGE_ME
    client_secret: CHANGE_ME
    # This is a comma separated list of GH organizations (OPTIONAL)
    gh_orgs: ""
  google:
    enabled: false
    client_id: CHANGE_ME
    client_secret: CHANGE_ME
    # eg. http://chronograf.foobar.com
    public_url: ""
    # This is a comma separated list of Google Apps domains (OPTIONAL)
    domains: ""
  heroku:
    enabled: false
    client_id: CHANGE_ME
    client_secret: CHANGE_ME
    # This is a comma separated list of Heroku organizations (OPTIONAL)
    he_orgs: ""

## Extra environment variables that will be passed onto deployment pods
env: {}

## The name of a secret in the same kubernetes namespace which contain values to be added to the environment
## This can be useful for auth tokens, etc
envFromSecret: ""
