# This is a workflow that is manually triggered to deploy supported components other than applications

name: Manual Deployment

# Controls when the action will run. Workflow runs when manually triggered using the UI
# or API.
on:
    workflow_dispatch:
        # Inputs the workflow accepts.
        inputs:
            dry_run:
                description: |
                    If set to 'yes', run all deployment in dry-run mode.
                    Otherwise, set to 'no'.
                default: "yes"
                required: true
            kubectl_version:
                description: "Version of kubectl to be installed"
                default: "1.19.9"
                required: true
            # Use following command to generate base64 code:
            #   cat <<EOF | base64 | tr -d '\n' | xargs echo
            #   <your_autoscalegroups_yaml>
            #   EOF
            auto_scale_groups:
                description: |
                    The auto scale groups defined in yaml to apply given as a string from base64 encoding.
                    See the comments in .github/workflows/manual.yml for how to get a base64 encoded yaml.
                    If this is set to empty, the nodeautoscaler/deploy/auto-scale-groups-configmap.yml in repo will be used.
                default: ""
                required: false
            rancher_url:
                description: |
                    The URL of target rancher when node auto scaler uses Rancher as backend node provisioner.
                    If set to empty, the value of secret RANCHER_URL will be used
                default: ""
                required: false

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
    # Deploy components
    deploy:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
        # Environment variables
        env:
            DRY_RUN: ${{ github.event.inputs.dry_run }}
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
            - name: Setup kubectl with empty input kubeconfig
              uses: ThomasKliszowski/setup-kubectl@v1
              with:
                  # Base64 encoded kube config
                  kube-config: ${{ secrets.KUBECONFIG }}
                  # Kube version used to download kubectl executable, defaults to latest
                  kube-version: ${{ github.event.inputs.kubectl_version }}
            # Check installed kubectl
            - name: Check installed kubectl
              run: |
                  kubectl version
            # Check configured kubeconfig
            - name: Check and persist kubeconfig
              id: step_kubeconfig
              run: |
                  kubectl config view
            # Checkout local repo
            - name: Checkout local repo
              uses: actions/checkout@v3
            # Replace ConfigMap of auto scale groups with input value
            - name: Generate configmap with auto scale groups
              env:
                  AUTO_SCALE_GROUPS: ${{ github.event.inputs.auto_scale_groups }}
              run: |
                  echo
                  echo "********** Got input auto scale groups ***********"
                  echo
                  echo ${AUTO_SCALE_GROUPS} | base64 -d | tee autoscalegroups.yaml
                  echo
                  echo "********** Generate configmap ************"
                  echo
                  kubectl create cm auto-scale-groups -n node-auto-scaler --from-file=autoscalegroups.yaml \
                     --dry-run=client -o yaml | tee ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/auto-scale-groups-configmap.yaml
            # Apply configmap in the repo
            - name: Deploy configmap of auto scale groups
              run: |
                  if [ "$DRY_RUN" == 'yes' ]
                  then
                    echo
                    echo "********** Deploy configmap in dry-run mode ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/auto-scale-groups-configmap.yaml \
                      --dry-run=client -o yaml
                  else
                    echo
                    echo "********** Deploy configmap ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/auto-scale-groups-configmap.yaml -o yaml
                  fi
            - name: Deploy rancher secret
              env:
                  RANCHER_URL: ${{ github.event.inputs.rancher_url }}
                  RANCHER_TOKEN: ${{ secrets.RANCHER_TOKEN }}
                  ENV_RANCHER_URL: ${{ secrets.RANCHER_URL }}
              run: |
                  if [ "$RANCHER_URL" == '' ]
                  then
                    echo
                    echo "********** No rancher_url input, use environment secret *************"
                    echo
                    RANCHER_URL=${ENV_RANCHER_URL}
                  fi
                  ENCODE_URL=$(echo ${RANCHER_URL} | tr -d '\n' | base64 | tr -d '\n')
                  sed -i "s/{{ RANCHER_URL }}/${ENCODE_URL}/g" ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/rancher-secret.yaml
                  ENCODE_TOKEN=$(echo ${RANCHER_TOKEN} | tr -d '\n' | base64 | tr -d '\n')
                  sed -i "s/{{ RANCHER_TOKEN }}/${ENCODE_TOKEN}/g" ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/rancher-secret.yaml
                  if [ "$DRY_RUN" == 'yes' ]
                  then
                    echo
                    echo "********** Deploy secret in dry-run mode ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/rancher-secret.yaml \
                      --dry-run=client
                  else
                    echo
                    echo "********** Deploy secret ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/rancher-secret.yaml
                  fi
            - name: Deploy node auto scaler
              run: |
                  if [ "$DRY_RUN" == 'yes' ]
                  then
                    echo
                    echo "********** Deploy node auto scaler in dry-run mode ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/node-auto-scaler-manifests.yaml \
                      --dry-run=client -o yaml
                  else
                    echo
                    echo "********** Deploy node auto scaler ************"
                    echo
                    kubectl apply -f ${GITHUB_WORKSPACE}/nodeautoscaler/deploy/node-auto-scaler-manifests.yaml -o yaml
                    kubectl -n node-auto-scaler rollout restart deploy node-auto-scaler
                  fi
