2015-03-06 22:34:38 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2015-05-01 16:19:44 +00:00
|
|
|
# Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-06 22:34:38 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# Common utilites for kube-up/kube-down
|
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o nounset
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
|
|
|
|
|
2015-04-17 17:19:53 +00:00
|
|
|
DEFAULT_KUBECONFIG="${HOME}/.kube/config"
|
|
|
|
|
2015-10-12 23:11:12 +00:00
|
|
|
# KUBE_VERSION_REGEX matches things like "v1.2.3"
|
|
|
|
KUBE_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
|
|
|
|
|
|
|
|
# KUBE_CI_VERSION_REGEX matches things like "v1.2.3-alpha.4.56+abcdefg"
|
|
|
|
KUBE_CI_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)-(.*)$"
|
|
|
|
|
|
|
|
|
2015-03-06 22:34:38 +00:00
|
|
|
# Generate kubeconfig data for the created cluster.
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBE_USER
|
|
|
|
# KUBE_PASSWORD
|
|
|
|
# KUBE_MASTER_IP
|
|
|
|
# KUBECONFIG
|
2015-04-10 00:07:24 +00:00
|
|
|
# CONTEXT
|
2015-03-06 22:34:38 +00:00
|
|
|
#
|
2015-04-28 05:39:39 +00:00
|
|
|
# If the apiserver supports bearer auth, also provide:
|
|
|
|
# KUBE_BEARER_TOKEN
|
|
|
|
#
|
2015-04-10 00:07:24 +00:00
|
|
|
# The following can be omitted for --insecure-skip-tls-verify
|
2015-03-06 22:34:38 +00:00
|
|
|
# KUBE_CERT
|
|
|
|
# KUBE_KEY
|
|
|
|
# CA_CERT
|
|
|
|
function create-kubeconfig() {
|
|
|
|
local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
|
|
|
|
|
2015-04-17 23:22:07 +00:00
|
|
|
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
|
2015-03-06 22:49:25 +00:00
|
|
|
# KUBECONFIG determines the file we write to, but it may not exist yet
|
|
|
|
if [[ ! -e "${KUBECONFIG}" ]]; then
|
|
|
|
mkdir -p $(dirname "${KUBECONFIG}")
|
|
|
|
touch "${KUBECONFIG}"
|
|
|
|
fi
|
2015-04-10 00:07:24 +00:00
|
|
|
local cluster_args=(
|
2015-04-14 08:03:12 +00:00
|
|
|
"--server=${KUBE_SERVER:-https://${KUBE_MASTER_IP}}"
|
2015-04-10 00:07:24 +00:00
|
|
|
)
|
|
|
|
if [[ -z "${CA_CERT:-}" ]]; then
|
|
|
|
cluster_args+=("--insecure-skip-tls-verify=true")
|
|
|
|
else
|
|
|
|
cluster_args+=(
|
|
|
|
"--certificate-authority=${CA_CERT}"
|
|
|
|
"--embed-certs=true"
|
|
|
|
)
|
|
|
|
fi
|
2015-04-28 05:39:39 +00:00
|
|
|
|
2015-04-17 21:04:14 +00:00
|
|
|
local user_args=()
|
2015-04-28 05:39:39 +00:00
|
|
|
if [[ ! -z "${KUBE_BEARER_TOKEN:-}" ]]; then
|
2015-04-17 21:04:14 +00:00
|
|
|
user_args+=(
|
|
|
|
"--token=${KUBE_BEARER_TOKEN}"
|
|
|
|
)
|
2015-06-03 23:54:56 +00:00
|
|
|
elif [[ ! -z "${KUBE_USER:-}" && ! -z "${KUBE_PASSWORD:-}" ]]; then
|
2015-04-17 21:04:14 +00:00
|
|
|
user_args+=(
|
2015-04-10 00:07:24 +00:00
|
|
|
"--username=${KUBE_USER}"
|
|
|
|
"--password=${KUBE_PASSWORD}"
|
2015-04-17 21:04:14 +00:00
|
|
|
)
|
|
|
|
fi
|
2015-04-10 00:07:24 +00:00
|
|
|
if [[ ! -z "${KUBE_CERT:-}" && ! -z "${KUBE_KEY:-}" ]]; then
|
|
|
|
user_args+=(
|
|
|
|
"--client-certificate=${KUBE_CERT}"
|
|
|
|
"--client-key=${KUBE_KEY}"
|
|
|
|
"--embed-certs=true"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
|
|
|
"${kubectl}" config set-cluster "${CONTEXT}" "${cluster_args[@]}"
|
2015-07-24 04:04:47 +00:00
|
|
|
if [[ -n "${user_args[@]:-}" ]]; then
|
|
|
|
"${kubectl}" config set-credentials "${CONTEXT}" "${user_args[@]}"
|
2015-07-24 04:03:23 +00:00
|
|
|
fi
|
2015-03-06 22:34:38 +00:00
|
|
|
"${kubectl}" config set-context "${CONTEXT}" --cluster="${CONTEXT}" --user="${CONTEXT}"
|
|
|
|
"${kubectl}" config use-context "${CONTEXT}" --cluster="${CONTEXT}"
|
|
|
|
|
2015-04-28 05:39:39 +00:00
|
|
|
# If we have a bearer token, also create a credential entry with basic auth
|
|
|
|
# so that it is easy to discover the basic auth password for your cluster
|
|
|
|
# to use in a web browser.
|
2015-06-03 23:54:56 +00:00
|
|
|
if [[ ! -z "${KUBE_BEARER_TOKEN:-}" && ! -z "${KUBE_USER:-}" && ! -z "${KUBE_PASSWORD:-}" ]]; then
|
2015-04-28 05:39:39 +00:00
|
|
|
"${kubectl}" config set-credentials "${CONTEXT}-basic-auth" "--username=${KUBE_USER}" "--password=${KUBE_PASSWORD}"
|
|
|
|
fi
|
|
|
|
|
2015-03-06 22:49:25 +00:00
|
|
|
echo "Wrote config for ${CONTEXT} to ${KUBECONFIG}"
|
2015-03-06 22:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Clear kubeconfig data for a context
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBECONFIG
|
|
|
|
# CONTEXT
|
|
|
|
function clear-kubeconfig() {
|
2015-04-17 23:22:07 +00:00
|
|
|
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
|
2015-03-06 22:34:38 +00:00
|
|
|
local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
|
|
|
|
"${kubectl}" config unset "clusters.${CONTEXT}"
|
|
|
|
"${kubectl}" config unset "users.${CONTEXT}"
|
2015-04-28 05:39:39 +00:00
|
|
|
"${kubectl}" config unset "users.${CONTEXT}-basic-auth"
|
2015-03-06 22:34:38 +00:00
|
|
|
"${kubectl}" config unset "contexts.${CONTEXT}"
|
|
|
|
|
|
|
|
local current
|
|
|
|
current=$("${kubectl}" config view -o template --template='{{ index . "current-context" }}')
|
|
|
|
if [[ "${current}" == "${CONTEXT}" ]]; then
|
|
|
|
"${kubectl}" config unset current-context
|
|
|
|
fi
|
|
|
|
|
2015-03-06 22:49:25 +00:00
|
|
|
echo "Cleared config for ${CONTEXT} from ${KUBECONFIG}"
|
2015-03-06 22:34:38 +00:00
|
|
|
}
|
2015-03-13 19:22:49 +00:00
|
|
|
|
2015-09-02 02:24:46 +00:00
|
|
|
|
|
|
|
function tear_down_alive_resources() {
|
|
|
|
local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
|
2015-09-19 10:36:03 +00:00
|
|
|
"${kubectl}" delete rc --all || true
|
|
|
|
"${kubectl}" delete pods --all || true
|
|
|
|
"${kubectl}" delete svc --all || true
|
|
|
|
"${kubectl}" delete pvc --all || true
|
2015-09-02 02:24:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 19:22:49 +00:00
|
|
|
# Gets username, password for the current-context in kubeconfig, if they exist.
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBECONFIG # if unset, defaults to global
|
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_USER
|
|
|
|
# KUBE_PASSWORD
|
|
|
|
#
|
|
|
|
# KUBE_USER,KUBE_PASSWORD will be empty if no current-context is set, or
|
|
|
|
# the current-context user does not exist or contain basicauth entries.
|
|
|
|
function get-kubeconfig-basicauth() {
|
2015-04-17 23:22:07 +00:00
|
|
|
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
|
2015-03-13 19:22:49 +00:00
|
|
|
# Templates to safely extract the username,password for the current-context
|
|
|
|
# user. The long chain of 'with' commands avoids indexing nil if any of the
|
|
|
|
# entries ("current-context", "contexts"."current-context", "users", etc)
|
|
|
|
# is missing.
|
|
|
|
# Note: we save dot ('.') to $root because the 'with' action overrides it.
|
|
|
|
# See http://golang.org/pkg/text/template/.
|
2015-04-06 18:56:13 +00:00
|
|
|
local username='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "username" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}'
|
|
|
|
local password='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "password" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}'
|
2015-03-13 19:22:49 +00:00
|
|
|
KUBE_USER=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${username}")
|
|
|
|
KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${password}")
|
|
|
|
# Handle empty/missing username|password
|
|
|
|
if [[ "${KUBE_USER}" == '<no value>' || "$KUBE_PASSWORD" == '<no value>' ]]; then
|
|
|
|
KUBE_USER=''
|
|
|
|
KUBE_PASSWORD=''
|
|
|
|
fi
|
|
|
|
}
|
2015-04-17 21:04:14 +00:00
|
|
|
|
2015-08-22 01:47:31 +00:00
|
|
|
# Generate basic auth user and password.
|
|
|
|
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_USER
|
|
|
|
# KUBE_PASSWORD
|
|
|
|
function gen-kube-basicauth() {
|
|
|
|
KUBE_USER=admin
|
|
|
|
KUBE_PASSWORD=$(python -c 'import string,random; print "".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16))')
|
|
|
|
}
|
|
|
|
|
2015-04-17 21:04:14 +00:00
|
|
|
# Get the bearer token for the current-context in kubeconfig if one exists.
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBECONFIG # if unset, defaults to global
|
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_BEARER_TOKEN
|
|
|
|
#
|
|
|
|
# KUBE_BEARER_TOKEN will be empty if no current-context is set, or the
|
|
|
|
# current-context user does not exist or contain a bearer token entry.
|
|
|
|
function get-kubeconfig-bearertoken() {
|
|
|
|
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
|
|
|
|
# Template to safely extract the token for the current-context user.
|
|
|
|
# The long chain of 'with' commands avoids indexing nil if any of the
|
|
|
|
# entries ("current-context", "contexts"."current-context", "users", etc)
|
|
|
|
# is missing.
|
|
|
|
# Note: we save dot ('.') to $root because the 'with' action overrides it.
|
|
|
|
# See http://golang.org/pkg/text/template/.
|
|
|
|
local token='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "token" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}'
|
|
|
|
KUBE_BEARER_TOKEN=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${token}")
|
|
|
|
# Handle empty/missing token
|
|
|
|
if [[ "${KUBE_BEARER_TOKEN}" == '<no value>' ]]; then
|
|
|
|
KUBE_BEARER_TOKEN=''
|
|
|
|
fi
|
|
|
|
}
|
2015-06-01 15:59:12 +00:00
|
|
|
|
2015-08-22 01:47:31 +00:00
|
|
|
# Generate bearer token.
|
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_BEARER_TOKEN
|
|
|
|
function gen-kube-bearertoken() {
|
|
|
|
KUBE_BEARER_TOKEN=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
|
|
|
|
}
|
|
|
|
|
2015-08-13 05:19:29 +00:00
|
|
|
# Get the master IP for the current-context in kubeconfig if one exists.
|
|
|
|
#
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBECONFIG # if unset, defaults to global
|
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_MASTER_URL
|
|
|
|
#
|
|
|
|
# KUBE_MASTER_URL will be empty if no current-context is set, or the
|
|
|
|
# current-context user does not exist or contain a server entry.
|
|
|
|
function detect-master-from-kubeconfig() {
|
|
|
|
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
|
|
|
|
# Template to safely extract the server for the current-context cluster.
|
|
|
|
# The long chain of 'with' commands avoids indexing nil if any of the
|
|
|
|
# entries ("current-context", "contexts"."current-context", "users", etc)
|
|
|
|
# is missing.
|
|
|
|
# Note: we save dot ('.') to $root because the 'with' action overrides it.
|
|
|
|
# See http://golang.org/pkg/text/template/.
|
|
|
|
local server_tpl='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $cluster := .context.cluster }}{{range $element := (index $dot "clusters")}}{{ if eq .name $cluster }}{{ index . "cluster" "server" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}'
|
|
|
|
KUBE_MASTER_URL=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${server_tpl}")
|
|
|
|
# Handle empty/missing server
|
|
|
|
if [[ "${KUBE_MASTER_URL}" == '<no value>' ]]; then
|
|
|
|
KUBE_MASTER_URL=''
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:11:12 +00:00
|
|
|
# Sets KUBE_VERSION variable to the proper version number (e.g. "v1.0.6",
|
|
|
|
# "v1.2.0-alpha.1.881+376438b69c7612") or a version' publication of the form
|
|
|
|
# <bucket>/<version> (e.g. "release/stable",' "ci/latest-1").
|
|
|
|
#
|
|
|
|
# See the docs on getting builds for more information about version
|
|
|
|
# publication.
|
2015-06-01 15:59:12 +00:00
|
|
|
#
|
|
|
|
# Args:
|
|
|
|
# $1 version string from command line
|
|
|
|
# Vars set:
|
|
|
|
# KUBE_VERSION
|
|
|
|
function set_binary_version() {
|
2015-10-12 23:11:12 +00:00
|
|
|
if [[ "${1}" =~ "/" ]]; then
|
|
|
|
KUBE_VERSION=$(gsutil cat gs://kubernetes-release/${1}.txt)
|
2015-06-01 15:59:12 +00:00
|
|
|
else
|
|
|
|
KUBE_VERSION=${1}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Figure out which binary use on the server and assure it is available.
|
|
|
|
# If KUBE_VERSION is specified use binaries specified by it, otherwise
|
|
|
|
# use local dev binaries.
|
|
|
|
#
|
|
|
|
# Assumed vars:
|
2015-10-12 23:11:12 +00:00
|
|
|
# KUBE_VERSION_REGEX
|
|
|
|
# KUBE_CI_VERSION_REGEX
|
2015-06-01 15:59:12 +00:00
|
|
|
# Vars set:
|
2015-10-12 23:11:12 +00:00
|
|
|
# KUBE_TAR_URL
|
|
|
|
# KUBE_TAR_HASH
|
2015-06-01 15:59:12 +00:00
|
|
|
# SERVER_BINARY_TAR_URL
|
2015-06-18 18:31:21 +00:00
|
|
|
# SERVER_BINARY_TAR_HASH
|
2015-06-01 15:59:12 +00:00
|
|
|
# SALT_TAR_URL
|
2015-06-18 18:31:21 +00:00
|
|
|
# SALT_TAR_HASH
|
2015-06-01 15:59:12 +00:00
|
|
|
function tars_from_version() {
|
|
|
|
if [[ -z "${KUBE_VERSION-}" ]]; then
|
|
|
|
find-release-tars
|
|
|
|
upload-server-tars
|
|
|
|
elif [[ ${KUBE_VERSION} =~ ${KUBE_VERSION_REGEX} ]]; then
|
2015-10-12 23:11:12 +00:00
|
|
|
KUBE_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes.tar.gz"
|
2015-06-01 15:59:12 +00:00
|
|
|
SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
|
|
|
|
SALT_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes-salt.tar.gz"
|
|
|
|
elif [[ ${KUBE_VERSION} =~ ${KUBE_CI_VERSION_REGEX} ]]; then
|
2015-10-12 23:11:12 +00:00
|
|
|
KUBE_TAR_URL="https://storage.googleapis.com/kubernetes-release/ci/${KUBE_VERSION}/kubernetes.tar.gz"
|
2015-06-01 15:59:12 +00:00
|
|
|
SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release/ci/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
|
|
|
|
SALT_TAR_URL="https://storage.googleapis.com/kubernetes-release/ci/${KUBE_VERSION}/kubernetes-salt.tar.gz"
|
|
|
|
else
|
|
|
|
echo "Version doesn't match regexp" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-10-12 23:11:12 +00:00
|
|
|
until KUBE_TAR_HASH=$(curl --fail --silent "${KUBE_TAR_URL}.sha1"); do
|
|
|
|
echo "Failure trying to curl release .sha1"
|
|
|
|
done
|
2015-06-18 18:31:21 +00:00
|
|
|
until SERVER_BINARY_TAR_HASH=$(curl --fail --silent "${SERVER_BINARY_TAR_URL}.sha1"); do
|
|
|
|
echo "Failure trying to curl release .sha1"
|
|
|
|
done
|
|
|
|
until SALT_TAR_HASH=$(curl --fail --silent "${SALT_TAR_URL}.sha1"); do
|
|
|
|
echo "Failure trying to curl Salt tar .sha1"
|
|
|
|
done
|
|
|
|
|
2015-10-12 23:11:12 +00:00
|
|
|
if ! curl -Ss --range 0-1 "${KUBE_TAR_URL}" >&/dev/null; then
|
|
|
|
echo "Can't find release at ${KUBE_TAR_URL}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-06-18 18:31:21 +00:00
|
|
|
if ! curl -Ss --range 0-1 "${SERVER_BINARY_TAR_URL}" >&/dev/null; then
|
2015-06-01 15:59:12 +00:00
|
|
|
echo "Can't find release at ${SERVER_BINARY_TAR_URL}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-06-18 18:31:21 +00:00
|
|
|
if ! curl -Ss --range 0-1 "${SALT_TAR_URL}" >&/dev/null; then
|
|
|
|
echo "Can't find Salt tar at ${SALT_TAR_URL}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-06-01 15:59:12 +00:00
|
|
|
}
|
2015-09-02 02:24:46 +00:00
|
|
|
|