Skip to content

Commit

Permalink
add golangci linters
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiang <[email protected]>
  • Loading branch information
dongjiang1989 committed Jan 23, 2025
1 parent f827bcb commit 72c910b
Show file tree
Hide file tree
Showing 34 changed files with 87 additions and 64 deletions.
49 changes: 49 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ linters:
- staticcheck
- unused
- testifylint
- revive
- errorlint
- whitespace
- gci
- misspell
- iface
- exptostd
- nilnesserr
Expand All @@ -23,3 +28,47 @@ issues:
# within test files.
- path: _test\.go
text: should not use dot imports
# Ignore error type switch case
- path: "pkg/loader/loader.go"
linters:
- errorlint

linters-settings:
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:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
- name: blank-imports
- name: comment-spacings
- name: context-as-argument
arguments:
# Allow functions with test or bench signatures.
- allowTypesBefore: "*testing.T,testing.TB"
- name: context-keys-type
- name: dot-imports
# A lot of false positives: incorrectly identifies channel draining as "empty code block".
# See https://github.com/mgechev/revive/issues/386
- name: empty-block
disabled: true
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: increment-decrement
- name: indent-error-flow
- name: range
- name: receiver-naming
- name: redefines-builtin-id
- name: superfluous-else
- name: time-naming
- name: unexported-return
- name: unreachable-code
- name: unused-parameter
disabled: true
- name: var-declaration
- name: var-naming
gci:
sections:
- standard
- default
7 changes: 4 additions & 3 deletions cmd/controller-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/deepcopy"
"sigs.k8s.io/controller-tools/pkg/genall"
Expand Down Expand Up @@ -198,10 +198,11 @@ func main() {
})

if err := cmd.Execute(); err != nil {
if _, noUsage := err.(noUsageError); !noUsage {
var errNoUsage noUsageError
if !errors.As(err, &errNoUsage) {
// print the usage unless we suppressed it
if err := cmd.Usage(); err != nil {
panic(err)
panic(errNoUsage)
}
}
fmt.Fprintf(cmd.OutOrStderr(), "run `%[1]s %[2]s -w` to see all available markers, or `%[1]s %[2]s -h` for usage\n", cmd.CalledAs(), strings.Join(os.Args[1:], " "))
Expand Down
2 changes: 1 addition & 1 deletion pkg/crd/desc_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func truncateString(desc string, maxLen int) string {

// Trying to chop off at closest word boundary (i.e. whitespace).
if n := strings.LastIndexFunc(desc, isWhiteSpace); n > 0 {
return desc[0 : n] + "..."
return desc[0:n] + "..."
}

return desc[0:maxLen] + "..."
Expand Down
2 changes: 0 additions & 2 deletions pkg/crd/desc_visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ package crd_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"sigs.k8s.io/controller-tools/pkg/crd"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"sync"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"sigs.k8s.io/controller-tools/pkg/loader"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/flatten_all_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"sigs.k8s.io/controller-tools/pkg/crd"
)

Expand Down
3 changes: 1 addition & 2 deletions pkg/crd/flatten_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"golang.org/x/tools/go/packages"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/loader"
)
Expand Down
1 change: 0 additions & 1 deletion pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
Expand Down
3 changes: 1 addition & 2 deletions pkg/crd/gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-tools/pkg/crd"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/genall"
Expand Down Expand Up @@ -162,7 +161,7 @@ var _ = Describe("CRD Generation proper defaulting", func() {
var fifty int = 50
gen := &crd.Generator{
CRDVersions: []string{"v1"},
MaxDescLen: &fifty,
MaxDescLen: &fifty,
}
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/known_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package crd
import (
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-tools/pkg/loader"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/markers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/crd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
)
Expand Down
3 changes: 1 addition & 2 deletions pkg/crd/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import (
"golang.org/x/tools/go/packages"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"

"sigs.k8s.io/controller-tools/pkg/crd"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/yaml"
)

func packageErrors(pkg *loader.Package, filterKinds ...packages.ErrorKind) error {
Expand Down
5 changes: 2 additions & 3 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"

"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
)
Expand Down Expand Up @@ -448,12 +447,12 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON

switch {
case field.Markers.Get("kubebuilder:validation:Optional") != nil:
// explicity optional - kubebuilder
// explicitly optional - kubebuilder
case field.Markers.Get("kubebuilder:validation:Required") != nil:
// explicitly required - kubebuilder
props.Required = append(props.Required, fieldName)
case field.Markers.Get("optional") != nil:
// explicity optional - kubernetes
// explicitly optional - kubernetes
case field.Markers.Get("required") != nil:
// explicitly required - kubernetes
props.Required = append(props.Required, fieldName)
Expand Down
2 changes: 0 additions & 2 deletions pkg/crd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"strings"

"github.com/gobuffalo/flect"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"sigs.k8s.io/controller-tools/pkg/loader"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/deepcopy/deepcopy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/deepcopy"
"sigs.k8s.io/controller-tools/pkg/genall"
Expand Down
3 changes: 1 addition & 2 deletions pkg/genall/genall.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"golang.org/x/tools/go/packages"
rawyaml "gopkg.in/yaml.v2"

"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
)
Expand Down Expand Up @@ -175,7 +174,7 @@ func (g GenerationContext) WriteYAML(itemPath, headerText string, objs []interfa
func yamlMarshal(o interface{}, options ...*WriteYAMLOptions) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
return nil, fmt.Errorf("error marshaling into JSON: %w", err)
}

return yamlJSONToYAMLWithFilter(j, options...)
Expand Down
3 changes: 1 addition & 2 deletions pkg/genall/help/pretty/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"fmt"
"io"

"sigs.k8s.io/controller-tools/pkg/genall/help"

"github.com/fatih/color"
"sigs.k8s.io/controller-tools/pkg/genall/help"
)

var (
Expand Down
8 changes: 4 additions & 4 deletions pkg/genall/help/pretty/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func (c *TableCalculator) ColumnWidths() []int {
maxColWidths := make([]int, len(c.cellSizesByCol))

for colInd, cellSizes := range c.cellSizesByCol {
max := 0
maxValue := 0
for _, cellSize := range cellSizes {
if max < cellSize {
max = cellSize
if maxValue < cellSize {
maxValue = cellSize
}
}
maxColWidths[colInd] = max
maxColWidths[colInd] = maxValue
}

actualMaxWidth := c.MaxWidth - c.Padding
Expand Down
4 changes: 3 additions & 1 deletion pkg/loader/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package loader

import (
"errors"
"fmt"
"go/token"
)
Expand All @@ -37,7 +38,8 @@ type Node interface {
// attaching it to the given AST node. It will automatically map
// over error lists.
func ErrFromNode(err error, node Node) error {
if asList, isList := err.(ErrList); isList {
var asList ErrList
if isList := errors.As(err, &asList); isList {
resList := make(ErrList, len(asList))
for i, baseErr := range asList {
resList[i] = ErrFromNode(baseErr, node)
Expand Down
1 change: 0 additions & 1 deletion pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-tools/pkg/loader"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/loader/testutils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/onsi/ginkgo"
pkgstest "golang.org/x/tools/go/packages/packagestest"

"sigs.k8s.io/controller-tools/pkg/loader"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/markers/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package markers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/markers/markers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

pkgstest "golang.org/x/tools/go/packages/packagestest"
"sigs.k8s.io/controller-tools/pkg/loader"
testloader "sigs.k8s.io/controller-tools/pkg/loader/testutils"
Expand Down
1 change: 0 additions & 1 deletion pkg/markers/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/rbac/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/markers"
)
Expand Down
2 changes: 0 additions & 2 deletions pkg/rbac/parser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

rbacv1 "k8s.io/api/rbac/v1"

"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
Expand Down
3 changes: 1 addition & 2 deletions pkg/schemapatcher/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
kyaml "sigs.k8s.io/yaml"

crdgen "sigs.k8s.io/controller-tools/pkg/crd"
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
yamlop "sigs.k8s.io/controller-tools/pkg/schemapatcher/internal/yaml"
kyaml "sigs.k8s.io/yaml"
)

// NB(directxman12): this code is quite fragile, but there are a sufficient
Expand Down
1 change: 0 additions & 1 deletion pkg/schemapatcher/gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-tools/pkg/genall"
. "sigs.k8s.io/controller-tools/pkg/schemapatcher"
)
Expand Down
Loading

0 comments on commit 72c910b

Please sign in to comment.