# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "gcloud-sqlproxy.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 "gcloud-sqlproxy.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- printf "%s" .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
 # deployment.yaml
{{- if ne (index .Values.cloudsql.instances 0).instance "instance" }}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "gcloud-sqlproxy.fullname" . }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.replicasCount }}
  template:
    metadata:
      labels:
        app: {{ template "gcloud-sqlproxy.fullname" . }}
    spec:
      {{- if .Values.rbac.create }}
      serviceAccountName: {{ template "gcloud-sqlproxy.fullname" . }}
      {{- end }}
      containers:
      - name: sqlproxy
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: {{ default "" .Values.imagePullPolicy | quote }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        command:
        - /cloud_sql_proxy
        - --dir=/cloudsql
        - -instances={{- range .Values.cloudsql.instances -}}
                        {{ .project }}:{{ .region }}:{{ .instance }}=tcp:0.0.0.0:{{ .port }},
                     {{- end }}
        {{ if or .Values.serviceAccountKey .Values.existingSecret -}}
        - -credential_file=/secrets/cloudsql/{{- if .Values.existingSecret -}} {{ .Values.existingSecretKey }} {{- else -}} credentials.json {{- end }}
        {{ end -}}
        ports:
        {{- range .Values.cloudsql.instances }}
        - name: {{ .instanceShortName | default (.instance | trunc 15) }}
          containerPort: {{ .port }}
        {{- end }}
        volumeMounts:
        {{ if or .Values.serviceAccountKey .Values.existingSecret -}}
        - name: cloudsql-oauth-credentials
          mountPath: /secrets/cloudsql
        {{ end -}}
        - name: cloudsql
          mountPath: /cloudsql
      volumes:
      {{ if or .Values.serviceAccountKey .Values.existingSecret -}}
      - name: cloudsql-oauth-credentials
        secret:
          secretName: {{ default (include "gcloud-sqlproxy.fullname" .) .Values.existingSecret }}
      {{ end -}}
      - name: cloudsql
        emptyDir: {}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
 # pdb.yaml
{{- if .Values.podDisruptionBudget -}}
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  labels:
    app: {{ template "gcloud-sqlproxy.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "gcloud-sqlproxy.fullname" . }}
spec:
  selector:
    matchLabels:
      app: {{ template "gcloud-sqlproxy.name" . }}
{{ .Values.podDisruptionBudget | indent 2 }}
{{- end -}}
 # role.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ template "gcloud-sqlproxy.fullname" . }}
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]
{{- end }}
 # rolebinding.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ .Release.Name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "gcloud-sqlproxy.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "gcloud-sqlproxy.fullname" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}
 # secrets.yaml
{{- if not .Values.existingSecret -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "gcloud-sqlproxy.fullname" . }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: Opaque
data:
  credentials.json: |-
    {{ .Values.serviceAccountKey }}
{{- end -}} # serviceaccount.yaml
{{- if .Values.rbac.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ template "gcloud-sqlproxy.fullname" . }}
{{- end }}
 # svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "gcloud-sqlproxy.fullname" . }}
  labels:
    app: {{ template "gcloud-sqlproxy.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  ports:
  {{- range .Values.cloudsql.instances }}
  - name: {{ .instance }}
    protocol: TCP
    port: {{ .port }}
    targetPort: {{ .instanceShortName | default (.instance | trunc 15) }}
  {{- end }}
  selector:
    app: {{ template "gcloud-sqlproxy.fullname" . }}

## Google Cloud SQL Proxy image
## ref: https://cloud.google.com/sql/docs/mysql/sql-proxy
## ref: https://cloud.google.com/sql/docs/postgres/sql-proxy
image: b.gcr.io/cloudsql-docker/gce-proxy
imageTag: "1.11"

## Specify a imagePullPolicy
## 'Always' if imageTag is 'latest', else set to 'IfNotPresent'
## ref: http://kubernetes.io/docs/user-guide/images/#pre-pulling-images
##
imagePullPolicy: IfNotPresent

## Replicas Set count
replicasCount: 1

## Set the GCP service account key JSON file.
## Service account has access be set to Cloud SQL instances
## the key must be encoded with base64
## e.g. `cat service-account.json | base64`
## only used if no existing secret is specified
##
serviceAccountKey: ""

## Specify an existing secret holding the cloud-sql service account credentials
existingSecret: ""
## The key in the existing secret that stores the credenials
existingSecretKey: ""


## SQL connection settings
##
cloudsql:
  ## PostgreSQL/MySQL instances:
  ## update with your GCP project, the region of your Cloud SQL instance
  ## and the name of your Cloud SQL instance
  ## PostgreSQL port 5432 or MySQL port 3306, or other port you set for your SQL instance.
  ## Use different ports for different instances.
  instances:
    # GCP instance name.
  - instance: "instance"
    # Optional abbreviation used to override the truncated instance name if the
    # 15 character instance name prefix is not unique for use as a port
    # identifier.
    instanceShortName: ""
    # GCP project where the instance exists.
    project: "project"
    # GCP region where the instance exists.
    region: "region"
    # Port number for the proxy to expose for this instance.
    port: 5432

rbac:
  create: false

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    cpu: 100m
    memory: 100Mi
  limits:
    memory: 150Mi
    cpu: 150m

## Node selector
nodeSelector: {}

## Tolerations
tolerations: []

## Affinity
affinity: {}

podDisruptionBudget: |
  maxUnavailable: 1
