Skip to content

🌱ci: Update golangci-lint to v2.0.2 and adjust configuration #1182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
with:
go-version: ${{ steps.vars.outputs.go_version }}
- name: golangci-lint
uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # tag=v6.5.2
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # tag=v7.0.0
with:
version: v1.64.6
args: --out-format=colored-line-number
version: v2.0.2
args: --output.text.print-linter-name=true --output.text.colors=true
working-directory: ${{matrix.working-directory}}
161 changes: 86 additions & 75 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
version: "2"
run:
allow-parallel-runners: true
modules-download-mode: readonly
# Increase the default deadline from 1m as some module operations can take a
# while if uncached!
timeout: 10m
go: "1.24"

modules-download-mode: readonly
allow-parallel-runners: true
linters:
# sync from https://github.com/kubernetes-sigs/controller-runtime/blob/main/.golangci.yml
disable-all: true
default: none
enable:
- asasalint
- asciicheck
Expand All @@ -25,10 +22,7 @@ linters:
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosimple
- govet
- importas
- ineffassign
Expand All @@ -40,74 +34,91 @@ linters:
- prealloc
- revive
- staticcheck
- stylecheck
- tagliatelle
- typecheck
- unconvert
- unparam
- unused
- whitespace

issues:
exclude-rules:
# Dot imports for gomega and ginkgo are allowed
# within test files.
- path: _test\.go
text: should not use dot imports
# Ignore error type switch case
- path: "pkg/loader/loader.go"
linters:
- errorlint
# Ignore test files
- linters:
- dupl
- ginkgolinter
path: _test\.go
- linters:
- gocritic
path: "pkg/markers/help.go"
- linters:
- exhaustive
path: "pkg/markers/parse.go|pkg/deepcopy/traverse.go|pkg/genall/help/types.go|pkg/crd/schema.go|pkg/crd/flatten.go"
# Ignore consider pre-allocating variables
- linters:
- prealloc
text: Consider pre-allocating
linters-settings:
govet:
enable-all: true
disable:
- fieldalignment
- shadow
importas:
no-unaliased: true
revive:
# By default, revive will enable only the linting rules that are named in the configuration file.
# So, it's needed to explicitly enable all required rules here.
settings:
govet:
disable:
- fieldalignment
- shadow
enable-all: true
importas:
no-unaliased: true
revive:
# By default, revive will enable only the linting rules that are named in the configuration file.
# So, it's needed to explicitly enable all required rules here.
rules:
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: superfluous-else
- name: unreachable-code
- name: redefines-builtin-id
#
# Rules in addition to the recommended configuration above.
#
- name: bool-literal-in-expr
- name: constant-logical-expr
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: superfluous-else
- name: unreachable-code
- name: redefines-builtin-id
#
# Rules in addition to the recommended configuration above.
#
- name: bool-literal-in-expr
- name: constant-logical-expr
# Dot imports for gomega and ginkgo are allowed
# within test files.
- path: _test\.go
text: should not use dot imports
# Ignore error type switch case
- linters:
- errorlint
path: pkg/loader/loader.go
# Ignore test files
- linters:
- dupl
- ginkgolinter
path: _test\.go
- linters:
- gocritic
path: pkg/markers/help.go
- linters:
- exhaustive
path: pkg/markers/parse.go|pkg/deepcopy/traverse.go|pkg/genall/help/types.go|pkg/crd/schema.go|pkg/crd/flatten.go
# Ignore consider pre-allocating variables
- linters:
- prealloc
text: Consider pre-allocating
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ GO_INSTALL := ./hack/go-install.sh
GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT_VER := $(shell cat .github/workflows/golangci-lint.yml | grep [[:space:]]version: | sed 's/.*version: //')
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER))
GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
GOLANGCI_LINT_PKG := github.com/golangci/golangci-lint/v2/cmd/golangci-lint

$(GOLANGCI_LINT): # Build golangci-lint from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOLANGCI_LINT_PKG) $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)
Expand Down
2 changes: 1 addition & 1 deletion pkg/applyconfiguration/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
outpkg := outputPkg(ctx.Collector, root)

arguments.OutputDir = filepath.Join(root.Dir, outpkg)
arguments.OutputPkg = filepath.Join(root.Package.PkgPath, outpkg)
arguments.OutputPkg = filepath.Join(root.PkgPath, outpkg)

// The following code is based on gengo/v2.Execute.
// We have lifted it from there so that we can adjust the markers on the types to make sure
Expand Down
6 changes: 3 additions & 3 deletions pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ func FixTopLevelMetadata(crd apiext.CustomResourceDefinition) {
// addAttribution adds attribution info to indicate controller-gen tool was used
// to generate this CRD definition along with the version info.
func addAttribution(crd *apiext.CustomResourceDefinition) {
if crd.ObjectMeta.Annotations == nil {
crd.ObjectMeta.Annotations = map[string]string{}
if crd.Annotations == nil {
crd.Annotations = map[string]string{}
}
crd.ObjectMeta.Annotations["controller-gen.kubebuilder.io/version"] = version.Version()
crd.Annotations["controller-gen.kubebuilder.io/version"] = version.Version()
}

// FindMetav1 locates the actual package representing metav1 amongst
Expand Down
6 changes: 3 additions & 3 deletions pkg/markers/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *Collector) parseMarkersInPackage(nodeMarkersRaw map[ast.Node][]markerCo
markerVals := make(map[string][]interface{})
for _, markerRaw := range markersRaw {
markerText := markerRaw.Text()
def := c.Registry.Lookup(markerText, target)
def := c.Lookup(markerText, target)
if def == nil {
continue
}
Expand Down Expand Up @@ -173,14 +173,14 @@ func (c *Collector) associateFileMarkers(file *ast.File) map[ast.Node][]markerCo
continue
}
markerText := marker.Text()
typeDef := c.Registry.Lookup(markerText, DescribesType)
typeDef := c.Lookup(markerText, DescribesType)
if typeDef != nil {
// prefer assuming type-level markers
markers[endOfMarkers] = marker
endOfMarkers++
continue
}
def := c.Registry.Lookup(markerText, DescribesPackage)
def := c.Lookup(markerText, DescribesPackage)
if def == nil {
// assume type-level unless proven otherwise
markers[endOfMarkers] = marker
Expand Down
12 changes: 5 additions & 7 deletions pkg/markers/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ func (a *Argument) parseString(scanner *sc.Scanner, raw string, out reflect.Valu
// the "hard" case -- bare tokens not including ',' (the argument
// separator), ';' (the slice separator), ':' (the map separator), or '}'
// (delimitted slice ender)
startPos := scanner.Position.Offset
startPos := scanner.Offset
for hint := peekNoSpace(scanner); hint != ',' && hint != ';' && hint != ':' && hint != '}' && hint != sc.EOF; hint = peekNoSpace(scanner) {
// skip this token
scanner.Scan()
}
endPos := scanner.Position.Offset + len(scanner.TokenText())
endPos := scanner.Offset + len(scanner.TokenText())
castAndSet(out, reflect.ValueOf(raw[startPos:endPos]))
}

Expand Down Expand Up @@ -868,11 +868,9 @@ func (d *Definition) Parse(rawMarker string) (interface{}, error) {
seen[""] = struct{}{} // mark as seen for strict definitions
} else if !d.Empty() && scanner.Peek() != sc.EOF {
// if we expect *and* actually have arguments passed
for {
for expect(scanner, sc.Ident, "argument name") {
// parse the argument name
if !expect(scanner, sc.Ident, "argument name") {
break
}

argName := scanner.TokenText()
if !expect(scanner, '=', "equals") {
break
Expand Down Expand Up @@ -913,7 +911,7 @@ func (d *Definition) Parse(rawMarker string) (interface{}, error) {
}

if tok := scanner.Scan(); tok != sc.EOF {
scanner.Error(scanner, fmt.Sprintf("extra arguments provided: %q", fields[scanner.Position.Offset:]))
scanner.Error(scanner, fmt.Sprintf("extra arguments provided: %q", fields[scanner.Offset:]))
}

if d.Strict {
Expand Down
2 changes: 1 addition & 1 deletion pkg/schemapatcher/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) (result error) {
// with them against older servers.
for _, crd := range set.CRDVersions {
if err := func() error {
outWriter, err := ctx.OutputRule.Open(nil, crd.FileName)
outWriter, err := ctx.Open(nil, crd.FileName)
if err != nil {
return err
}
Expand Down
Loading