From 516032f336339dd7b2b3982683577721af1a4ab1 Mon Sep 17 00:00:00 2001 From: Yifan Gu Date: Wed, 9 Dec 2015 17:50:16 -0800 Subject: [PATCH] kube-addons: Use python container if python is not found on the machine. To build the python image, BUILD_PYTHON_IMAGE should be set during make. When the addon script is running, it will check if python is installed on the machine, if not, it will use the python image that built previously. --- build/common.sh | 10 ++++++++++ cluster/addons/README.md | 2 ++ cluster/addons/python-image/Dockerfile | 3 +++ cluster/addons/python-image/README.md | 6 ++++++ .../salt/kube-addons/kube-addon-update.sh | 4 ++-- .../saltbase/salt/kube-addons/kube-addons.sh | 19 ++++++++++++++++--- 6 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 cluster/addons/python-image/Dockerfile create mode 100644 cluster/addons/python-image/README.md diff --git a/build/common.sh b/build/common.sh index 9af436e87b..3682ddeb52 100755 --- a/build/common.sh +++ b/build/common.sh @@ -819,6 +819,16 @@ function kube::release::write_addon_docker_images_for_server() { ) & done + if [[ ! -z "${BUILD_PYTHON_IMAGE:-}" ]]; then + ( + kube::log::status "Building Docker python image" + + local img_name=python:2.7-slim-pyyaml + docker build -t "${img_name}" "${KUBE_ROOT}/cluster/addons/python-image" + docker save "${img_name}" > "${1}/${img_name}.tar" + ) & + fi + kube::util::wait-for-jobs || { kube::log::error "unable to pull or write addon image"; return 1; } kube::log::status "Addon images done" ) diff --git a/cluster/addons/README.md b/cluster/addons/README.md index 44dc4e4ebb..d604932a9c 100644 --- a/cluster/addons/README.md +++ b/cluster/addons/README.md @@ -58,6 +58,8 @@ of: pods. 1. Note that this cannot happen for Services as their version is always empty. +Note that in order to run the updator script, python is required on the machine. +For OS distros that don't have python installed, a python container will be used. diff --git a/cluster/addons/python-image/Dockerfile b/cluster/addons/python-image/Dockerfile new file mode 100644 index 0000000000..67accb37e5 --- /dev/null +++ b/cluster/addons/python-image/Dockerfile @@ -0,0 +1,3 @@ +FROM python:2.7-slim + +RUN pip install pyyaml diff --git a/cluster/addons/python-image/README.md b/cluster/addons/python-image/README.md new file mode 100644 index 0000000000..c9f2fbf3ad --- /dev/null +++ b/cluster/addons/python-image/README.md @@ -0,0 +1,6 @@ +# Python image + +The python image here is used by OS distros that don't have python installed to +run python scripts to parse the yaml files in the addon updator script. + +[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/cluster/addons/python-image/README.md?pixel)]() diff --git a/cluster/saltbase/salt/kube-addons/kube-addon-update.sh b/cluster/saltbase/salt/kube-addons/kube-addon-update.sh index 6b61b9ad11..9cd7b70f11 100755 --- a/cluster/saltbase/salt/kube-addons/kube-addon-update.sh +++ b/cluster/saltbase/salt/kube-addons/kube-addon-update.sh @@ -98,7 +98,7 @@ function log() { function get-object-kind-from-file() { # prints to stdout, so log cannot be used #WARNING: only yaml is supported - cat $1 | python -c ''' + cat $1 | ${PYTHON} -c ''' try: import pipes,sys,yaml y = yaml.load(sys.stdin) @@ -120,7 +120,7 @@ function get-object-nsname-from-file() { # prints to stdout, so log cannot be used #WARNING: only yaml is supported #addons that do not specify a namespace are assumed to be in "default". - cat $1 | python -c ''' + cat $1 | ${PYTHON} -c ''' try: import pipes,sys,yaml y = yaml.load(sys.stdin) diff --git a/cluster/saltbase/salt/kube-addons/kube-addons.sh b/cluster/saltbase/salt/kube-addons/kube-addons.sh index c9fd9a0ea9..b8ccdbe47a 100644 --- a/cluster/saltbase/salt/kube-addons/kube-addons.sh +++ b/cluster/saltbase/salt/kube-addons/kube-addons.sh @@ -24,6 +24,16 @@ ADDON_CHECK_INTERVAL_SEC=${TEST_ADDON_CHECK_INTERVAL_SEC:-600} SYSTEM_NAMESPACE=kube-system token_dir=${TOKEN_DIR:-/srv/kubernetes} +function ensure_python() { + if ! python --version > /dev/null 2>&1; then + echo "No python on the machine, will use a python image" + local -r PYTHON_IMAGE=python:2.7-slim-pyyaml + export PYTHON="docker run --interactive --rm --net=none ${PYTHON_IMAGE} python" + else + export PYTHON=python + fi +} + function create-kubeconfig-secret() { local -r token=$1 local -r username=$2 @@ -152,11 +162,16 @@ function load-docker-images() { # managed result is of that. Start everything below that directory. echo "== Kubernetes addon manager started at $(date -Is) with ADDON_CHECK_INTERVAL_SEC=${ADDON_CHECK_INTERVAL_SEC} ==" +# Load any images that we may need +load-docker-images /srv/salt/kube-addons-images + +ensure_python + # Load the kube-env, which has all the environment variables we care # about, in a flat yaml format. kube_env_yaml="/var/cache/kubernetes-install/kube_env.yaml" if [ ! -e "${kubelet_kubeconfig_file}" ]; then - eval $(python -c ''' + eval $(${PYTHON} -c ''' import pipes,sys,yaml for k,v in yaml.load(sys.stdin).iteritems(): @@ -164,8 +179,6 @@ for k,v in yaml.load(sys.stdin).iteritems(): ''' < "${kube_env_yaml}") fi -# Load any images that we may need -load-docker-images /srv/salt/kube-addons-images # Create the namespace that will be used to host the cluster-level add-ons. start_addon /etc/kubernetes/addons/namespace.yaml 100 10 "" &