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 15, 2024
1 parent 34320c4 commit cc2a0a9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func setupSuite() error {
emTestEnsemble = &eventMeshTestEnsemble{}

// define logger
var err error
defaultLogger, err := logger.New(string(kymalogger.JSON), string(kymalogger.INFO))
if err != nil {
return err
Expand Down Expand Up @@ -127,8 +126,7 @@ func setupSuite() error {
eventMesh, credentials := setupEventMesh(defaultLogger)

// Init the Subscription validator.
sinkValidator := validator.NewSinkValidator(k8sManager.GetClient())
subscriptionValidator := validator.NewSubscriptionValidator(sinkValidator)
subscriptionValidator := validator.NewSubscriptionValidator(validator.NewSinkValidator(k8sManager.GetClient()))

col := metrics.NewCollector()
testReconciler := subscriptioncontrollereventmesh.NewReconciler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func setupSuite() (*eventMeshTestEnsemble, error) {
emTestEnsemble := &eventMeshTestEnsemble{}

// define logger
var err error
defaultLogger, err := logger.New(string(kymalogger.JSON), string(kymalogger.INFO))
if err != nil {
return nil, err
Expand Down Expand Up @@ -129,8 +128,7 @@ func setupSuite() (*eventMeshTestEnsemble, error) {
col := metrics.NewCollector()

// Init the Subscription validator.
sinkValidator := validator.NewSinkValidator(k8sManager.GetClient())
subscriptionValidator := validator.NewSubscriptionValidator(sinkValidator)
subscriptionValidator := validator.NewSubscriptionValidator(validator.NewSinkValidator(k8sManager.GetClient()))

testReconciler = subscriptioncontrollereventmesh.NewReconciler(
k8sManager.GetClient(),
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/eventing/subscription/validator/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (sv sinkValidator) Validate(ctx context.Context, sink string) error {
return fmt.Errorf("%w: %w", ErrSinkValidationFailed, err)
}

if len(subDomains) < 2 {
const minSubDomains = 2
if len(subDomains) < minSubDomains {
return fmt.Errorf("%w: sink format should contain the service name.namespace", ErrSinkValidationFailed)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
package validator

// TODO(marcobebway): Add tests for the subscription validator.
18 changes: 10 additions & 8 deletions internal/webhook/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ func CleanupResources(ctx context.Context, client kctrlclient.Client) []error {
mutatingWebhookConfiguration = "subscription-mutating-webhook-configuration"
validatingWebhookConfiguration = "subscription-validating-webhook-configuration"
)
var errList = make([]error, 0, 5)
appendIfError(errList, deleteService(ctx, client, namespace, service))
appendIfError(errList, deleteCronJob(ctx, client, namespace, cronjob))
appendIfError(errList, deleteJob(ctx, client, namespace, job))
appendIfError(errList, deleteMutatingWebhookConfiguration(ctx, client, namespace, mutatingWebhookConfiguration))
appendIfError(errList, deleteValidatingWebhookConfiguration(ctx, client, namespace, validatingWebhookConfiguration))
const capacity = 5
errList := make([]error, 0, capacity)
errList = appendIfError(errList, deleteService(ctx, client, namespace, service))
errList = appendIfError(errList, deleteCronJob(ctx, client, namespace, cronjob))
errList = appendIfError(errList, deleteJob(ctx, client, namespace, job))
errList = appendIfError(errList, deleteMutatingWebhookConfiguration(ctx, client, namespace, mutatingWebhookConfiguration))
errList = appendIfError(errList, deleteValidatingWebhookConfiguration(ctx, client, namespace, validatingWebhookConfiguration))
return errList
}

Expand Down Expand Up @@ -104,8 +105,9 @@ func deleteValidatingWebhookConfiguration(ctx context.Context, client kctrlclien
)
}

func appendIfError(errList []error, err error) {
func appendIfError(errList []error, err error) []error {
if err != nil {
errList = append(errList, err)
return append(errList, err)
}
return errList
}
25 changes: 25 additions & 0 deletions internal/webhook/cleanup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package webhook

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
)

func Test_appendIfError(t *testing.T) {
// given
var errList []error

// when
errList = appendIfError(errList, nil)
errList = appendIfError(errList, nil)
// then
require.Empty(t, errList)

// when
errList = appendIfError(errList, errors.New("error1")) //nolint:goerr113
errList = appendIfError(errList, errors.New("error2")) //nolint:goerr113
// then
require.Len(t, errList, 2)
}

0 comments on commit cc2a0a9

Please sign in to comment.