Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobebway committed Apr 12, 2024
1 parent 341b6cc commit 7cebd98
Show file tree
Hide file tree
Showing 29 changed files with 91 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
- name: golangci-lint
uses: golangci/[email protected]
with:
version: v1.55
version: v1.57
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ linters:
- testpackage
- wrapcheck
- paralleltest
- copyloopvar
- perfsprint

linters-settings:
stylecheck:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest
## Tool Versions
KUSTOMIZE_VERSION ?= v5.0.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
GOLANG_CI_LINT_VERSION ?= v1.55.2
GOLANG_CI_LINT_VERSION ?= v1.57

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down
8 changes: 4 additions & 4 deletions api/eventing/v1alpha2/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ func makeSubscriptionSpecValidCondition(err error) Condition {
func (s *SubscriptionStatus) setCondition(condition Condition) {
isFound, isSet := false, false
conditions := make([]Condition, 0, len(s.Conditions))
for _, c := range s.Conditions {
if c.Type != condition.Type {
conditions = append(conditions, c)
for _, cond := range s.Conditions {
if cond.Type != condition.Type {
conditions = append(conditions, cond)
continue
}
isFound = true
if !ConditionEquals(c, condition) {
if !ConditionEquals(cond, condition) {
isSet = true
conditions = append(conditions, condition)
}
Expand Down
5 changes: 3 additions & 2 deletions api/eventing/v1alpha2/subscription_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ func (s *Subscription) GetUniqueTypes() []string {
return result
}

// GetDuplicateTypes returns the duplicate types from subscription spec.
// GetDuplicateTypes returns the duplicate types from the Subscription spec.
func (s *Subscription) GetDuplicateTypes() []string {
if len(s.Spec.Types) == 0 {
return s.Spec.Types
}
const duplicatesCount = 2
types := make(map[string]int, len(s.Spec.Types))
duplicates := make([]string, 0, len(s.Spec.Types))
for _, t := range s.Spec.Types {
if types[t]++; types[t] == 2 {
if types[t]++; types[t] == duplicatesCount {
duplicates = append(duplicates, t)
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/operator/v1alpha1/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func assertConditionsEqual(t *testing.T, expected, actual []kmetav1.Condition) {
t.Helper()

assert.Equal(t, len(expected), len(actual))
for i := 0; i < len(expected); i++ {
for i := range len(expected) {
assertConditionEqual(t, expected[i], actual[i])
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/go-logr/zapr"
apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
natsio "github.com/nats-io/nats.go"
kapiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
kapixclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand All @@ -38,7 +39,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
operatorv1alpha1 "github.com/kyma-project/eventing-manager/api/operator/v1alpha1"
natsconnection "github.com/kyma-project/eventing-manager/internal/connection/nats"
Expand Down
3 changes: 2 additions & 1 deletion hack/e2e/common/eventing/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (p *Publisher) SendLegacyEvent(source, eventType, payload string) error {
func (p *Publisher) SendCloudEvent(event *cloudevents.Event, encoding binding.Encoding) error {
newCtx := context.Background()
ctx := cloudevents.ContextWithTarget(newCtx, p.PublishEndpoint())
//nolint:exhaustive // we only support the two checked encodings. Every other encoding will result in an error.

//nolint:exhaustive // we only support binary and structured encoding.
switch encoding {
case binding.EncodingBinary:
{
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/errors/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func IsSkippable(err error) bool {
if err == nil {
return true
}
_, ok := err.(skippable) //nolint:errorlint // here we do not want to check the chain
_, ok := err.(skippable)
return ok
}

Expand Down
31 changes: 18 additions & 13 deletions internal/controller/eventing/subscription/eventmesh/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"time"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
pkgerrors "github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/xerrors"
Expand All @@ -25,7 +26,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
controllererrors "github.com/kyma-project/eventing-manager/internal/controller/errors"
"github.com/kyma-project/eventing-manager/internal/controller/events"
Expand Down Expand Up @@ -144,21 +144,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req kctrl.Request) (kctrl.Re
}

// Validate subscription.
if validationErr := r.validate(ctx, sub); validationErr != nil {
// Update subscription status accordingly.
sub.Status.SetNotReady()
sub.Status.ClearTypes()
sub.Status.ClearBackend()
sub.Status.ClearConditions()
sub.Status.SetSubscriptionSpecValidCondition(validationErr)
if validationErr := r.handleSubscriptionValidation(ctx, sub); validationErr != nil {
if updateErr := r.updateStatus(ctx, currentSubscription, sub, log); updateErr != nil {
return kctrl.Result{}, errors.Join(validationErr, updateErr)
}

return kctrl.Result{}, reconcile.TerminalError(validationErr)
} else {
var noError error = nil
sub.Status.SetSubscriptionSpecValidCondition(noError)
}

// sync APIRule for the desired subscription
Expand Down Expand Up @@ -194,7 +184,22 @@ func (r *Reconciler) Reconcile(ctx context.Context, req kctrl.Request) (kctrl.Re
return result, nil
}

func (r *Reconciler) validate(ctx context.Context, subscription *eventingv1alpha2.Subscription) error {
func (r *Reconciler) handleSubscriptionValidation(ctx context.Context, desiredSubscription *eventingv1alpha2.Subscription) error {
if validationErr := r.validateSubscriptionSpec(ctx, desiredSubscription); validationErr != nil {
desiredSubscription.Status.SetNotReady()
desiredSubscription.Status.ClearTypes()
desiredSubscription.Status.ClearBackend()
desiredSubscription.Status.ClearConditions()
desiredSubscription.Status.SetSubscriptionSpecValidCondition(validationErr)
return reconcile.TerminalError(validationErr)
}

var noError error = nil
desiredSubscription.Status.SetSubscriptionSpecValidCondition(noError)
return noError
}

func (r *Reconciler) validateSubscriptionSpec(ctx context.Context, subscription *eventingv1alpha2.Subscription) error {
if errList := subscription.ValidateSpec(); len(errList) > 0 {
return errors.Join(errList.ToAggregate())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -21,7 +23,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/backend/eventmesh"
Expand All @@ -36,7 +37,6 @@ import (
"github.com/kyma-project/eventing-manager/pkg/object"
"github.com/kyma-project/eventing-manager/test/utils"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
)

// TestReconciler_Reconcile tests the return values of the Reconcile() method of the reconciler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"testing"
"time"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
"github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
emstypes "github.com/kyma-project/eventing-manager/pkg/ems/api/events/types"
"github.com/kyma-project/eventing-manager/pkg/object"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:gosec //this is just a test, and security issues found here will not result in code used in a prod environment
package test

import (
Expand All @@ -13,6 +12,8 @@ import (

"github.com/avast/retry-go/v3"
"github.com/go-logr/zapr"
apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
"github.com/stretchr/testify/require"
kcorev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -29,7 +30,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
subscriptioncontrollereventmesh "github.com/kyma-project/eventing-manager/internal/controller/eventing/subscription/eventmesh"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
Expand All @@ -45,7 +45,6 @@ import (
"github.com/kyma-project/eventing-manager/pkg/utils"
testutils "github.com/kyma-project/eventing-manager/test/utils"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
)

type eventMeshTestEnsemble struct {
Expand Down Expand Up @@ -287,11 +286,6 @@ func ensureK8sResourceCreated(ctx context.Context, t *testing.T, obj client.Obje
require.NoError(t, emTestEnsemble.k8sClient.Create(ctx, obj))
}

func ensureK8sResourceNotCreated(ctx context.Context, t *testing.T, obj client.Object, err error) {
t.Helper()
require.Equal(t, emTestEnsemble.k8sClient.Create(ctx, obj), err)
}

func ensureK8sResourceDeleted(ctx context.Context, t *testing.T, obj client.Object) {
t.Helper()
require.NoError(t, emTestEnsemble.k8sClient.Delete(ctx, obj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ import (
"github.com/avast/retry-go/v3"
"github.com/go-logr/zapr"
apigatewayv1beta1 "github.com/kyma-project/api-gateway/apis/gateway/v1beta1"
eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
subscriptioncontrollereventmesh "github.com/kyma-project/eventing-manager/internal/controller/eventing/subscription/eventmesh"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
backendeventmesh "github.com/kyma-project/eventing-manager/pkg/backend/eventmesh"
"github.com/kyma-project/eventing-manager/pkg/backend/metrics"
"github.com/kyma-project/eventing-manager/pkg/backend/sink"
backendutils "github.com/kyma-project/eventing-manager/pkg/backend/utils"
emstypes "github.com/kyma-project/eventing-manager/pkg/ems/api/events/types"
"github.com/kyma-project/eventing-manager/pkg/env"
"github.com/kyma-project/eventing-manager/pkg/featureflags"
"github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/kyma-project/eventing-manager/pkg/utils"
testutils "github.com/kyma-project/eventing-manager/test/utils"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
"github.com/stretchr/testify/require"
kcorev1 "k8s.io/api/core/v1"
Expand All @@ -41,6 +27,21 @@ import (
kctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
subscriptioncontrollereventmesh "github.com/kyma-project/eventing-manager/internal/controller/eventing/subscription/eventmesh"
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
backendeventmesh "github.com/kyma-project/eventing-manager/pkg/backend/eventmesh"
"github.com/kyma-project/eventing-manager/pkg/backend/metrics"
"github.com/kyma-project/eventing-manager/pkg/backend/sink"
backendutils "github.com/kyma-project/eventing-manager/pkg/backend/utils"
emstypes "github.com/kyma-project/eventing-manager/pkg/ems/api/events/types"
"github.com/kyma-project/eventing-manager/pkg/env"
"github.com/kyma-project/eventing-manager/pkg/featureflags"
"github.com/kyma-project/eventing-manager/pkg/logger"
"github.com/kyma-project/eventing-manager/pkg/utils"
testutils "github.com/kyma-project/eventing-manager/test/utils"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
)

type eventMeshTestEnsemble struct {
Expand Down
39 changes: 19 additions & 20 deletions internal/controller/eventing/subscription/jetstream/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,33 +131,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, req kctrl.Request) (kctrl.Re
}

// Validate subscription.
if validationErr := r.validate(ctx, desiredSubscription); validationErr != nil {
if validationErr := r.handleSubscriptionValidation(ctx, desiredSubscription); validationErr != nil {
if errors.Is(validationErr, sink.ErrSinkValidationFailed) {
if deleteErr := r.Backend.DeleteSubscriptionsOnly(desiredSubscription); deleteErr != nil {
r.namedLogger().Errorw(
"Failed to delete JetStream subscriptions",
"namespace", desiredSubscription.Namespace,
"name", desiredSubscription.Name,
"error", deleteErr,
)
log.Errorw("Failed to delete JetStream subscriptions", "error", deleteErr)
return kctrl.Result{}, deleteErr
}
}

// Update subscription status accordingly.
desiredSubscription.Status.SetNotReady()
desiredSubscription.Status.ClearTypes()
desiredSubscription.Status.ClearBackend()
desiredSubscription.Status.ClearConditions()
desiredSubscription.Status.SetSubscriptionSpecValidCondition(validationErr)
if updateErr := r.updateSubscriptionStatus(ctx, desiredSubscription, log); updateErr != nil {
return kctrl.Result{}, errors.Join(validationErr, updateErr)
}

return kctrl.Result{}, reconcile.TerminalError(validationErr)
} else {
var noError error = nil
desiredSubscription.Status.SetSubscriptionSpecValidCondition(noError)
return kctrl.Result{}, validationErr
}

// update the cleanEventTypes and config values in the subscription status, if changed
Expand Down Expand Up @@ -187,7 +171,22 @@ func (r *Reconciler) Reconcile(ctx context.Context, req kctrl.Request) (kctrl.Re
return kctrl.Result{}, r.syncSubscriptionStatus(ctx, desiredSubscription, nil, log)
}

func (r *Reconciler) validate(ctx context.Context, subscription *eventingv1alpha2.Subscription) error {
func (r *Reconciler) handleSubscriptionValidation(ctx context.Context, desiredSubscription *eventingv1alpha2.Subscription) error {
if validationErr := r.validateSubscriptionSpec(ctx, desiredSubscription); validationErr != nil {
desiredSubscription.Status.SetNotReady()
desiredSubscription.Status.ClearTypes()
desiredSubscription.Status.ClearBackend()
desiredSubscription.Status.ClearConditions()
desiredSubscription.Status.SetSubscriptionSpecValidCondition(validationErr)
return reconcile.TerminalError(validationErr)
}

var noError error = nil
desiredSubscription.Status.SetSubscriptionSpecValidCondition(noError)
return noError
}

func (r *Reconciler) validateSubscriptionSpec(ctx context.Context, subscription *eventingv1alpha2.Subscription) error {
if errList := subscription.ValidateSpec(); len(errList) > 0 {
return errors.Join(errList.ToAggregate())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

kymalogger "github.com/kyma-project/kyma/common/logging/logger"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/kyma-project/eventing-manager/pkg/env"
"github.com/kyma-project/eventing-manager/pkg/logger"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/avast/retry-go/v3"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
natsioserver "github.com/nats-io/nats-server/v2/server"
"github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
Expand Down Expand Up @@ -36,7 +37,6 @@ import (
"github.com/kyma-project/eventing-manager/pkg/env"
"github.com/kyma-project/eventing-manager/pkg/logger"
eventingtesting "github.com/kyma-project/eventing-manager/testing"
kymalogger "github.com/kyma-project/kyma/common/logging/logger"
)

const (
Expand Down Expand Up @@ -395,7 +395,6 @@ func StartTestEnv(ens *Ensemble) error {
}
}),
)

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 7cebd98

Please sign in to comment.