# Copyright 2022 Google LLC
#
# 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
#
#     https://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: batch/v1
kind: Job
metadata:
  name: apigee-cassandra-restore
  namespace: apigee
spec:
  template:
    spec:
      serviceAccountName: apigee-cassandra-backup-and-restore
      initContainers:
      - name: check-user
        image: gcr.io/apigee-release/hybrid/apigee-hybrid-cassandra-client:1.8.0-preview.2
        imagePullPolicy: Always
        command: [
          'bash',
          '-c',
          'until cqlsh apigee-cassandra-default-0.apigee-cassandra-default.${APIGEE_NAMESPACE}.svc.cluster.local -u ${CASS_USERNAME} -p ${CASS_PASSWORD} --ssl -e "select * from system_schema.keyspaces;" &> /dev/null; do sleep 5; echo "waiting for roles to be created"; done;'
        ]
        env:
        - name: APIGEE_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: CASS_USERNAME
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds
              key: admin.user
        - name: CASS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds
              key: admin.password
        volumeMounts:
        - name: tls-volume
          readOnly: true
          mountPath: /opt/apigee/ssl
      containers:
      - name: apigee-cassandra-restore
        command:
        - /bin/bash
        - -c
        - /usr/bin/RestoreDBMaster.sh
        image: gcr.io/apigee-release/hybrid/apigee-cassandra-backup-utility:1.8.0-preview.2
        imagePullPolicy: Always
        env:
        - name: CASSANDRA_CLUSTER_NAME
          value: apigeecluster
        - name: CASSANDRA_DC
          value: dc-1
        - name: BACKUP_KEY_FILE # Used with non-GCS backup and restores only.
          value: "/var/secrets/keys/key"
        - name: CASSANDRA_DB_USER
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds # Should be modified in kustomization patch if secret has a different name.
              key: admin.user
        - name: CASSANDRA_DB_PASS
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds # Should be modified in kustomization patch if secret has a different name.
              key: admin.password
        - name: APIGEE_JMX_USER
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds # Should be modified in kustomization patch if secret has a different name.
              key: jmx.user
        - name: APIGEE_JMX_PASSWORD
          valueFrom:
            secretKeyRef:
              name: apigee-datastore-default-creds # Should be modified in kustomization patch if secret has a different name.
              key: jmx.password
        volumeMounts:
        - name: apigee-cassandra-restore # Used with GCS backup and restores only.
          mountPath: /var/secrets/google
        - name: apigee-cassandra-restore-key-file # Used with non-GCS backup and restores only.
          mountPath: /var/secrets/keys
        - name: tls-volume
          readOnly: true
          mountPath: /opt/apigee/ssl
      volumes:
      - name: apigee-cassandra-restore # Used with GCS backup and restores only.
        secret:
          defaultMode: 0644
          items:
          - key: dbbackup_key.json
            path: dbbackup_key.json
          secretName: "apigee-cassandra-backup-and-restore-gcp-sa-key"
          optional: true
      - name: apigee-cassandra-restore-key-file # Used with non-GCS backup and restores only.
        secret:
          defaultMode: 0400
          secretName: apigee-cassandra-backup-and-restore-key-file
          optional: true
      - name: tls-volume
        secret:
          secretName: "apigee-cassandra-backup-and-restore-tls"
      restartPolicy: OnFailure