From b3705b6e3580de196edd0d5bfd89e077e02bf10b Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Sat, 11 Mar 2017 00:18:38 -0600 Subject: [PATCH] hack/cluster: consolidate cluster/ utils to hack/lib/util.sh Per Clayton's suggestion, move stuff from cluster/lib/util.sh to hack/lib/util.sh. Also consolidate ensure-temp-dir and use the hack/lib/util.sh implementation rather than cluster/common.sh. --- cluster/aws/util.sh | 2 +- cluster/common.sh | 13 +-------- cluster/gce/upgrade.sh | 2 +- cluster/gce/util.sh | 6 ++-- cluster/gke/util.sh | 2 +- cluster/lib/util.sh | 46 ------------------------------- cluster/photon-controller/util.sh | 2 +- cluster/vagrant/util.sh | 2 +- cluster/validate-cluster.sh | 2 +- federation/cluster/common.sh | 2 +- hack/lib/init.sh | 1 - hack/lib/util.sh | 29 +++++++++++++++++++ hack/make-rules/verify.sh | 2 +- hack/update-all.sh | 1 - test/kubemark/start-kubemark.sh | 2 +- 15 files changed, 42 insertions(+), 72 deletions(-) delete mode 100644 cluster/lib/util.sh diff --git a/cluster/aws/util.sh b/cluster/aws/util.sh index 483bda04cb4..39830b9be85 100755 --- a/cluster/aws/util.sh +++ b/cluster/aws/util.sh @@ -15,7 +15,7 @@ # limitations under the License. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../.. -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" echo -e "${color_red}WARNING${color_norm}: The bash deployment for AWS is obsolete. The" >&2 echo -e "v1.5.x releases are the last to support cluster/kube-up.sh with AWS." >&2 diff --git a/cluster/common.sh b/cluster/common.sh index 6514657d06c..3adaf1559f1 100755 --- a/cluster/common.sh +++ b/cluster/common.sh @@ -24,7 +24,7 @@ KUBE_ROOT=$(cd $(dirname "${BASH_SOURCE}")/.. && pwd) DEFAULT_KUBECONFIG="${HOME}/.kube/config" -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" source "${KUBE_ROOT}/cluster/lib/logging.sh" # KUBE_RELEASE_VERSION_REGEX matches things like "v1.2.3" or "v1.2.3-alpha.4" # @@ -308,17 +308,6 @@ function load-or-gen-kube-bearertoken() { fi } -# Create a temp dir that'll be deleted at the end of this bash session. -# -# Vars set: -# KUBE_TEMP -function ensure-temp-dir { - if [[ -z ${KUBE_TEMP-} ]]; then - export KUBE_TEMP=$(mktemp -d -t kubernetes.XXXXXX) - trap 'rm -rf "${KUBE_TEMP}"' EXIT - fi -} - # Get the master IP for the current-context in kubeconfig if one exists. # # Assumed vars: diff --git a/cluster/gce/upgrade.sh b/cluster/gce/upgrade.sh index 0013af1f280..61e774eb1b0 100755 --- a/cluster/gce/upgrade.sh +++ b/cluster/gce/upgrade.sh @@ -192,7 +192,7 @@ function wait-for-master() { # Assumed vars # KUBE_VERSION function prepare-upgrade() { - ensure-temp-dir + kube::util::ensure-temp-dir detect-project detect-node-names # sets INSTANCE_GROUPS write-cluster-name diff --git a/cluster/gce/util.sh b/cluster/gce/util.sh index 39b9e5bc76f..2fe05f6c13a 100755 --- a/cluster/gce/util.sh +++ b/cluster/gce/util.sh @@ -21,7 +21,7 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../.. source "${KUBE_ROOT}/cluster/gce/${KUBE_CONFIG_FILE-"config-default.sh"}" source "${KUBE_ROOT}/cluster/common.sh" -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" if [[ "${NODE_OS_DISTRIBUTION}" == "debian" || "${NODE_OS_DISTRIBUTION}" == "container-linux" || "${NODE_OS_DISTRIBUTION}" == "trusty" || "${NODE_OS_DISTRIBUTION}" == "gci" ]]; then source "${KUBE_ROOT}/cluster/gce/${NODE_OS_DISTRIBUTION}/node-helper.sh" @@ -581,7 +581,7 @@ function add-instance-metadata-from-file() { # KUBE_ROOT # function kube-up() { - ensure-temp-dir + kube::util::ensure-temp-dir detect-project load-or-gen-kube-basicauth @@ -1620,7 +1620,7 @@ function prepare-push() { OUTPUT=${KUBE_ROOT}/_output/logs mkdir -p ${OUTPUT} - ensure-temp-dir + kube::util::ensure-temp-dir detect-project detect-master detect-node-names diff --git a/cluster/gke/util.sh b/cluster/gke/util.sh index ecdd36bd1b2..95f91efb4cc 100755 --- a/cluster/gke/util.sh +++ b/cluster/gke/util.sh @@ -22,7 +22,7 @@ KUBE_PROMPT_FOR_UPDATE=${KUBE_PROMPT_FOR_UPDATE:-"n"} KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../.. source "${KUBE_ROOT}/cluster/gke/${KUBE_CONFIG_FILE:-config-default.sh}" source "${KUBE_ROOT}/cluster/common.sh" -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" function with-retry() { local retry_limit=$1 diff --git a/cluster/lib/util.sh b/cluster/lib/util.sh deleted file mode 100644 index 7082fb7ce2d..00000000000 --- a/cluster/lib/util.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# Copyright 2015 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. - -# Wait for background jobs to finish. Return with -# an error status if any of the jobs failed. -kube::util::wait-for-jobs() { - local fail=0 - local job - for job in $(jobs -p); do - wait "${job}" || fail=$((fail + 1)) - done - return ${fail} -} - -# kube::util::join -# Concatenates the list elements with the delimiter passed as first parameter -# -# Ex: kube::util::join , a b c -# -> a,b,c -function kube::util::join { - local IFS="$1" - shift - echo "$*" -} - -# Some useful colors. -if [[ -z "${color_start-}" ]]; then - declare -r color_start="\033[" - declare -r color_red="${color_start}0;31m" - declare -r color_yellow="${color_start}0;33m" - declare -r color_green="${color_start}0;32m" - declare -r color_norm="${color_start}0m" -fi diff --git a/cluster/photon-controller/util.sh b/cluster/photon-controller/util.sh index bc9d0b572a9..0b05cac9cd6 100755 --- a/cluster/photon-controller/util.sh +++ b/cluster/photon-controller/util.sh @@ -156,7 +156,7 @@ function kube-up { verify-prereqs verify-ssh-prereqs verify-photon-config - ensure-temp-dir + kube::util::ensure-temp-dir find-release-tars find-image-id diff --git a/cluster/vagrant/util.sh b/cluster/vagrant/util.sh index 0b4be7fd840..64efdfddda0 100755 --- a/cluster/vagrant/util.sh +++ b/cluster/vagrant/util.sh @@ -106,7 +106,7 @@ function verify-prereqs { # Create a set of provision scripts for the master and each of the nodes function create-provision-scripts { - ensure-temp-dir + kube::util::ensure-temp-dir ( echo "#! /bin/bash" diff --git a/cluster/validate-cluster.sh b/cluster/validate-cluster.sh index d696178a5dd..a94587bb656 100755 --- a/cluster/validate-cluster.sh +++ b/cluster/validate-cluster.sh @@ -30,7 +30,7 @@ if [ -f "${KUBE_ROOT}/cluster/env.sh" ]; then source "${KUBE_ROOT}/cluster/env.sh" fi -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" source "${KUBE_ROOT}/cluster/kube-util.sh" # Run kubectl and retry upon failure. diff --git a/federation/cluster/common.sh b/federation/cluster/common.sh index 3d4755062d4..4efb5b7986f 100644 --- a/federation/cluster/common.sh +++ b/federation/cluster/common.sh @@ -250,7 +250,7 @@ function create-federation-api-objects { done # Create server certificates. - ensure-temp-dir + kube::util::ensure-temp-dir echo "Creating federation apiserver certs for federation api host: ${FEDERATION_API_HOST} ( is this a dns name?: ${IS_DNS_NAME} )" MASTER_NAME="federation-apiserver" create-federation-apiserver-certs ${FEDERATION_API_HOST} export FEDERATION_APISERVER_CA_CERT_BASE64="${FEDERATION_APISERVER_CA_CERT_BASE64}" diff --git a/hack/lib/init.sh b/hack/lib/init.sh index a07f6f2e123..4eadc166b2b 100644 --- a/hack/lib/init.sh +++ b/hack/lib/init.sh @@ -37,7 +37,6 @@ export no_proxy=127.0.0.1,localhost THIS_PLATFORM_BIN="${KUBE_ROOT}/_output/bin" source "${KUBE_ROOT}/hack/lib/util.sh" -source "${KUBE_ROOT}/cluster/lib/util.sh" source "${KUBE_ROOT}/cluster/lib/logging.sh" kube::log::install_errexit diff --git a/hack/lib/util.sh b/hack/lib/util.sh index 6a0386a3415..fafd9a62e08 100755 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -694,6 +694,35 @@ EOF fi } +# Wait for background jobs to finish. Return with +# an error status if any of the jobs failed. +kube::util::wait-for-jobs() { + local fail=0 + local job + for job in $(jobs -p); do + wait "${job}" || fail=$((fail + 1)) + done + return ${fail} +} +# kube::util::join +# Concatenates the list elements with the delimiter passed as first parameter +# +# Ex: kube::util::join , a b c +# -> a,b,c +function kube::util::join { + local IFS="$1" + shift + echo "$*" +} + +# Some useful colors. +if [[ -z "${color_start-}" ]]; then + declare -r color_start="\033[" + declare -r color_red="${color_start}0;31m" + declare -r color_yellow="${color_start}0;33m" + declare -r color_green="${color_start}0;32m" + declare -r color_norm="${color_start}0m" +fi # ex: ts=2 sw=2 et filetype=sh diff --git a/hack/make-rules/verify.sh b/hack/make-rules/verify.sh index 4c91ffddbe0..eeff8c78a0e 100755 --- a/hack/make-rules/verify.sh +++ b/hack/make-rules/verify.sh @@ -19,7 +19,7 @@ set -o nounset set -o pipefail KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../.. -source "${KUBE_ROOT}/cluster/lib/util.sh" +source "${KUBE_ROOT}/hack/lib/util.sh" # Excluded checks are always skipped. EXCLUDED_CHECKS=( diff --git a/hack/update-all.sh b/hack/update-all.sh index 7e4d1306553..ab70a809bfc 100755 --- a/hack/update-all.sh +++ b/hack/update-all.sh @@ -22,7 +22,6 @@ set -o pipefail KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. source "${KUBE_ROOT}/hack/lib/init.sh" source "${KUBE_ROOT}/hack/lib/util.sh" -source "${KUBE_ROOT}/cluster/lib/util.sh" SILENT=true ALL=false diff --git a/test/kubemark/start-kubemark.sh b/test/kubemark/start-kubemark.sh index e1615b88037..bce8d3dc316 100755 --- a/test/kubemark/start-kubemark.sh +++ b/test/kubemark/start-kubemark.sh @@ -74,7 +74,7 @@ EOF # Generate certs/keys for CA, master, kubelet and kubecfg, and tokens for kubelet # and kubeproxy. function generate-pki-config { - ensure-temp-dir + kube::util::ensure-temp-dir gen-kube-bearertoken gen-kube-basicauth create-certs ${MASTER_IP}