apiVersion: v1
kind: ConfigMap
metadata:
  name: gitlab-upgrade-check
  namespace: public-service
  labels:
    app: gitlab
    component: gitlab
  annotations:
    "helm.sh/hook": pre-upgrade
    "helm.sh/hook-weight": "-11"
    "helm.sh/hook-delete-policy": before-hook-creation
data:
  runcheck: |
    set -e
    
    notify() {
      echo "$1"
      echo -n "$1 " >> /dev/termination-log
    }
    
    greater_version()
    {
      test "$(printf '%s\n' "$@" | sort -V | tail -n 1)" = "$1";
    }

    secrets_dir="/etc/secrets/postgresql"
    if [ -d "${secrets_dir}" ]; then
      if [ ! "$(ls -A ${secrets_dir}/..data/)" = "" ]; then
        if [ ! -f "${secrets_dir}/postgresql-postgres-password" ] || [ ! -f "${secrets_dir}/postgresql-password" ]; then
          notify "You seem to be upgrading from a previous version of GitLab using the bundled PostgreSQL chart"
          notify "There are some manual steps which need to be performed in order to upgrade the database"
          notify "Please see the upgrade documentation for instructions on how to proceed:"
          notify "https://docs.gitlab.com/charts/installation/upgrade.html"
          exit 1
        fi
      fi
    fi
    MIN_VERSION=13.0
    CHART_MIN_VERSION=4.0
    
    if ! awk 'BEGIN{exit(!(ARGV[1] ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/))}' "$GITLAB_VERSION"; then
      exit 0
    fi
    
    NEW_MAJOR_VERSION=$(echo $GITLAB_VERSION | awk -F "." '{print $1}')
    NEW_MINOR_VERSION=$(echo $GITLAB_VERSION | awk -F "." '{print $1"."$2}')
    
    NEW_CHART_MAJOR_VERSION=$(echo $CHART_VERSION | awk -F "." '{print $1}')
    NEW_CHART_MINOR_VERSION=$(echo $CHART_VERSION | awk -F "." '{print $1"."$2}')
    
    if [ ! -f /chart-info/gitlabVersion ]; then
      notify "It seems you are attempting an unsupported upgrade path."
      notify "Please follow the upgrade documentation at https://docs.gitlab.com/ee/policy/maintenance.html#upgrade-recommendations"
      exit 1
    fi
    
    OLD_VERSION_STRING=$(cat /chart-info/gitlabVersion)
    OLD_CHART_VERSION_STRING=$(cat /chart-info/gitlabChartVersion)
    
    if ! awk 'BEGIN{exit(!(ARGV[1] ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/))}' "$OLD_VERSION_STRING"; then
      exit 0
    fi
    
    OLD_MAJOR_VERSION=$(echo $OLD_VERSION_STRING | awk -F "." '{print $1}')
    OLD_MINOR_VERSION=$(echo $OLD_VERSION_STRING | awk -F "." '{print $1"."$2}')
    OLD_CHART_MAJOR_VERSION=$(echo $OLD_CHART_VERSION_STRING | awk -F "." '{print $1}')
    OLD_CHART_MINOR_VERSION=$(echo $OLD_CHART_VERSION_STRING | awk -F "." '{print $1"."$2}')
    
    if [ ${OLD_MAJOR_VERSION} -lt ${NEW_MAJOR_VERSION} ] || [ ${OLD_CHART_MAJOR_VERSION} -lt ${NEW_CHART_MAJOR_VERSION} ]; then
      if ( ! greater_version $OLD_MINOR_VERSION $MIN_VERSION ) || ( ! greater_version $OLD_CHART_MINOR_VERSION $CHART_MIN_VERSION ); then
        notify "It seems you are upgrading the GitLab Helm Chart from ${OLD_CHART_VERSION_STRING} (GitLab ${OLD_VERSION_STRING}) to ${CHART_VERSION} (GitLab ${GITLAB_VERSION})."
        notify "It is required to upgrade to the latest ${CHART_MIN_VERSION}.x version first before proceeding."
        notify "Please follow the upgrade documentation at https://docs.gitlab.com/charts/releases/4_0.html"
        notify "and upgrade to GitLab Helm Chart version ${CHART_MIN_VERSION}.x before upgrading to ${CHART_VERSION}."
        exit 1
      fi
    fi
