Skip to content

Commit 06d584d

Browse files
authored
Merge pull request #181 from gabrielggg/main
add shellcheck verification step to makefile
2 parents 5862cce + 534d923 commit 06d584d

10 files changed

+132
-46
lines changed

hack/build-install-yaml.sh

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
set -o errexit
1818
set -o nounset
1919
set -o pipefail
20-
21-
thisyear=`date +"%Y"`
20+
shopt -s nullglob
21+
thisyear=$(date +"%Y")
2222

2323
mkdir -p release/
2424

@@ -39,22 +39,28 @@ cat << EOF >> release/standard-install.yaml
3939
#
4040
EOF
4141

42-
for file in `ls config/crd/experimental/policy*.yaml`
42+
for file in config/crd/experimental/policy*.yaml
4343
do
44-
echo "---" >> release/experimental-install.yaml
45-
echo "#" >> release/experimental-install.yaml
46-
echo "# $file" >> release/experimental-install.yaml
47-
echo "#" >> release/experimental-install.yaml
48-
cat $file >> release/experimental-install.yaml
44+
[[ -e "$file" ]] || break;
45+
{
46+
echo "---"
47+
echo "#"
48+
echo "# $file"
49+
echo "#"
50+
cat "$file"
51+
} >> release/experimental-install.yaml
4952
done
5053

51-
for file in `ls config/crd/standard/policy*.yaml`
54+
for file in config/crd/standard/policy*.yaml
5255
do
53-
echo "---" >> release/standard-install.yaml
54-
echo "#" >> release/standard-install.yaml
55-
echo "# $file" >> release/standard-install.yaml
56-
echo "#" >> release/standard-install.yaml
57-
cat $file >> release/standard-install.yaml
56+
[[ -e "$file" ]] || break;
57+
{
58+
echo "---"
59+
echo "#"
60+
echo "# $file"
61+
echo "#"
62+
cat "$file"
63+
} >> release/standard-install.yaml
5864
done
5965

6066

hack/crd-e2e.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21-
readonly GOPATH="$(mktemp -d)"
22-
readonly CLUSTER_NAME="verify-network-policy-api"
21+
GOPATH="$(mktemp -d)"
22+
readonly GOPATH
23+
CLUSTER_NAME="verify-network-policy-api"
24+
readonly CLUSTER_NAME
2325

2426
export KUBECONFIG="${GOPATH}/.kubeconfig"
2527
export GOFLAGS GO111MODULE GOPATH
@@ -49,8 +51,9 @@ res=0
4951
# Create cluster
5052
KIND_CREATE_ATTEMPTED=true
5153
kind create cluster --name "${CLUSTER_NAME}" || res=$?
52-
53-
for KUST_FOLDER in bases patches; do
54+
#echo $bases
55+
#echo $patches
56+
for _ in bases patches; do
5457
go run sigs.k8s.io/controller-tools/cmd/controller-gen rbac:roleName=manager-role crd paths=./apis/... output:crd:dir=./config/crd/bases output:stdout || res=$?
5558
kubectl kustomize config/crd | kubectl apply -f - || res=$?
5659

hack/kube-env.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
if [[ -z "${color_start-}" ]]; then
1919
declare -r color_start="\033["
2020
declare -r color_red="${color_start}0;31m"
21+
export color_red
2122
declare -r color_yellow="${color_start}0;33m"
23+
export color_yellow
2224
declare -r color_green="${color_start}0;32m"
25+
export color_green
2326
declare -r color_norm="${color_start}0m"
27+
export color_norm
2428
fi
2529

2630
# Returns the server version as MMmmpp, with MM as the major
@@ -37,8 +41,8 @@ function kube_server_version() {
3741
# Server Version: &version.Info{Major:"0", Minor:"7+", GitVersion:"v0.7.0-dirty", GitCommit:"ad44234f7152e9c66bc2853575445c7071335e57", GitTreeState:"dirty"}
3842
# and capture the GitVersion portion (which has the patch level)
3943
server_version=$(${KUBECTL} --match-server-version=false version | grep "Server Version:")
40-
read major minor patch < <(
41-
echo ${server_version} | \
44+
read -r major minor patch < <(
45+
echo "${server_version}" | \
4246
sed "s/.*GitVersion:\"v\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\).*/\1 \2 \3/")
43-
printf "%02d%02d%02d" ${major} ${minor} ${patch} | sed 's/^0*//'
47+
printf "%02d%02d%02d" "${major}" "${minor}" "${patch}" | sed 's/^0*//'
4448
}

hack/make-docs.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21-
readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"
22-
cd $SCRIPT_ROOT
21+
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
22+
readonly SCRIPT_ROOT
23+
cd "$SCRIPT_ROOT"
2324

2425
# Wrap sed to deal with GNU and BSD sed flags.
2526
run::sed() {

hack/update-codegen.sh

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2086
3+
#Double quote to prevent globbing and word splitting. dont apply for this specific scenario
24

35
# Copyright 2020 The Kubernetes Authors.
46
#
@@ -18,14 +20,17 @@ set -o errexit
1820
set -o nounset
1921
set -o pipefail
2022

21-
readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"
23+
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
24+
readonly SCRIPT_ROOT
2225

2326
# Keep outer module cache so we don't need to redownload them each time.
2427
# The build cache already is persisted.
25-
readonly GOMODCACHE="$(go env GOMODCACHE)"
28+
GOMODCACHE="$(go env GOMODCACHE)"
29+
readonly GOMODCACHE
2630
readonly GO111MODULE="on"
2731
readonly GOFLAGS="-mod=readonly"
28-
readonly GOPATH="$(mktemp -d)"
32+
GOPATH="$(mktemp -d)"
33+
readonly GOPATH
2934
readonly MIN_REQUIRED_GO_VER="1.19"
3035

3136
function go_version_matches {
@@ -79,16 +84,14 @@ go run k8s.io/code-generator/cmd/informer-gen \
7984
--output-package "${OUTPUT_PKG}/informers" \
8085
${COMMON_FLAGS}
8186

82-
for VERSION in v1alpha1 ## Add more versions eventually
83-
do
84-
echo "Generating ${VERSION} register at ${APIS_PKG}/apis/${VERSION}"
85-
go run k8s.io/code-generator/cmd/register-gen \
86-
--input-dirs "${APIS_PKG}/apis/${VERSION}" \
87-
--output-package "${APIS_PKG}/apis/${VERSION}" \
88-
${COMMON_FLAGS}
89-
90-
echo "Generating ${VERSION} deepcopy at ${APIS_PKG}/apis/${VERSION}"
91-
go run sigs.k8s.io/controller-tools/cmd/controller-gen \
92-
object:headerFile=${SCRIPT_ROOT}/hack/boilerplate.generatego.txt \
93-
paths="${APIS_PKG}/apis/${VERSION}"
94-
done
87+
VERSION=v1alpha1
88+
echo "Generating ${VERSION} register at ${APIS_PKG}/apis/${VERSION}"
89+
go run k8s.io/code-generator/cmd/register-gen \
90+
--input-dirs "${APIS_PKG}/apis/${VERSION}" \
91+
--output-package "${APIS_PKG}/apis/${VERSION}" \
92+
${COMMON_FLAGS}
93+
94+
echo "Generating ${VERSION} deepcopy at ${APIS_PKG}/apis/${VERSION}"
95+
go run sigs.k8s.io/controller-tools/cmd/controller-gen \
96+
object:headerFile="${SCRIPT_ROOT}/hack/boilerplate.generatego.txt" \
97+
paths="${APIS_PKG}/apis/${VERSION}"

hack/verify-all.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
set -o errexit
1818
set -o nounset
1919
set -o pipefail
20+
shopt -s nullglob
2021

21-
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2223
source "${SCRIPT_ROOT}/hack/kube-env.sh"
2324

2425
SILENT=true
2526

2627
function is-excluded {
2728
for e in $EXCLUDE; do
28-
if [[ $1 -ef ${BASH_SOURCE} ]]; then
29+
if [[ $1 -ef ${BASH_SOURCE[0]} ]]; then
2930
return
3031
fi
3132
if [[ $1 -ef "$SCRIPT_ROOT/hack/$e" ]]; then
@@ -54,9 +55,10 @@ fi
5455
EXCLUDE="verify-all.sh"
5556

5657
ret=0
57-
for t in `ls $SCRIPT_ROOT/hack/verify-*.sh`
58+
for t in "$SCRIPT_ROOT"/hack/verify-*.sh
5859
do
59-
if is-excluded $t ; then
60+
[[ -e "$t" ]] || break;
61+
if is-excluded "$t" ; then
6062
echo "Skipping $t"
6163
continue
6264
fi

hack/verify-codegen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21-
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
21+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2222

2323
make -C "$SCRIPT_ROOT" generate
2424

hack/verify-golint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ set -o nounset
1919
set -o pipefail
2020

2121
readonly VERSION="v1.55.2"
22-
readonly KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
23+
readonly KUBE_ROOT
2324

2425
cd "${KUBE_ROOT}"
2526

hack/verify-govet.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ set -euo pipefail
1818

1919
echo "Verifying govet"
2020

21-
go vet $(go list ./... | grep -v vendor)
21+
for key in $(go list ./... | grep -v vendor); do
22+
go vet "$key"
23+
done
2224

2325
echo "Done"

hack/verify-shellcheck.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
readonly VERSION="v0.9.0"
22+
OWNER=koalaman
23+
REPO="shellcheck"
24+
BINARY=shellcheck
25+
FORMAT=tar.xz
26+
#supported OS's linux and darwin
27+
#supported cpu architectures:
28+
#linux: x86_64, aarch64, armv6hf
29+
#darwin: x86_64 (only this architecture because the official shellcheck repo only delivers this binary)
30+
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
31+
ARCH=$(uname -m)
32+
GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
33+
NAME=${BINARY}-${VERSION}.${OS}.${ARCH}
34+
TARBALL=${NAME}.${FORMAT}
35+
TARBALL_URL=${GITHUB_DOWNLOAD}/${VERSION}/${TARBALL}
36+
BINDIR=${BINDIR:-./bin}
37+
38+
echo "Installing shellcheck..."
39+
40+
http_download_curl() {
41+
local_file=$1
42+
source_url=$2
43+
code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
44+
if [ "$code" != "200" ]; then
45+
log_debug "http_download_curl received HTTP status $code"
46+
return 1
47+
fi
48+
return 0
49+
}
50+
51+
tmpdir=$(mktemp -d)
52+
http_download_curl "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
53+
srcdir="${tmpdir}/${NAME}"
54+
rm -rf "${srcdir}"
55+
(cd "${tmpdir}" && tar --no-same-owner -xf "${TARBALL}")
56+
binexe="./shellcheck-${VERSION}/shellcheck"
57+
install "${tmpdir}/${binexe}" "${BINDIR}/"
58+
rm -rf "${tmpdir}"
59+
60+
echo "Running shellcheck scan..."
61+
62+
#scanning all .sh scripts inside the hack directory
63+
#ignoring SC2001 in shellcheck because complex sed substitution is required.
64+
./bin/shellcheck -e SC2001 hack/*.sh

0 commit comments

Comments
 (0)