# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "memcached.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 "memcached.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 -}}

{{/*
Common labels
*/}}
{{- define "memcached.labels" -}}
app.kubernetes.io/name: {{ include "memcached.name" . }}
helm.sh/chart: {{ include "memcached.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "memcached.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # pdb.yaml
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: {{ template "memcached.fullname" . }}
  namespace: {{ .Release.Namespace }}
  annotations:
    helm.sh/hook: "post-upgrade, post-install"
    helm.sh/hook-delete-policy: "before-hook-creation"
  labels:
{{ include "memcached.labels" . | indent 4 }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "memcached.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  minAvailable: {{ .Values.pdbMinAvailable }}
 # servicemonitor.yaml
{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "memcached.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "memcached.labels" . | indent 4 }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "memcached.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  endpoints:
  - port: metrics
    interval: {{ .Values.metrics.serviceMonitor.interval }}
{{- end }}
 # statefulset.yaml
apiVersion: apps/v1
kind: {{ .Values.kind }}
metadata:
  name: {{ template "memcached.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "memcached.labels" . | indent 4 }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "memcached.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  {{- if eq .Values.kind "StatefulSet" }}
  serviceName: {{ template "memcached.fullname" . }}
  {{- end }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "memcached.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
{{- with .Values.podLabels }}
{{ toYaml . | indent 8 }}
{{- end}}
{{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      affinity:
        podAntiAffinity:
        {{- if eq .Values.AntiAffinity "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: "kubernetes.io/hostname"
            labelSelector:
              matchLabels:
                app.kubernetes.io/name: {{ include "memcached.name" . }}
                app.kubernetes.io/instance: {{ .Release.Name }}
        {{- else if eq .Values.AntiAffinity "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 5
            podAffinityTerm:
              topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app.kubernetes.io/name: {{ include "memcached.name" . }}
                  app.kubernetes.io/instance: {{ .Release.Name }}
        {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      containers:
      - name: {{ template "memcached.fullname" . }}
        image: {{ .Values.image }}
        imagePullPolicy: {{ default "" .Values.imagePullPolicy | quote }}
        {{- if .Values.securityContext.enabled }}
        securityContext:
          runAsUser: {{ .Values.securityContext.runAsUser }}
        {{- end }}
        command:
        - memcached
        - -m {{ .Values.memcached.maxItemMemory }}
        {{- if .Values.memcached.extendedOptions }}
        - -o
        - {{ .Values.memcached.extendedOptions }}
        {{- end }}
        {{- if .Values.memcached.verbosity }}
        - -{{ .Values.memcached.verbosity }}
        {{- end }}
{{- with .Values.memcached.extraArgs }}
{{ toYaml . | indent 8 }}
{{- end }}
        ports:
        - name: memcache
          containerPort: 11211
        livenessProbe:
          tcpSocket:
            port: memcache
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          tcpSocket:
            port: memcache
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.resources | indent 10 }}
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ .Values.metrics.image }}
        imagePullPolicy: {{ default "" .Values.metrics.imagePullPolicy | quote }}
        {{- if .Values.securityContext.enabled }}
        securityContext:
          runAsUser: {{ .Values.securityContext.runAsUser }}
        {{- end }}
        ports:
        - name: metrics
          containerPort: 9150
        resources:
{{ toYaml .Values.metrics.resources | indent 10 }}
{{- end }}
{{- with .Values.extraContainers }}
{{ tpl . $ | indent 6 }}
{{- end }}
{{- with .Values.extraVolumes }}
      volumes:
{{ tpl . $ | indent 6 }}
{{- end }}
{{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | trim | indent 8}}
{{- end }}
{{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | trim | indent 8}}
{{- end }}
{{- with .Values.affinity }}
      affinity:
{{ toYaml . | trim | indent 8}}
{{- end }}
{{- if eq .Values.kind "StatefulSet" }}
  updateStrategy:
{{- else }}
  strategy:
{{- end }}
    type: {{ .Values.updateStrategy.type }}
    {{- if (eq "Recreate" .Values.updateStrategy.type) }}
    rollingUpdate: null
    {{- end }}
 # svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "memcached.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "memcached.labels" . | indent 4 }}
  annotations:
{{ toYaml .Values.serviceAnnotations | indent 4 }}
spec:
  clusterIP: None
  ports:
  - name: memcache
    port: 11211
    targetPort: memcache
{{- if .Values.metrics.enabled }}
  - name: metrics
    port: 9150
    targetPort: metrics
{{- end }}
  selector:
    app.kubernetes.io/name: {{ include "memcached.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

## Memcached image and tag
## ref: https://hub.docker.com/r/library/memcached/tags/
##
image: memcached:1.5.20

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

## Replica count
replicaCount: 3

## Pod disruption budget minAvailable count
## Ensure this value is lower than replicaCount in order to allow a worker
## node to drain successfully
pdbMinAvailable: 2

## Select AntiAffinity as either hard or soft, default is hard
AntiAffinity: "hard"

memcached:
  ## Various values that get set as command-line flags.
  ## ref: https://github.com/memcached/memcached/wiki/ConfiguringServer#commandline-arguments
  ##
  maxItemMemory: 64
  verbosity: v
  extendedOptions: modern

  ## Additional command line arguments to pass to memcached
  ## E.g. to specify a maximum value size
  ## extraArgs:
  ##   - -I 2m
  extraArgs: []

## Define various attributes of the service
serviceAnnotations: {}
#  prometheus.io/scrape: "true"

## StatefulSet or Deployment
kind: StatefulSet

## Update Strategy for the StatefulSet or Deployment
## ref: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#update-strategies
## ref: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment
updateStrategy:
  type: RollingUpdate

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

## Key:value pair for assigning pod to specific sets of nodes
## ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
nodeSelector: {}

## ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
tolerations: {}

## Advanced scheduling controls
## ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
affinity: {}

## Memcached pod Security Context
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
securityContext:
  enabled: true
  fsGroup: 1001
  runAsUser: 1001

metrics:
  ## Expose memcached metrics in Prometheus format
  enabled: false
  serviceMonitor:
    enabled: false
    interval: 15s

  ## Memcached exporter image and tag
  image: quay.io/prometheus/memcached-exporter:v0.6.0

  ## 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

  ## Configure resource requests and limits
  ## ref: http://kubernetes.io/docs/user-guide/compute-resources/
  ##
  resources: {}

extraContainers: |

extraVolumes: |

## Custom metadata labels to be applied to statefulset and pods
# podLabels:
#   foo: "bar"
#   bar: "foo"

# To be added to the server pod(s)
podAnnotations: {}

## Set pod priority class
# priorityClassName: ""
