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
|
2015-10-26 17:38:53 +00:00
|
|
|
current=$("${kubectl}" config view -o jsonpath='{.current-context}')
|
2015-03-06 22:34:38 +00:00
|
|
|
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
|
2015-10-26 17:38:53 +00:00
|
|
|
# KUBE_CONTEXT # if unset, defaults to current-context
|
2015-03-13 19:22:49 +00:00
|
|
|
#
|
|
|
|
# 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-10-26 17:38:53 +00:00
|
|
|
|
|
|
|
local cc="current-context"
|
|
|
|
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
|
|
|
|
cc="${KUBE_CONTEXT}"
|
2015-03-13 19:22:49 +00:00
|
|
|
fi
|
2015-10-26 17:38:53 +00:00
|
|
|
local user=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.user}")
|
|
|
|
KUBE_USER=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.username}")
|
|
|
|
KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.password}")
|
2015-03-13 19:22:49 +00:00
|
|
|
}
|
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
|
2015-10-26 17:38:53 +00:00
|
|
|
# KUBE_CONTEXT # if unset, defaults to current-context
|
2015-04-17 21:04:14 +00:00
|
|
|
#
|
|
|
|
# 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}
|
2015-10-26 17:38:53 +00:00
|
|
|
|
|
|
|
local cc="current-context"
|
|
|
|
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
|
|
|
|
cc="${KUBE_CONTEXT}"
|
2015-04-17 21:04:14 +00:00
|
|
|
fi
|
2015-10-26 17:38:53 +00:00
|
|
|
local user=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.user}")
|
|
|
|
KUBE_BEARER_TOKEN=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.token}")
|
2015-04-17 21:04:14 +00:00
|
|
|
}
|
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-10-26 17:38:53 +00:00
|
|
|
|
|
|
|
function load-or-gen-kube-basicauth() {
|
|
|
|
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
|
|
|
|
get-kubeconfig-basicauth
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "${KUBE_USER:-}" || -z "${KUBE_PASSWORD:-}" ]]; then
|
|
|
|
gen-kube-basicauth
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function load-or-gen-kube-bearertoken() {
|
|
|
|
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
|
|
|
|
get-kubeconfig-bearertoken
|
|
|
|
fi
|
|
|
|
if [[ -z "${KUBE_BEARER_TOKEN:-}" ]]; then
|
|
|
|
gen-kube-bearertoken
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
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
|
2015-10-26 17:38:53 +00:00
|
|
|
# KUBE_CONTEXT # if unset, defaults to current-context
|
2015-08-13 05:19:29 +00:00
|
|
|
#
|
|
|
|
# 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}
|
2015-10-26 17:38:53 +00:00
|
|
|
|
|
|
|
local cc="current-context"
|
|
|
|
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
|
|
|
|
cc="${KUBE_CONTEXT}"
|
2015-08-13 05:19:29 +00:00
|
|
|
fi
|
2015-10-26 17:38:53 +00:00
|
|
|
local cluster=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.cluster}")
|
|
|
|
KUBE_MASTER_URL=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.clusters[?(@.name == \"${cluster}\")].cluster.server}")
|
2015-08-13 05:19:29 +00:00
|
|
|
}
|
|
|
|
|
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-16 19:34:18 +00:00
|
|
|
# KUBE_VERSION
|
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_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
|
|
|
|
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
|
|
|
|
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-16 19:34:18 +00:00
|
|
|
if ! SERVER_BINARY_TAR_HASH=$(curl -Ss --fail "${SERVER_BINARY_TAR_URL}.sha1"); then
|
2015-06-18 18:31:21 +00:00
|
|
|
echo "Failure trying to curl release .sha1"
|
2015-10-16 19:34:18 +00:00
|
|
|
fi
|
|
|
|
if ! SALT_TAR_HASH=$(curl -Ss --fail "${SALT_TAR_URL}.sha1"); then
|
2015-06-18 18:31:21 +00:00
|
|
|
echo "Failure trying to curl Salt tar .sha1"
|
2015-10-12 23:11:12 +00:00
|
|
|
fi
|
2015-10-16 19:34:18 +00:00
|
|
|
|
|
|
|
if ! curl -Ss --head "${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-10-16 19:34:18 +00:00
|
|
|
if ! curl -Ss --head "${SALT_TAR_URL}" >&/dev/null; then
|
2015-06-18 18:31:21 +00:00
|
|
|
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
|
|
|
|
2015-10-16 18:28:00 +00:00
|
|
|
# Verify and find the various tar files that we are going to use on the server.
|
|
|
|
#
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBE_ROOT
|
|
|
|
# Vars set:
|
|
|
|
# SERVER_BINARY_TAR
|
|
|
|
# SALT_TAR
|
|
|
|
function find-release-tars() {
|
|
|
|
SERVER_BINARY_TAR="${KUBE_ROOT}/server/kubernetes-server-linux-amd64.tar.gz"
|
|
|
|
if [[ ! -f "$SERVER_BINARY_TAR" ]]; then
|
|
|
|
SERVER_BINARY_TAR="${KUBE_ROOT}/_output/release-tars/kubernetes-server-linux-amd64.tar.gz"
|
|
|
|
fi
|
|
|
|
if [[ ! -f "$SERVER_BINARY_TAR" ]]; then
|
|
|
|
echo "!!! Cannot find kubernetes-server-linux-amd64.tar.gz" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
SALT_TAR="${KUBE_ROOT}/server/kubernetes-salt.tar.gz"
|
|
|
|
if [[ ! -f "$SALT_TAR" ]]; then
|
|
|
|
SALT_TAR="${KUBE_ROOT}/_output/release-tars/kubernetes-salt.tar.gz"
|
|
|
|
fi
|
|
|
|
if [[ ! -f "$SALT_TAR" ]]; then
|
|
|
|
echo "!!! Cannot find kubernetes-salt.tar.gz" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|