Commit 54803cbb authored by Thong Kuah's avatar Thong Kuah
Browse files

feat: Allow replicas to be set to zero

In some cases, we want to set replicas to zero, such as when we want
perform DB dump for migration to a new DB.

Add test for testing that a fraction of a non-zero replica (for
incremental rollouts) have a floor of 1.
parent 08af7988
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -153,6 +153,34 @@ test-get-replicas-multiple:
        exit 1
      fi

test-get-replicas-fraction:
  <<: *test-job
  variables:
    GIT_STRATEGY: none
    CI_ENVIRONMENT_SLUG: production
    REPLICAS: "2"
  script:
    - replicas=$(auto-deploy get_replicas "stable" "25")
    - |
      if [[ $replicas != 1 ]]; then
        echo "$replicas should 1, (25% of 2 is 0.5, so set a floor of 1)"
        exit 1
      fi

test-get-replicas-zero:
  <<: *test-job
  variables:
    GIT_STRATEGY: none
    CI_ENVIRONMENT_SLUG: production
    REPLICAS: "0"
  script:
    - replicas=$(auto-deploy get_replicas "stable" "100")
    - |
      if [[ $replicas != 0 ]]; then
        echo "$replicas should equal 0, as requested"
        exit 1
      fi

test-ensure-namespace:
  <<: *test-job
  variables:
+7 −4
Original line number Diff line number Diff line
@@ -330,7 +330,6 @@ function deploy_name() {
  echo $name
}

# shellcheck disable=SC2004 # $/${} is unnecessary on arithmetic variables.
# shellcheck disable=SC2086 # double quote to prevent globbing
# shellcheck disable=SC2153 # incorrectly thinks replicas vs REPLICAS is a misspelling
function get_replicas() {
@@ -359,12 +358,16 @@ function get_replicas() {
  fi

  local replicas="${new_replicas:-1}"
  replicas="$(($replicas * $percentage / 100))"
  replicas="$((replicas * percentage / 100))"

  # always return at least one replicas
  if [[ $replicas -gt 0 ]]; then
  if [[ $new_replicas == 0 ]]; then
    # If zero replicas requested, then return 0
    echo "$new_replicas"
  elif [[ $replicas -gt 0 ]]; then
    echo "$replicas"
  else
    # Return one if calculated replicas is zero
    # E.g. 25% of 2 replicas is 0 (integer division)
    echo 1
  fi
}