Skip to content

Commit

Permalink
bin: shellscript housekeeping (#13469)
Browse files Browse the repository at this point in the history
- Use single quotes for static strings, not double
- Use sh and not bash for _os.sh that has no bash-specific content
- Remove superfluous double quotes on variable assignments
- Only use bash-specific [[ ]] if statements for bash-specific content
- Consistently use style with if; then on same line

Change-Id: I1575971262773149db32021efbf2d7f1d4df9771

Signed-off-by: Joakim Roubert <[email protected]>
  • Loading branch information
joakimr-axis authored Dec 12, 2024
1 parent 0d9c86c commit 2b92b07
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 67 deletions.
6 changes: 3 additions & 3 deletions bin/_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ docker_build() {
shift

rootdir=${ROOTDIR:-$( cd "$bindir"/.. && pwd )}
cache_params=""
cache_params=''

if [ "$ACTIONS_CACHE_URL" ]; then
cache_params="--cache-from type=gha,scope=$name-$DOCKER_TARGET --cache-to type=gha,scope=$name-$DOCKER_TARGET,mode=max"
fi

output_params="--load"
output_params='--load'
if [ "$DOCKER_TARGET" = 'multi-arch' ]; then
output_params="--platform $SUPPORTED_ARCHS"
if [ "$DOCKER_PUSH" ]; then
output_params+=" --push"
output_params+=' --push'
else
echo 'Error: env DOCKER_PUSH=1 is missing
When building the multi-arch images it is required to push the images to the registry
Expand Down
6 changes: 3 additions & 3 deletions bin/_os.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

set -eu

export OS_ARCH_ALL="linux-amd64 linux-arm64 linux-arm darwin darwin-arm64 windows"
export OS_ARCH_ALL='linux-amd64 linux-arm64 linux-arm darwin darwin-arm64 windows'

architecture() {
arch=$(uname -m)
Expand Down Expand Up @@ -32,7 +32,7 @@ architecture() {

os() {
os=$(uname -s)
arch=""
arch=''
case $os in
CYGWIN* | MINGW64*)
os=windows
Expand Down
2 changes: 1 addition & 1 deletion bin/_tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ clean_head() {
}

named_tag() {
tag="$(git name-rev --tags --name-only "$(git_sha_head)")"
tag=$(git name-rev --tags --name-only "$(git_sha_head)")
tag=${tag%"^0"}
echo "${tag}"
}
Expand Down
14 changes: 7 additions & 7 deletions bin/_test-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ k8s_version_min='+v1.22'
k8s_version_max='docker.io/rancher/k3s:v1.29.6-k3s2'

bindir=$( cd "${BASH_SOURCE[0]%/*}" && pwd )
testdir="$bindir"/../test/integration
testdir=$bindir/../test/integration

##### Test setup helpers #####

Expand All @@ -20,7 +20,7 @@ export all_test_names=(cluster-domain cni-calico-deep multicluster "${default_te
images_load_default=(proxy controller policy-controller web metrics-api tap)

tests_usage() {
progname="${0##*/}"
progname=${0##*/}
echo "Run Linkerd integration tests.
Optionally specify a test with the --name flag: [${all_test_names[*]}]
Expand Down Expand Up @@ -56,7 +56,7 @@ Available Commands:
}

cleanup_usage() {
progname="${0##*/}"
progname=${0##*/}
echo "Cleanup Linkerd integration tests.
Usage:
Expand All @@ -76,7 +76,7 @@ handle_tests_input() {
export skip_cluster_create=''
export skip_cluster_delete=''
export cleanup_docker=''
export linkerd_path=""
export linkerd_path=''

while [ $# -ne 0 ]; do
case $1 in
Expand Down Expand Up @@ -227,7 +227,7 @@ cleanup_cluster() {

setup_min_cluster() {
local name=$1
export helm_path="$bindir"/helm
export helm_path=$bindir/helm

check_linkerd_binary
if [ -z "$skip_cluster_create" ]; then
Expand All @@ -239,7 +239,7 @@ setup_min_cluster() {

setup_cluster() {
local name=$1
export helm_path="$bindir"/helm
export helm_path=$bindir/helm

check_linkerd_binary
if [ -z "$skip_cluster_create" ]; then
Expand Down Expand Up @@ -428,7 +428,7 @@ run_helm-upgrade_test() {
fi

setup_helm
helm_viz_chart="$( cd "$bindir"/.. && pwd )"/viz/charts/linkerd-viz
helm_viz_chart=$( cd "$bindir"/.. && pwd )/viz/charts/linkerd-viz
run_test "$testdir/install/install_test.go" --helm-path="$helm_path" --helm-charts="$helm_charts" \
--viz-helm-chart="$helm_viz_chart" --viz-helm-stable-chart="linkerd/linkerd-viz" --helm-release="$helm_release_name" --upgrade-helm-from-version="$edge_version"
helm_cleanup
Expand Down
2 changes: 1 addition & 1 deletion bin/certs-openssl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
#

set -eu

# Creates the root and issuer (intermediary) self-signed certificates for the control plane using openssl.
Expand Down
3 changes: 1 addition & 2 deletions bin/docker
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ targetbin=$( cd "$bindir"/.. && pwd )/target/bin
dockerbin=$targetbin/.docker-$dockerversion

if [ ! -f "$dockerbin" ]; then
filename=docker-$dockerversion.tgz
if [ "$(uname -s)" = Darwin ]; then
os=mac
filename=docker-$dockerversion.tgz
else
os=linux
filename=docker-$dockerversion.tgz
fi

url=https://download.docker.com/$os/static/stable/x86_64/$filename
Expand Down
2 changes: 1 addition & 1 deletion bin/docker-push
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -eu

if [ $# -eq 1 ]; then
tag="${1:-}"
tag=${1:-}
else
echo "usage: ${0##*/} tag" >&2
exit 64
Expand Down
12 changes: 6 additions & 6 deletions bin/fetch-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ set -eu

bindir=$( cd "${0%/*}" && pwd )
rootdir=$( cd "$bindir"/.. && pwd )
builddir="$rootdir/target/proxy"
builddir=$rootdir/target/proxy

proxy_repo="${LINKERD2_PROXY_REPO:-}"
proxy_repo=${LINKERD2_PROXY_REPO:-}
if [ -z "$proxy_repo" ]; then
proxy_repo=linkerd/linkerd2-proxy
fi

releases_url=https://api.github.com/repos/"$proxy_repo"/releases

github_token="${GITHUB_TOKEN:-}"
github_token=${GITHUB_TOKEN:-}
if [ -z "$github_token" ] && [ -n "${GITHUB_TOKEN_FILE:-}" ] && [ -f "$GITHUB_TOKEN_FILE" ]; then
github_token=$(cat "$GITHUB_TOKEN_FILE")
fi
Expand All @@ -42,16 +42,16 @@ if ! ghcurl "$releases_url" | jq '.[] | select(.name == "'"$version"'")' > relea
exit 1
fi

pkgname="linkerd2-proxy-${version}-${arch}"
pkgname=linkerd2-proxy-${version}-${arch}

pkgfile="${pkgname}.tar.gz"
pkgfile=${pkgname}.tar.gz
pkgurl=$(jq -r '.assets[] | select(.name == "'"$pkgfile"'") | .url' release.json)
if ! ghcurl -H 'Accept: application/octet-stream' -o "$pkgfile" "$pkgurl" ; then
echo "Failed to fetch $pkgurl" >&2
exit 1
fi

shafile="${pkgname}.txt"
shafile=${pkgname}.txt
shaurl=$(jq -r '.assets[] | select(.name == "'"$shafile"'") | .url' release.json)
if ! ghcurl -H 'Accept: application/octet-stream' -o "$shafile" "$shaurl" ; then
echo "Failed to fetch $shaurl" >&2
Expand Down
2 changes: 1 addition & 1 deletion bin/go-mod-tree
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ $# -gt 1 ]; then
echo "Usage: $0 [root]" >&2
exit 64
fi
declare -r MODULE="${1:-github.com/linkerd/linkerd2}"
declare -r MODULE=${1:-github.com/linkerd/linkerd2}

GRAPH=$(go mod graph)
declare -r GRAPH
Expand Down
2 changes: 1 addition & 1 deletion bin/go-mod-versions
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ $# -ne 1 ]; then
echo "Usage: $0 <module>" >&2
exit 64
fi
declare -r MODULE="$1"
declare -r MODULE=$1

if [[ "$MODULE" == *@* ]]; then
echo 'The dependency must not specify an exact version.' >&2
Expand Down
2 changes: 1 addition & 1 deletion bin/go-mod-why
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ $# -ne 1 ]; then
echo "Usage: $0 <module>" >&2
exit 64
fi
declare -r MODULE="$1"
declare -r MODULE=$1

GRAPH=$(go mod graph)
declare -r GRAPH
Expand Down
2 changes: 1 addition & 1 deletion bin/helm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [ ! -f "$helmbin" ]; then
arm) dpkg --print-architecture | grep -q arm64 && arch=arm64 || arch=arm ;;
esac
fi
helmcurl="https://get.helm.sh/helm-$helmversion-$os-$arch.tar.gz"
helmcurl=https://get.helm.sh/helm-$helmversion-$os-$arch.tar.gz
targetdir=$os-$arch
tmp=$(mktemp -d -t helm.XXX)
mkdir -p "$targetbin"
Expand Down
6 changes: 3 additions & 3 deletions bin/helm-bump-edge
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bindir=$( cd "${BASH_SOURCE[0]%/*}" && pwd )
. "$bindir"/_tag.sh
tag=$(named_tag)

edge_tag_regex="edge-([0-9][0-9])\.([0-9]|[0-9][0-9])\.([0-9]+)"
edge_tag_regex='edge-([0-9][0-9])\.([0-9]|[0-9][0-9])\.([0-9]+)'

# Get the current edge version.
url=https://run.linkerd.io/install-edge
Expand All @@ -24,15 +24,15 @@ yyyy=$(date +"%Y")
new_mm=$(date +"%-m")

# If this is a new month, `new_xx` should be 1; otherwise increment it.
if [[ "$new_mm" != "$current_mm" ]]; then
if [ "$new_mm" != "$current_mm" ]; then
new_xx=1
else
new_xx=$((current_xx+1))
fi

expected_tag="edge-$yy.$new_mm.$new_xx"

if [[ "$tag" != "$expected_tag" ]]; then
if [ "$tag" != "$expected_tag" ]; then
echo "Tag ($tag) doesn't match computed edge version ($expected_tag)"
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions bin/helm-docs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ helmdocsv=1.12.0
bindir=$( cd "${0%/*}" && pwd ) # Change to script dir and set bin dir to this
targetbin=$( cd "$bindir"/.. && pwd )/target/bin
helmdocsbin=$targetbin/helm-docs-$helmdocsv
os=""
arch=""
os=''
arch=''

if [ ! -f "$helmdocsbin" ]; then
case $(uname | tr '[:upper:]' '[:lower:]') in
Expand Down
11 changes: 5 additions & 6 deletions bin/image-load
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ if [ "$k3d" ]; then
if [ -z "$cluster" ]; then
cluster=k3s-default
fi
bin="$bindir"/k3d
bin=$bindir/k3d
image_sub_cmd=(image import -c "$cluster")
else
kind=1
bin="$bindir"/kind
bin=$bindir/kind
if [ -z "$cluster" ]; then
cluster=kind
fi
Expand All @@ -106,8 +106,7 @@ else
fi
fi

if [ -z "$archive" ]
then
if [ -z "$archive" ]; then
# shellcheck source=_tag.sh
. "$bindir"/_tag.sh
# shellcheck source=_docker.sh
Expand All @@ -122,9 +121,9 @@ fi
rm -f load_fail
for i in "${!images[@]}"; do
if [ $archive ]; then
param="image-archives/${images[$i]}.tar"
param=image-archives/${images[$i]}.tar
else
param="$DOCKER_REGISTRY/${images[$i]}:$TAG"
param=$DOCKER_REGISTRY/${images[$i]}:$TAG
if [ $preload ]; then
docker pull -q "$param" || (echo "Error pulling image $param"; touch load_fail) &
fi
Expand Down
14 changes: 5 additions & 9 deletions bin/install-pr
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@ done

pr=$1

if [ -z "$pr" ]
then
if [ -z "$pr" ]; then
echo "Error: ${0##*/} accepts 1 argument
Usage:
${0##*/} ####" >&2
exit 1
fi

if [ -z "$GITHUB_TOKEN" ]
then
if [ -z "$GITHUB_TOKEN" ]; then
# shellcheck disable=SC2016
echo 'Error: Generate a personal access token at https://github.com/settings/tokens and set it in the $GITHUB_TOKEN env var'
exit 1
Expand Down Expand Up @@ -108,18 +106,16 @@ do
docker load -i "image-archives/$image.tar"
done

if [ "$is_kind" = true ] || [ "$is_k3d" = true ]
then
if [ "$is_kind" = true ] || [ "$is_k3d" = true ]; then
# When importing into k3d or kind, the images must be renamed to use the
# proper registry so that users don't have to change the default install
# output.
docker_rename_registry "$tag" 'ghcr.io/linkerd' 'cr.l5d.io/linkerd'
distro=k3d
if [ "$is_kind" = true ]
then
if [ "$is_kind" = true ]; then
distro=kind
fi
export TAG="$tag"
export TAG=$tag
"$bindir"/image-load --"$distro" --cluster "$cluster"
else
# The images were built with the ghcr.io registry so when pushing to a
Expand Down
2 changes: 1 addition & 1 deletion bin/k3d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bindir=$( cd "${0%/*}" && pwd )
. "$bindir"/_os.sh

targetbin=$( cd "$bindir"/.. && pwd )/target/bin
k3dbin="$targetbin/k3d-${K3D_VERSION}"
k3dbin=$targetbin/k3d-${K3D_VERSION}

if [ ! -f "$k3dbin" ]; then
arch=$(architecture)
Expand Down
6 changes: 3 additions & 3 deletions bin/rust-toolchain-version
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ if [[ $toolchain =~ $version_regex ]]; then
fi

# Otherwise, no matching line was found, so print an error.
if [[ "${GITHUB_ACTIONS:-false}" == "true" ]]; then
echo "::error file=rust-toolchain.toml::failed to parse rust-toolchain.toml"
if [ "${GITHUB_ACTIONS:-false}" = 'true' ]; then
echo '::error file=rust-toolchain.toml::failed to parse rust-toolchain.toml'
else
echo "failed to parse rust-toolchain.toml"
echo 'failed to parse rust-toolchain.toml'
fi

exit 1
8 changes: 4 additions & 4 deletions bin/test-scale
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ done
BOOKS_APP=$("$bindir"/scurl https://raw.githubusercontent.com/BuoyantIO/booksapp/main/k8s/mysql-app.yml)

# add "-sleep=10ms" param to the traffic app (~100rps)
traffic_param=" - \"webapp:7000\""
traffic_param=' - "webapp:7000"'
sleep_param=$(cat <<-END
- "-sleep=10ms"
- "webapp:7000"
END
)
BOOKS_APP="${BOOKS_APP/$traffic_param/$sleep_param}"
BOOKS_APP=${BOOKS_APP/$traffic_param/$sleep_param}

# inject
BOOKS_APP=$(echo "$BOOKS_APP" | "$linkerd_path" -l "$linkerd_namespace" inject -)
Expand All @@ -128,10 +128,10 @@ EMOJIVOTO=$("$bindir"/scurl https://run.linkerd.io/emojivoto.yml)
# delete namespace
EMOJIVOTO=$(echo "$EMOJIVOTO" | tail -n +6)
emojins='namespace: emojivoto'
EMOJIVOTO="${EMOJIVOTO//$emojins/}"
EMOJIVOTO=${EMOJIVOTO//$emojins/}
emojins=.emojivoto:
newns=:
EMOJIVOTO="${EMOJIVOTO//$emojins/$newns}"
EMOJIVOTO=${EMOJIVOTO//$emojins/$newns}

# inject
EMOJIVOTO=$(echo "$EMOJIVOTO" | "$linkerd_path" -l "$linkerd_namespace" inject -)
Expand Down
Loading

0 comments on commit 2b92b07

Please sign in to comment.