#!/bin/bash -x

if [ -z "${ENABLE_DASH}" ]; then
    ENABLE_DASH=true
fi

if [ -z "${DATA_INTEGRITY_VALIDATION_TESTS}" ]; then
    DATA_INTEGRITY_VALIDATION_TESTS=""
fi

if [ -z "${DASH_UID}" ]; then
    if [ -e /build.properties ]; then
      DASH_UID=`cat /build.properties | grep -i "DASH_UID=" | grep -Eo '[0-9]+'`
    else
      DASH_UID="0"
    fi
fi

SECURITY_CONTEXT=false

# System checks https://github.com/portworx/torpedo/blob/86232cb195400d05a9f83d57856f8f29bdc9789d/tests/common.go#L2173
# should be skipped from AfterSuite() if this flag is set to true. This is to avoid distracting test failures due to
# unstable testing environments.
TORPEDO_SKIP_SYSTEM_CHECKS=false
if [[ ! -z "${TORPEDO_SKIP_SYSTEM_CHECKS}" ]]; then
    TORPEDO_SKIP_SYSTEM_CHECKS=true
fi

if [ "${IS_OCP}" == true ]; then
    SECURITY_CONTEXT=true
fi

if [ -z "${SCALE_FACTOR}" ]; then
    SCALE_FACTOR="10"
fi

if [ -z "${VOLUME_PROVIDER}" ]; then
    VOLUME_PROVIDER="none"
fi

if [ -z "${OBJECT_STORE_PROVIDER}" ]; then
    OBJECT_STORE_PROVIDER="aws"
fi

if [ -z "${SPEC_DIR}" ]; then
    SPEC_DIR="../drivers/scheduler/k8s/specs"
fi

if [ -z "${SCHEDULER}" ]; then
    SCHEDULER="k8s"
fi

if [ -z "${LOGLEVEL}" ]; then
    LOGLEVEL="debug"
fi

if [ -z "${CHAOS_LEVEL}" ]; then
    CHAOS_LEVEL="5"
fi

if [ -z "${MIN_RUN_TIME}" ]; then
    MIN_RUN_TIME="0"
fi

if [[ -z "$FAIL_FAST" || "$FAIL_FAST" = true ]]; then
    FAIL_FAST="--fail-fast"
else
    FAIL_FAST="-keep-going"
fi

SKIP_ARG=""
if [ -n "$SKIP_TESTS" ]; then
    skipRegex=$(echo $SKIP_TESTS | sed -e 's/,/}|{/g')
    SKIP_ARG="--skip={$skipRegex}"
fi

FOCUS_ARG=""
if [ -n "$FOCUS_TESTS" ]; then
    focusRegex=$(echo $FOCUS_TESTS | sed -e 's/,/}|{/g')
    FOCUS_ARG="--focus={$focusRegex}"
fi

if [ -z "${UPGRADE_ENDPOINT_URL}" ]; then
    UPGRADE_ENDPOINT_URL=""
fi

if [ -z "${UPGRADE_ENDPOINT_VERSION}" ]; then
    UPGRADE_ENDPOINT_VERSION=""
fi

if [ -z "${UPGRADE_STORAGE_DRIVER_ENDPOINT_LIST}" ]; then
    UPGRADE_STORAGE_DRIVER_ENDPOINT_LIST=""
fi

if [ -z "${SKIP_PX_OPERATOR_UPGRADE}" ]; then
    SKIP_PX_OPERATOR_UPGRADE=false
fi

if [ -z "${ENABLE_STORK_UPGRADE}" ]; then
    ENABLE_STORK_UPGRADE=false
fi

if [ -z "${IS_PURE_VOLUMES}" ]; then
    IS_PURE_VOLUMES=false
fi

if [ -z "${DEPLOY_PDS_APPS}" ]; then
    DEPLOY_PDS_APPS=false
fi

if [ -z "${PURE_FA_CLONE_MANY_TEST}" ]; then
    PURE_FA_CLONE_MANY_TEST=false
fi

if [ -z "${PURE_SAN_TYPE}" ]; then
    PURE_SAN_TYPE=ISCSI
fi

if [ -n "${PROVISIONER}" ]; then
    PROVISIONER="$PROVISIONER"
fi

if [ -z "${STORAGE_DRIVER}" ]; then
    STORAGE_DRIVER="pxd"
fi

if [ -z "${MAX_STORAGE_NODES_PER_AZ}" ]; then
    MAX_STORAGE_NODES_PER_AZ="2"
fi

if [ -z "${PROVISIONER}" ]; then
    PROVISIONER="portworx"
fi

if [ -z "${IS_HYPER_CONVERGED}" ]; then
    IS_HYPER_CONVERGED=true
fi

if [ -z "${PX_POD_RESTART_CHECK}" ]; then
    PX_POD_RESTART_CHECK=false
fi

CONFIGMAP=""
if [ -n "${CONFIG_MAP}" ]; then
    CONFIGMAP="${CONFIG_MAP}"
fi

if [ -z "${TORPEDO_IMG}" ]; then
    TORPEDO_IMG="portworx/torpedo:latest"
    echo "Using default torpedo image: ${TORPEDO_IMG}"
fi

if [ -z "${TIMEOUT}" ]; then
    TIMEOUT="720h0m0s"
    echo "Using default timeout of ${TIMEOUT}"
fi

if [ -z "$DRIVER_START_TIMEOUT" ]; then
    DRIVER_START_TIMEOUT="10m0s"
    echo "Using default timeout of ${DRIVER_START_TIMEOUT}"
fi

if [ -z "$SECRET_TYPE" ]; then
    SECRET_TYPE="k8s"
    echo "Using default secret type of ${SECRET_TYPE}"
fi

APP_DESTROY_TIMEOUT_ARG=""
if [ -n "${APP_DESTROY_TIMEOUT}" ]; then
    APP_DESTROY_TIMEOUT_ARG="--destroy-app-timeout=$APP_DESTROY_TIMEOUT"
fi

if [ -z "$LICENSE_EXPIRY_TIMEOUT_HOURS" ]; then
    LICENSE_EXPIRY_TIMEOUT_HOURS="1h0m0s"
    echo "Using default license expiry timeout of ${LICENSE_EXPIRY_TIMEOUT_HOURS}"
fi

if [ -z "$METERING_INTERVAL_MINS" ]; then
    METERING_INTERVAL_MINS="10m0s"
    echo "Using default metering of ${METERING_INTERVAL_MINS}"
fi

if [ -z "$STORAGENODE_RECOVERY_TIMEOUT" ]; then
    STORAGENODE_RECOVERY_TIMEOUT="35m0s"
    echo "Using default storage node recovery timeout of ${STORAGENODE_RECOVERY_TIMEOUT}"
fi

AZURE_TENANTID=""
if [ -n "$AZURE_TENANT_ID" ]; then
    AZURE_TENANTID="${AZURE_TENANT_ID}"
fi

AZURE_CLIENTID=""
if [ -n "$AZURE_CLIENT_ID" ]; then
    AZURE_CLIENTID="${AZURE_CLIENT_ID}"
fi

AZURE_CLIENTSECRET=""
if [ -n "$AZURE_CLIENT_SECRET" ]; then
    AZURE_CLIENTSECRET="${AZURE_CLIENT_SECRET}"
fi

CSI_GENERIC_CONFIGMAP=""
if [ -n "${CSI_GENERIC_DRIVER_CONFIGMAP}" ]; then
    CSI_GENERIC_CONFIGMAP="${CSI_GENERIC_DRIVER_CONFIGMAP}"
fi

if [ -z "$AWS_REGION" ]; then
    AWS_REGION="us-east-1"
    echo "Using default AWS_REGION of ${AWS_REGION}"
fi

if [ -z "$TORPEDO_JOB_TYPE" ]; then
    TORPEDO_JOB_TYPE="functional"
fi

if [ -z "$TORPEDO_JOB_NAME" ]; then
    TORPEDO_JOB_NAME="torpedo-daily-job"
fi

ANTHOS_ADMIN_WS_NODE=""
if [ -n "$ANTHOS_ADMIN_WS_NODE" ]; then
    ANTHOS_ADMIN_WS_NODE="${ANTHOS_ADMIN_WS_NODE}"
fi
ANTHOS_INST_PATH=""
if [ -n "$ANTHOS_INST_PATH" ]; then
    ANTHOS_INST_PATH="${ANTHOS_INST_PATH}"
fi

for i in $@
do
case $i in
	--backup-driver)
	BACKUP_DRIVER=$2
	shift
	shift
	;;
esac
done

echo "checking if we need to override test suite: ${TEST_SUITE}"

if [[ "$TEST_SUITE" != *"pds.test"* ]] && [[ "$TEST_SUITE" != *"backup.test"* ]]; then
    TEST_SUITE='"bin/basic.test"'
fi

echo "Using test suite: ${TEST_SUITE}"

if [ -z "${AUTOPILOT_UPGRADE_VERSION}" ]; then
    AUTOPILOT_UPGRADE_VERSION=""
fi

kubectl delete secret torpedo
kubectl delete pod torpedo
state=`kubectl get pod torpedo | grep -v NAME | awk '{print $3}'`
timeout=0
while [ "$state" == "Terminating" -a $timeout -le 600 ]; do
  echo "Terminating torpedo..."
  sleep 1
  state=`kubectl get pod torpedo | grep -v NAME | awk '{print $3}'`
  timeout=$[$timeout+1]
done

if [ $timeout -gt 600 ]; then
  echo "Torpedo is taking too long to terminate. Operation timeout."
  describe_pod_then_exit
fi

TORPEDO_CUSTOM_PARAM_VOLUME=""
TORPEDO_CUSTOM_PARAM_MOUNT=""
CUSTOM_APP_CONFIG_PATH=""
if [ -n "${CUSTOM_APP_CONFIG}" ]; then
    kubectl create configmap custom-app-config --from-file=custom_app_config.yml=${CUSTOM_APP_CONFIG}
    CUSTOM_APP_CONFIG_PATH="/mnt/torpedo/custom_app_config.yml"
    TORPEDO_CUSTOM_PARAM_VOLUME="{ \"name\": \"custom-app-config-volume\", \"configMap\": { \"name\": \"custom-app-config\", \"items\": [{\"key\": \"custom_app_config.yml\", \"path\": \"custom_app_config.yml\"}] } }"
    TORPEDO_CUSTOM_PARAM_MOUNT="{ \"name\": \"custom-app-config-volume\", \"mountPath\": \"${CUSTOM_APP_CONFIG_PATH}\", \"subPath\": \"custom_app_config.yml\" }"
fi

TORPEDO_SSH_KEY_VOLUME=""
TORPEDO_SSH_KEY_MOUNT=""
if [ -n "${TORPEDO_SSH_KEY}" ]; then
    kubectl create secret generic key4torpedo --from-file=${TORPEDO_SSH_KEY}
    TORPEDO_SSH_KEY_VOLUME="{ \"name\": \"ssh-key-volume\", \"secret\": { \"secretName\": \"key4torpedo\", \"defaultMode\": 256 }}"
    TORPEDO_SSH_KEY_MOUNT="{ \"name\": \"ssh-key-volume\", \"mountPath\": \"/home/torpedo/\" }"
fi

ORACLE_API_KEY_VOLUME=""
if [ -n "${ORACLE_API_KEY}" ]; then
    ORACLE_API_KEY_VOLUME="{ \"name\": \"oracle-api-key-volume\", \"secret\": { \"secretName\": \"key4oracle\", \"defaultMode\": 256 }}"
    ORACLE_API_KEY_MOUNT="{ \"name\": \"oracle-api-key-volume\", \"mountPath\": \"/home/oci/\" }"
fi

TESTRESULTS_VOLUME="{ \"name\": \"testresults\", \"hostPath\": { \"path\": \"/mnt/testresults/\", \"type\": \"DirectoryOrCreate\" } }"
TESTRESULTS_MOUNT="{ \"name\": \"testresults\", \"mountPath\": \"/testresults/\" }"

AWS_VOLUME="{ \"name\": \"aws-volume\", \"configMap\": { \"name\": \"aws-cm\", \"items\": [{\"key\": \"credentials\", \"path\": \"credentials\"}, {\"key\": \"config\", \"path\": \"config\"}]} }"
AWS_VOLUME_MOUNT="{ \"name\": \"aws-volume\", \"mountPath\": \"/root/.aws/\" }"

VOLUMES="${TESTRESULTS_VOLUME}"

if [ "${STORAGE_DRIVER}" == "aws" ]; then
  VOLUMES="${VOLUMES},${AWS_VOLUME}"
  VOLUME_MOUNTS="${VOLUME_MOUNTS},${AWS_VOLUME_MOUNT}"
fi

if [ -n "${PROVIDERS}" ]; then
  echo "Create configs for providers",${PROVIDERS}
  for i in ${PROVIDERS//,/ };do
     if [ "${i}" == "aws" ]; then
      VOLUMES="${VOLUMES},${AWS_VOLUME}"
      VOLUME_MOUNTS="${VOLUME_MOUNTS},${AWS_VOLUME_MOUNT}"
     fi
  done
fi

if [ -n "${TORPEDO_SSH_KEY_VOLUME}" ]; then
    VOLUMES="${VOLUMES},${TORPEDO_SSH_KEY_VOLUME}"
fi

VOLUME_MOUNTS="${TESTRESULTS_MOUNT}"

if [ -n "${TORPEDO_SSH_KEY_MOUNT}" ]; then
    VOLUME_MOUNTS="${VOLUME_MOUNTS},${TORPEDO_SSH_KEY_MOUNT}"
fi

if [ -n "${ORACLE_API_KEY_MOUNT}" ]; then
    VOLUME_MOUNTS="${VOLUME_MOUNTS},${ORACLE_API_KEY_MOUNT}"
fi

if [ -n "${ORACLE_API_KEY_VOLUME}" ]; then
    VOLUMES="${VOLUMES},${ORACLE_API_KEY_VOLUME}"
fi

if [ -n "${TORPEDO_CUSTOM_PARAM_VOLUME}" ]; then
    VOLUMES="${VOLUMES},${TORPEDO_CUSTOM_PARAM_VOLUME}"
fi

if [ -n "${TORPEDO_CUSTOM_PARAM_MOUNT}" ]; then
    VOLUME_MOUNTS="${VOLUME_MOUNTS},${TORPEDO_CUSTOM_PARAM_MOUNT}"
fi

BUSYBOX_IMG="busybox"
if [ -n "${INTERNAL_DOCKER_REGISTRY}" ]; then
    BUSYBOX_IMG="${INTERNAL_DOCKER_REGISTRY}/busybox"
    TORPEDO_IMG="${INTERNAL_DOCKER_REGISTRY}/${TORPEDO_IMG}"
fi

kubectl create configmap cloud-config --from-file=/config/cloud-json

# List of additional kubeconfigs of k8s clusters to register with px-backup, px-dr
FROM_FILE=""
CLUSTER_CONFIGS=""
echo "Create kubeconfig configmap",${KUBECONFIGS}
if [ -n "${KUBECONFIGS}" ]; then
  for i in ${KUBECONFIGS//,/ };do
     FROM_FILE="${FROM_FILE} --from-file=${i}"
     if [[ -z ${CLUSTER_CONFIGS} ]]; then
       CLUSTER_CONFIGS="`basename ${i}`"
     else
       CLUSTER_CONFIGS="${CLUSTER_CONFIGS},`basename ${i}`"
     fi
  done
  kubectl create configmap kubeconfigs ${FROM_FILE}
fi

K8S_VENDOR_KEY=""
if [ -z "${NODE_DRIVER}" ]; then
    NODE_DRIVER="ssh"
fi
if [ -n "${K8S_VENDOR}" ]; then
    case "$K8S_VENDOR" in
        gke)
            NODE_DRIVER="gke"
            ;;
        aks)
            NODE_DRIVER="aks"
            ;;
        oracle)
            NODE_DRIVER="oracle"
            ;;
    esac
fi

echo '' > torpedo.yaml

if [ ! -z $IMAGE_PULL_SERVER ] && [ ! -z $IMAGE_PULL_USERNAME ] && [ ! -z $IMAGE_PULL_PASSWORD ]; then
  echo "Adding Docker registry secret ..."
  auth=$(echo "$IMAGE_PULL_USERNAME:$IMAGE_PULL_PASSWORD" | base64)
  secret=$(echo "{\"auths\":{\"$IMAGE_PULL_SERVER\":{\"username\":\"$IMAGE_PULL_USERNAME\",\"password\":\"$IMAGE_PULL_PASSWORD\",\"auth\":"$auth"}}}" | base64 -w 0)
  cat >> torpedo.yaml <<EOF
---
apiVersion: v1
kind: Secret
metadata:
  name: torpedo
type: docker-registry
data:
  .dockerconfigjson: $secret

EOF
  sed -i '/spec:/a\  imagePullSecrets:\n    - name: torpedo' torpedo.yaml
fi

cat >> torpedo.yaml <<EOF
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: torpedo-account
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
   name: torpedo-role
rules:
  -
    apiGroups:
      # have access to everything except Secrets
      - "*"
    resources: ["*"]
    verbs: ["*"]
  - nonResourceURLs: ["*"]
    verbs: ["*"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: torpedo-role-binding
subjects:
- kind: ServiceAccount
  name: torpedo-account
  namespace: default
roleRef:
  kind: ClusterRole
  name: torpedo-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Pod
metadata:
  name: torpedo
  labels:
    app: torpedo
spec:
  tolerations:
  - key: node-role.kubernetes.io/master
    operator: Equal
    effect: NoSchedule
  - key: node-role.kubernetes.io/controlplane
    operator: Equal
    value: "true"
  - key: node-role.kubernetes.io/control-plane
    operator: Exists
  - key: node-role.kubernetes.io/etcd
    operator: Equal
    value: "true"
  - key: apps
    operator: Equal
    value: "false"
    effect: "NoSchedule"
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node-role.kubernetes.io/master
            operator: "Exists"
        - matchExpressions:
          - key: node-role.kubernetes.io/control-plane
            operator: "Exists"
        - matchExpressions:
          - key: node-role.kubernetes.io/controlplane
            operator: "In"
            values: ["true"]
        - matchExpressions:
          - key: px/enabled
            operator: "In"
            values: ["false"]
  initContainers:
  - name: init-sysctl
    image: ${BUSYBOX_IMG}
    imagePullPolicy: IfNotPresent
    securityContext:
      privileged: true
    command: ["sh", "-c", "mkdir -p /mnt/testresults && chmod 777 /mnt/testresults/"]
  containers:
  - name: torpedo
    image: ${TORPEDO_IMG}
    imagePullPolicy: Always
    securityContext:
      privileged: ${SECURITY_CONTEXT}
    command: ["sh", "-c"]
    args: ["cd /bin && ./taas -spec-dir=$SPEC_DIR"]
    tty: true
    volumeMounts: [${VOLUME_MOUNTS}]
    env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName
    - name: K8S_VENDOR
      value: "${K8S_VENDOR}"
    - name: TORPEDO_SSH_USER
      value: "${TORPEDO_SSH_USER}"
    - name: TORPEDO_SSH_PASSWORD
      value: "${TORPEDO_SSH_PASSWORD}"
    - name: TORPEDO_SSH_KEY
      value: "${TORPEDO_SSH_KEY}"
    - name: AZURE_TENANT_ID
      value: "${AZURE_TENANTID}"
    - name: VOLUME_PROVIDER
      value: "${VOLUME_PROVIDER}"
    - name: OBJECT_STORE_PROVIDER
      value: "${OBJECT_STORE_PROVIDER}"
    - name: AZURE_CLIENT_ID
      value: "${AZURE_CLIENTID}"
    - name: AZURE_CLIENT_SECRET
      value: "${AZURE_CLIENTSECRET}"
    - name: AZURE_ACCOUNT_NAME
      value: "${AZURE_ACCOUNT_NAME}"
    - name: RANCHER_TOKEN
      value: "${RANCHER_TOKEN}"
    - name: RANCHER_URL
      value: "${RANCHER_URL}"
    - name: AZURE_ACCOUNT_KEY
      value: "${AZURE_ACCOUNT_KEY}"
    - name: AZURE_SUBSCRIPTION_ID
      value: "${AZURE_SUBSCRIPTION_ID}"
    - name: AZURE_CLUSTER_NAME
      value: "${AZURE_CLUSTER_NAME}"
    - name: AWS_ACCESS_KEY_ID
      value: "${AWS_ACCESS_KEY_ID}"
    - name: AWS_SECRET_ACCESS_KEY
      value: "${AWS_SECRET_ACCESS_KEY}"
    - name: AWS_REGION
      value: "${AWS_REGION}"
    - name: KUBECONFIGS
      value: "${CLUSTER_CONFIGS}"
    - name: S3_ENDPOINT
      value: "${S3_ENDPOINT}"
    - name: S3_AWS_ACCESS_KEY_ID
      value: "${S3_AWS_ACCESS_KEY_ID}"
    - name: S3_AWS_SECRET_ACCESS_KEY
      value: "${S3_AWS_SECRET_ACCESS_KEY}"
    - name: S3_REGION
      value: "${S3_REGION}"
    - name: BUCKET_NAME
      value: "${BUCKET_NAME}"
    - name: LOCKED_BUCKET_NAME
      value: "${LOCKED_BUCKET_NAME}"
    - name: S3_DISABLE_SSL
      value: "${S3_DISABLE_SSL}"
    - name: DIAGS_BUCKET
      value: "${DIAGS_BUCKET}"
    - name: PROVIDERS
      value: "${PROVIDERS}"
    - name: CLUSTER_PROVIDER
      value: "${CLUSTER_PROVIDER}"
    - name: INTERNAL_DOCKER_REGISTRY
      value: "$INTERNAL_DOCKER_REGISTRY"
    - name: IMAGE_PULL_SERVER
      value: "$IMAGE_PULL_SERVER"
    - name: IMAGE_PULL_USERNAME
      value: "$IMAGE_PULL_USERNAME"
    - name: IMAGE_PULL_PASSWORD
      value: "$IMAGE_PULL_PASSWORD"
    - name: VSPHERE_USER
      value: "${VSPHERE_USER}"
    - name: VSPHERE_PWD
      value: "${VSPHERE_PWD}"
    - name: VSPHERE_HOST_IP
      value: "${VSPHERE_HOST_IP}"
    - name: IBMCLOUD_API_KEY
      value: "${IBMCLOUD_API_KEY}"
    - name: CONTROL_PLANE_URL
      value: "${CONTROL_PLANE_URL}"
    - name: DS_VERSION
      value: "${DS_VERSION}"
    - name: DS_BUILD
      value: "${DS_BUILD}"
    - name: NAMESPACE
      value: "${NAMESPACE}"
    - name: NO_OF_NODES
      value: "${NO_OF_NODES}"
    - name: DATA_SERVICE
      value: "${DATA_SERVICE}"
    - name: DEPLOY_ALL_VERSIONS
      value: "${DEPLOY_ALL_VERSIONS}"
    - name: DEPLOY_ALL_IMAGES
      value: "${DEPLOY_ALL_IMAGES}"
    - name: DEPLOY_ALL_DATASERVICE
      value: "${DEPLOY_ALL_DATASERVICE}"
    - name: GCP_PROJECT_ID
      value: "${GCP_PROJECT_ID}"
    - name: PDS_USERNAME
      value: "${PDS_USERNAME}"
    - name: PDS_PASSWORD
      value: "${PDS_PASSWORD}"
    - name: PDS_CLIENT_SECRET
      value: "${PDS_CLIENT_SECRET}"
    - name: PDS_CLIENT_ID
      value: "${PDS_CLIENT_ID}"
    - name: PDS_PARAM_CM
      value: "${PDS_PARAM_CM}"
    - name: PDS_ISSUER_URL
      value: "${PDS_ISSUER_URL}"
    - name: CLUSTER_TYPE
      value: "${CLUSTER_TYPE}"
    - name: TARGET_KUBECONFIG
      value: "${TARGET_KUBECONFIG}"
    - name: TARGET_CLUSTER_NAME
      value: "${TARGET_CLUSTER_NAME}"
    - name: PX_ORACLE_user_ocid
      value: "${PX_ORACLE_user_ocid}"
    - name: PX_ORACLE_fingerprint
      value: "${PX_ORACLE_fingerprint}"
    - name: PX_ORACLE_private_key_path
      value: "${ORACLE_API_KEY}"
    - name: INSTANCE_GROUP
      value: "${INSTANCE_GROUP}"
    - name: LOGGLY_API_TOKEN
      value: "${LOGGLY_API_TOKEN}"
    - name: PODMETRIC_METERING_INTERVAL_MINUTES
      value: "${PODMETRIC_METERING_INTERVAL_MINUTES}"
    - name: TARGET_PXBACKUP_VERSION
      value: "${TARGET_PXBACKUP_VERSION}"
    - name: TARGET_STORK_VERSION
      value: "${TARGET_STORK_VERSION}"
    - name: PX_BACKUP_HELM_REPO_BRANCH
      value: "${PX_BACKUP_HELM_REPO_BRANCH}"
    - name: BACKUP_TYPE
      value: "${BACKUP_TYPE}"
    - name: NFS_SERVER_ADDR
      value: "${NFS_SERVER_ADDR}"
    - name: NFS_SUB_PATH
      value: "${NFS_SUB_PATH}"
    - name: NFS_MOUNT_OPTION
      value: "${NFS_MOUNT_OPTION}"
    - name: NFS_PATH
      value: "${NFS_PATH}"
    - name: SKIP_PX_OPERATOR_UPGRADE
      value: "${SKIP_PX_OPERATOR_UPGRADE}"
    - name: VOLUME_SNAPSHOT_CLASS
      value: "${VOLUME_SNAPSHOT_CLASS}"
    - name: S3_SSE_TYPE
      value: "${S3_SSE_TYPE}"
    - name: S3_POLICY_SID
      value: "${S3_POLICY_SID}"
    - name: S3_ENCRYPTION_POLICY
      value: "${S3_ENCRYPTION_POLICY}"
  volumes: [${VOLUMES}]
  restartPolicy: Never
  serviceAccountName: torpedo-account


EOF

cat torpedo.yaml

echo "Deploying torpedo pod..."
kubectl apply -f torpedo.yaml

cat >> taas-service.yaml <<EOF
---
apiVersion: v1
kind: Service
metadata:
  name: taas-serv
spec:
  selector:
    app: torpedo
  ports:
    - name: http
      port: 8080
      targetPort: 8080
      protocol: TCP
  type: NodePort
EOF

kubectl apply -f taas-service.yaml
sleep 180
echo "taas service created"

echo "Waiting for torpedo to start running"

function describe_pod_then_exit {
  echo "Pod description:"
  kubectl describe pod torpedo
  exit 1
}

first_iteration=true

for i in $(seq 1 600); do
  echo "Iteration: $i"
  state=$(kubectl get pod torpedo | grep -v NAME | awk '{print $3}')

  if [ "$state" == "Error" ]; then
    echo "Error: Torpedo finished with $state state"
    describe_pod_then_exit
  elif [ "$state" == "Running" ]; then
    echo "Torpedo is up and running"
    exit 0
  fi

  sleep 1
done

echo "Error: Failed to wait for torpedo to start running..."
describe_pod_then_exit
