Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Forked from jbeda/gke-kubecfg.sh
Last active August 29, 2015 14:10
Show Gist options
  • Save kiasaki/cc061d1d1ee0ff3968b6 to your computer and use it in GitHub Desktop.
Save kiasaki/cc061d1d1ee0ff3968b6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
PROJECT=
ZONE=
CLUSTER=
usage() {
cat >&2 <<EOF
This scripts helps you use kubecfg against a GKE cluster.
This assumes you've interacted with your GKE cluster enough so that gcloud has
cached the important bits about the cluster.
Usage:
$0 [--project=<project>] --zone <zone> --cluster-name <cluster> [kubecfg args]
EOF
exit 1
}
[[ $# > 1 ]] || usage
if [[ -z "${PROJECT}" ]]; then
PROJECT=$(gcloud config list project | tail -n 1 | cut -f 3 -d ' ')
fi
args=()
while [[ $# > 0 ]]; do
key="$1"
shift
case $key in
--project)
PROJECT="$1"
shift
;;
--zone)
ZONE="$1"
shift
;;
--cluster-name)
CLUSTER="$1"
shift
;;
*)
args+=("$key")
;;
esac
done
[[ -n "${PROJECT}" && -n "${ZONE}" && -n "${CLUSTER}" ]] || usage
CONFIG_PATH="${HOME}/.config/gcloud/container/${PROJECT}.${ZONE}.${CLUSTER}"
[[ -d "${CONFIG_PATH}" ]] || {
echo "Error finding config information for cluster." >&2
echo "Looking in $CONFIG_PATH" >&2
exit 2
}
ENDPOINT_IP=$(cat "${CONFIG_PATH}/cluster.json" | perl -n -e'/"endpoint": "(.*)"/ && print $1')
kubecfg -h https://${ENDPOINT_IP} \
-auth "${CONFIG_PATH}/kubernetes_auth" \
-certificate_authority "${CONFIG_PATH}/ca.crt" \
-client_certificate "${CONFIG_PATH}/kubecfg.crt" \
-client_key "${CONFIG_PATH}/kubecfg.key" \
"${args[@]:+${args[@]}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment