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
14 changes: 11 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,26 @@ linters-settings:
## kubernetes APIs get the prefix `k`. As an example check `kcorev1`

alias:
- pkg: "log"
alias: golog
the1bit marked this conversation as resolved.
Show resolved Hide resolved
- pkg: k8s.io/api/core/v1
alias: kcorev1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: kmetav1
- pkg: k8s.io/client-go/dynamic/fake
alias: kdynamicfake
- pkg: github.com/kyma-project/eventing-publisher-proxy/testing
alias: epptestingutils
- pkg : "github.com/kyma-project/eventing-manager/pkg/logger"
alias: emlogger
- pkg : "github.com/cloudevents/sdk-go/v2"
alias: cev2
- pkg : "github.com/cloudevents/sdk-go/v2/protocol/http"
alias: cehttp
alias: cehttpv2
the1bit marked this conversation as resolved.
Show resolved Hide resolved
- pkg : "github.com/cloudevents/sdk-go/v2/event"
alias: ceevent
alias: ceeventv2
- pkg : "github.com/cloudevents/sdk-go/v2/client"
alias: ceclient
alias: ceclientv2
- pkg : "github.com/kyma-project/kyma/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1"
alias: applicationv1alpha1

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
4 changes: 2 additions & 2 deletions pkg/application/fake/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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,7 +15,7 @@ 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)
}

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 (
ceevent "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 ceevent.Event) (*ceevent.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
9 changes: 4 additions & 5 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"
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 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"

ceevent "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 ceevent.Event) (*ceevent.Event, error) {
func (gb *GenericBuilder) Build(event ceeventv2.Event) (*ceeventv2.Event, error) {
// format logger
namedLogger := gb.namedLogger(event.Source(), event.Type())

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

cloudevents "github.com/cloudevents/sdk-go/v2"
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"
Expand Down Expand Up @@ -102,7 +102,7 @@ func Test_Build(t *testing.T) {
// build cloud event
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/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builder

import (
ceevent "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 ceevent.Event) (*ceevent.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
22 changes: 11 additions & 11 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"go.uber.org/zap"

"github.com/cloudevents/sdk-go/v2/binding"
ceclient "github.com/cloudevents/sdk-go/v2/client"
ceevent "github.com/cloudevents/sdk-go/v2/event"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
ceclientv2 "github.com/cloudevents/sdk-go/v2/client"
ceeventv2 "github.com/cloudevents/sdk-go/v2/event"
cehttpv2 "github.com/cloudevents/sdk-go/v2/protocol/http"

"github.com/kyma-project/eventing-publisher-proxy/pkg/cloudevents/builder"
"github.com/kyma-project/eventing-publisher-proxy/pkg/cloudevents/eventtype"
Expand All @@ -45,7 +45,7 @@ type Handler struct {
Sender sender.GenericSender
HealthChecker health.Checker
// Defaulter sets default values to incoming events
Defaulter ceclient.EventDefaulter
Defaulter ceclientv2.EventDefaulter
// LegacyTransformer handles transformations needed to handle legacy events
LegacyTransformer legacy.RequestToCETransformer
// RequestTimeout timeout for outgoing requests
Expand Down Expand Up @@ -126,7 +126,7 @@ func (h *Handler) maxBytes(f http.HandlerFunc) http.HandlerFunc {
// It writes to the user request if any error occurs.
// Otherwise, returns the result.
func (h *Handler) handleSendEventAndRecordMetricsLegacy(
writer http.ResponseWriter, request *http.Request, event *ceevent.Event) error {
writer http.ResponseWriter, request *http.Request, event *ceeventv2.Event) error {
err := h.sendEventAndRecordMetrics(request.Context(), event, h.Sender.URL(), request.Header)
if err != nil {
h.namedLogger().Error(err)
Expand All @@ -145,7 +145,7 @@ func (h *Handler) handleSendEventAndRecordMetricsLegacy(
// It writes to the user request if any error occurs.
// Otherwise, return the published event.
func (h *Handler) handlePublishLegacyEvent(w http.ResponseWriter, r *http.Request,
data *api.PublishRequestData) (*ceevent.Event, error) {
data *api.PublishRequestData) (*ceeventv2.Event, error) {
ceEvent, err := h.LegacyTransformer.TransformPublishRequestToCloudEvent(data)
if err != nil {
legacy.WriteJSONResponse(w, legacy.ErrorResponse(http.StatusInternalServerError, err))
Expand All @@ -172,7 +172,7 @@ func (h *Handler) handlePublishLegacyEvent(w http.ResponseWriter, r *http.Reques
// It writes to the user request if any error occurs.
// Otherwise, return the published event.
func (h *Handler) handlePublishLegacyEventV1alpha1(w http.ResponseWriter, r *http.Request,
data *api.PublishRequestData) (*ceevent.Event, error) {
data *api.PublishRequestData) (*ceeventv2.Event, error) {
event, _ := h.LegacyTransformer.WriteLegacyRequestsToCE(w, data)
if event == nil {
h.namedLogger().Error("Failed to transform legacy event to CloudEvent, event is nil")
Expand Down Expand Up @@ -284,8 +284,8 @@ func (h *Handler) publishCloudEvents(w http.ResponseWriter, r *http.Request) {
}

// extractCloudEventFromRequest converts an incoming CloudEvent request to an Event.
func extractCloudEventFromRequest(r *http.Request) (*ceevent.Event, error) {
message := cehttp.NewMessageFromHttpRequest(r)
func extractCloudEventFromRequest(r *http.Request) (*ceeventv2.Event, error) {
message := cehttpv2.NewMessageFromHttpRequest(r)
defer func() { _ = message.Finish(nil) }()

event, err := binding.ToEvent(context.Background(), message)
Expand All @@ -301,7 +301,7 @@ func extractCloudEventFromRequest(r *http.Request) (*ceevent.Event, error) {
}

// sendEventAndRecordMetrics dispatches an Event and records metrics based on dispatch success.
func (h *Handler) sendEventAndRecordMetrics(ctx context.Context, event *ceevent.Event,
func (h *Handler) sendEventAndRecordMetrics(ctx context.Context, event *ceeventv2.Event,
host string, header http.Header) error {
ctx, cancel := context.WithTimeout(ctx, h.RequestTimeout)
defer cancel()
Expand Down Expand Up @@ -349,7 +349,7 @@ func writeResponse(writer http.ResponseWriter, statusCode int, respBody []byte)
}

// applyDefaults applies the default values (if any) to the given Cloud Event.
func (h *Handler) applyDefaults(ctx context.Context, event *ceevent.Event) {
func (h *Handler) applyDefaults(ctx context.Context, event *ceeventv2.Event) {
if h.Defaulter != nil {
newEvent := h.Defaulter(ctx, *event)
*event = newEvent
Expand Down
4 changes: 2 additions & 2 deletions pkg/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/kyma-project/eventing-publisher-proxy/pkg/metrics/metricstest"
"github.com/kyma-project/eventing-publisher-proxy/pkg/options"
"github.com/kyma-project/eventing-publisher-proxy/pkg/sender"
testingutils "github.com/kyma-project/eventing-publisher-proxy/testing"
epptestingutils "github.com/kyma-project/eventing-publisher-proxy/testing"
)

func TestHandler_publishCloudEvents(t *testing.T) {
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestHandler_publishCloudEvents(t *testing.T) {
eventTypeCleaner: tt.fields.eventTypeCleaner,
ceBuilder: ceBuilder,
Options: &options.Options{},
OldEventTypePrefix: testingutils.OldEventTypePrefix,
OldEventTypePrefix: epptestingutils.OldEventTypePrefix,
}
writer := httptest.NewRecorder()

Expand Down
Loading