Commit ab10b74b authored by James Fargher's avatar James Fargher
Browse files

Merge branch 'get_replicas_zero' into 'master'

feat: Allow replicas to be set to zero

See merge request gitlab-org/cluster-integration/auto-deploy-image!54
parents 2a50aa57 54803cbb
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -139,6 +139,48 @@ test-get-replicas:
        exit 1
      fi

test-get-replicas-multiple:
  <<: *test-job
  variables:
    GIT_STRATEGY: none
    CI_ENVIRONMENT_SLUG: production
    REPLICAS: "2"
  script:
    - replicas=$(auto-deploy get_replicas "stable" "100")
    - |
      if [[ $replicas != 2 ]]; then
        echo "$replicas should equal 2"
        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
}