# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "swift.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 "swift.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" $name .Release.Name | trunc 63 -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "swift.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "swift.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # cluster-role-binding.yaml
{{ if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: {{ template "swift.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "swift.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "swift.fullname" . }}
subjects:
- kind: ServiceAccount
  name: {{ template "swift.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
{{ end }}
 # cluster-role.yaml
{{ if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: {{ template "swift.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "swift.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
rules:
- apiGroups: [""]
  resources: ["nodes", "services"]
  verbs: ["list"]
{{ end }}
 # deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "swift.fullname" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "swift.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "swift.name" . }}"
        release: "{{ .Release.Name }}"
    spec:
      serviceAccountName: {{ template "swift.serviceAccountName" . }}
      containers:
      - args:
        - run
        - --v={{ .Values.logLevel }}
        - --connector=incluster
        - --tiller-insecure-skip-verify=true
        image: {{ .Values.swift.image }}:{{ .Values.swift.tag }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        {{- if .Values.imagePullSecrets }}
        imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
        {{- end }}
        name: swift
        ports:
        - containerPort: 9855
          name: pt
          protocol: TCP
        - containerPort: 50055
          name: tls
          protocol: TCP
        - containerPort: 56790
          name: ops
          protocol: TCP
        volumeMounts:
            - mountPath: /tmp
              name: chart-volume
      volumes:
        - name: chart-volume
          emptyDir: {}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end -}}
 # service-account.yaml
{{ if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "swift.serviceAccountName" . }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "{{ template "swift.name" . }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
{{ end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "swift.fullname" . }}
  labels:
    app: "{{ template "swift.name" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  ports:
  - name: pt
    port: 9855
    targetPort: pt
  - name: tls
    port: 50055
    targetPort: tls
  - name: ops
    port: 56790
    targetPort: ops
  selector:
    app: "{{ template "swift.name" . }}"
    release: "{{ .Release.Name }}"

# Default values for swift.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
swift:
  image: appscode/swift
  tag: 0.7.3
## Optionally specify an array of imagePullSecrets.
## Secrets must be manually created in the namespace.
## ref: https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod
##
# imagePullSecrets:
#   - name: myRegistryKeySecretName
## Specify a imagePullPolicy
## ref: http://kubernetes.io/docs/user-guide/images/#pre-pulling-images
##
imagePullPolicy: IfNotPresent
## Node labels for pod assignment
## Ref: https://kubernetes.io/docs/user-guide/node-selection/
##
nodeSelector: {}
## Log level for proxy
logLevel: 3

## Install Default RBAC roles and bindings
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:
