Skip to content

Commit 37e1056

Browse files
authored
Merge pull request kubernetes-retired#169 from nobbs/refactor/tools-staticcheck
Add `staticcheck` to the make build process
2 parents adced6e + 4e80572 commit 37e1056

File tree

194 files changed

+51417
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+51417
-27
lines changed

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ endif
8080

8181
CONTROLLER_GEN ?= ${CURDIR}/bin/controller-gen
8282

83+
STATICCHECK ?= ${CURDIR}/bin/staticcheck
84+
8385
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8486
GOBIN ?= $(shell go env GOPATH)/bin
8587

@@ -106,7 +108,7 @@ test-only:
106108
go test ${DIRS} -coverprofile cover.out
107109

108110
# Builds all binaries (manager and kubectl) and manifests
109-
build: generate fmt vet manifests
111+
build: generate fmt vet staticcheck manifests
110112
go build -o bin/manager ./cmd/manager/main.go
111113
GOOS=linux GOARCH=amd64 go build \
112114
-o bin/kubectl/kubectl-hns_linux_amd64 \
@@ -199,6 +201,13 @@ check-fmt:
199201
vet:
200202
go vet ${DIRS}
201203

204+
# Run staticcheck against code
205+
staticcheck: build-staticcheck
206+
$(STATICCHECK) ${DIRS}
207+
208+
build-staticcheck:
209+
go build -o $(STATICCHECK) honnef.co/go/tools/cmd/staticcheck
210+
202211
# Generate code
203212
generate: controller-gen
204213
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./api/...
@@ -265,21 +274,21 @@ else
265274
endif
266275

267276
# Build the docker image
268-
docker-build: generate fmt vet
277+
docker-build: generate fmt vet staticcheck
269278
@echo "Warning: this does not run tests. Run 'make test' to ensure tests are passing."
270279
docker build . -t ${HNC_IMG}
271280

272281

273282
buildx-setup:
274283
## This script needs to be run to start the emulator for multiarch builds
275-
# This driver translates the instruction set to different platforms
284+
# This driver translates the instruction set to different platforms
276285
docker run --rm --privileged linuxkit/binfmt:v0.8
277286
docker buildx create --use --name=qemu
278287
docker buildx inspect --bootstrap
279288

280289

281290
# Build and push multi-arch image
282-
docker-push-multi: buildx-setup generate fmt vet
291+
docker-push-multi: buildx-setup generate fmt vet staticcheck
283292
@echo "Warning: this does not run tests. Run 'make test' to ensure tests are passing."
284293
docker buildx build \
285294
--push \
@@ -466,4 +475,3 @@ krew-install: krew-build
466475
# Uninstall kubectl plugin locally using krew.
467476
krew-uninstall:
468477
-kubectl krew uninstall hns
469-

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/spf13/cobra v1.2.1
1515
go.opencensus.io v0.23.0
1616
go.uber.org/zap v1.19.1
17+
honnef.co/go/tools v0.2.2
1718
k8s.io/api v0.23.2
1819
k8s.io/apiextensions-apiserver v0.23.2
1920
k8s.io/apimachinery v0.23.2
@@ -31,6 +32,7 @@ require (
3132
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
3233
github.com/Azure/go-autorest/logger v0.2.1 // indirect
3334
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
35+
github.com/BurntSushi/toml v0.3.1 // indirect
3436
github.com/PuerkitoBio/purell v1.1.1 // indirect
3537
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
3638
github.com/aws/aws-sdk-go v1.23.20 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+Z
5858
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
5959
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
6060
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
61+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
6162
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
6263
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
6364
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
@@ -1007,6 +1008,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
10071008
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
10081009
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
10091010
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
1011+
honnef.co/go/tools v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
1012+
honnef.co/go/tools v0.2.2/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
10101013
k8s.io/api v0.23.0/go.mod h1:8wmDdLBHBNxtOIytwLstXt5E9PddnZb0GaMcqsvDBpg=
10111014
k8s.io/api v0.23.2 h1:62cpzreV3dCuj0hqPi8r4dyWh48ogMcyh+ga9jEGij4=
10121015
k8s.io/api v0.23.2/go.mod h1:sYuDb3flCtRPI8ghn6qFrcK5ZBu2mhbElxRE95qpwlI=

hack/tools.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
package hack
88

99
import (
10+
_ "honnef.co/go/tools/cmd/staticcheck"
1011
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
1112
)

internal/anchor/reconciler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (r *Reconciler) shouldDeleteSubns(log logr.Logger, inst *api.SubnamespaceAn
245245
return false
246246

247247
default:
248-
log.Error(errors.New("Bad anchor state"), "state", inst.Status.State)
248+
log.Error(errors.New("bad anchor state"), "state", inst.Status.State)
249249
// Stay on the safe side - don't delete
250250
return false
251251
}
@@ -295,7 +295,7 @@ func (r *Reconciler) shouldFinalizeAnchor(log logr.Logger, inst *api.Subnamespac
295295

296296
default:
297297
// Should never happen, so log an error and let it be deleted.
298-
log.Error(errors.New("Illegal state"), "Unknown state", "state", inst.Status.State)
298+
log.Error(errors.New("illegal state"), "Unknown state", "state", inst.Status.State)
299299
return true
300300
}
301301
}
@@ -362,6 +362,7 @@ func (r *Reconciler) writeInstance(ctx context.Context, log logr.Logger, inst *a
362362

363363
// deleteInstance deletes the anchor instance. Note: Make sure there's no
364364
// finalizers on the instance before calling this function.
365+
//lint:ignore U1000 Ignore for now, as it may be used again in the future
365366
func (r *Reconciler) deleteInstance(ctx context.Context, inst *api.SubnamespaceAnchor) error {
366367
if r.ReadOnly {
367368
return nil

internal/config/namespace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ func setManagedMeta(patterns []string, option string, regexes *[]*regexp.Regexp)
9898
for _, p := range patterns {
9999
r, err := regexp.Compile("^" + p + "$")
100100
if err != nil {
101-
return fmt.Errorf("Illegal value for %s %q: %w", option, p, err)
101+
return fmt.Errorf("illegal value for %s %q: %w", option, p, err)
102102
}
103103
if r.MatchString(api.MetaGroup) {
104-
return fmt.Errorf("Illegal value for %s %q: cannot specify a pattern that matches %q", option, p, api.MetaGroup)
104+
return fmt.Errorf("illegal value for %s %q: cannot specify a pattern that matches %q", option, p, api.MetaGroup)
105105
}
106106
*regexes = append(*regexes, r)
107107
}

internal/forest/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (ns *Namespace) UpdateAllowCascadingDeletion(acd bool) bool {
148148
// AllowsCascadingDeletion returns true if the namespace's or any of the ancestors'
149149
// allowCascadingDeletion field is set to true.
150150
func (ns *Namespace) AllowsCascadingDeletion() bool {
151-
if ns.allowCascadingDeletion == true {
151+
if ns.allowCascadingDeletion {
152152
return true
153153
}
154154
if ns.parent == nil || ns.CycleNames() != nil {

internal/hncconfig/reconciler_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package hncconfig_test
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"testing"
87
"time"
@@ -454,6 +453,7 @@ func setTypeConfig(ctx context.Context, group, resource string, mode api.Synchro
454453

455454
// updateTypeConfig is like setTypeConfig but it doesn't wait to confirm that the change was
456455
// successful.
456+
//lint:ignore U1000 Ignore for now, as it may be used again in the future
457457
func updateTypeConfig(ctx context.Context, group, resource string, mode api.SynchronizationMode) {
458458
updateTypeConfigWithOffset(ctx, 1, group, resource, mode)
459459
}
@@ -483,6 +483,7 @@ func updateTypeConfigWithOffset(ctx context.Context, offset int, group, resource
483483
}).Should(Succeed(), "While updating type config for %s/%s to %s", group, resource, mode)
484484
}
485485

486+
//lint:ignore U1000 Ignore for now, as it may be used again in the future
486487
func unsetTypeConfig(ctx context.Context, group, resource string) {
487488
removeTypeConfigWithOffset(1, ctx, group, resource)
488489
Eventually(typeStatusMode(ctx, group, resource)).Should(Equal(testModeMisssing))
@@ -580,11 +581,11 @@ func getNumPropagatedObjects(ctx context.Context, group, resource string) func()
580581
if t.NumPropagatedObjects != nil {
581582
return *t.NumPropagatedObjects, nil
582583
}
583-
return -1, errors.New(fmt.Sprintf("NumPropagatedObjects field is not set for "+
584-
"group %s, resource %s", group, resource))
584+
return -1, fmt.Errorf("NumPropagatedObjects field is not set for "+
585+
"group %s, resource %s", group, resource)
585586
}
586587
}
587-
return -1, errors.New(fmt.Sprintf("group %s, resource %s is not found in status", group, resource))
588+
return -1, fmt.Errorf("group %s, resource %s is not found in status", group, resource)
588589
}
589590
}
590591

@@ -601,7 +602,7 @@ func hasNumSourceObjects(ctx context.Context, group, resource string) func() (bo
601602
return t.NumSourceObjects != nil, nil
602603
}
603604
}
604-
return false, errors.New(fmt.Sprintf("group %s, resource %s is not found in status", group, resource))
605+
return false, fmt.Errorf("group %s, resource %s is not found in status", group, resource)
605606
}
606607
}
607608

@@ -618,10 +619,10 @@ func getNumSourceObjects(ctx context.Context, group, resource string) func() (in
618619
if t.NumSourceObjects != nil {
619620
return *t.NumSourceObjects, nil
620621
}
621-
return -1, errors.New(fmt.Sprintf("NumSourceObjects field is not set for "+
622-
"group %s, resource %s", group, resource))
622+
return -1, fmt.Errorf("NumSourceObjects field is not set for "+
623+
"group %s, resource %s", group, resource)
623624
}
624625
}
625-
return -1, errors.New(fmt.Sprintf("group %s, resource %s is not found in status", group, resource))
626+
return -1, fmt.Errorf("group %s, resource %s is not found in status", group, resource)
626627
}
627628
}

internal/integtest/helpers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"fmt"
77
"strings"
88

9-
. "github.com/onsi/ginkgo"
10-
. "github.com/onsi/gomega"
9+
. "github.com/onsi/ginkgo" //lint:ignore ST1001 Ignoring this for now
10+
. "github.com/onsi/gomega" //lint:ignore ST1001 Ignoring this for now
1111
corev1 "k8s.io/api/core/v1"
1212
"k8s.io/apimachinery/pkg/api/errors"
1313
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -82,15 +82,15 @@ func TryUpdateHierarchy(ctx context.Context, h *api.HierarchyConfiguration) erro
8282
func GetLabel(ctx context.Context, from, label string) func() string {
8383
return func() string {
8484
ns := GetNamespace(ctx, from)
85-
val, _ := ns.GetLabels()[label]
85+
val := ns.GetLabels()[label]
8686
return val
8787
}
8888
}
8989

9090
func GetAnnotation(ctx context.Context, from, key string) func() string {
9191
return func() string {
9292
ns := GetNamespace(ctx, from)
93-
val, _ := ns.GetAnnotations()[key]
93+
val := ns.GetAnnotations()[key]
9494
return val
9595
}
9696
}
@@ -378,7 +378,7 @@ func ObjectInheritedFrom(ctx context.Context, resource string, nsName, name stri
378378
if inst.GetLabels() == nil {
379379
return ""
380380
}
381-
lif, _ := inst.GetLabels()[api.LabelInheritedFrom]
381+
lif := inst.GetLabels()[api.LabelInheritedFrom]
382382
return lif
383383
}
384384

internal/integtest/setup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"testing"
2222
"time"
2323

24-
. "github.com/onsi/ginkgo"
25-
. "github.com/onsi/gomega"
24+
. "github.com/onsi/ginkgo" //lint:ignore ST1001 Ignoring this for now
25+
. "github.com/onsi/gomega" //lint:ignore ST1001 Ignoring this for now
2626
corev1 "k8s.io/api/core/v1"
2727
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2828
"k8s.io/client-go/kubernetes/scheme"

0 commit comments

Comments
 (0)