Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Forked from jbeda/gke-kubecfg.sh
Last active August 29, 2015 14:10

Revisions

  1. @jbeda jbeda revised this gist Nov 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gke-kubecfg.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ 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]
    $0 [--project=<project>] --zone <zone> --cluster-name <cluster> [kubecfg args]
    EOF
    exit 1
    }
  2. @jbeda jbeda created this gist Nov 5, 2014.
    85 changes: 85 additions & 0 deletions gke-kubecfg.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/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[@]}}"