# Copyright 2017 The Openstack-Helm Authors.
#
# 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.

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ .Values.service_name }}
spec:
  serviceName: {{ .Values.service_name }}
  replicas: {{ .Values.replicas }}
  template:
    metadata:
      labels:
        app: {{ .Values.service_name }}
    spec:
      nodeSelector:
        {{ .Values.labels.node_selector_key }}: {{ .Values.labels.node_selector_value }}
      containers:
      - name: {{ .Values.service_name }}
        image: {{ .Values.images.postgresql }}
        imagePullPolicy: {{ .Values.images.pull_policy }}
        ports:
        - containerPort: {{ .Values.network.port.postgresql }}
        livenessProbe:
          exec:
            command:
            - pg_isready
          initialDelaySeconds: 20
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - pg_isready
          initialDelaySeconds: 20
          timeoutSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: postgresql-data
          mountPath: /var/lib/postgresql
      volumes:
{{- if .Values.development.enabled }}
      - name: postgresql-data
        hostPath:
          path: {{ .Values.development.storage_path }}
{{- else }}
  volumeClaimTemplates:
  - metadata:
      name: postgresql-data
      annotations:
        {{ .Values.volume.class_path }}: {{ .Values.volume.class_name }}
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: {{ .Values.volume.size }}
{{- end }}