-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkubectl.sh
executable file
·53 lines (42 loc) · 1.47 KB
/
kubectl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
#
# Load k8s cluster credentials for kubectl and run a given command.
set -x
set -e
set -u
USAGE="$0 <project> <cluster> <command>"
PROJECT=${1:?Please provide the project: $USAGE}
CLUSTER=${2:?Please provide the cluster: $USAGE}
shift 2
# Add gcloud to PATH.
source "${HOME}/google-cloud-sdk/path.bash.inc"
KEYFILE="/tmp/${PROJECT}.json"
if [[ ! -f "${KEYFILE}" ]] ; then
echo "ERROR: service account key file for $PROJECT not found!"
exit 1
fi
# All operations are performed as the service account named in KEYFILE.
# For all options see:
# https://cloud.google.com/sdk/gcloud/reference/auth/activate-service-account
gcloud auth activate-service-account --key-file "${KEYFILE}"
# For all options see:
# https://cloud.google.com/sdk/gcloud/reference/config/set
gcloud config set core/project "${PROJECT}"
gcloud config set core/disable_prompts true
gcloud config set core/verbosity debug
# Identify the cluster ZONE.
LOCATION=$( gcloud container clusters list \
--format='table[no-heading](location)' \
--filter "name='$CLUSTER'" )
if [[ -z "$LOCATION" ]] ; then
echo "ERROR: could not find zone for $CLUSTER"
exit 1
fi
# Get credentials from the cluster. (Try zone and then region)
gcloud container clusters get-credentials $CLUSTER --zone $LOCATION \
|| gcloud container clusters get-credentials $CLUSTER --region $LOCATION
# Make the project and cluster available to sub-commands.
export PROJECT
export CLUSTER
# Run command given on the rest of the command line.
$@