fix shellcheck failure golang.sh

This commit is contained in:
toyoda 2019-03-08 13:05:14 +09:00
parent c48ed1c5ac
commit a52af3a9d5
2 changed files with 30 additions and 25 deletions

View File

@ -34,7 +34,6 @@
./hack/jenkins/test-dockerized.sh ./hack/jenkins/test-dockerized.sh
./hack/jenkins/upload-to-gcs.sh ./hack/jenkins/upload-to-gcs.sh
./hack/jenkins/verify-dockerized.sh ./hack/jenkins/verify-dockerized.sh
./hack/lib/golang.sh
./hack/lib/init.sh ./hack/lib/init.sh
./hack/lib/logging.sh ./hack/lib/logging.sh
./hack/lib/protoc.sh ./hack/lib/protoc.sh

View File

@ -345,12 +345,13 @@ kube::golang::unset_platform_envs() {
# Create the GOPATH tree under $KUBE_OUTPUT # Create the GOPATH tree under $KUBE_OUTPUT
kube::golang::create_gopath_tree() { kube::golang::create_gopath_tree() {
local go_pkg_dir="${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}" local go_pkg_dir="${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}"
local go_pkg_basedir=$(dirname "${go_pkg_dir}") local go_pkg_basedir
go_pkg_basedir=$(dirname "${go_pkg_dir}")
mkdir -p "${go_pkg_basedir}" mkdir -p "${go_pkg_basedir}"
# TODO: This symlink should be relative. # TODO: This symlink should be relative.
if [[ ! -e "${go_pkg_dir}" || "$(readlink ${go_pkg_dir})" != "${KUBE_ROOT}" ]]; then if [[ ! -e "${go_pkg_dir}" || "$(readlink "${go_pkg_dir}")" != "${KUBE_ROOT}" ]]; then
ln -snf "${KUBE_ROOT}" "${go_pkg_dir}" ln -snf "${KUBE_ROOT}" "${go_pkg_dir}"
fi fi
@ -427,10 +428,11 @@ kube::golang::setup_env() {
# cross-compiling, and `go install -o <file>` only works for a single pkg. # cross-compiling, and `go install -o <file>` only works for a single pkg.
local subdir local subdir
subdir=$(kube::realpath . | sed "s|${KUBE_ROOT}||") subdir=$(kube::realpath . | sed "s|${KUBE_ROOT}||")
cd "${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}/${subdir}" cd "${KUBE_GOPATH}/src/${KUBE_GO_PACKAGE}/${subdir}" || return 1
# Set GOROOT so binaries that parse code can work properly. # Set GOROOT so binaries that parse code can work properly.
export GOROOT=$(go env GOROOT) GOROOT=$(go env GOROOT)
export GOROOT
# Unset GOBIN in case it already exists in the current session. # Unset GOBIN in case it already exists in the current session.
unset GOBIN unset GOBIN
@ -479,10 +481,11 @@ kube::golang::outfile_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"
local bin
bin=$(basename "${binary}")
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}")
if [[ ${GOOS} == "windows" ]]; then if [[ ${GOOS} == "windows" ]]; then
bin="${bin}.exe" bin="${bin}.exe"
fi fi
@ -493,7 +496,7 @@ kube::golang::outfile_for_binary() {
# Returns 0 if the binary can be built with coverage, 1 otherwise. # Returns 0 if the binary can be built with coverage, 1 otherwise.
# NB: this ignores whether coverage is globally enabled or not. # NB: this ignores whether coverage is globally enabled or not.
kube::golang::is_instrumented_package() { kube::golang::is_instrumented_package() {
return $(kube::util::array_contains "$1" "${KUBE_COVERAGE_INSTRUMENTED_PACKAGES[@]}") return "$(kube::util::array_contains "$1" "${KUBE_COVERAGE_INSTRUMENTED_PACKAGES[@]}")"
} }
# Argument: the name of a Kubernetes package (e.g. k8s.io/kubernetes/cmd/kube-scheduler) # Argument: the name of a Kubernetes package (e.g. k8s.io/kubernetes/cmd/kube-scheduler)
@ -501,7 +504,8 @@ kube::golang::is_instrumented_package() {
kube::golang::path_for_coverage_dummy_test() { kube::golang::path_for_coverage_dummy_test() {
local package="$1" local package="$1"
local path="${KUBE_GOPATH}/src/${package}" local path="${KUBE_GOPATH}/src/${package}"
local name=$(basename "${package}") local name
name=$(basename "${package}")
echo "${path}/zz_generated_${name}_test.go" echo "${path}/zz_generated_${name}_test.go"
} }
@ -510,8 +514,9 @@ kube::golang::path_for_coverage_dummy_test() {
# This unit test will invoke the package's standard entry point when run. # This unit test will invoke the package's standard entry point when run.
kube::golang::create_coverage_dummy_test() { kube::golang::create_coverage_dummy_test() {
local package="$1" local package="$1"
local name="$(basename "${package}")" local name
cat <<EOF > $(kube::golang::path_for_coverage_dummy_test "${package}") name="$(basename "${package}")"
cat <<EOF > "$(kube::golang::path_for_coverage_dummy_test "${package}")"
package main package main
import ( import (
"testing" "testing"
@ -539,7 +544,7 @@ EOF
# It is not an error to call this for a nonexistent test. # It is not an error to call this for a nonexistent test.
kube::golang::delete_coverage_dummy_test() { kube::golang::delete_coverage_dummy_test() {
local package="$1" local package="$1"
rm -f $(kube::golang::path_for_coverage_dummy_test "${package}") rm -f "$(kube::golang::path_for_coverage_dummy_test "${package}")"
} }
# Arguments: a list of kubernetes packages to build. # Arguments: a list of kubernetes packages to build.
@ -572,7 +577,7 @@ kube::golang::build_some_binaries() {
fi fi
done done
if [[ "${#uncovered[@]}" != 0 ]]; then if [[ "${#uncovered[@]}" != 0 ]]; then
V=2 kube::log::info "Building ${uncovered[@]} without coverage..." V=2 kube::log::info "Building ${uncovered[*]} without coverage..."
go install "${build_args[@]}" "${uncovered[@]}" go install "${build_args[@]}" "${uncovered[@]}"
else else
V=2 kube::log::info "Nothing to build without coverage." V=2 kube::log::info "Nothing to build without coverage."
@ -594,11 +599,11 @@ kube::golang::build_binaries_for_platform() {
for binary in "${binaries[@]}"; do for binary in "${binaries[@]}"; do
if [[ "${binary}" =~ ".test"$ ]]; then if [[ "${binary}" =~ ".test"$ ]]; then
tests+=(${binary}) tests+=("${binary}")
elif kube::golang::is_statically_linked_library "${binary}"; then elif kube::golang::is_statically_linked_library "${binary}"; then
statics+=(${binary}) statics+=("${binary}")
else else
nonstatics+=(${binary}) nonstatics+=("${binary}")
fi fi
done done
@ -625,10 +630,11 @@ kube::golang::build_binaries_for_platform() {
fi fi
for test in "${tests[@]:+${tests[@]}}"; do for test in "${tests[@]:+${tests[@]}}"; do
local outfile=$(kube::golang::outfile_for_binary "${test}" "${platform}") local outfile testpkg
local testpkg="$(dirname ${test})" outfile=$(kube::golang::outfile_for_binary "${test}" "${platform}")
testpkg=$(dirname "${test}")
mkdir -p "$(dirname ${outfile})" mkdir -p "$(dirname "${outfile}")"
go test -c \ go test -c \
${goflags:+"${goflags[@]}"} \ ${goflags:+"${goflags[@]}"} \
-gcflags "${gogcflags:-}" \ -gcflags "${gogcflags:-}" \
@ -645,13 +651,13 @@ kube::golang::get_physmem() {
# Linux kernel version >=3.14, in kb # Linux kernel version >=3.14, in kb
if mem=$(grep MemAvailable /proc/meminfo | awk '{ print $2 }'); then if mem=$(grep MemAvailable /proc/meminfo | awk '{ print $2 }'); then
echo $(( ${mem} / 1048576 )) echo $(( mem / 1048576 ))
return return
fi fi
# Linux, in kb # Linux, in kb
if mem=$(grep MemTotal /proc/meminfo | awk '{ print $2 }'); then if mem=$(grep MemTotal /proc/meminfo | awk '{ print $2 }'); then
echo $(( ${mem} / 1048576 )) echo $(( mem / 1048576 ))
return return
fi fi
@ -660,7 +666,7 @@ kube::golang::get_physmem() {
# platform case, which is a Dockerized build), but this is provided # platform case, which is a Dockerized build), but this is provided
# for completeness. # for completeness.
if mem=$(sysctl -n hw.memsize 2>/dev/null); then if mem=$(sysctl -n hw.memsize 2>/dev/null); then
echo $(( ${mem} / 1073741824 )) echo $(( mem / 1073741824 ))
return return
fi fi
@ -713,7 +719,7 @@ kube::golang::build_binaries() {
fi fi
local binaries local binaries
binaries=($(kube::golang::binaries_from_targets "${targets[@]}")) while IFS="" read -r binary; do binaries+=("$binary"); done < <(kube::golang::binaries_from_targets "${targets[@]}")
local parallel=false local parallel=false
if [[ ${#platforms[@]} -gt 1 ]]; then if [[ ${#platforms[@]} -gt 1 ]]; then
@ -735,14 +741,14 @@ kube::golang::build_binaries() {
for platform in "${platforms[@]}"; do ( for platform in "${platforms[@]}"; do (
kube::golang::set_platform_envs "${platform}" kube::golang::set_platform_envs "${platform}"
kube::log::status "${platform}: build started" kube::log::status "${platform}: build started"
kube::golang::build_binaries_for_platform ${platform} kube::golang::build_binaries_for_platform "${platform}"
kube::log::status "${platform}: build finished" kube::log::status "${platform}: build finished"
) &> "/tmp//${platform//\//_}.build" & ) &> "/tmp//${platform//\//_}.build" &
done done
local fails=0 local fails=0
for job in $(jobs -p); do for job in $(jobs -p); do
wait ${job} || let "fails+=1" wait "${job}" || (( fails+=1 ))
done done
for platform in "${platforms[@]}"; do for platform in "${platforms[@]}"; do
@ -755,7 +761,7 @@ kube::golang::build_binaries() {
kube::log::status "Building go targets for ${platform}:" "${targets[@]}" kube::log::status "Building go targets for ${platform}:" "${targets[@]}"
( (
kube::golang::set_platform_envs "${platform}" kube::golang::set_platform_envs "${platform}"
kube::golang::build_binaries_for_platform ${platform} kube::golang::build_binaries_for_platform "${platform}"
) )
done done
fi fi