Commit 3fb943be authored by Hordur Freyr Yngvason's avatar Hordur Freyr Yngvason Committed by Thong Kuah
Browse files

fix: Initialize DB on stable track

In https://gitlab.com/gitlab-org/gitlab/issues/30465 we found that
non-stable releasese, e.g. canary and incremental rollouts, were not
working as intended when DB_INITIALIZE was set.

This was due to `helm upgrade` "failing" if no resources will be
created, see https://github.com/helm/helm/issues/4670. This affected us
because in the DB_INITIALIZE case, we do not create any deployments nor
do we activate any services.

The solution proposed in this commit is to always run the database
initialization step on the stable track, with services and postgres
configured accordingly. The fact that initializeCommand is set should be
enough to prevent deployments (this is how the chart works), but for
additional safety we set replicas to 0.
parent 403789c3
Loading
Loading
Loading
Loading
+48 −56
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ function deploy() {
  track="${1-stable}"
  percentage="${2:-100}"
  name=$(deploy_name "$track")
  stable_name=$(deploy_name stable)

  if [[ -z "$CI_COMMIT_TAG" ]]; then
    image_repository=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG}
@@ -145,12 +146,12 @@ function deploy() {
    echo "No helm values file found at ${helm_values_file@Q}"
  fi

  if [[ -n "$DB_INITIALIZE" && -z "$(helm ls -q "^$stable_name$")" ]]; then
    echo "Initializing service URL and database. No deployment will be created"
    # shellcheck disable=SC2086 # HELM_UPGRADE_EXTRA_ARGS -- double quote variables to prevent globbing
  if [[ -n "$DB_INITIALIZE" && -z "$(helm ls -q "^$name$")" ]]; then
    echo "Deploying first release with database initialization..."
    helm upgrade --install \
      --wait \
      --set service.enabled="$service_enabled" \
      --set service.enabled="true" \
      --set gitlab.app="$CI_PROJECT_PATH_SLUG" \
      --set gitlab.env="$CI_ENVIRONMENT_SLUG" \
      --set gitlab.envName="$CI_ENVIRONMENT_NAME" \
@@ -160,7 +161,7 @@ function deploy() {
      --set image.tag="$image_tag" \
      --set image.pullPolicy=IfNotPresent \
      --set image.secrets[0].name="$secret_name" \
      --set application.track="$track" \
      --set application.track="stable" \
      --set application.database_url="$DATABASE_URL" \
      --set application.secretName="$APPLICATION_SECRET_NAME" \
      --set application.secretChecksum="$APPLICATION_SECRET_CHECKSUM" \
@@ -168,7 +169,7 @@ function deploy() {
      --set service.url="$CI_ENVIRONMENT_URL" \
      --set service.additionalHosts="$additional_hosts" \
      --set replicaCount="$replicas" \
      --set postgresql.enabled="$postgres_enabled" \
      --set postgresql.enabled="$POSTGRES_ENABLED" \
      --set postgresql.managed="$postgres_managed" \
      --set postgresql.managedClassSelector="$postgres_managed_selector" \
      --set postgresql.nameOverride="postgres" \
@@ -182,21 +183,12 @@ function deploy() {
      "${helm_values_args[@]}" \
      $HELM_UPGRADE_EXTRA_ARGS \
      --namespace="$KUBE_NAMESPACE" \
      "$name" \
      "$stable_name" \
      chart/
  fi

    echo "Deploying second release..."
    helm upgrade --reuse-values \
      --wait \
      --set application.initializeCommand="" \
      --set application.migrateCommand="$DB_MIGRATE" \
      "${helm_values_args[@]}" \
      $HELM_UPGRADE_EXTRA_ARGS \
      --namespace="$KUBE_NAMESPACE" \
      "$name" \
      chart/
  else
    echo "Deploying new release..."
  echo "Deploying new $track release..."
  # shellcheck disable=SC2086 # HELM_UPGRADE_EXTRA_ARGS -- double quote variables to prevent globbing
  helm upgrade --install \
    --wait \
    --set service.enabled="$service_enabled" \
@@ -225,6 +217,7 @@ function deploy() {
    --set postgresql.postgresPassword="$POSTGRES_PASSWORD" \
    --set postgresql.postgresDatabase="$POSTGRES_DB" \
    --set postgresql.imageTag="$POSTGRES_VERSION" \
    --set application.initializeCommand="" \
    --set application.migrateCommand="$DB_MIGRATE" \
    --set ingress.modSecurity.enabled="$modsecurity_enabled" \
    --set ingress.modSecurity.secRuleEngine="$AUTO_DEVOPS_MODSECURITY_SEC_RULE_ENGINE" \
@@ -233,7 +226,6 @@ function deploy() {
    --namespace="$KUBE_NAMESPACE" \
    "$name" \
    chart/
  fi

  if [[ -z "$ROLLOUT_STATUS_DISABLED" ]]; then
    kubectl rollout status -n "$KUBE_NAMESPACE" -w "$ROLLOUT_RESOURCE_TYPE/$name"