14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
+ function kube::util::sourced_variable {
18
+ # Call this function to tell shellcheck that a variable is supposed to
19
+ # be used from other calling context. This helps quiet an "unused
20
+ # variable" warning from shellcheck and also document your code.
21
+ true
22
+ }
23
+
17
24
kube::util::sortable_date () {
18
25
date " +%Y%m%d-%H%M%S"
19
26
}
@@ -39,7 +46,7 @@ kube::util::wait_for_url() {
39
46
local times=${4:- 30}
40
47
local maxtime=${5:- 1}
41
48
42
- which curl > /dev/null || {
49
+ command -v curl > /dev/null || {
43
50
kube::log::usage " curl must be installed"
44
51
exit 1
45
52
}
@@ -69,15 +76,19 @@ kube::util::trap_add() {
69
76
local new_cmd
70
77
71
78
# Grab the currently defined trap commands for this trap
72
- existing_cmd=` trap -p " ${trap_add_name} " | awk -F" '" ' {print $2}' `
79
+ existing_cmd=$( trap -p " ${trap_add_name} " | awk -F" '" ' {print $2}' )
73
80
74
81
if [[ -z " ${existing_cmd} " ]]; then
75
82
new_cmd=" ${trap_add_cmd} "
76
83
else
77
84
new_cmd=" ${trap_add_cmd} ;${existing_cmd} "
78
85
fi
79
86
80
- # Assign the test
87
+ # Assign the test. Disable the shellcheck warning telling that trap
88
+ # commands should be single quoted to avoid evaluating them at this
89
+ # point instead evaluating them at run time. The logic of adding new
90
+ # commands to a single trap requires them to be evaluated right away.
91
+ # shellcheck disable=SC2064
81
92
trap " ${new_cmd} " " ${trap_add_name} "
82
93
done
83
94
}
@@ -173,8 +184,10 @@ kube::util::find-binary-for-platform() {
173
184
# The bazel go rules place some binaries in subtrees like
174
185
# "bazel-bin/source/path/linux_amd64_pure_stripped/binaryname", so make sure
175
186
# the platform name is matched in the path.
176
- locations+=($( find " ${KUBE_ROOT} /bazel-bin/" -type f -executable \
177
- \( -path " */${platform/ \/ / _} */${lookfor} " -o -path " */${lookfor} " \) 2> /dev/null || true) )
187
+ while IFS=$' \n ' read -r location; do
188
+ locations+=(" $location " );
189
+ done < <( find " ${KUBE_ROOT} /bazel-bin/" -type f -executable \
190
+ \( -path " */${platform/ \/ / _} */${lookfor} " -o -path " */${lookfor} " \) 2> /dev/null || true)
178
191
179
192
# List most recently-updated location.
180
193
local -r bin=$( (ls -t " ${locations[@]} " 2> /dev/null || true) | head -1 )
@@ -197,6 +210,10 @@ kube::util::gen-docs() {
197
210
genyaml=$( kube::util::find-binary " genyaml" )
198
211
genfeddocs=$( kube::util::find-binary " genfeddocs" )
199
212
213
+ # TODO: If ${genfeddocs} is not used from anywhere (it isn't used at
214
+ # least from k/k tree), remove it completely.
215
+ kube::util::sourced_variable " ${genfeddocs} "
216
+
200
217
mkdir -p " ${dest} /docs/user-guide/kubectl/"
201
218
" ${gendocs} " " ${dest} /docs/user-guide/kubectl/"
202
219
mkdir -p " ${dest} /docs/admin/"
@@ -222,18 +239,18 @@ kube::util::gen-docs() {
222
239
" ${genyaml} " " ${dest} /docs/yaml/kubectl/"
223
240
224
241
# create the list of generated files
225
- pushd " ${dest} " > /dev/null
242
+ pushd " ${dest} " > /dev/null || return 1
226
243
touch docs/.generated_docs
227
244
find . -type f | cut -sd / -f 2- | LC_ALL=C sort > docs/.generated_docs
228
- popd > /dev/null
245
+ popd > /dev/null || return 1
229
246
}
230
247
231
248
# Removes previously generated docs-- we don't want to check them in. $KUBE_ROOT
232
249
# must be set.
233
250
kube::util::remove-gen-docs () {
234
251
if [ -e " ${KUBE_ROOT} /docs/.generated_docs" ]; then
235
252
# remove all of the old docs; we don't want to check them in.
236
- while read file; do
253
+ while read -r file; do
237
254
rm " ${KUBE_ROOT} /${file} " 2> /dev/null || true
238
255
done < " ${KUBE_ROOT} /docs/.generated_docs"
239
256
# The docs/.generated_docs file lists itself, so we don't need to explicitly
@@ -249,18 +266,14 @@ kube::util::remove-gen-docs() {
249
266
# * Special handling for groups suffixed with ".k8s.io": foo.k8s.io/v1 -> apis/foo/v1
250
267
# * Very special handling for when both group and version are "": / -> api
251
268
kube::util::group-version-to-pkg-path () {
252
- staging_apis=(
253
- $(
254
- cd " ${KUBE_ROOT} /staging/src/k8s.io/api" &&
255
- find . -name types.go -exec dirname {} \; | sed " s|\./||g" | sort
256
- ) )
257
-
258
269
local group_version=" $1 "
259
270
260
- if [[ " ${staging_apis[@]} " =~ " ${group_version/ .* k8s.io/ } " ]]; then
261
- echo " vendor/k8s.io/api/${group_version/ .* k8s.io/ } "
262
- return
263
- fi
271
+ while IFS=$' \n ' read -r api; do
272
+ if [[ " ${api} " = " ${group_version/ .* k8s.io/ } " ]]; then
273
+ echo " vendor/k8s.io/api/${group_version/ .* k8s.io/ } "
274
+ return
275
+ fi
276
+ done < <( cd " ${KUBE_ROOT} /staging/src/k8s.io/api" && find . -name types.go -exec dirname {} \; | sed " s|\./||g" | sort)
264
277
265
278
# "v1" is the API GroupVersion
266
279
if [[ " ${group_version} " == " v1" ]]; then
@@ -322,7 +335,7 @@ kube::util::git_upstream_remote_name() {
322
335
kube::util::create-fake-git-tree () {
323
336
local -r target_dir=${1:- $(pwd)}
324
337
325
- pushd " ${target_dir} " > /dev/null
338
+ pushd " ${target_dir} " > /dev/null || return 1
326
339
git init > /dev/null
327
340
git config --local user.email
" [email protected] "
328
341
git config --local user.name " $0 "
@@ -331,7 +344,7 @@ kube::util::create-fake-git-tree() {
331
344
if (( ${KUBE_VERBOSE:- 5} >= 6 )) ; then
332
345
kube::log::status " ${target_dir} is now a git tree."
333
346
fi
334
- popd > /dev/null
347
+ popd > /dev/null || return 1
335
348
}
336
349
337
350
# Checks whether godep restore was run in the current GOPATH, i.e. that all referenced repos exist
@@ -344,16 +357,16 @@ kube::util::godep_restored() {
344
357
345
358
local root
346
359
local old_rev=" "
347
- while read path rev; do
348
- rev=$( echo " ${rev} " | sed " s/[ '\" ]//g " ) # remove quotes which are around revs sometimes
360
+ while read -r path rev; do
361
+ rev=" ${rev// [\ '\"]} " # remove quotes which are around revs sometimes
349
362
350
363
if [[ " ${rev} " == " ${old_rev} " ]] && [[ " ${path} " == " ${root} " * ]]; then
351
364
# avoid checking the same git/hg root again
352
365
continue
353
366
fi
354
367
355
368
root=" ${path} "
356
- while [ " ${root} " != " ." -a ! -d " ${gopath} /src/${root} /.git" -a ! -d " ${gopath} /src/${root} /.hg" ]; do
369
+ while [ " ${root} " != " ." ] && [ ! -d " ${gopath} /src/${root} /.git" ] && [ ! -d " ${gopath} /src/${root} /.hg" ]; do
357
370
root=$( dirname " ${root} " )
358
371
done
359
372
if [ " ${root} " == " ." ]; then
@@ -387,7 +400,7 @@ kube::util::ensure_clean_working_dir() {
387
400
exit 1
388
401
fi | sed ' s/^/ /'
389
402
echo -e " \nCommit your changes in another terminal and then continue here by pressing enter."
390
- read
403
+ read -r
391
404
done 1>&2
392
405
}
393
406
@@ -479,7 +492,8 @@ kube::util::has_changes() {
479
492
local -r pattern=$2
480
493
local -r not_pattern=${3:- totallyimpossiblepattern}
481
494
482
- local base_ref=$( kube::util::base_ref " ${git_branch} " )
495
+ local base_ref
496
+ base_ref=$( kube::util::base_ref " ${git_branch} " )
483
497
echo " Checking for '${pattern} ' changes against '${base_ref} '"
484
498
485
499
# notice this uses ... to find the first shared ancestor
@@ -499,11 +513,11 @@ kube::util::download_file() {
499
513
local -r url=$1
500
514
local -r destination_file=$2
501
515
502
- rm ${destination_file} 2& > /dev/null || true
516
+ rm " ${destination_file} " 2& > /dev/null || true
503
517
504
518
for i in $( seq 5)
505
519
do
506
- if ! curl -fsSL --retry 3 --keepalive-time 2 ${url} -o ${destination_file} ; then
520
+ if ! curl -fsSL --retry 3 --keepalive-time 2 " ${url} " -o " ${destination_file} " ; then
507
521
echo " Downloading ${url} failed. $(( 5 - i)) retries left."
508
522
sleep 1
509
523
else
@@ -518,8 +532,7 @@ kube::util::download_file() {
518
532
# Sets:
519
533
# OPENSSL_BIN: The path to the openssl binary to use
520
534
function kube::util::test_openssl_installed {
521
- openssl version >& /dev/null
522
- if [ " $? " != " 0" ]; then
535
+ if ! openssl version >& /dev/null; then
523
536
echo " Failed to run openssl. Please ensure openssl is installed"
524
537
exit 1
525
538
fi
@@ -602,7 +615,7 @@ function kube::util::write_client_kubeconfig {
602
615
local api_port=$5
603
616
local client_id=$6
604
617
local token=${7:- }
605
- cat << EOF | ${sudo} tee "${dest_dir} "/${client_id} .kubeconfig > /dev/null
618
+ cat << EOF | ${sudo} tee "${dest_dir} "/" ${client_id} " .kubeconfig > /dev/null
606
619
apiVersion: v1
607
620
kind: Config
608
621
clusters:
635
648
636
649
# Determines if docker can be run, failures may simply require that the user be added to the docker group.
637
650
function kube::util::ensure_docker_daemon_connectivity {
638
- DOCKER=(docker ${DOCKER_OPTS} )
651
+ IFS=" " read -ra DOCKER <<< " ${DOCKER_OPTS}"
652
+ DOCKER=(docker " ${DOCKER[@]} " )
639
653
if ! " ${DOCKER[@]} " info > /dev/null 2>&1 ; then
640
654
cat << 'EOF ' >&2
641
655
Can't connect to 'docker' daemon. please fix and retry.
@@ -713,7 +727,7 @@ function kube::util::ensure-cfssl {
713
727
fi
714
728
715
729
mkdir -p " ${cfssldir} "
716
- pushd " ${cfssldir} " > /dev/null
730
+ pushd " ${cfssldir} " > /dev/null || return 1
717
731
718
732
echo " Unable to successfully run 'cfssl' from ${PATH} ; downloading instead..."
719
733
kernel=$( uname -s)
@@ -742,7 +756,7 @@ function kube::util::ensure-cfssl {
742
756
echo " Hint: export PATH=\$ PATH:\$ GOPATH/bin; go get -u github.com/cloudflare/cfssl/cmd/..."
743
757
exit 1
744
758
fi
745
- popd > /dev/null
759
+ popd > /dev/null || return 1
746
760
}
747
761
748
762
# kube::util::ensure_dockerized
@@ -766,12 +780,13 @@ function kube::util::ensure_dockerized {
766
780
function kube::util::ensure-gnu-sed {
767
781
if LANG=C sed --help 2>&1 | grep -q GNU; then
768
782
SED=" sed"
769
- elif which gsed & > /dev/null; then
783
+ elif command -v gsed & > /dev/null; then
770
784
SED=" gsed"
771
785
else
772
786
kube::log::error " Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >&2
773
787
return 1
774
788
fi
789
+ kube::util::sourced_variable " ${SED} "
775
790
}
776
791
777
792
# kube::util::check-file-in-alphabetical-order <file>
@@ -794,7 +809,7 @@ function kube::util::check-file-in-alphabetical-order {
794
809
# kube::util::require-jq
795
810
# Checks whether jq is installed.
796
811
function kube::util::require-jq {
797
- if ! which jq & > /dev/null; then
812
+ if ! command -v jq & > /dev/null; then
798
813
echo " jq not found. Please install." 1>&2
799
814
return 1
800
815
fi
@@ -809,6 +824,14 @@ if [[ -z "${color_start-}" ]]; then
809
824
declare -r color_blue=" ${color_start} 1;34m"
810
825
declare -r color_cyan=" ${color_start} 1;36m"
811
826
declare -r color_norm=" ${color_start} 0m"
827
+
828
+ kube::util::sourced_variable " ${color_start} "
829
+ kube::util::sourced_variable " ${color_red} "
830
+ kube::util::sourced_variable " ${color_yellow} "
831
+ kube::util::sourced_variable " ${color_green} "
832
+ kube::util::sourced_variable " ${color_blue} "
833
+ kube::util::sourced_variable " ${color_cyan} "
834
+ kube::util::sourced_variable " ${color_norm} "
812
835
fi
813
836
814
837
# ex: ts=2 sw=2 et filetype=sh
0 commit comments