Repository: kubernetes/kubernetes
File: cluster/gce/util.sh (Shell)

Code before:
    fi
    # Now we can safely delete the VM.
    gcloud compute instances delete \
      --project "${PROJECT}" \
      --quiet \
      --delete-disks all \
      --zone "${ZONE}" \
      "${REPLICA_NAME}" &
  fi

  # Wait for last batch of jobs
  kube::util::wait-for-jobs || {
    echo -e "Failed to delete instance group(s) and replica(s)." >&2
  }

  if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
    # Deliberately do not quote, do not change unless a bug is found
    # shellcheck disable=SC2068
    for template in ${templates[@]:-}; do
      {
        if gcloud compute instance-templates describe --project "${PROJECT}" "${template}" &>/dev/null; then
          gcloud compute instance-templates delete \
            --project "${PROJECT}" \
            --quiet \
            "${template}" &
        fi
      } &
    done
  fi

  # Delete the master replica pd (possibly leaked by kube-up if master create failed).
  # TODO(jszczepkowski): remove also possibly leaked replicas' pds
  local -r replica_pd="${REPLICA_NAME:-${MASTER_NAME}}-pd"
  if gcloud compute disks describe "${replica_pd}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
    gcloud compute disks delete \
      --project "${PROJECT}" \
      --quiet \
      --zone "${ZONE}" \
      "${replica_pd}" &&
    echo "Deleted ${replica_pd}" & # "gcloud compute disks delete --quiet" does not print anything, in contrast to other sub-commands.
  fi

  # Wait for last batch of jobs
  kube::util::wait-for-jobs || {
    echo -e "Failed to delete instance templates and/or replica pd." >&2
  }

  # Check if this are any remaining master replicas.
  local REMAINING_MASTER_COUNT
  REMAINING_MASTER_COUNT=$(gcloud compute instances list \
    --project "${PROJECT}" \

Review comment (suggestion):
I think this commands blocks waiting for the API operation to finish, that means that `kube::util::wait-for-jobs ` will do what we expect to do

Code after:
    fi
    # Detach replica from LB if needed.
    if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
      remove-from-internal-loadbalancer "${REPLICA_NAME}" "${ZONE}"
    fi
    # Now we can safely delete the VM.
    gcloud compute instances delete \
      --project "${PROJECT}" \
      --quiet \
      --delete-disks all \
      --zone "${ZONE}" \
      "${REPLICA_NAME}" &
  fi

  # Wait for last batch of jobs
  kube::util::wait-for-jobs || {
    echo -e "Failed to delete instance group(s) and replica(s)." >&2
  }

  if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
    # Deliberately do not quote, do not change unless a bug is found
    # shellcheck disable=SC2068
    for template in ${templates[@]:-}; do
      {
        if gcloud compute instance-templates describe --project "${PROJECT}" "${template}" &>/dev/null; then
          gcloud compute instance-templates delete \
            --project "${PROJECT}" \
            --quiet \
            "${template}" &
        fi
      } &
    done
  fi

  # Delete the master replica pd (possibly leaked by kube-up if master create failed).
  # TODO(jszczepkowski): remove also possibly leaked replicas' pds
  local -r replica_pd="${REPLICA_NAME:-${MASTER_NAME}}-pd"
  if gcloud compute disks describe "${replica_pd}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
    gcloud compute disks delete \
      --project "${PROJECT}" \
      --quiet \
      --zone "${ZONE}" \
      "${replica_pd}" &&
    echo "Deleted ${replica_pd}" & # "gcloud compute disks delete --quiet" does not print anything, in contrast to other sub-commands.
  fi

  # Wait for last batch of jobs
  kube::util::wait-for-jobs || {
    echo -e "Failed to delete instance templates and/or replica pd." >&2
  }