Skip to content

Commit

Permalink
Create a get-kube-binaries script to download client/server tarballs
Browse files Browse the repository at this point in the history
Also fix some errors in get-kube.sh, and remove client architectures we
don't officially support.
  • Loading branch information
ixdy committed Oct 7, 2016
1 parent f93e01d commit 74991aa
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 39 deletions.
199 changes: 199 additions & 0 deletions cluster/get-kube-binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#!/bin/bash

# Copyright 2016 The Kubernetes Authors.
#
# 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.

# This script downloads and installs the Kubernetes client and server binaries.
# It is intended to be called from an extracted Kubernetes release tarball.
#
# We automatically choose the correct client binaries to download.
#
# Options:
# Set KUBERNETES_SERVER_ARCH to choose the server (Kubernetes cluster)
# architecture to download:
# * amd64 [default]
# * arm
# * arm64
# * ppc64le
#
# Set KUBERNETES_SKIP_CONFIRM to skip the installation confirmation prompt.
# Set KUBERNETES_RELEASE_URL to choose where to download binaries from.
# (Defaults to https://storage.googleapis.com/kubernetes-release/release).

set -o errexit
set -o nounset
set -o pipefail

KUBE_ROOT=$(cd $(dirname "${BASH_SOURCE}")/.. && pwd)

KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL:-https://storage.googleapis.com/kubernetes-release/release}"

function detect_kube_release() {
if [[ ! -e "${KUBE_ROOT}/version" ]]; then
echo "Can't determine Kubernetes release." >&2
echo "This script should only be run from a prebuilt Kubernetes release." >&2
echo "Did you mean to use get-kube.sh instead?" >&2
exit 1
fi

KUBERNETES_RELEASE=$(cat "${KUBE_ROOT}/version")
DOWNLOAD_URL_PREFIX="${KUBERNETES_RELEASE_URL}/${KUBERNETES_RELEASE}"
}

function detect_client_info() {
local kernel=$(uname -s)
case "${kernel}" in
Darwin)
CLIENT_PLATFORM="darwin"
;;
Linux)
CLIENT_PLATFORM="linux"
;;
*)
echo "Unknown, unsupported platform: ${kernel}." >&2
echo "Supported platforms: Linux, Darwin." >&2
echo "Bailing out." >&2
exit 2
esac

# TODO: migrate the kube::util::host_platform function out of hack/lib and
# use it here.
local machine=$(uname -m)
case "${machine}" in
x86_64*|i?86_64*|amd64*)
CLIENT_ARCH="amd64"
;;
aarch64*|arm64*)
CLIENT_ARCH="arm64"
;;
arm*)
CLIENT_ARCH="arm"
;;
i?86*)
CLIENT_ARCH="386"
;;
*)
echo "Unknown, unsupported architecture (${machine})." >&2
echo "Supported architectures x86_64, i686, arm, arm64." >&2
echo "Bailing out." >&2
exit 3
;;
esac
}

function md5sum_file() {
if which md5 >/dev/null 2>&1; then
md5 -q "$1"
else
md5sum "$1" | awk '{ print $1 }'
fi
}

function sha1sum_file() {
if which shasum >/dev/null 2>&1; then
shasum -a1 "$1" | awk '{ print $1 }'
else
sha1sum "$1" | awk '{ print $1 }'
fi
}

function download_tarball() {
local -r download_path="$1"
local -r file="$2"
url="${DOWNLOAD_URL_PREFIX}/${file}"
mkdir -p "${download_path}"
if [[ $(which curl) ]]; then
curl -L "${url}" -o "${download_path}/${file}"
elif [[ $(which wget) ]]; then
wget "${url}" -O "${download_path}/${file}"
else
echo "Couldn't find curl or wget. Bailing out." >&2
exit 4
fi
echo
local md5sum=$(md5sum_file "${download_path}/${file}")
echo "md5sum(${file})=${md5sum}"
local sha1sum=$(sha1sum_file "${download_path}/${file}")
echo "sha1sum(${file})=${sha1sum}"
echo
# TODO: add actual verification
}

function extract_tarball() {
local -r tarfile="$1"
local -r platform="$2"
local -r arch="$3"

platforms_dir="${KUBE_ROOT}/platforms/${platform}/${arch}"
echo "Extracting ${tarfile} into ${platforms_dir}"
mkdir -p "${platforms_dir}"
# Tarball looks like kubernetes/{client,server}/bin/BINARY"
tar -xzf "${tarfile}" --strip-components 3 -C "${platforms_dir}"
# Create convenience symlink
ln -sf "${platforms_dir}" "$(dirname ${tarfile})/bin"
echo "Add '$(dirname ${tarfile})/bin' to your PATH to use newly-installed binaries."
}

detect_kube_release

SERVER_PLATFORM="linux"
SERVER_ARCH="${KUBERNETES_SERVER_ARCH:-amd64}"
SERVER_TAR="kubernetes-server-${SERVER_PLATFORM}-${SERVER_ARCH}.tar.gz"

detect_client_info
CLIENT_TAR="kubernetes-client-${CLIENT_PLATFORM}-${CLIENT_ARCH}.tar.gz"

echo "Kubernetes release: ${KUBERNETES_RELEASE}"
echo "Server: ${SERVER_PLATFORM}/${SERVER_ARCH}"
echo "Client: ${CLIENT_PLATFORM}/${CLIENT_ARCH}"
echo

# TODO: remove this check and default to true when we stop shipping server
# tarballs in kubernetes.tar.gz
DOWNLOAD_SERVER_TAR=false
if [[ ! -e "${KUBE_ROOT}/server/${SERVER_TAR}" ]]; then
DOWNLOAD_SERVER_TAR=true
echo "Will download ${SERVER_TAR} from ${DOWNLOAD_URL_PREFIX}"
fi

# TODO: remove this check and default to true when we stop shipping kubectl
# in kubernetes.tar.gz
DOWNLOAD_CLIENT_TAR=false
if [[ ! -x "${KUBE_ROOT}/platforms/${CLIENT_PLATFORM}/${CLIENT_ARCH}/kubectl" ]]; then
DOWNLOAD_CLIENT_TAR=true
echo "Will download and extract ${CLIENT_TAR} from ${DOWNLOAD_URL_PREFIX}"
fi

if [[ "${DOWNLOAD_CLIENT_TAR}" == false && "${DOWNLOAD_SERVER_TAR}" == false ]]; then
echo "Nothing additional to download."
exit 0
fi

if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
echo "Is this ok? [Y]/n"
read confirm
if [[ "${confirm}" =~ ^[nN]$ ]]; then
echo "Aborting."
exit 0
fi
fi

if "${DOWNLOAD_SERVER_TAR}"; then
download_tarball "${KUBE_ROOT}/server" "${SERVER_TAR}"
fi

if "${DOWNLOAD_CLIENT_TAR}"; then
download_tarball "${KUBE_ROOT}/client" "${CLIENT_TAR}"
extract_tarball "${KUBE_ROOT}/client/${CLIENT_TAR}" "${CLIENT_PLATFORM}" "${CLIENT_ARCH}"
fi
119 changes: 80 additions & 39 deletions cluster/get-kube.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,43 @@
# OpenStack-Heat
# * export KUBERNETES_PROVIDER=openstack-heat; wget -q -O - https://get.k8s.io | bash
#
# Set KUBERNETES_SKIP_DOWNLOAD to non-empty to skip downloading a release.
# Set KUBERNETES_RELEASE to choose a specific release instead of the current
# stable release, (e.g. 'v1.3.7').
# See https://github.com/kubernetes/kubernetes/releases for release options.
# Set KUBERNETES_RELEASE_URL to choose where to download binaries from.
# (Defaults to https://storage.googleapis.com/kubernetes-release/release).
#
# Set KUBERNETES_SERVER_ARCH to choose the server (Kubernetes cluster)
# architecture to download:
# * amd64 [default]
# * arm
# * arm64
#
# Set KUBERNETES_SKIP_DOWNLOAD to skip downloading a release.
# Set KUBERNETES_SKIP_CONFIRM to skip the installation confirmation prompt.
# Set KUBERNETES_RELEASE to the release you want to use (e.g. 'v1.2.0'). See https://github.com/kubernetes/kubernetes/releases for release options
# Set KUBERNETES_SKIP_CREATE_CLUSTER to skip starting a cluster.

set -o errexit
set -o nounset
set -o pipefail

KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL:-https://storage.googleapis.com/kubernetes-release/release}"

# Use the script from inside the Kubernetes tarball to fetch the client and
# server binaries (if not included in kubernetes.tar.gz).
function download_kube_binaries {
(
cd kubernetes
if [[ -x ./cluster/get-kube-binaries.sh ]]; then
./cluster/get-kube-binaries.sh
fi
)
}

function create_cluster {
if [[ -n "${KUBERNETES_SKIP_CREATE_CLUSTER}" ]]; then
exit 0
fi
echo "Creating a kubernetes on ${KUBERNETES_PROVIDER:-gce}..."
(
cd kubernetes
Expand All @@ -64,16 +93,16 @@ function create_cluster {
)
}

if [[ "${KUBERNETES_SKIP_DOWNLOAD-}" ]]; then
if [[ -n "${KUBERNETES_SKIP_DOWNLOAD-}" ]]; then
create_cluster
exit 0
fi

if [[ -d "./kubernetes" ]]; then
if [[ -n "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
echo "'kubernetes' directory already exist. Should we skip download step and start to create cluster based on it? [Y]/n"
read confirm
if [[ "$confirm" == "y" ]]; then
if [[ ! "${confirm}" =~ ^[nN]$ ]]; then
echo "Skipping download step."
create_cluster
exit 0
Expand All @@ -94,54 +123,65 @@ function get_latest_version_number {
}

release=${KUBERNETES_RELEASE:-$(get_latest_version_number)}
release_url=https://storage.googleapis.com/kubernetes-release/release/${release}/kubernetes.tar.gz
release_url="${KUBERNETES_RELEASE_URL}/${release}/kubernetes.tar.gz"

uname=$(uname)
if [[ "${uname}" == "Darwin" ]]; then
platform="darwin"
elif [[ "${uname}" == "Linux" ]]; then
platform="linux"
else
echo "Unknown, unsupported platform: (${uname})."
echo "Supported platforms: Linux, Darwin."
echo "Bailing out."
exit 2
fi
# TODO: remove client checks once kubernetes.tar.gz no longer includes client
# binaries by default.
kernel=$(uname -s)
case "${kernel}" in
Darwin)
platform="darwin"
;;
Linux)
platform="linux"
;;
*)
echo "Unknown, unsupported platform: ${kernel}." >&2
echo "Supported platforms: Linux, Darwin." >&2
echo "Bailing out." >&2
exit 2
esac

machine=$(uname -m)
if [[ "${machine}" == "x86_64" ]]; then
arch="amd64"
elif [[ "${machine}" == "i686" ]]; then
arch="386"
elif [[ "${machine}" == "arm*" ]]; then
arch="arm"
elif [[ "${machine}" == "s390x*" ]]; then
arch="s390x"
elif [[ "${machine}" == "ppc64le" ]]; then
arch="ppc64le"
else
echo "Unknown, unsupported architecture (${machine})."
echo "Supported architectures x86_64, i686, arm, s390x, ppc64le."
echo "Bailing out."
exit 3
fi
case "${machine}" in
x86_64*|i?86_64*|amd64*)
arch="amd64"
;;
aarch64*|arm64*)
arch="arm64"
;;
arm*)
arch="arm"
;;
i?86*)
arch="386"
;;
*)
echo "Unknown, unsupported architecture (${machine})." >&2
echo "Supported architectures x86_64, i686, arm, arm64." >&2
echo "Bailing out." >&2
exit 3
;;
esac

file=kubernetes.tar.gz

echo "Downloading kubernetes release ${release} to ${PWD}/kubernetes.tar.gz"
if [[ -n "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
echo "Downloading kubernetes release ${release}"
echo " from ${release_url}"
echo " to ${PWD}/kubernetes.tar.gz"
if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
echo "Is this ok? [Y]/n"
read confirm
if [[ "$confirm" == "n" ]]; then
if [[ "${confirm}" =~ ^[nN]$ ]]; then
echo "Aborting."
exit 0
fi
fi

if [[ $(which wget) ]]; then
wget -N ${release_url}
elif [[ $(which curl) ]]; then
if [[ $(which curl) ]]; then
curl -L -z ${file} ${release_url} -o ${file}
elif [[ $(which wget) ]]; then
wget -N ${release_url}
else
echo "Couldn't find curl or wget. Bailing out."
exit 1
Expand All @@ -150,4 +190,5 @@ fi
echo "Unpacking kubernetes release ${release}"
tar -xzf ${file}

download_kube_binaries
create_cluster

0 comments on commit 74991aa

Please sign in to comment.