apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "memcached.fullname" . }}
  labels:
    app: {{ template "memcached.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  updateStrategy:
    type: RollingUpdate
  serviceName: {{ template "memcached.fullname" . }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "memcached.fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      affinity:
        podAntiAffinity:
        {{- if eq .Values.AntiAffinity "hard" }}
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: "kubernetes.io/hostname"
            labelSelector:
              matchLabels:
                app:  {{ template "memcached.fullname" . }}
                release: {{ .Release.Name | quote }}
        {{- else if eq .Values.AntiAffinity "soft" }}
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 5
            podAffinityTerm:
              topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app:  {{ template "memcached.fullname" . }}
                  release: {{ .Release.Name | quote }}
        {{- end }}
      containers:
      - name: {{ template "memcached.fullname" . }}
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: {{ default "" .Values.imagePullPolicy | quote }}
        command:
        - memcached
        - -m {{ .Values.memcached.maxItemMemory  }}
        {{- if .Values.memcached.extendedOptions }}
        - -o
        - {{ .Values.memcached.extendedOptions }}
        {{- end }}
        {{- if .Values.memcached.verbosity }}
        - -{{ .Values.memcached.verbosity }}
        {{- 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 }}
