# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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: v1
kind: ConfigMap
metadata:
 name: postgres-environment-config
data:
 PG_DATABASE: "{{.Values.databaseEnvironment.pgDatabase}}"
 PG_MODE: "{{.Values.databaseEnvironment.pgMode}}"
 PG_PASSWORD: "{{.Values.databaseEnvironment.pgPassword}}"
 PG_PRIMARY_HOST: "{{.Values.databaseEnvironment.pgPrimaryHost}}"
 PG_PRIMARY_PASSWORD: "{{.Values.databaseEnvironment.pgPrimaryPassword}}"
 PG_PRIMARY_PORT: "{{.Values.databaseEnvironment.pgPrimaryPort}}"
 PG_PRIMARY_USER: "{{.Values.databaseEnvironment.pgPrimaryUser}}"
 PG_ROOT_PASSWORD: "{{.Values.databaseEnvironment.pgRootPassword}}"
 PG_USER: "{{.Values.databaseEnvironment.pgUser}}"
 PGDATA_PATH_OVERRIDE: "{{.Values.databaseEnvironment.pgdataPathOverride}}"
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{.Values.pv.name}}
spec:
  storageClassName: ""
  capacity:
    storage: {{.Values.pv.storage}}
  accessModes:
    - {{.Values.pv.mode}}
  hostPath:
    path: "{{.Values.pv.path}}"
  persistentVolumeReclaimPolicy: Retain
#  gcePersistentDisk:
#    pdName: postgres-data
#    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{.Values.pvc.name}}
spec:
  accessModes:
    - {{.Values.pv.mode}}
  resources:
    requests:
      storage: {{.Values.pv.storage}}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{{.Values.databaseConfig.name}}"
  labels:
    name: "{{.Values.databaseConfig.name}}"
    heritage: {{.Release.Service | quote }}
    release: {{.Release.Name | quote }}
    chart: "{{.Chart.Name}}-{{.Chart.Version}}"
  annotations:
    "helm.sh/created": {{.Release.Time.Seconds | quote }}
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: "{{.Values.databaseConfig.name}}"
    spec:
      securityContext:
        fsGroup: 1000
      containers:
      - image: "{{.Values.databaseConfig.repository}}/{{.Values.databaseConfig.container}}:{{.Values.databaseConfig.tag}}"
        name: {{.Values.databaseConfig.name}}
#TODO: Need to impliment the readiness probe
#        readinessProbe:
#          exec:
#            command:
#            - "/opt/cpm/bin/readiness.sh"
#          initialDelaySeconds: 10
#          timeoutSeconds: 1
#        livenessProbe:
#          exec:
#            command:
#            - "/opt/cpm/bin/liveness.sh"
#          initialDelaySeconds: 120
#          timeoutSeconds: 1
        envFrom:
        - configMapRef:
           name: postgres-environment-config
        volumeMounts:
          - mountPath: {{.Values.pv.path}}
            name: {{.Values.pv.name}}
        ports:
          - containerPort: {{.Values.databaseConfig.port}}
            protocol: TCP
        resources:
          requests:
            cpu: {{ .Values.databaseConfig.cpu }}
            memory: {{ .Values.databaseConfig.memory }}
      volumes:
        - name: {{.Values.pv.name}}
          persistentVolumeClaim:
            claimName: {{.Values.pvc.name}}
            readOnly: false
      restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Service
metadata:
  name: "{{.Values.databaseConfig.name}}"
  labels:
    name: "{{.Values.databaseConfig.name}}"
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: "{{.Chart.Name}}-{{.Chart.Version}}"
spec:
  type: "{{.Values.serviceType}}"
  ports:
  - protocol: TCP
    port: {{.Values.databaseConfig.port}}
    targetPort: {{.Values.databaseConfig.port}}
    nodePort: 0
    name: "{{.Values.databaseConfig.name}}"
  selector:
    name: "{{.Values.databaseConfig.name}}"
  sessionAffinity: None
status:
  loadBalancer: {}