Skip to content
This repository was archived by the owner on Mar 26, 2021. It is now read-only.

Commit 6fce5ea

Browse files
authored
Merge pull request #167 from font/verify
Add repo-infra scripts to verify go source code
2 parents 3d079c9 + 6fc60ba commit 6fce5ea

File tree

6 files changed

+181
-8
lines changed

6 files changed

+181
-8
lines changed

docs/development.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ generated clients and other generated files.
116116
1. Add the generated files to your PR, preferably in a separate, generated-only
117117
commit so that they are easier to review.
118118

119+
## Verify Go source files
120+
121+
To verify and fix your Go source files:
122+
123+
1. Run from the root of this repository `./hack/verify-go-src.sh -v --rootdir $(pwd)`
124+
125+
This runs all the Go source verification scripts in
126+
[`./hack/go-tools/`](/hack/go-tools/).
127+
128+
You can also run any of the scripts individually. For example:
129+
130+
1. Run `./hack/go-tools/verify-govet.sh`
131+
132+
The return code of the script indicates success or failure.
133+
119134
## Interacting with the k8s-bot
120135

121136
The cluster-registry repo is monitored by the k8s-ci-robot. You can find a list

hack/go-tools/verify-gofmt.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
find_files() {
19+
find . -not \( \
20+
\( \
21+
-wholename '*/vendor/*' \
22+
\) -prune \
23+
\) -name '*.go'
24+
}
25+
26+
GOFMT="gofmt -s"
27+
bad_files=$(find_files | xargs $GOFMT -l)
28+
if [[ -n "${bad_files}" ]]; then
29+
echo "!!! '$GOFMT' needs to be run on the following files: "
30+
echo "${bad_files}"
31+
exit 1
32+
fi

hack/go-tools/verify-govet.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
go vet -v $(go list ./... | grep -v /vendor/)

hack/verify-go-src.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
# This script is intended to be used via subtree in a top-level directory:
19+
# <repo>/
20+
# repo-infra/
21+
# verify/
22+
# Or via vendoring and passing root directory as vendor/repo-infra/verify-*.sh --rootdir **full path to your repo dir**
23+
# <repo>/
24+
# vendor/
25+
# repo-infra/
26+
# ...
27+
#
28+
29+
30+
SILENT=true
31+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
32+
33+
# Convert long opts to short ones to read through getopts
34+
for arg in "$@"; do
35+
shift
36+
case "$arg" in
37+
"--rootdir") set -- "$@" "-r";;
38+
*)
39+
set -- "$@" "$arg"
40+
;;
41+
esac
42+
done
43+
44+
OPTIND=1
45+
while getopts "vr:" opt; do
46+
case ${opt} in
47+
v)
48+
SILENT=false
49+
;;
50+
r)
51+
REPO_ROOT=${OPTARG}
52+
;;
53+
\?)
54+
echo "Invalid flag: -${OPTARG}" >&2
55+
exit 1
56+
;;
57+
esac
58+
done
59+
60+
shift "$(($OPTIND-1))"
61+
62+
echo "Working directory: ${REPO_ROOT}"
63+
64+
GO_TOOLS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/go-tools"
65+
66+
function run-cmd {
67+
if ${SILENT}; then
68+
"$@" &> /dev/null
69+
else
70+
"$@"
71+
fi
72+
}
73+
74+
# Some useful colors.
75+
if [[ -z "${color_start-}" ]]; then
76+
declare -r color_start="\033["
77+
declare -r color_red="${color_start}0;31m"
78+
declare -r color_yellow="${color_start}0;33m"
79+
declare -r color_green="${color_start}0;32m"
80+
declare -r color_norm="${color_start}0m"
81+
fi
82+
83+
function run-checks {
84+
local -r pattern=$1
85+
local -r runner=$2
86+
87+
for t in $(ls ${pattern})
88+
do
89+
echo -e "Verifying ${t}"
90+
local start=$(date +%s)
91+
cd $REPO_ROOT && run-cmd "${runner}" "${t}" && tr=$? || tr=$?
92+
local elapsed=$(($(date +%s) - ${start}))
93+
if [[ ${tr} -eq 0 ]]; then
94+
echo -e "${color_green}SUCCESS${color_norm} ${t}\t${elapsed}s"
95+
else
96+
echo -e "${color_red}FAILED${color_norm} ${t}\t${elapsed}s"
97+
ret=1
98+
fi
99+
done
100+
}
101+
102+
if ${SILENT} ; then
103+
echo "Running in silent mode, run with -v if you want to see script logs."
104+
fi
105+
106+
ret=0
107+
run-checks "${GO_TOOLS_DIR}/*.sh" bash
108+
exit ${ret}

pkg/crinit/aggregated/aggregated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func createClusterRoleBindingObject(clientset client.Interface, name, subjectKin
377377
Labels: labels,
378378
},
379379
Subjects: []rbacv1.Subject{
380-
rbacv1.Subject{
380+
{
381381
Kind: subjectKind,
382382
Name: subjectName,
383383
Namespace: subjectNamespace,
@@ -407,7 +407,7 @@ func createExtensionAPIServerAuthenticationRoleBinding(clientset client.Interfac
407407
Labels: util.ComponentLabel,
408408
},
409409
Subjects: []rbacv1.Subject{
410-
rbacv1.Subject{
410+
{
411411
Kind: rbacv1.ServiceAccountKind,
412412
Name: serviceAccountName,
413413
Namespace: namespace,

pkg/crinit/standalone/standalone_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestCreateService(t *testing.T) {
298298
},
299299
Status: v1.NodeStatus{
300300
Addresses: []v1.NodeAddress{
301-
v1.NodeAddress{
301+
{
302302
Type: v1.NodeExternalIP,
303303
Address: "200.0.0.1",
304304
},
@@ -353,7 +353,7 @@ func TestCreateService(t *testing.T) {
353353
createAction := action.(clientgotesting.CreateAction)
354354
svc := createAction.GetObject().(*v1.Service)
355355
if svc.Spec.Type == v1.ServiceTypeLoadBalancer {
356-
svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{v1.LoadBalancerIngress{IP: loadBalancerAddress, Hostname: loadBalancerHostname}}
356+
svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{IP: loadBalancerAddress, Hostname: loadBalancerHostname}}
357357
return false, svc, nil
358358
}
359359
return false, nil, nil
@@ -434,7 +434,7 @@ func TestGetClusterNodeIPs(t *testing.T) {
434434
},
435435
Status: v1.NodeStatus{
436436
Addresses: []v1.NodeAddress{
437-
v1.NodeAddress{
437+
{
438438
Type: v1.NodeExternalIP,
439439
Address: "200.0.0.1",
440440
},
@@ -448,7 +448,7 @@ func TestGetClusterNodeIPs(t *testing.T) {
448448
},
449449
Status: v1.NodeStatus{
450450
Addresses: []v1.NodeAddress{
451-
v1.NodeAddress{
451+
{
452452
Type: v1.NodeInternalIP,
453453
Address: "10.0.0.1",
454454
},
@@ -462,11 +462,11 @@ func TestGetClusterNodeIPs(t *testing.T) {
462462
},
463463
Status: v1.NodeStatus{
464464
Addresses: []v1.NodeAddress{
465-
v1.NodeAddress{
465+
{
466466
Type: v1.NodeExternalIP,
467467
Address: "200.0.0.2",
468468
},
469-
v1.NodeAddress{
469+
{
470470
Type: v1.NodeInternalIP,
471471
Address: "10.0.0.2",
472472
},

0 commit comments

Comments
 (0)