Skip to content
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

Add golangci-lint config #55

Merged
merged 22 commits into from
Jan 26, 2024
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ vendor/

**/.env
**/.env.dev


# Lint files
lint-report.json
411 changes: 139 additions & 272 deletions .golangci.yaml

Large diffs are not rendered by default.

37 changes: 36 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ IMG_REGISTRY_PORT ?= 8888
IMG_REGISTRY ?= op-skr-registry.localhost:$(IMG_REGISTRY_PORT)/unsigned/eventing-images
IMG ?= $(IMG_REGISTRY)/$(APP_NAME):$(APP_VERSION)

# Lint issue category
CATEGORY = "default"

# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
Expand Down Expand Up @@ -40,9 +43,41 @@ help: ## Display this help.
##@ Development

.PHONY: lint
lint: ## Check and fix lint issues using `golangci-lint`
lint: ## Check lint issues using `golangci-lint`
golangci-lint run --timeout 5m --config=./.golangci.yaml

.PHONY: lint-compact
lint-compact: ## Check lint issues using `golangci-lint` in compact result format
golangci-lint run --timeout 5m --config=./.golangci.yaml --print-issued-lines=false

.PHONY: lint-fix
lint-fix: ## Check and fix lint issues using `golangci-lint`
golangci-lint run --fix --timeout 5m --config=./.golangci.yaml

.PHONY: lint-report
lint-report: ## Check lint issues using `golangci-lint` then export them to a file, then print the list of linters used
golangci-lint run --timeout 5m --config=./.golangci.yaml --issues-exit-code 0 --out-format json > ./lint-report.json

.PHONY: lint-report-issue-category
lint-report-issue-category: ## Get lint issues categories
make lint-report-clean
make lint-report
cat ./lint-report.json | jq '.Issues[].FromLinter' | jq -s 'map({(.):1})|add|keys_unsorted'

.PHONY: lint-report-get-category
lint-report-get-category: ## Get lint issues by category
cat ./lint-report.json | jq --arg CATEGORY $$CATEGORY '.Issues[] | select(.FromLinter==$$CATEGORY)'

.PHONY: lint-repost-count-issue
lint-repost-count-issue: ## Count lint issues
make lint-report-clean
make lint-report
cat ./lint-report.json | jq '.Issues | length'

.PHONY: lint-report-clean
lint-report-clean: ## Clean lint report
rm -f ./lint-report.json

.PHONY: fmt
fmt: ## Reformat files using `go fmt`
go fmt $$($(DIRS_TO_CHECK))
Expand Down
4 changes: 2 additions & 2 deletions cmd/eventing-publisher-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
golog "log"

"github.com/kelseyhightower/envconfig"
kymalogger "github.com/kyma-project/eventing-manager/pkg/logger"
emlogger "github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/prometheus/client_golang/prometheus"

"github.com/kyma-project/eventing-publisher-proxy/pkg/commander"
Expand Down Expand Up @@ -44,7 +44,7 @@ func main() {
}

// init the logger
logger, err := kymalogger.New(cfg.AppLogFormat, cfg.AppLogLevel)
logger, err := emlogger.New(cfg.AppLogFormat, cfg.AppLogLevel)
if err != nil {
golog.Fatalf("Failed to initialize logger, error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/application/applicationtest/applicationtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
package applicationtest

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

applicationv1alpha1 "github.com/kyma-project/kyma/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1"
)

func NewApplication(name string, labels map[string]string) *applicationv1alpha1.Application {
return &applicationv1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{
ObjectMeta: kmetav1.ObjectMeta{
Name: name,
Labels: labels,
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/application/fake/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"log"

corev1 "k8s.io/api/core/v1"
kcorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
dynamicfake "k8s.io/client-go/dynamic/fake"
kdynamicfake "k8s.io/client-go/dynamic/fake"

applicationv1alpha1 "github.com/kyma-project/kyma/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1"

Expand All @@ -15,13 +15,13 @@ import (

func NewApplicationListerOrDie(ctx context.Context, app *applicationv1alpha1.Application) *application.Lister {
scheme := setupSchemeOrDie()
dynamicClient := dynamicfake.NewSimpleDynamicClient(scheme, app)
dynamicClient := kdynamicfake.NewSimpleDynamicClient(scheme, app)
return application.NewLister(ctx, dynamicClient)
}

func setupSchemeOrDie() *runtime.Scheme {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
if err := kcorev1.AddToScheme(scheme); err != nil {
log.Fatalf("Failed to setup scheme with error: %v", err)
}
if err := applicationv1alpha1.AddToScheme(scheme); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/application/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"errors"
"time"

kymalogger "github.com/kyma-project/eventing-manager/pkg/logger"
emlogger "github.com/kyma-project/eventing-manager/pkg/logger"
applicationv1alpha1 "github.com/kyma-project/kyma/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1"
v1 "k8s.io/api/core/v1"
kcorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -25,10 +25,10 @@ type Lister struct {
func NewLister(ctx context.Context, client dynamic.Interface) *Lister {
const defaultResync = 10 * time.Second
gvr := GroupVersionResource()
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil)
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, defaultResync, kcorev1.NamespaceAll, nil)
factory.ForResource(gvr)
lister := factory.ForResource(gvr).Lister()
logger, _ := kymalogger.New("json", "error")
logger, _ := emlogger.New("json", "error")
informers.WaitForCacheSyncOrDie(ctx, factory, logger)
return &Lister{lister: lister}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/builder/eventmesh.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builder

import (
cev2event "github.com/cloudevents/sdk-go/v2/event"
ceeventv2 "github.com/cloudevents/sdk-go/v2/event"

"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/logger"
Expand All @@ -27,7 +27,7 @@ func NewEventMeshBuilder(prefix string, eventMeshNamespace string, cleaner clean
}
}

func (emb *EventMeshBuilder) Build(event cev2event.Event) (*cev2event.Event, error) {
func (emb *EventMeshBuilder) Build(event ceeventv2.Event) (*ceeventv2.Event, error) {
ceEvent, err := emb.genericBuilder.Build(event)
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions pkg/cloudevents/builder/eventmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
golog "log"
"testing"

cloudevents "github.com/cloudevents/sdk-go/v2"

cev2 "github.com/cloudevents/sdk-go/v2"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
kymalogger "github.com/kyma-project/eventing-manager/pkg/logger"
emlogger "github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application/applicationtest"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application/fake"
testingutils "github.com/kyma-project/eventing-publisher-proxy/testing"
epptestingutils "github.com/kyma-project/eventing-publisher-proxy/testing"
"github.com/stretchr/testify/require"
)

Expand All @@ -25,7 +24,7 @@ func Test_EventMesh_Build(t *testing.T) {
const eventMeshPrefix = "one.two.three"

// init the logger
logger, err := kymalogger.New("json", "debug")
logger, err := emlogger.New("json", "debug")
if err != nil {
golog.Fatalf("Failed to initialize logger, error: %v", err)
}
Expand Down Expand Up @@ -97,9 +96,9 @@ func Test_EventMesh_Build(t *testing.T) {

// given
// build cloud event
builder := testingutils.NewCloudEventBuilder()
builder := epptestingutils.NewCloudEventBuilder()
payload, _ := builder.BuildStructured()
newEvent := cloudevents.NewEvent()
newEvent := cev2.NewEvent()
err = json.Unmarshal([]byte(payload), &newEvent)
require.NoError(t, err)
newEvent.SetType(tc.givenType)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/builder/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

cev2event "github.com/cloudevents/sdk-go/v2/event"
ceeventv2 "github.com/cloudevents/sdk-go/v2/event"

"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/logger"
Expand Down Expand Up @@ -35,7 +35,7 @@ func (gb *GenericBuilder) isApplicationListerEnabled() bool {
return gb.applicationLister != nil
}

func (gb *GenericBuilder) Build(event cev2event.Event) (*cev2event.Event, error) {
func (gb *GenericBuilder) Build(event ceeventv2.Event) (*ceeventv2.Event, error) {
// format logger
namedLogger := gb.namedLogger(event.Source(), event.Type())

Expand Down
14 changes: 7 additions & 7 deletions pkg/cloudevents/builder/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
golog "log"
"testing"

cloudevents "github.com/cloudevents/sdk-go/v2"
testingutils "github.com/kyma-project/eventing-publisher-proxy/testing"
cev2 "github.com/cloudevents/sdk-go/v2"
epptestingutils "github.com/kyma-project/eventing-publisher-proxy/testing"

"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
kymalogger "github.com/kyma-project/eventing-manager/pkg/logger"
emlogger "github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application/applicationtest"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application/fake"
Expand All @@ -21,7 +21,7 @@ func Test_Build(t *testing.T) {
t.Parallel()

// init the logger
logger, err := kymalogger.New("json", "debug")
logger, err := emlogger.New("json", "debug")
if err != nil {
golog.Fatalf("Failed to initialize logger, error: %v", err)
}
Expand Down Expand Up @@ -100,9 +100,9 @@ func Test_Build(t *testing.T) {

// given
// build cloud event
builder := testingutils.NewCloudEventBuilder()
builder := epptestingutils.NewCloudEventBuilder()
payload, _ := builder.BuildStructured()
newEvent := cloudevents.NewEvent()
newEvent := cev2.NewEvent()
err = json.Unmarshal([]byte(payload), &newEvent)
require.NoError(t, err)
newEvent.SetType(tc.givenType)
Expand Down Expand Up @@ -143,7 +143,7 @@ func Test_GetAppNameOrSource(t *testing.T) {
t.Parallel()

// init the logger
logger, err := kymalogger.New("json", "debug")
logger, err := emlogger.New("json", "debug")
if err != nil {
golog.Fatalf("Failed to initialize logger, error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/builder/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builder

import (
cev2event "github.com/cloudevents/sdk-go/v2/event"
ceeventv2 "github.com/cloudevents/sdk-go/v2/event"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/kyma-project/eventing-publisher-proxy/pkg/application"
Expand All @@ -12,7 +12,7 @@ const (
)

type CloudEventBuilder interface {
Build(event cev2event.Event) (*cev2event.Event, error)
Build(event ceeventv2.Event) (*ceeventv2.Event, error)
}

type GenericBuilder struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"

"github.com/cloudevents/sdk-go/v2/binding"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
cehttpv2 "github.com/cloudevents/sdk-go/v2/protocol/http"
)

// WriteRequestWithHeaders writes a CloudEvent HTTP request with the given message and adds the given headers to it.
Expand All @@ -17,7 +17,7 @@ func WriteRequestWithHeaders(
req *http.Request,
headers http.Header,
transformers ...binding.Transformer) error {
err := cehttp.WriteRequest(ctx, message, req, transformers...)
err := cehttpv2.WriteRequest(ctx, message, req, transformers...)
if err != nil {
return errors.Wrap(err, "failed to write Request")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"reflect"
"testing"

cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
cehttpv2 "github.com/cloudevents/sdk-go/v2/protocol/http"

"github.com/kyma-project/eventing-publisher-proxy/internal"
"github.com/kyma-project/eventing-publisher-proxy/pkg/eventmesh"
Expand All @@ -20,7 +20,7 @@ func TestWriteRequestWithHeaders(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
req.Header.Add(internal.HeaderContentType, internal.ContentTypeApplicationCloudEventsJSON)

message := cehttp.NewMessageFromHttpRequest(req)
message := cehttpv2.NewMessageFromHttpRequest(req)
defer func() { _ = message.Finish(nil) }()

additionalHeaders := http.Header{
Expand Down
Loading