Skip to content

Commit

Permalink
enable more linters (#288)
Browse files Browse the repository at this point in the history
* enable containedctx

* modify lint target to download golangci-lint

* enable testifylint

* enable whitespace

* enable nonamedreturns

* enable perfsprint

* enable goerr113

* fix broken build

* add cache

* generate code

* organize code
  • Loading branch information
k15r authored Dec 15, 2023
1 parent a5d84e4 commit e0a307d
Show file tree
Hide file tree
Showing 87 changed files with 975 additions and 485 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Cache Binaries
id: cache-binaries
uses: actions/cache@v3
with:
path: bin
key: ${{ runner.os }}-bin

- name: Install k3d tools
run: |
make -C hack/ci/ install-k3d-tools
- name: Install Kyma CLI & setup k3d cluster using kyma CLI
run: |
make kyma
Expand Down
6 changes: 0 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ linters:
- varcheck # deprecated (since v1.49.0) and replaced by 'unused'
- wsl # too strict and mostly code is not more readable
### disabled for now... will be enabled 1 by 1
- containedctx
- dupl
- errorlint
- forcetypeassert
Expand All @@ -31,20 +30,16 @@ linters:
- goconst
- gocritic
- godox
- goerr113
- gomnd
- gosec
- inamedparam
- ireturn
- maintidx
- noctx
- nolintlint
- nonamedreturns
- paralleltest
- perfsprint
- prealloc
- stylecheck
- testifylint
- testpackage
- thelper
- tparallel
Expand All @@ -54,7 +49,6 @@ linters:
- wrapcheck
- godot
- tagalign
- whitespace

linters-settings:
stylecheck:
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest
## Tool Versions
KUSTOMIZE_VERSION ?= v5.0.0
CONTROLLER_TOOLS_VERSION ?= v0.11.3
GOLANG_CI_LINT_VERSION ?= v1.55.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand All @@ -245,8 +246,15 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

lint:
golangci-lint run --fix --timeout 5m
golangci_lint:
test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version | grep -q $(GOLANG_CI_LINT_VERSION) || \
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION)

lint: golangci_lint
$(LOCALBIN)/golangci-lint run

lint-fix: golangci_lint
$(LOCALBIN)/golangci-lint run --fix

go-gen:
go generate ./...
Expand Down
4 changes: 2 additions & 2 deletions api/eventing/v1alpha1/fixtures_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1alpha1_test

import (
"fmt"
"strconv"

kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -136,7 +136,7 @@ func newV2DefaultSubscription(opts ...eventingtesting.SubscriptionOpt) *v1alpha2
Sink: defaultSink,
ID: defaultID,
Config: map[string]string{
v1alpha2.MaxInFlightMessages: fmt.Sprint(defaultMaxInFlight),
v1alpha2.MaxInFlightMessages: strconv.Itoa(defaultMaxInFlight),
},
},
Status: v1alpha2.SubscriptionStatus{
Expand Down
4 changes: 2 additions & 2 deletions api/eventing/v1alpha1/subscription_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (src *Subscription) setProtocolSettings(dst *v1alpha2.Subscription) {
dst.Spec.Config[v1alpha2.ProtocolSettingsContentMode] = *src.Spec.ProtocolSettings.ContentMode
}
if src.Spec.ProtocolSettings.ExemptHandshake != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsExemptHandshake] = fmt.Sprint(*src.Spec.ProtocolSettings.ExemptHandshake)
dst.Spec.Config[v1alpha2.ProtocolSettingsExemptHandshake] = strconv.FormatBool(*src.Spec.ProtocolSettings.ExemptHandshake)
}
if src.Spec.ProtocolSettings.Qos != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsQos] = *src.Spec.ProtocolSettings.Qos
Expand Down Expand Up @@ -270,7 +270,7 @@ func (src *Subscription) natsSpecConfigToV2(dst *v1alpha2.Subscription) {
if dst.Spec.Config == nil {
dst.Spec.Config = map[string]string{}
}
dst.Spec.Config[v1alpha2.MaxInFlightMessages] = fmt.Sprint(src.Spec.Config.MaxInFlightMessages)
dst.Spec.Config[v1alpha2.MaxInFlightMessages] = strconv.Itoa(src.Spec.Config.MaxInFlightMessages)
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/eventing/v1alpha1/subscription_conversion_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func Test_Conversion(t *testing.T) {
convertedV1Alpha2 := &v1alpha2.Subscription{}
err := v1alpha1.V1ToV2(testCase.alpha1Sub, convertedV1Alpha2)
if err != nil && testCase.wantErrMsgV1toV2 != "" {
require.Equal(t, err.Error(), testCase.wantErrMsgV1toV2)
require.Equal(t, testCase.wantErrMsgV1toV2, err.Error())
} else {
require.NoError(t, err)
v1ToV2Assertions(t, testCase.alpha2Sub, convertedV1Alpha2)
Expand All @@ -224,7 +224,7 @@ func Test_Conversion(t *testing.T) {
convertedV1Alpha1 := &v1alpha1.Subscription{}
err := v1alpha1.V2ToV1(convertedV1Alpha1, testCase.alpha2Sub)
if err != nil && testCase.wantErrMsgV2toV1 != "" {
require.Equal(t, err.Error(), testCase.wantErrMsgV2toV1)
require.Equal(t, testCase.wantErrMsgV2toV1, err.Error())
} else {
require.NoError(t, err)
v2ToV1Assertions(t, testCase.alpha1Sub, convertedV1Alpha1)
Expand Down
4 changes: 2 additions & 2 deletions api/eventing/v1alpha2/subscription_webhook_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Test_Default(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc.givenSub.Default()
require.Equal(t, tc.givenSub, tc.wantSub)
require.Equal(t, tc.wantSub, tc.givenSub)
})
}
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func Test_validateSubscription(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := tc.givenSub.ValidateSubscription()
require.Equal(t, err, tc.wantErr)
require.Equal(t, tc.wantErr, err)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/operator/v1alpha1/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func TestClearConditions(t *testing.T) {
},
},
}
require.NotEqual(t, 0, len(givenEventingStatus.Conditions))
require.NotEmpty(t, givenEventingStatus.Conditions)

// when
givenEventingStatus.ClearConditions()

// then
require.Len(t, givenEventingStatus.Conditions, 0)
require.Empty(t, givenEventingStatus.Conditions)
}

func TestClearPublisherService(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion api/operator/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions hack/e2e/common/eventing/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ const (
CloudEventPublishEndpointFormat = "%s/publish"
)

var (
ErrFailedSendingCE = errors.New("failed to send cloudevent")
ErrEncodingNotSupported = errors.New("unsupported cloudevent encoding")
)

type Publisher struct {
ctx context.Context
clientCE client.Client
clientHTTP *http.Client
publisherURL string
logger *zap.Logger
}

func NewPublisher(ctx context.Context, clientCE client.Client, clientHTTP *http.Client, publisherURL string, logger *zap.Logger) *Publisher {
func NewPublisher(clientCE client.Client, clientHTTP *http.Client, publisherURL string, logger *zap.Logger) *Publisher {
return &Publisher{
ctx: ctx,
clientCE: clientCE,
clientHTTP: clientHTTP,
publisherURL: publisherURL,
Expand Down Expand Up @@ -125,7 +128,7 @@ func (p *Publisher) SendCloudEvent(event *cloudevents.Event, encoding binding.En
}
default:
{
return fmt.Errorf("failed to use unsupported cloudevent encoding:[%s]", encoding.String())
return fmt.Errorf("%w:[%s]", ErrEncodingNotSupported, encoding.String())
}
}

Expand All @@ -142,11 +145,11 @@ func (p *Publisher) SendCloudEvent(event *cloudevents.Event, encoding binding.En
switch {
case cloudevents.IsUndelivered(result):
{
return fmt.Errorf("failed to send cloudevent-%s undelivered:[%s] response:[%s]", encoding.String(), ce.Type(), result)
return fmt.Errorf("%w: encoding:[%s] undelivered:[%s] response:[%s]", ErrFailedSendingCE, encoding.String(), ce.Type(), result)
}
case cloudevents.IsNACK(result):
{
return fmt.Errorf("failed to send cloudevent-%s nack:[%s] response:[%s]", encoding.String(), ce.Type(), result)
return fmt.Errorf("%w: encoding:[%s] nack:[%s] response:[%s]", ErrFailedSendingCE, encoding.String(), ce.Type(), result)
}
case cloudevents.IsACK(result):
{
Expand All @@ -155,7 +158,7 @@ func (p *Publisher) SendCloudEvent(event *cloudevents.Event, encoding binding.En
}
default:
{
return fmt.Errorf("failed to send cloudevent-%s unknown:[%s] response:[%s]", encoding.String(), ce.Type(), result)
return fmt.Errorf("%w: encoding:[%s] unknown:[%s] response:[%s]", ErrFailedSendingCE, encoding.String(), ce.Type(), result)
}
}
}
5 changes: 1 addition & 4 deletions hack/e2e/common/eventing/sinkclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eventing

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -22,7 +21,6 @@ const (
)

type SinkClient struct {
ctx context.Context
clientHTTP *http.Client
sinkURL string
logger *zap.Logger
Expand All @@ -34,9 +32,8 @@ type SinkEvent struct {
ceevent.Event
}

func NewSinkClient(ctx context.Context, clientHTTP *http.Client, sinkURL string, logger *zap.Logger) *SinkClient {
func NewSinkClient(clientHTTP *http.Client, sinkURL string, logger *zap.Logger) *SinkClient {
return &SinkClient{
ctx: ctx,
clientHTTP: clientHTTP,
sinkURL: sinkURL,
logger: logger,
Expand Down
2 changes: 1 addition & 1 deletion hack/e2e/common/eventing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewCloudEvent(eventSource, eventType string, encoding binding.Encoding) (*c
ce.SetType(eventType)
ce.SetSource(eventSource)
if err := ce.SetData(cloudevents.ApplicationJSON, data); err != nil {
return nil, fmt.Errorf("failed to set cloudevent-%s data with error:[%s]", encoding.String(), err)
return nil, fmt.Errorf("failed to set cloudevent-%s data with error:[%w]", encoding.String(), err)
}
return &ce, nil
}
1 change: 1 addition & 0 deletions hack/e2e/common/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func ConvertSelectorLabelsToString(labels map[string]string) string {
return strings.Join(result, ",")
}

//nolint:goerr113 //TODO: this is ERRORHANDLING NOT a LOGGER!!!!
func AppendMsgToError(err error, msg string) error {
return errors.Join(err, fmt.Errorf("\n==> %s", msg))
}
Loading

0 comments on commit e0a307d

Please sign in to comment.