Merge pull request #60308 from ipuustin/shell-bugfix4

Automatic merge from submit-queue (batch tested with PRs 59310, 60424, 60308, 60436, 60020). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fix potential issues and bugs in hack/lib/*.sh scripts using shellcheck

**What this PR does / why we need it**:

This PR is continuing the work for cleaning up the shell scripts using shellcheck and manual inspection. The plan is to make the scripts more robust in case for unexpected input and also to fix potential bugs.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

The changes are for scripts in `hack/lib/` directory, and as library scripts are used from multiple places. It is not trivial to see all possible places from where the scripts are accessed, so careful review is needed. I tried to make sure that the changes would be compatible for older bash versions too.

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2018-02-27 07:11:47 -08:00 committed by GitHub
commit a9ef75b98f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 31 deletions

View File

@ -37,7 +37,8 @@ kube::golang::server_targets() {
echo "${targets[@]}" echo "${targets[@]}"
} }
readonly KUBE_SERVER_TARGETS=($(kube::golang::server_targets)) IFS=" " read -ra KUBE_SERVER_TARGETS <<< "$(kube::golang::server_targets)"
readonly KUBE_SERVER_TARGETS
readonly KUBE_SERVER_BINARIES=("${KUBE_SERVER_TARGETS[@]##*/}") readonly KUBE_SERVER_BINARIES=("${KUBE_SERVER_TARGETS[@]##*/}")
# The set of server targets that we are only building for Kubernetes nodes # The set of server targets that we are only building for Kubernetes nodes
@ -51,15 +52,20 @@ kube::golang::node_targets() {
echo "${targets[@]}" echo "${targets[@]}"
} }
readonly KUBE_NODE_TARGETS=($(kube::golang::node_targets)) IFS=" " read -ra KUBE_NODE_TARGETS <<< "$(kube::golang::node_targets)"
readonly KUBE_NODE_TARGETS
readonly KUBE_NODE_BINARIES=("${KUBE_NODE_TARGETS[@]##*/}") readonly KUBE_NODE_BINARIES=("${KUBE_NODE_TARGETS[@]##*/}")
readonly KUBE_NODE_BINARIES_WIN=("${KUBE_NODE_BINARIES[@]/%/.exe}") readonly KUBE_NODE_BINARIES_WIN=("${KUBE_NODE_BINARIES[@]/%/.exe}")
if [[ -n "${KUBE_BUILD_PLATFORMS:-}" ]]; then if [[ -n "${KUBE_BUILD_PLATFORMS:-}" ]]; then
readonly KUBE_SERVER_PLATFORMS=(${KUBE_BUILD_PLATFORMS}) IFS=" " read -ra KUBE_SERVER_PLATFORMS <<< "$KUBE_BUILD_PLATFORMS"
readonly KUBE_NODE_PLATFORMS=(${KUBE_BUILD_PLATFORMS}) IFS=" " read -ra KUBE_NODE_PLATFORMS <<< "$KUBE_BUILD_PLATFORMS"
readonly KUBE_TEST_PLATFORMS=(${KUBE_BUILD_PLATFORMS}) IFS=" " read -ra KUBE_TEST_PLATFORMS <<< "$KUBE_BUILD_PLATFORMS"
readonly KUBE_CLIENT_PLATFORMS=(${KUBE_BUILD_PLATFORMS}) IFS=" " read -ra KUBE_CLIENT_PLATFORMS <<< "$KUBE_BUILD_PLATFORMS"
readonly KUBE_SERVER_PLATFORMS
readonly KUBE_NODE_PLATFORMS
readonly KUBE_TEST_PLATFORMS
readonly KUBE_CLIENT_PLATFORMS
elif [[ "${KUBE_FASTBUILD:-}" == "true" ]]; then elif [[ "${KUBE_FASTBUILD:-}" == "true" ]]; then
readonly KUBE_SERVER_PLATFORMS=(linux/amd64) readonly KUBE_SERVER_PLATFORMS=(linux/amd64)
readonly KUBE_NODE_PLATFORMS=(linux/amd64) readonly KUBE_NODE_PLATFORMS=(linux/amd64)
@ -146,7 +152,8 @@ kube::golang::test_targets() {
) )
echo "${targets[@]}" echo "${targets[@]}"
} }
readonly KUBE_TEST_TARGETS=($(kube::golang::test_targets)) IFS=" " read -ra KUBE_TEST_TARGETS <<< "$(kube::golang::test_targets)"
readonly KUBE_TEST_TARGETS
readonly KUBE_TEST_BINARIES=("${KUBE_TEST_TARGETS[@]##*/}") readonly KUBE_TEST_BINARIES=("${KUBE_TEST_TARGETS[@]##*/}")
readonly KUBE_TEST_BINARIES_WIN=("${KUBE_TEST_BINARIES[@]/%/.exe}") readonly KUBE_TEST_BINARIES_WIN=("${KUBE_TEST_BINARIES[@]/%/.exe}")
# If you update this list, please also update build/BUILD. # If you update this list, please also update build/BUILD.
@ -177,7 +184,8 @@ kube::golang::server_test_targets() {
echo "${targets[@]}" echo "${targets[@]}"
} }
readonly KUBE_TEST_SERVER_TARGETS=($(kube::golang::server_test_targets)) IFS=" " read -ra KUBE_TEST_SERVER_TARGETS <<< "$(kube::golang::server_test_targets)"
readonly KUBE_TEST_SERVER_TARGETS
readonly KUBE_TEST_SERVER_BINARIES=("${KUBE_TEST_SERVER_TARGETS[@]##*/}") readonly KUBE_TEST_SERVER_BINARIES=("${KUBE_TEST_SERVER_TARGETS[@]##*/}")
readonly KUBE_TEST_SERVER_PLATFORMS=("${KUBE_SERVER_PLATFORMS[@]}") readonly KUBE_TEST_SERVER_PLATFORMS=("${KUBE_SERVER_PLATFORMS[@]}")
@ -316,7 +324,7 @@ EOF
fi fi
local go_version local go_version
go_version=($(go version)) IFS=" " read -ra go_version <<< "$(go version)"
local minimum_go_version local minimum_go_version
minimum_go_version=go1.9.1 minimum_go_version=go1.9.1
if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then
@ -393,7 +401,7 @@ kube::golang::place_bins() {
# The substitution on platform_src below will replace all slashes with # The substitution on platform_src below will replace all slashes with
# underscores. It'll transform darwin/amd64 -> darwin_amd64. # underscores. It'll transform darwin/amd64 -> darwin_amd64.
local platform_src="/${platform//\//_}" local platform_src="/${platform//\//_}"
if [[ $platform == $host_platform ]]; then if [[ "$platform" == "$host_platform" ]]; then
platform_src="" platform_src=""
rm -f "${THIS_PLATFORM_BIN}" rm -f "${THIS_PLATFORM_BIN}"
ln -s "${KUBE_OUTPUT_BINPATH}/${platform}" "${THIS_PLATFORM_BIN}" ln -s "${KUBE_OUTPUT_BINPATH}/${platform}" "${THIS_PLATFORM_BIN}"
@ -457,7 +465,7 @@ kube::golang::output_filename_for_binary() {
local binary=$1 local binary=$1
local platform=$2 local platform=$2
local output_path="${KUBE_GOPATH}/bin" local output_path="${KUBE_GOPATH}/bin"
if [[ $platform != $host_platform ]]; then if [[ "$platform" != "$host_platform" ]]; then
output_path="${output_path}/${platform//\//_}" output_path="${output_path}/${platform//\//_}"
fi fi
local bin=$(basename "${binary}") local bin=$(basename "${binary}")
@ -640,7 +648,8 @@ kube::golang::build_binaries() {
targets=("${KUBE_ALL_TARGETS[@]}") targets=("${KUBE_ALL_TARGETS[@]}")
fi fi
local -a platforms=(${KUBE_BUILD_PLATFORMS:-}) local -a platforms
IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS:-}"
if [[ ${#platforms[@]} -eq 0 ]]; then if [[ ${#platforms[@]} -eq 0 ]]; then
platforms=("${host_platform}") platforms=("${host_platform}")
fi fi
@ -666,7 +675,7 @@ kube::golang::build_binaries() {
kube::golang::build_kube_toolchain kube::golang::build_kube_toolchain
kube::log::status "Generating bindata:" "${KUBE_BINDATAS[@]}" kube::log::status "Generating bindata:" "${KUBE_BINDATAS[@]}"
for bindata in ${KUBE_BINDATAS[@]}; do for bindata in "${KUBE_BINDATAS[@]}"; do
# Only try to generate bindata if the file exists, since in some cases # Only try to generate bindata if the file exists, since in some cases
# one-off builds of individual directories may exclude some files. # one-off builds of individual directories may exclude some files.
if [[ -f "${KUBE_ROOT}/${bindata}" ]]; then if [[ -f "${KUBE_ROOT}/${bindata}" ]]; then

View File

@ -131,7 +131,7 @@ function kube::readlinkdashf {
cd "$1" cd "$1"
pwd -P pwd -P
else else
cd $(dirname "$1") cd "$(dirname "$1")"
local f local f
f=$(basename "$1") f=$(basename "$1")
if [[ -L "$f" ]]; then if [[ -L "$f" ]]; then

View File

@ -57,7 +57,7 @@ function kube::protoc::check_protoc() {
# $1: Full path to the directory where the api.proto file is # $1: Full path to the directory where the api.proto file is
function kube::protoc::protoc() { function kube::protoc::protoc() {
local package=${1} local package=${1}
gogopath=$(dirname $(kube::util::find-binary "protoc-gen-gogo")) gogopath=$(dirname "$(kube::util::find-binary "protoc-gen-gogo")")
PATH="${gogopath}:${PATH}" protoc \ PATH="${gogopath}:${PATH}" protoc \
--proto_path="${package}" \ --proto_path="${package}" \

View File

@ -30,13 +30,13 @@ kube::util::wait_for_url() {
} }
local i local i
for i in $(seq 1 $times); do for i in $(seq 1 "$times"); do
local out local out
if out=$(curl --max-time 1 -gkfs $url 2>/dev/null); then if out=$(curl --max-time 1 -gkfs "$url" 2>/dev/null); then
kube::log::status "On try ${i}, ${prefix}: ${out}" kube::log::status "On try ${i}, ${prefix}: ${out}"
return 0 return 0
fi fi
sleep ${wait} sleep "${wait}"
done done
kube::log::error "Timed out waiting for ${prefix} to answer at ${url}; tried ${times} waiting ${wait} between each" kube::log::error "Timed out waiting for ${prefix} to answer at ${url}; tried ${times} waiting ${wait} between each"
return 1 return 1
@ -209,7 +209,7 @@ kube::util::gen-docs() {
# Puts a placeholder for every generated doc. This makes the link checker work. # Puts a placeholder for every generated doc. This makes the link checker work.
kube::util::set-placeholder-gen-docs() { kube::util::set-placeholder-gen-docs() {
local list_file="${KUBE_ROOT}/docs/.generated_docs" local list_file="${KUBE_ROOT}/docs/.generated_docs"
if [ -e ${list_file} ]; then if [[ -e "${list_file}" ]]; then
# remove all of the old docs; we don't want to check them in. # remove all of the old docs; we don't want to check them in.
while read file; do while read file; do
if [[ "${list_file}" != "${KUBE_ROOT}/${file}" ]]; then if [[ "${list_file}" != "${KUBE_ROOT}/${file}" ]]; then
@ -244,11 +244,9 @@ kube::util::remove-gen-docs() {
kube::util::group-version-to-pkg-path() { kube::util::group-version-to-pkg-path() {
staging_apis=( staging_apis=(
$( $(
pushd ${KUBE_ROOT}/staging/src/k8s.io/api > /dev/null cd "${KUBE_ROOT}/staging/src/k8s.io/api" &&
find . -name types.go | xargs -n1 dirname | sed "s|\./||g" | sort find . -name types.go -exec dirname {} \; | sed "s|\./||g" | sort
popd > /dev/null ))
)
)
local group_version="$1" local group_version="$1"
@ -274,15 +272,9 @@ kube::util::group-version-to-pkg-path() {
meta/v1) meta/v1)
echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1" echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1"
;; ;;
meta/v1)
echo "../vendor/k8s.io/apimachinery/pkg/apis/meta/v1"
;;
meta/v1beta1) meta/v1beta1)
echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1" echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1"
;; ;;
meta/v1beta1)
echo "../vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1"
;;
unversioned) unversioned)
echo "pkg/api/unversioned" echo "pkg/api/unversioned"
;; ;;
@ -461,7 +453,12 @@ kube::util::ensure_godep_version() {
kube::util::ensure_no_staging_repos_in_gopath() { kube::util::ensure_no_staging_repos_in_gopath() {
kube::util::ensure_single_dir_gopath kube::util::ensure_single_dir_gopath
local error=0 local error=0
for repo in $(ls ${KUBE_ROOT}/staging/src/k8s.io); do for repo_file in "${KUBE_ROOT}"/staging/src/k8s.io/*; do
if [[ ! -d "$repo_file" ]]; then
# not a directory or there were no files
continue;
fi
repo="$(basename "$repo_file")"
if [ -e "${GOPATH}/src/k8s.io/${repo}" ]; then if [ -e "${GOPATH}/src/k8s.io/${repo}" ]; then
echo "k8s.io/${repo} exists in GOPATH. Remove before running godep-save.sh." 1>&2 echo "k8s.io/${repo} exists in GOPATH. Remove before running godep-save.sh." 1>&2
error=1 error=1