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

{{/*
Return the appropriate apiVersion for daemonset.
*/}}
{{- define "daemonset.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else if semverCompare "^1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for statefulset.
*/}}
{{- define "statefulset.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else if semverCompare "^1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  config.json: |-
{{- if .Values.mcrouterCommandParams.configFile }}{{ range .Files.Lines .Values.mcrouterCommandParams.configFile }}
    {{ . }}{{ end }}
{{- else }}
    {{- if .Values.memcached.enabled }}
    {
      "pools": {
        "A": {
          "servers": [
            {{- $global := . }}
            {{- range $i, $e := until (.Values.memcached.replicaCount | int) }}
            "{{ $global.Release.Name }}-memcached-{{ $i }}.{{ $global.Release.Name }}-memcached.{{ $global.Release.Namespace }}.svc.cluster.local:11211",
            {{- end }}
          ]
        }
      },
      "route": "PoolRoute|A"
    }
    {{- end }}
{{- end }} # daemonset.yaml
{{- if eq .Values.controller "daemonset" }}
apiVersion: {{ template "daemonset.apiVersion" . }}
kind: DaemonSet
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
  annotations:
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
  selector:
    matchLabels:
      app: {{ template "fullname" . }}
  template:
    metadata:
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
    spec:
      containers:
      - name: {{ template "fullname" . }}
        image: {{ .Values.image }}
        imagePullPolicy: {{ default "" .Values.imagePullPolicy | quote }}
        command: ["mcrouter"]
        args:
        - -p {{ .Values.mcrouterCommandParams.port }}
        - --config-file=/etc/mcrouter/config.json
        volumeMounts:
        - name: config
          mountPath: /etc/mcrouter
        ports:
        - name: mcrouter-port
          containerPort: {{ .Values.mcrouterCommandParams.port }}
          hostPort: {{ .Values.daemonset.hostPort }}
        livenessProbe:
          tcpSocket:
            port: mcrouter-port
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          tcpSocket:
            port: mcrouter-port
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.resources | indent 10 }}
      volumes:
      - name: config
        configMap:
          name: {{ template "fullname" . }}
{{- end -}} # statefulset.yaml
{{- if eq .Values.controller "statefulset" }}
apiVersion: {{ template "statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  annotations:
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
  serviceName: {{ template "fullname" . }}
  replicas: {{ .Values.statefulset.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      affinity:
        podAntiAffinity:
        {{- if eq .Values.statefulset.antiAffinity "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: "kubernetes.io/hostname"
            labelSelector:
              matchLabels:
                app:  {{ template "fullname" . }}
                release: {{ .Release.Name | quote }}
        {{- else if eq .Values.statefulset.antiAffinity "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 5
            podAffinityTerm:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app:  {{ template "fullname" . }}
                  release: {{ .Release.Name | quote }}
        {{- end }}
      containers:
      - name: {{ template "fullname" . }}
        image: {{ .Values.image }}
        imagePullPolicy: {{ default "" .Values.imagePullPolicy | quote }}
        command: ["mcrouter"]
        args:
        - -p {{ .Values.mcrouterCommandParams.port }}
        - --config-file=/etc/mcrouter/config.json
        volumeMounts:
        - name: config
          mountPath: /etc/mcrouter
        ports:
        - name: mcrouter-port
          containerPort: {{ .Values.mcrouterCommandParams.port }}
        livenessProbe:
          tcpSocket:
            port: mcrouter-port
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          tcpSocket:
            port: mcrouter-port
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.resources | indent 10 }}
      volumes:
      - name: config
        configMap:
          name: {{ template "fullname" . }}
{{- end }}
 # svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  clusterIP: None
  ports:
  - name: mcrouter-port
    port: {{ .Values.mcrouterCommandParams.port }}
    targetPort: mcrouter-port
  selector:
    app: {{ template "fullname" . }}

## Container image
image: jphalip/mcrouter:0.36.0

## Memcached chart parameters
memcached:
  ## If true, the Memcached chart will be installed as a dependency
  enabled: true
  ## Number of Memcached replicas
  replicaCount: 3

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

## Parameters for the mcrouter command
mcrouterCommandParams:
  port: 5000

  ## Pass a custom JSON config, if not specified one will be generated per the number of memcached replicas
  ## https://github.com/facebook/mcrouter/wiki/Config-Files
  # configFile:

## Information about the memcached service
memcachedService:
  # serviceName: "memcached"
  # replicaCount: 3
  port: 11211
  namespace: "default"

## Pod controller. Possible values: "daemonset", "statefulset".
controller: "daemonset"

## Values for the daemonset controller
daemonset:
  ## Host port for the mcrouter pod
  hostPort: 5000

## Values for the statefulset controller
statefulset:
  ## Number of replicas
  replicas: 1
  ## Select AntiAffinity as either hard or soft, default is hard
  antiAffinity: "hard"
