From 1dd53fd3bae86ae483d40b50fe57f9669f65916e Mon Sep 17 00:00:00 2001 From: Walter Fender Date: Thu, 23 Jan 2020 14:58:18 -0800 Subject: [PATCH] Fix issue with GCE scripts assuming Python2. For bug #87482. Newer OSs are now defaulting to Python3. This breaks the kube-up scripts for GCE. Adding code to detect this and explicitly use Python2. --- cluster/gce/gci/configure-helper.sh | 8 ++++++-- cluster/gce/gci/configure.sh | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cluster/gce/gci/configure-helper.sh b/cluster/gce/gci/configure-helper.sh index 079d1031154..76e936d6f73 100644 --- a/cluster/gce/gci/configure-helper.sh +++ b/cluster/gce/gci/configure-helper.sh @@ -1527,7 +1527,7 @@ function start-kube-proxy { # $5: pod name, which should be either etcd or etcd-events function prepare-etcd-manifest { local host_name=${ETCD_HOSTNAME:-$(hostname -s)} - local host_ip=$(python -c "import socket;print(socket.gethostbyname(\"${host_name}\"))") + local host_ip=$(${PYTHON} -c "import socket;print(socket.gethostbyname(\"${host_name}\"))") local etcd_cluster="" local cluster_state="new" local etcd_protocol="http" @@ -2713,9 +2713,14 @@ function main() { KUBE_HOME="/home/kubernetes" KUBE_BIN=${KUBE_HOME}/bin + PYTHON="python" CONTAINERIZED_MOUNTER_HOME="${KUBE_HOME}/containerized_mounter" PV_RECYCLER_OVERRIDE_TEMPLATE="${KUBE_HOME}/kube-manifests/kubernetes/pv-recycler-template.yaml" + if [[ "$(python -V)" =~ "Python 3" ]]; then + PYTHON="/usr/bin/python2.7" + fi + if [[ ! -e "${KUBE_HOME}/kube-env" ]]; then echo "The ${KUBE_HOME}/kube-env file does not exist!! Terminate cluster initialization." exit 1 @@ -2723,7 +2728,6 @@ function main() { source "${KUBE_HOME}/kube-env" - if [[ -f "${KUBE_HOME}/kubelet-config.yaml" ]]; then echo "Found Kubelet config file at ${KUBE_HOME}/kubelet-config.yaml" KUBELET_CONFIG_FILE_ARG="--config ${KUBE_HOME}/kubelet-config.yaml" diff --git a/cluster/gce/gci/configure.sh b/cluster/gce/gci/configure.sh index 23340a69bd9..2d788f47cce 100644 --- a/cluster/gce/gci/configure.sh +++ b/cluster/gce/gci/configure.sh @@ -64,7 +64,7 @@ function download-kube-env { -o "${tmp_kube_env}" \ http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-env # Convert the yaml format file into a shell-style file. - eval $(python -c ''' + eval $(${PYTHON} -c ''' import pipes,sys,yaml for k,v in yaml.load(sys.stdin).iteritems(): print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v)))) @@ -103,7 +103,7 @@ function download-kube-master-certs { -o "${tmp_kube_master_certs}" \ http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-master-certs # Convert the yaml format file into a shell-style file. - eval $(python -c ''' + eval $(${PYTHON} -c ''' import pipes,sys,yaml for k,v in yaml.load(sys.stdin).iteritems(): print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v)))) @@ -126,7 +126,7 @@ function validate-hash { # Get default service account credentials of the VM. GCE_METADATA_INTERNAL="http://metadata.google.internal/computeMetadata/v1/instance" function get-credentials { - curl --fail --retry 5 --retry-delay 3 ${CURL_RETRY_CONNREFUSED} --silent --show-error "${GCE_METADATA_INTERNAL}/service-accounts/default/token" -H "Metadata-Flavor: Google" -s | python -c \ + curl --fail --retry 5 --retry-delay 3 ${CURL_RETRY_CONNREFUSED} --silent --show-error "${GCE_METADATA_INTERNAL}/service-accounts/default/token" -H "Metadata-Flavor: Google" -s | ${PYTHON} -c \ 'import sys; import json; print(json.loads(sys.stdin.read())["access_token"])' } @@ -471,6 +471,11 @@ set-broken-motd KUBE_HOME="/home/kubernetes" KUBE_BIN="${KUBE_HOME}/bin" +PYTHON="python" + +if [[ "$(python -V)" =~ "Python 3" ]]; then + PYTHON="/usr/bin/python2.7" +fi # download and source kube-env download-kube-env