<%#
 Copyright 2013-2017 the original author or authors from the JBooter project.

 This file is part of the JBooter project, see https://jbooter.github.io/
 for more information.

 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.
-%>
# DB template.
#
# This defines all the configurable parameters and other objects that are needed to run the MySQLDB service. This template can even be
# saved in OpenShift namespace as well so that have the flexibility to do any project specific customizations. Pls note wherever
# displayName says *** PLEASE DO NOT CHANGE THIS ***, don't touch that as those parameters will be referenced in other places.
#
apiVersion: v1
kind: Template
metadata:
  name: <%= app.baseName.toLowerCase() %>-mysqldb-template
  namespace: <%= openshiftNamespace %>
  annotations:
    description: This template defines objects that are required to spin up a mysqldb pod
    tags: db, <%= app.baseName.toLowerCase() %>-mysqldb<% if (storageType == 'persistent') { %> ,persistent <% } %> <% if (storageType == 'ephemeral') { %> ,ephemeral <% } %>
    openshift.io/display-name: <%= app.baseName.toLowerCase() %>-mysqldb-template
    openshift.io/long-description: "This template provides objects that are required to spin up a mysqldb pod. <% if (storageType ==
    'persistent') { %>The database is stored on persistent storage, so any restart of the service will not cause any impact to the data.
     Please make sure you have provisioned PVs (Persistent Volumes) before using this template. <% } %><% if (storageType == 'ephemeral') {
     %>The database is not stored on persistent storage, so any restart of the service will result in all data being lost.<% } %>"
    openshift.io/provider-display-name: JBooter-OpenShift
labels:
  app: <%= app.baseName.toLowerCase() %>-mysqldb
  createdBy: JBooter-Team
parameters:
  -
    name: APPLICATION_NAME
    value: <%= app.baseName.toLowerCase() %>-mysqldb
    description: Name of the application
    required: true
    displayName: Application Name
  -
    name: SECRET_REF
    value: ds-secret-ref
    description: Name of the secret reference
    required: true
    displayName: "*** PLEASE DO NOT CHANGE THIS ***"
  -
    name: MYSQL_USER
    displayName: MySQL Connection Username
    description: Username for MySQL user that will be used for accessing the database.
    generate: expression
    from: user[A-Z0-9]{3}
    required: true
  -
    name: MYSQL_PASSWORD
    displayName: MySQL Connection Password
    description: Password for the MySQL connection user.
    generate: expression
    from: "[a-zA-Z0-9]{16}"
    required: true
  -
    name: MYSQL_ROOT_PASSWORD
    displayName: MySQL root user Password
    description: Password for the MySQL root user.
    generate: expression
    from: "[a-zA-Z0-9]{16}"
    required: true
  -
    name: DATABASE_NAME
    value: <%= app.baseName.toLowerCase() %>
    displayName: MySQL DB Name
    description: Name of the MySQL database accessed.
    required: true
  -
    name: VOLUME_CAPACITY
    displayName: Volume Capacity
    description: Volume space available for data, e.g. 512Mi, 2Gi.
    value: 1Gi
    required: true
objects:
  -
    apiVersion: v1
    kind: Secret
    metadata:
      name: ${SECRET_REF}
    stringData:
      database-user: "${MYSQL_USER}"
      database-password: "${MYSQL_PASSWORD}"
      database-root-password: "${MYSQL_ROOT_PASSWORD}"
<%_ if (storageType == 'persistent') { _%>
  -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: ${APPLICATION_NAME}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: "${VOLUME_CAPACITY}"
<%_ } _%>
  -
    apiVersion: v1
    kind: DeploymentConfig
    metadata:
      name: ${APPLICATION_NAME}
      labels:
        app: ${APPLICATION_NAME}
    spec:
# This is to define the deployment strategy (either be Rolling or Recreate)
#     strategy:
#       type: Rolling
#       rollingParams:
#         updatePeriodSeconds: 1
#         intervalSeconds: 1
#         timeoutSeconds: 600
#         maxUnavailable: 25%
#         maxSurge: 25%
#       resources:
      triggers:
        -
          type: ConfigChange
      replicas: 1
      selector:
        app: ${APPLICATION_NAME}
      template:
        metadata:
          labels:
            app: ${APPLICATION_NAME}
        spec:
          volumes:
          - name: ${APPLICATION_NAME}-data
            <%_ if (storageType == 'persistent') { _%>
            persistentVolumeClaim:
              claimName: ${APPLICATION_NAME}
            <%_ } _%>
            <%_ if (storageType == 'ephemeral') { _%>
            emptyDir: {}
            <%_ } _%>
          containers:
          - name: ${APPLICATION_NAME}
            image: <%= DOCKER_MYSQL %>
            env:
#            - name: MYSQL_USER
#              value: root
#            - name: MYSQL_ALLOW_EMPTY_PASSWORD
#              value: 'yes'
            - name: MYSQL_DATABASE
              value: ${DATABASE_NAME}
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-user
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-password
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${SECRET_REF}
                  key: database-root-password
    #        command:
    #        - mysqld
    #        - --lower_case_table_names=1
    #        - --skip-ssl
            ports:
            - containerPort: 3306
              protocol: TCP
            readinessProbe:
              timeoutSeconds: 1
              initialDelaySeconds: 5
              exec:
                command:
                - "/bin/sh"
                - "-i"
                - "-c"
                - MYSQL_PWD="$MYSQL_PASSWORD" mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE
                  -e 'SELECT 1'
            livenessProbe:
              timeoutSeconds: 1
              initialDelaySeconds: 30
              tcpSocket:
                port: 3306
            volumeMounts:
            - name: ${APPLICATION_NAME}-data
              mountPath: /var/lib/mysql
            resources:
            imagePullPolicy: IfNotPresent
          restartPolicy: Always
          terminationGracePeriodSeconds: 30
  -
    apiVersion: v1
    kind: Service
    metadata:
      name: ${APPLICATION_NAME}
      labels:
        app: ${APPLICATION_NAME}
    spec:
      ports:
        -
          name: mysqldb-bc
          protocol: TCP
          port: 3306
          targetPort: 3306
        # No need to mention the port explicitly as each pod and service gets assigned with an IP, a service within a pod is accessible/routable at :80
        -
          name: mysqldb
          protocol: TCP
          port: 80
          targetPort: 3306
      selector:
        app: ${APPLICATION_NAME}
