Skip to content

Commit

Permalink
Upgrade lint (#267)
Browse files Browse the repository at this point in the history
* Update linter

Update to same version used by other OF repos.

Update config to match other OF repos.

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>

* Run goimports

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>

* Fix lint

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>

---------

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>
  • Loading branch information
ncdc committed Jan 11, 2024
1 parent f78cdcc commit 25716f8
Show file tree
Hide file tree
Showing 46 changed files with 561 additions and 321 deletions.
6 changes: 3 additions & 3 deletions .bingo/Variables.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ $(BINGO): $(BINGO_DIR)/bingo.mod
@echo "(re)installing $(GOBIN)/bingo-v0.9.0"
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=bingo.mod -o=$(GOBIN)/bingo-v0.9.0 "github.com/bwplotka/bingo"

GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.51.2
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.55.2
$(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/golangci-lint-v1.51.2"
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.51.2 "github.com/golangci/golangci-lint/cmd/golangci-lint"
@echo "(re)installing $(GOBIN)/golangci-lint-v1.55.2"
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.55.2 "github.com/golangci/golangci-lint/cmd/golangci-lint"

GORELEASER := $(GOBIN)/goreleaser-v1.20.0
$(GORELEASER): $(BINGO_DIR)/goreleaser.mod
Expand Down
2 changes: 1 addition & 1 deletion .bingo/golangci-lint.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT

go 1.20

require github.com/golangci/golangci-lint v1.51.2 // cmd/golangci-lint
require github.com/golangci/golangci-lint v1.55.2 // cmd/golangci-lint
156 changes: 156 additions & 0 deletions .bingo/golangci-lint.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .bingo/variables.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi

BINGO="${GOBIN}/bingo-v0.9.0"

GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.51.2"
GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.55.2"

GORELEASER="${GOBIN}/goreleaser-v1.20.0"

Expand Down
89 changes: 75 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,78 @@
run:
timeout: 5m
# Default timeout is 1m, up to give more room
timeout: 4m

linters:
disable-all: true
enable:
- govet
- errcheck
- staticcheck
- unused
- gosimple
- structcheck
- varcheck
- ineffassign
- deadcode
- typecheck
- gofmt
- goconst
- asciicheck
- bodyclose
- errorlint
- ginkgolinter
- gofmt
- goimports
- gosec
- importas
- misspell
- nestif
- nonamedreturns
- prealloc
- revive
- stylecheck
- tparallel
- unconvert
- unparam
- unused
- whitespace

linters-settings:
errorlint:
errorf: false

importas:
alias:
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: apierrors
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: apiextensionsv1
- pkg: k8s.io/apimachinery/pkg/util/runtime
alias: utilruntime
- pkg: "^k8s\\.io/api/([^/]+)/(v[^/]+)$"
alias: $1$2
- pkg: sigs.k8s.io/controller-runtime
alias: ctrl

goimports:
local-prefixes: github.com/operator-framework/helm-operator-plugins

revive:
ignore-generated-header: false
severity: warning
confidence: 0.8
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: error-return
- name: error-strings
- name: error-naming
- name: exported
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
- name: unused-parameter
- name: unreachable-code
- name: redefines-builtin-id

output:
format: tab
1 change: 0 additions & 1 deletion hack/generate/samples/generate_testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ func main() {

log.Infof("creating Hybrid Memcached Sample")
hybrid.GenerateMemcachedSamples(binaryPath, samplesPath)

}
4 changes: 3 additions & 1 deletion hack/generate/samples/internal/pkg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type SampleContext struct {
}

// NewSampleContext returns a SampleContext containing a new kubebuilder TestContext.
func NewSampleContext(binary string, path string, env ...string) (s SampleContext, err error) {
func NewSampleContext(binary string, path string, env ...string) (SampleContext, error) {
var s SampleContext
var err error
s.TestContext, err = testutils.NewPartialTestContext(binary, path, env...)
return s, err
}
1 change: 0 additions & 1 deletion internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func run(cmd *cobra.Command, f *flags.Flags) {
log.Error(err, "Manager exited non-zero.")
os.Exit(1)
}

}

// exitIfUnsupported prints an error containing unsupported field names and exits
Expand Down
1 change: 0 additions & 1 deletion internal/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
" holding the leader lock (required if running locally with leader"+
" election enabled).",
)

}

// ToManagerOptions uses the flag set in f to configure options.
Expand Down
4 changes: 2 additions & 2 deletions internal/sdk/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var (
func WaitForDeletion(ctx context.Context, cl client.Reader, o client.Object) error {
key := client.ObjectKeyFromObject(o)

return wait.PollUntilContextCancel(ctx, time.Millisecond*10, true, func(ctx context.Context) (done bool, err error) {
err = cl.Get(ctx, key, o)
return wait.PollUntilContextCancel(ctx, time.Millisecond*10, true, func(ctx context.Context) (bool, error) {
err := cl.Get(ctx, key, o)
if apierrors.IsNotFound(err) {
return true, nil
}
Expand Down
20 changes: 10 additions & 10 deletions internal/sdk/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -38,13 +38,13 @@ var _ = Describe("Controllerutil", func() {
var (
ctx context.Context
cancel context.CancelFunc
pod *v1.Pod
pod *corev1.Pod
client client.Client
)

BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
pod = &v1.Pod{
pod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "testName",
Namespace: "testNamespace",
Expand Down Expand Up @@ -95,13 +95,13 @@ var _ = Describe("Controllerutil", func() {
dependent = createObject(clusterScoped, types.NamespacedName{Namespace: "", Name: "dependent"})
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeTrue())
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
It("should be true for namespace-scoped dependents", func() {
dependent = createObject(namespaceScoped, types.NamespacedName{Namespace: "ns1", Name: "dependent"})
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeTrue())
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
})
When("owner is namespace scoped", func() {
Expand All @@ -112,22 +112,22 @@ var _ = Describe("Controllerutil", func() {
dependent = createObject(clusterScoped, types.NamespacedName{Namespace: "", Name: "dependent"})
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeFalse())
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
When("dependent is in owner namespace", func() {
It("should be true", func() {
dependent = createObject(namespaceScoped, types.NamespacedName{Namespace: "ns1", Name: "dependent"})
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeTrue())
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
})
When("dependent is not in owner namespace", func() {
It("should be false", func() {
dependent = createObject(namespaceScoped, types.NamespacedName{Namespace: "ns2", Name: "dependent"})
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeFalse())
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
})
})
Expand All @@ -144,13 +144,13 @@ var _ = Describe("Controllerutil", func() {
It("fails when owner REST mapping is missing", func() {
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeFalse())
Expect(err).NotTo(BeNil())
Expect(err).To(HaveOccurred())
})
It("fails when dependent REST mapping is missing", func() {
rm.Add(clusterScoped, meta.RESTScopeRoot)
supportsOwnerRef, err := SupportsOwnerReference(rm, owner, dependent)
Expect(supportsOwnerRef).To(BeFalse())
Expect(err).NotTo(BeNil())
Expect(err).To(HaveOccurred())
})
})
})
Expand Down
13 changes: 11 additions & 2 deletions internal/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ type TestContext struct {

// NewTestContext returns a TestContext containing a new kubebuilder TestContext.
// Construct if your environment is connected to a live cluster, ex. for e2e tests.
func NewTestContext(binaryName string, env ...string) (tc TestContext, err error) {
func NewTestContext(binaryName string, env ...string) (TestContext, error) {
var (
tc TestContext
err error
)
if tc.TestContext, err = kbtestutils.NewTestContext(binaryName, env...); err != nil {
return tc, err
}
Expand All @@ -59,7 +63,12 @@ func NewTestContext(binaryName string, env ...string) (tc TestContext, err error
// NewPartialTestContext returns a TestContext containing a partial kubebuilder TestContext.
// This object needs to be populated with GVK information. The underlying TestContext is
// created directly rather than through a constructor so cluster-based setup is skipped.
func NewPartialTestContext(binaryName, dir string, env ...string) (tc TestContext, err error) {
func NewPartialTestContext(binaryName, dir string, env ...string) (TestContext, error) {
var (
tc TestContext
err error
)

cc := &kbtestutils.CmdContext{
Env: env,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func getMostRecentTag(m debug.Module) string {
// This is necessary to handle projects that
// import this project at untagged commits.
if len(split) > 1 && sv.Patch > 0 {
sv.Patch -= 1
sv.Patch--
}
return fmt.Sprintf("v%s", sv.FinalizeVersion())
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

golangv4 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4"

"github.com/operator-framework/helm-operator-plugins/internal/cmd/hybrid-operator/run"
"github.com/operator-framework/helm-operator-plugins/internal/version"
pluginv1alpha "github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha"
golangv4 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
helmkube "helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -350,7 +350,7 @@ func createPatch(existing runtime.Object, expected *resource.Info) ([]byte, apit

// On newer K8s versions, CRDs aren't unstructured but has this dedicated type
_, isCRDv1beta1 := versionedObject.(*apiextv1beta1.CustomResourceDefinition)
_, isCRDv1 := versionedObject.(*apiextv1.CustomResourceDefinition)
_, isCRDv1 := versionedObject.(*apiextensionsv1.CustomResourceDefinition)

if isUnstructured || isCRDv1beta1 || isCRDv1 {
// fall back to generic JSON merge patch
Expand Down
Loading

0 comments on commit 25716f8

Please sign in to comment.