# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "uchiwa.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 "uchiwa.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 "uchiwa.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | 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 "sensu.fullname" -}}
{{- printf "%s-%s" .Release.Name "sensu" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "uchiwa.fullname" . }}
  labels:
    app: {{ template "uchiwa.name" . }}
    chart: {{ template "uchiwa.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}

data:
  uchiwa.json : |-
{{- if .Values.config }}
{{ .Values.config | indent 6 }}
{{- else }}
    {
      "sensu": [
      {
        "name": "Sensu",
        "host": "{{ .Release.Name }}-sensu",
        "port": {{ .Values.sensu.httpPort }}
      }
      ],
      "uchiwa": {
        "host": "0.0.0.0",
        "port": {{ .Values.port }},
        "refresh": 10,
        "loglevel": "info"
      }
    }
{{- end }}

 # deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "uchiwa.fullname" . }}
  labels:
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "uchiwa.fullname" . }}
        release: {{ .Release.Name | quote }}
    spec:
      containers:
      - name: uchiwa
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: {{ .Values.pullPolicy }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        env:
          - name: UCHIWA_BIND
            value: {{ .Values.host | quote }}
          - name: UCHIWA_SERVICE_PORT
            value: {{ .Values.port | quote }}
          - name: UCHIWA_REFRESH
            value: {{ .Values.refresh | quote }}
          - name: UCHIWA_LOG_LEVEL
            value: {{ .Values.loglevel | quote }}
          - name: SENSU_DC_NAME
            value: "Sensu"
          - name: SENSU_HOSTNAME
            value: {{ template "sensu.fullname" . }}
        livenessProbe:
          httpGet:
            path: /
            port: {{ .Values.port }}
        readinessProbe:
          httpGet:
            path: /
            port: {{ .Values.port }}
        ports:
        - containerPort: {{ .Values.port }}
        volumeMounts:
          - mountPath: /etc/sensu/
            name: uchiwa-config
      volumes:
        - configMap:
            defaultMode: 420
            name: {{ template "uchiwa.fullname" . }}
          name: uchiwa-config
 # svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "uchiwa.fullname" . }}
  labels:
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  {{ if .Values.deis.routable }}
    router.deis.io/routable: "true"
  annotations:
    router.deis.io/domains: {{ .Values.deis.domains | quote }}
  {{ end }}
spec:
  type: {{ .Values.serviceType }}
  ports:
  - port: {{ .Values.httpPort }}
    targetPort: {{ .Values.port }}
  selector:
    app: {{ template "uchiwa.fullname" . }}

# Docker image name
image: "sstarcher/uchiwa"
# Docker image tag
imageTag: "0.22"

# Image pull policy for the container
pullPolicy: "IfNotPresent"

# How many uchiwa containers to spawn
replicaCount: 1

# How to publish the service http://kubernetes.io/docs/user-guide/services/#publishing-services---service-types
serviceType: ClusterIP

# Service port to expose Uchiwa on
httpPort: 80

# If set to true, the service will be exposed via the Deis Router if setup https://github.com/deis/router
deis:
  routable: false
  domains: uchiwa

# CPU and Memory limit and request for Uchiwa
resources:
  limits:
    memory: 50Mi
  requests:
    memory: 50Mi
    cpu: 10m

# Uchiwa configuration https://docs.uchiwa.io/getting-started/configuration/
host: '0.0.0.0'
port: 3000
refresh: 10
loglevel: info

config: {}
# Example config input below:
# config: |-
#   {
#     "sensu": [
#     {
#       "name": "Sensu",
#       "host": "YOUR-SENSU-HOST",
#       "port": UNQUOTED-SENSU-PORT
#     }
#     ],
#     "uchiwa": {
#       "host": "0.0.0.0",
#       "port": UNQUOTED-UCHIWA-PORT,
#       "refresh": 10,
#       "loglevel": "info"
#     }
#   }
