Skip to content

Commit

Permalink
max unavailable eq 1 script (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
new23d authored Aug 20, 2024
1 parent 71fd57c commit 375fdcc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
16 changes: 14 additions & 2 deletions discriminat.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ variable "instances_per_zone" {
default = 1
}

variable "mig_update_policy_type" {
type = string
description = "OPPORTUNISTIC or PROACTIVE. Set to OPPORTUNISTIC to prevent a `terraform apply` from initiating and waiting for a rolling update. This may be useful if DiscrimiNAT instances are set to use External IPs and there aren't any spare for new instances to attach to themselves (and warm up their cache) before being brought into service by the load balancer. This wouldn't be a problem if DiscrimiNAT instances are set to route through Google Cloud NAT, however. See the script `rmig-update-maxUnavailable-1.sh` to initiate a rolling update, one-by-one, if setting to OPPORTUNISTIC."
default = "PROACTIVE"
}

variable "mig_target_size" {
type = number
description = "If left unset, automatically sets to the number of zones_names * instances_per_zone."
default = null
}

variable "block-project-ssh-keys" {
type = bool
description = "Strongly suggested to leave this to the default, that is to NOT allow project-wide SSH keys to login into the firewall."
Expand Down Expand Up @@ -193,7 +205,7 @@ resource "google_compute_region_instance_group_manager" "discriminat" {
name = "discriminat-${local.suffix}"
base_instance_name = "discriminat-${local.suffix}"
distribution_policy_zones = local.zones
target_size = length(local.zones) * var.instances_per_zone
target_size = var.mig_target_size == null ? length(local.zones) * var.instances_per_zone : var.mig_target_size

region = var.region
project = var.project_id
Expand All @@ -210,7 +222,7 @@ resource "google_compute_region_instance_group_manager" "discriminat" {
}

update_policy {
type = "PROACTIVE"
type = var.mig_update_policy_type
instance_redistribution_type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = length(local.zones)
Expand Down
42 changes: 42 additions & 0 deletions rmig-update-maxUnavailable-1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## set env vars

export GOOGLE_CLOUD_PROJECT=foo-bar #change-me

export MIG_NAME=discriminat-foo #change-me
export MIG_EXPECTED_SIZE=3 #change-me
export MIG_REGION=europe-west6 #change-me

## env vars done

# this script exists because of the following outstanding issues on gcp, where maxUnavailable cannot be set to 1
# https://issuetracker.google.com/issues/201753226
# https://issuetracker.google.com/issues/243973366

## script follows

CURRENT_SIZE=$(gcloud --format json compute instance-groups managed describe --region $MIG_REGION $MIG_NAME | jq .targetSize)

if [ $CURRENT_SIZE -ne $MIG_EXPECTED_SIZE ]; then
echo -e "\nCURRENT SIZE $CURRENT_SIZE does not match MIG EXPECTED SIZE $MIG_EXPECTED_SIZE\n\nexiting\n" 1>&2
kill -INT $$
fi

echo "EXPECTED and CURRENT SIZE: $MIG_EXPECTED_SIZE"

echo -e "\nPROGRESS 0/$MIG_EXPECTED_SIZE\n"
gcloud compute instance-groups managed wait-until $MIG_NAME --stable --region $MIG_REGION

for i in $(seq 1 $MIG_EXPECTED_SIZE); do
gcloud compute instance-groups managed resize $MIG_NAME --size $(($MIG_EXPECTED_SIZE - 1)) --region $MIG_REGION 1> /dev/null
echo -e "\nPROGRESS $(($i-1)).25/$MIG_EXPECTED_SIZE\n"
gcloud compute instance-groups managed wait-until $MIG_NAME --stable --region $MIG_REGION
echo -e "\nPROGRESS $(($i-1)).5/$MIG_EXPECTED_SIZE\n"
gcloud compute instance-groups managed resize $MIG_NAME --size $MIG_EXPECTED_SIZE --region $MIG_REGION 1> /dev/null
echo -e "\nPROGRESS $(($i-1)).75/$MIG_EXPECTED_SIZE\n"
gcloud compute instance-groups managed wait-until $MIG_NAME --stable --region $MIG_REGION
echo -e "\nPROGRESS $i/$MIG_EXPECTED_SIZE\n"
done

echo "FINAL SIZE: $(gcloud --format json compute instance-groups managed describe --region $MIG_REGION $MIG_NAME | jq .targetSize)"

## script done

0 comments on commit 375fdcc

Please sign in to comment.