# © Copyright IBM Corporation 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

{{- if .Capabilities.APIVersions.Has "apps/v1beta2" }}
apiVersion: apps/v1beta2
{{- else }}
apiVersion: apps/v1beta1
{{- end }}
kind: StatefulSet
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "fullname" . }}
  serviceName: {{ .Values.service.name }}
  replicas: 1
  {{- if and (.Capabilities.KubeVersion.Major | hasPrefix "1") (.Capabilities.KubeVersion.Minor | hasPrefix "7") }}
  # Set updateStrategy to "RollingUpdate", if we're on Kubernetes 1.7.
  # It's already the default for apps/v1beta2 (Kubernetes 1.8 onwards)
  updateStrategy:
    type: RollingUpdate
  {{- end }}
  template:
    metadata:
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
        QM_IDENTIFIER: "{{ .Release.Name }}"
    spec:
      {{- if .Values.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }}
      {{- end }}
      containers:
        - name: qmgr
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
          - containerPort: 1414
          - containerPort: 9443
          env:
          - name: LICENSE
            value: {{ .Values.license }}
          - name: MQ_QMGR_NAME
            value: {{ .Values.queueManager.name | default .Release.Name | replace "-" "" }}
          #- name: MQ_DISABLE_WEB_CONSOLE
          #  value: "true"
          - name: MQ_ADMIN_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "fullname" . }}
                key: adminPassword
          {{- if .Values.queueManager.dev.appPassword }}
          - name: MQ_APP_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "fullname" . }}
                key: appPassword
          {{- end }}
          {{- if .Values.persistence.enabled }}
          volumeMounts:
          - mountPath: "/mnt/mqm"
            name: "{{ template "fullname" . }}-{{ .Values.dataPVC.name }}"
          {{- end }}
          resources:
            limits:
{{ toYaml .Values.resources.limits | indent 14 }}
            requests:
{{ toYaml .Values.resources.requests | indent 14 }}
  volumeClaimTemplates:
  {{- if .Values.persistence.enabled }}
  - metadata:
      name: "{{ template "fullname" . }}-{{ .Values.dataPVC.name }}"
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      {{- if .Values.persistence.useDynamicProvisioning }}
      # If present, use the storageClassName from the values.yaml, else use the
      # default storageClass setup by Kubernetes Administrator
      #
      # Setting storageClassName to nil means use the default storage class
      storageClassName: {{ default nil .Values.dataPVC.storageClassName | quote }}
      {{- else }}
      # Disable dynamic provisioning
      storageClassName: ""
      {{- end }}
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: {{ .Values.dataPVC.size | quote }}
  {{- end }}
