Skip to content

Commit

Permalink
refactor: remove use of gcloud beta, factor our kubectl pip comomand …
Browse files Browse the repository at this point in the history
…for APPLY, format

Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem committed Jun 4, 2020
1 parent 4f6747e commit 068cb68
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions kustomize/kustomize.bash
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
#!/bin/bash

# If there is no current context, get one.
if [[ $(kubectl config current-context 2>/dev/null) == "" || "$FORCE_GET_CREDENTIALS" = true ]]; then
# This tries to read environment variables. If not set, it grabs from gcloud
cluster=${CLOUDSDK_CONTAINER_CLUSTER:-$(gcloud config get-value container/cluster 2>/dev/null)}
region=${CLOUDSDK_COMPUTE_REGION:-$(gcloud config get-value compute/region 2>/dev/null)}
zone=${CLOUDSDK_COMPUTE_ZONE:-$(gcloud config get-value compute/zone 2>/dev/null)}
project=${GCLOUD_PROJECT:-$(gcloud config get-value core/project 2>/dev/null)}

function var_usage() {
cat <<EOF
No cluster is set. To set the cluster (and the region/zone where it is found), set the environment variables
function var_usage() {
cat <<EOF
No cluster is set. To set the cluster (and the region/zone where it is found), set the environment variables
CLOUDSDK_COMPUTE_REGION=<cluster region> (regional clusters)
CLOUDSDK_COMPUTE_ZONE=<cluster zone> (zonal clusters)
CLOUDSDK_COMPUTE_ZONE=<cluster zone>
CLOUDSDK_CONTAINER_CLUSTER=<cluster name>
EOF
exit 1
}

[[ -z "$cluster" ]] && var_usage
[ ! "$zone" -o "$region" ] && var_usage

if [ -n "$region" ]; then
echo "Running: gcloud config set container/use_v1_api_client false"
gcloud config set container/use_v1_api_client false
echo "Running: gcloud beta container clusters get-credentials --project=\"$project\" --region=\"$region\" \"$cluster\""
gcloud beta container clusters get-credentials --project="$project" --region="$region" "$cluster" || exit
else
echo "Running: gcloud container clusters get-credentials --project=\"$project\" --zone=\"$zone\" \"$cluster\""
gcloud container clusters get-credentials --project="$project" --zone="$zone" "$cluster" || exit
fi

exit 1
}

cluster=${CLOUDSDK_CONTAINER_CLUSTER:-$(gcloud config get-value container/cluster 2>/dev/null)}
region=${CLOUDSDK_COMPUTE_REGION:-$(gcloud config get-value compute/region 2>/dev/null)}
zone=${CLOUDSDK_COMPUTE_ZONE:-$(gcloud config get-value compute/zone 2>/dev/null)}
project=${CLOUDSDK_CORE_PROJECT:-$(gcloud config get-value core/project 2>/dev/null)}

[[ -z "$cluster" ]] && var_usage
[ ! "$zone" -o "$region" ] && var_usage

if [ -n "$region" ]; then
echo "Running: gcloud container clusters get-credentials --project=\"$project\" --region=\"$region\" \"$cluster\""
gcloud container clusters get-credentials --project="$project" --region="$region" "$cluster" || exit
else
echo "Running: gcloud container clusters get-credentials --project=\"$project\" --zone=\"$zone\" \"$cluster\""
gcloud container clusters get-credentials --project="$project" --zone="$zone" "$cluster" || exit
fi

if [ "$APPLY" = true ]; then
KUBECTL_PIPE="kubectl apply -f -"

if [ "$DEBUG" = true ]; then
echo "Running: kustomize $@ | kubectl apply -f -"
echo "Running: kustomize $@ | $KUBECTL_PIPE"
fi

kustomize "$@" | kubectl apply -f -
kustomize "$@" | $KUBECTL_PIPE

else
if [ "$DEBUG" = true ]; then
echo "Running: kustomize $@"
Expand Down

0 comments on commit 068cb68

Please sign in to comment.