Skip to content

Commit 732ceb8

Browse files
authored
Merge pull request #47 from olivercodes/fmt-vet-lint-crd-scripts
scripts for prow precommit and crd jobs
2 parents 17aae7f + 6e6b1a2 commit 732ceb8

File tree

8 files changed

+327
-2
lines changed

8 files changed

+327
-2
lines changed

.golangci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
run:
2+
timeout: 10m
3+
issues-exit-code: 1
4+
tests: true
5+
skip-dirs-use-default: true
6+
modules-download-mode: readonly
7+
allow-parallel-runners: false
8+
9+
linters:
10+
fast: false
11+
enable:
12+
- gofmt
13+
- goimports
14+
- revive
15+
- govet
16+
- misspell
17+
- exportloopref
18+
disable:
19+
- scopelint
20+
disable-all: false
21+
presets:
22+
- bugs
23+
- unused
24+
25+
# all available settings of specific linters
26+
linters-settings:
27+
gofmt:
28+
# simplify code: gofmt with `-s` option, true by default
29+
simplify: true
30+
goimports:
31+
local-prefixes: sigs.k8s.io/network-policy-api
32+
golint:
33+
min-confidence: 0.9
34+
govet:
35+
# report about shadowed variables
36+
check-shadowing: true
37+
misspell:
38+
locale: US
39+
ignore-words:
40+
41+
issues:
42+
exclude-rules:
43+
# Exclude some linters from running on tests files.
44+
- path: _test\.go
45+
linters:
46+
- gocyclo
47+
- errcheck
48+
- dupl
49+
exclude:
50+
- Using the variable on range scope `tc` in function literal

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ vet: ## Run go vet against code.
3737

3838
all: generate manifests fmt vet ## Runs all the development targets
3939

40+
verify:
41+
hack/verify-all.sh -v
42+
43+
crd-e2e:
44+
hack/crd-e2e.sh -v
45+
4046
##@ Deployment
4147
install: manifests ## Install CRDs into the K8s cluster specified in ~/.kube/config.
4248
kubectl kustomize config/crd | kubectl apply -f -
@@ -50,5 +56,4 @@ docs:
5056

5157
.PHONY: local-docs ## Deploy the docs locally
5258
local-docs:
53-
mkdocs serve
54-
59+
mkdocs serve

hack/crd-e2e.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 2020 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 GOPATH="$(mktemp -d)"
22+
readonly CLUSTER_NAME="verify-network-policy-api"
23+
24+
export KUBECONFIG="${GOPATH}/.kubeconfig"
25+
export GOFLAGS GO111MODULE GOPATH
26+
export PATH="${GOPATH}/bin:${PATH}"
27+
28+
# Cleanup logic for cleanup on exit
29+
CLEANED_UP=false
30+
cleanup() {
31+
if [ "$CLEANED_UP" = "true" ]; then
32+
return
33+
fi
34+
35+
if [ "${KIND_CREATE_ATTEMPTED:-}" = true ]; then
36+
kind delete cluster --name "${CLUSTER_NAME}" || true
37+
fi
38+
CLEANED_UP=true
39+
}
40+
41+
trap cleanup INT TERM
42+
43+
# For exit code
44+
res=0
45+
46+
# Install kind
47+
(go install sigs.k8s.io/[email protected]) || res=$?
48+
49+
# Create cluster
50+
KIND_CREATE_ATTEMPTED=true
51+
kind create cluster --name "${CLUSTER_NAME}" || res=$?
52+
53+
for KUST_FOLDER in bases patches; do
54+
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=$?
55+
kubectl kustomize config/crd | kubectl apply -f - || res=$?
56+
57+
# Temporary workaround for https://github.com/kubernetes/kubernetes/issues/104090
58+
sleep 8
59+
60+
done
61+
62+
# Clean up and exit
63+
cleanup || res=$?
64+
exit $res

hack/kube-env.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# Some useful colors.
18+
if [[ -z "${color_start-}" ]]; then
19+
declare -r color_start="\033["
20+
declare -r color_red="${color_start}0;31m"
21+
declare -r color_yellow="${color_start}0;33m"
22+
declare -r color_green="${color_start}0;32m"
23+
declare -r color_norm="${color_start}0m"
24+
fi
25+
26+
# Returns the server version as MMmmpp, with MM as the major
27+
# component, mm the minor component, and pp as the patch
28+
# revision. e.g. 0.7.1 is echoed as 701, and 1.0.11 would be
29+
# 10011. (This makes for easy integer comparison in bash.)
30+
function kube_server_version() {
31+
local server_version
32+
local major
33+
local minor
34+
local patch
35+
36+
# This sed expression is the POSIX BRE to match strings like:
37+
# Server Version: &version.Info{Major:"0", Minor:"7+", GitVersion:"v0.7.0-dirty", GitCommit:"ad44234f7152e9c66bc2853575445c7071335e57", GitTreeState:"dirty"}
38+
# and capture the GitVersion portion (which has the patch level)
39+
server_version=$(${KUBECTL} --match-server-version=false version | grep "Server Version:")
40+
read major minor patch < <(
41+
echo ${server_version} | \
42+
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*//'
44+
}

hack/verify-all.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
source "${SCRIPT_ROOT}/hack/kube-env.sh"
23+
24+
SILENT=true
25+
26+
function is-excluded {
27+
for e in $EXCLUDE; do
28+
if [[ $1 -ef ${BASH_SOURCE} ]]; then
29+
return
30+
fi
31+
if [[ $1 -ef "$SCRIPT_ROOT/hack/$e" ]]; then
32+
return
33+
fi
34+
done
35+
return 1
36+
}
37+
38+
while getopts ":v" opt; do
39+
case $opt in
40+
v)
41+
SILENT=false
42+
;;
43+
\?)
44+
echo "Invalid flag: -$OPTARG" >&2
45+
exit 1
46+
;;
47+
esac
48+
done
49+
50+
if $SILENT ; then
51+
echo "Running in the silent mode, run with -v if you want to see script logs."
52+
fi
53+
54+
EXCLUDE="verify-all.sh"
55+
56+
ret=0
57+
for t in `ls $SCRIPT_ROOT/hack/verify-*.sh`
58+
do
59+
if is-excluded $t ; then
60+
echo "Skipping $t"
61+
continue
62+
fi
63+
if $SILENT ; then
64+
echo -e "Verifying $t"
65+
if bash "$t" &> /dev/null; then
66+
echo -e "${color_green}SUCCESS${color_norm}"
67+
else
68+
echo -e "${color_red}FAILED${color_norm}"
69+
ret=1
70+
fi
71+
else
72+
if bash "$t"; then
73+
echo -e "${color_green}SUCCESS: $t ${color_norm}"
74+
else
75+
echo -e "${color_red}Test FAILED: $t ${color_norm}"
76+
ret=1
77+
fi
78+
fi
79+
done
80+
exit $ret

hack/verify-gofmt.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 -euo pipefail
18+
19+
echo "Verifying gofmt"
20+
21+
diff=$(find . -name "*.go" | grep -v "\/vendor\/" | grep -v "\/cmd\/cyclonus\/" | xargs gofmt -s -d 2>&1)
22+
if [[ -n "${diff}" ]]; then
23+
echo "${diff}"
24+
echo
25+
echo "Please run make fmt to fix the issue(s)"
26+
exit 1
27+
fi
28+
echo "No issue found"

hack/verify-golint.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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="v1.50.1"
22+
readonly KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
23+
24+
cd "${KUBE_ROOT}"
25+
26+
# See configuration file in ${KUBE_ROOT}/.golangci.yml.
27+
echo "Installing golangci-lint..."
28+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s "$VERSION"
29+
30+
echo "Running golangci-lint..."
31+
./bin/golangci-lint run --deadline=10m

hack/verify-govet.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 -euo pipefail
18+
19+
echo "Verifying govet"
20+
21+
go vet $(go list ./... | grep -v vendor)
22+
23+
echo "Done"

0 commit comments

Comments
 (0)