Skip to content

Commit

Permalink
Add golang-ci with errcheck enabled to test suite to ensure all error…
Browse files Browse the repository at this point in the history
…s produced are checked
  • Loading branch information
michaelasp committed Aug 12, 2022
1 parent c577834 commit f02492a
Show file tree
Hide file tree
Showing 15 changed files with 1,522 additions and 51 deletions.
28 changes: 28 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
linters:
# Disable all linters.
# Default: false
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
enable:
- errcheck

issues:
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- errcheck

linters-settings:
errcheck:
exclude-functions:
- fmt.Fprintf
- fmt.Fprint
- (net/http.ResponseWriter).Write
- (*net/http.Server).Shutdown
- (*flag.FlagSet).Parse
- (*os.File).Close
- (io.Closer).Close
- (flag.Value).Set
- k8s.io/apimachinery/pkg/util/wait.PollUntil
110 changes: 68 additions & 42 deletions build/test.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
#!/bin/sh
#
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

export CGO_ENABLED=0

TARGETS=$(for d in "$@"; do echo ./$d/...; done)

echo "Running tests:"
go test -i -installsuffix "static" ${TARGETS}
go test -installsuffix "static" ${TARGETS}
echo
# #!/bin/sh
# #
# # Copyright 2016 The Kubernetes Authors.
# #
# # Licensed under the Apache License, Version 2.0 (the "License");
# # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# #
# # http://www.apache.org/licenses/LICENSE-2.0
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.

echo -n "Checking gofmt: "
ERRS=$(find "$@" -type f -name \*.go | xargs gofmt -l 2>&1 || true)
if [ -n "${ERRS}" ]; then
echo "FAIL - the following files need to be gofmt'ed:"
for e in ${ERRS}; do
echo " $e"
done
echo
exit 1
fi
echo "PASS"
echo
# set -o errexit
# set -o nounset
# set -o pipefail

# export CGO_ENABLED=0

echo -n "Checking go vet: "
ERRS=$(go vet ${TARGETS} 2>&1 || true)
# TARGETS=$(for d in "$@"; do echo ./$d/...; done)

# echo "Running tests:"
# go test -i -installsuffix "static" ${TARGETS}
# go test -installsuffix "static" ${TARGETS}
# echo

# echo -n "Checking gofmt: "
# ERRS=$(find "$@" -type f -name \*.go | xargs gofmt -l 2>&1 || true)
# if [ -n "${ERRS}" ]; then
# echo "FAIL - the following files need to be gofmt'ed:"
# for e in ${ERRS}; do
# echo " $e"
# done
# echo
# exit 1
# fi
# echo "PASS"
# echo

# echo -n "Checking go vet: "
# ERRS=$(go vet ${TARGETS} 2>&1 || true)
# if [ -n "${ERRS}" ]; then
# echo "FAIL"
# echo "${ERRS}"
# echo
# exit 1
# fi
# echo "PASS"
# echo

# Set gobin here, install dependencies after this
export GOBIN=$PWD/hack/tools/bin
export PATH=$GOBIN:$PATH

# Explicitly opt into go modules, even though we're inside a GOPATH directory
export GO111MODULE=on

# Install errcheck
echo 'Installing golangci-lint'
cd "hack/tools"
go mod download github.com/mitchellh/go-ps
go install github.com/golangci/golangci-lint/cmd/golangci-lint
cd "../.."

echo -n "Checking linters: "
ERRS=$(golangci-lint run ${TARGETS} 2>&1 || true)
if [ -n "${ERRS}" ]; then
echo "FAIL"
echo "${ERRS}"
Expand All @@ -50,3 +75,4 @@ if [ -n "${ERRS}" ]; then
fi
echo "PASS"
echo

4 changes: 3 additions & 1 deletion cmd/echo/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func process(w http.ResponseWriter, r *http.Request, b []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to compress data: %v", err)
}
zw.Close()
if err := zw.Close(); err != nil {
return nil, fmt.Errorf("failed to close gzip writer: %v", err)
}
w.Header().Set("Content-Encoding", "gzip")
return compressed.Bytes(), nil
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/echo/app/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ func generateInsecureCertAndKey(organization string, validFrom time.Time, validF
klog.Fatalf("Failed to create certificate: %s", err)
}
var certBytes bytes.Buffer
pem.Encode(&certBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err := pem.Encode(&certBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
klog.Fatalf("Failed to encode certificate: %v", err)
}

var keyBytes bytes.Buffer
pb := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}
pem.Encode(&keyBytes, pb)
if err := pem.Encode(&keyBytes, pb); err != nil {
klog.Fatalf("Failed to encode RSA Key: %v", err)
}

return certBytes.Bytes(), keyBytes.Bytes(), nil
}
4 changes: 3 additions & 1 deletion cmd/glbc/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ func getFlagPage(w http.ResponseWriter, r *http.Request) {
Version: version.Version,
Verbosity: flag.Lookup("v").Value.String(),
}
flagPageTemplate.Execute(w, s)
if err := flagPageTemplate.Execute(w, s); err != nil {
klog.Errorf("Unable to apply flag page template: %v", err)
}
}

var flagPageTemplate = template.Must(template.New("").Parse(`GCE Ingress Controller "GLBC"
Expand Down
5 changes: 4 additions & 1 deletion cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ func runControllers(ctx *ingctx.ControllerContext) {
lbc := controller.NewLoadBalancerController(ctx, stopCh)
if ctx.EnableASMConfigMap {
ctx.ASMConfigController.RegisterInformer(ctx.ConfigMapInformer, func() {
lbc.Stop(false) // We want to trigger a restart, don't have to clean up all the resources.
// We want to trigger a restart, don't have to clean up all the resources.
if err := lbc.Stop(false); err != nil {
klog.Errorf("Failed to stop the load balancer controller: %v", err)
}
})
}

Expand Down
11 changes: 11 additions & 0 deletions errcheck_excludes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fmt.Fprintf
fmt.Fprint
(net/http.ResponseWriter).Write
(*net/http.Server).Shutdown
(*flag.FlagSet).Parse
(*os.File).Close
(io.Closer).Close
(flag.Value).Set

// We sometimes call this with a function that has no chance to error
k8s.io/apimachinery/pkg/util/wait.PollUntil
1 change: 1 addition & 0 deletions hack/tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
169 changes: 169 additions & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
module k8s.io/ingress-gce/hack/tools

go 1.19

require github.com/golangci/golangci-lint v1.47.0

require (
4d63.com/gochecknoglobals v0.1.0 // indirect
github.com/Antonboom/errname v0.1.7 // indirect
github.com/Antonboom/nilnil v0.1.1 // indirect
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.2.2 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/OpenPeeDeeP/depguard v1.1.0 // indirect
github.com/alexkohler/prealloc v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
github.com/ashanbrown/forbidigo v1.3.0 // indirect
github.com/ashanbrown/makezero v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bkielbasa/cyclop v1.2.0 // indirect
github.com/blizzy78/varnamelen v0.8.0 // indirect
github.com/bombsimon/wsl/v3 v3.3.0 // indirect
github.com/breml/bidichk v0.2.3 // indirect
github.com/breml/errchkjson v0.3.0 // indirect
github.com/butuzov/ireturn v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/charithe/durationcheck v0.0.9 // indirect
github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4 // indirect
github.com/daixiang0/gci v0.6.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denis-tingaikin/go-header v0.4.3 // indirect
github.com/esimonov/ifshort v1.0.4 // indirect
github.com/ettle/strcase v0.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/firefart/nonamedreturns v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/go-critic/go-critic v0.6.3 // indirect
github.com/go-toolsmith/astcast v1.0.0 // indirect
github.com/go-toolsmith/astcopy v1.0.0 // indirect
github.com/go-toolsmith/astequal v1.0.1 // indirect
github.com/go-toolsmith/astfmt v1.0.0 // indirect
github.com/go-toolsmith/astp v1.0.0 // indirect
github.com/go-toolsmith/strparse v1.0.0 // indirect
github.com/go-toolsmith/typep v1.0.2 // indirect
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a // indirect
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0 // indirect
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca // indirect
github.com/golangci/misspell v0.3.5 // indirect
github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20210914165742-4cc7213b9bc8 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/gostaticanalysis/forcetypeassert v0.1.0 // indirect
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
github.com/julz/importas v0.1.0 // indirect
github.com/kisielk/errcheck v1.6.2 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
github.com/kulti/thelper v0.6.3 // indirect
github.com/kunwardeep/paralleltest v1.0.6 // indirect
github.com/kyoh86/exportloopref v0.1.8 // indirect
github.com/ldez/gomoddirectives v0.2.3 // indirect
github.com/ldez/tagliatelle v0.3.1 // indirect
github.com/leonklingele/grouper v1.1.0 // indirect
github.com/lufeee/execinquery v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/maratori/testpackage v1.1.0 // indirect
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mbilski/exhaustivestruct v1.2.0 // indirect
github.com/mgechev/revive v1.2.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moricho/tparallel v0.2.1 // indirect
github.com/nakabonne/nestif v0.3.1 // indirect
github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354 // indirect
github.com/nishanths/exhaustive v0.8.1 // indirect
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/phayes/checkstyle v0.0.0-20170904204023-bfd46e6a821d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polyfloyd/go-errorlint v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/quasilyte/go-ruleguard v0.3.16-0.20220213074421-6aa060fab41a // indirect
github.com/quasilyte/gogrep v0.0.0-20220120141003-628d8b3623b5 // indirect
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/ryancurrah/gomodguard v1.2.4 // indirect
github.com/ryanrolds/sqlclosecheck v0.3.0 // indirect
github.com/sanposhiho/wastedassign/v2 v2.0.6 // indirect
github.com/securego/gosec/v2 v2.12.0 // indirect
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sivchari/containedctx v1.0.2 // indirect
github.com/sivchari/nosnakecase v1.7.0 // indirect
github.com/sivchari/tenv v1.7.0 // indirect
github.com/sonatard/noctx v0.0.1 // indirect
github.com/sourcegraph/go-diff v0.6.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
github.com/sylvia7788/contextcheck v1.0.4 // indirect
github.com/tdakkota/asciicheck v0.1.1 // indirect
github.com/tetafro/godot v1.4.11 // indirect
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 // indirect
github.com/tomarrell/wrapcheck/v2 v2.6.2 // indirect
github.com/tommy-muehle/go-mnd/v2 v2.5.0 // indirect
github.com/ultraware/funlen v0.0.3 // indirect
github.com/ultraware/whitespace v0.0.5 // indirect
github.com/uudashr/gocognit v1.0.6 // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
gitlab.com/bosi/decorder v0.2.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.3.3 // indirect
mvdan.cc/gofumpt v0.3.1 // indirect
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
mvdan.cc/unparam v0.0.0-20220706161116-678bad134442 // indirect
)
Loading

0 comments on commit f02492a

Please sign in to comment.