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

enable more linters (part 4) #354

Merged
merged 15 commits into from
Dec 22, 2023
19 changes: 3 additions & 16 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,19 @@ linters:
- wsl # too strict and mostly code is not more readable
### disabled for now... will be enabled 1 by 1
- dupl
- errorlint
- forcetypeassert
- funlen
- ginkgolinter
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- godox
- gomnd
- gosec
- inamedparam
- ireturn
- maintidx
- noctx
- nolintlint
- paralleltest
- prealloc
- stylecheck
- testpackage
- thelper
- tparallel
- unconvert
- unparam
- varnamelen
- wrapcheck
- godot
- tagalign

linters-settings:
stylecheck:
Expand Down Expand Up @@ -226,6 +210,9 @@ issues:
- path: "_test\\.go"
linters:
- wrapcheck
- gochecknoglobals
- funlen # Table driven unit and integration tests exceed function length by design
- maintidx # Table driven unit and integration tests exceed maintainability index by design
- linters:
- importas
text: has alias "" which is not part of config # Ignore false positives that emerged due to https://github.com/julz/importas/issues/15.
Expand Down
7 changes: 2 additions & 5 deletions api/eventing/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ const (
ConditionControllerReady ConditionType = "Subscription Controller Ready"
)

var allSubscriptionConditions = MakeSubscriptionConditions()

type Condition struct {
// Short description of the condition.
Type ConditionType `json:"type,omitempty"`
Expand Down Expand Up @@ -78,7 +76,7 @@ func (s *SubscriptionStatus) InitializeConditions() {
}

func (s SubscriptionStatus) IsReady() bool {
if !ContainSameConditionTypes(allSubscriptionConditions, s.Conditions) {
if !ContainSameConditionTypes(MakeSubscriptionConditions(), s.Conditions) {
return false
}

Expand Down Expand Up @@ -166,8 +164,7 @@ func MakeCondition(conditionType ConditionType, reason ConditionReason, status k
Status: status,
LastTransitionTime: kmetav1.Now(),
Reason: reason,
// TODO: https://github.com/kyma-project/kyma/issues/9770
Message: message,
Message: message,
}
}

Expand Down
10 changes: 5 additions & 5 deletions api/eventing/v1alpha1/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

const (
eventSource = "source"
orderCreatedEventType = "prefix." + "noapp." + "order.created.v1"
orderUpdatedEventType = "prefix." + "app." + "order.updated.v1"
orderDeletedEventType = "prefix." + "noapp." + "order.deleted.v1"
orderDeletedEventTypeNonClean = "prefix." + "noapp." + "order.deleted_&.v1"
orderProcessedEventType = "prefix." + "noapp." + "order.processed.v1"
orderCreatedEventType = "prefix.noapp.order.created.v1"
orderUpdatedEventType = "prefix.app.order.updated.v1"
orderDeletedEventType = "prefix.noapp.order.deleted.v1"
orderDeletedEventTypeNonClean = "prefix.noapp.order.deleted_&.v1"
orderProcessedEventType = "prefix.noapp.order.processed.v1"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions api/eventing/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package v1alpha1 contains API Schema definitions for the eventing v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=eventing.kyma-project.io
//
//nolint:gochecknoglobals // required for utilizing the API
package v1alpha1

import (
Expand Down
148 changes: 74 additions & 74 deletions api/eventing/v1alpha1/subscription_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func InitializeEventTypeCleaner(cleaner eventtype.Cleaner) {
}

// ConvertTo converts this Subscription in version v1 to the Hub version v2.
func (src *Subscription) ConvertTo(dstRaw conversion.Hub) error {
func (s *Subscription) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*v1alpha2.Subscription)
if !ok {
return errors.Errorf(ErrorHubVersionMsg)
}
return V1ToV2(src, dst)
return V1ToV2(s, dst)
}

// V1ToV2 copies the v1alpha1-type field values into v1alpha2-type field values.
Expand Down Expand Up @@ -61,12 +61,12 @@ func V1ToV2(src *Subscription, dst *v1alpha2.Subscription) error {
}

// ConvertFrom converts this Subscription from the Hub version (v2) to v1.
func (dst *Subscription) ConvertFrom(srcRaw conversion.Hub) error {
func (s *Subscription) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*v1alpha2.Subscription)
if !ok {
return errors.Errorf(ErrorHubVersionMsg)
}
return V2ToV1(dst, src)
return V2ToV1(s, src)
}

// V2ToV1 copies the v1alpha2-type field values into v1alpha1-type field values.
Expand Down Expand Up @@ -120,117 +120,117 @@ func V2ToV1(dst *Subscription, src *v1alpha2.Subscription) error {
}

// setV2TypeMatching sets the default typeMatching on the v1alpha2 Subscription version.
func (src *Subscription) setV2TypeMatching(dst *v1alpha2.Subscription) {
func (s *Subscription) setV2TypeMatching(dst *v1alpha2.Subscription) {
dst.Spec.TypeMatching = v1alpha2.TypeMatchingExact
}

// setV2ProtocolFields converts the protocol-related fields from v1alpha1 to v1alpha2 Subscription version.
func (src *Subscription) setV2ProtocolFields(dst *v1alpha2.Subscription) {
func (s *Subscription) setV2ProtocolFields(dst *v1alpha2.Subscription) {
dst.Spec.Config = map[string]string{}
if src.Spec.Protocol != "" {
dst.Spec.Config[v1alpha2.Protocol] = src.Spec.Protocol
if s.Spec.Protocol != "" {
dst.Spec.Config[v1alpha2.Protocol] = s.Spec.Protocol
}
// protocol settings
if src.Spec.ProtocolSettings != nil {
src.setProtocolSettings(dst)
if s.Spec.ProtocolSettings != nil {
s.setProtocolSettings(dst)
}
}

func (src *Subscription) setProtocolSettings(dst *v1alpha2.Subscription) {
if src.Spec.ProtocolSettings.ContentMode != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsContentMode] = *src.Spec.ProtocolSettings.ContentMode
func (s *Subscription) setProtocolSettings(dst *v1alpha2.Subscription) {
if s.Spec.ProtocolSettings.ContentMode != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsContentMode] = *s.Spec.ProtocolSettings.ContentMode
}
if src.Spec.ProtocolSettings.ExemptHandshake != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsExemptHandshake] = strconv.FormatBool(*src.Spec.ProtocolSettings.ExemptHandshake)
if s.Spec.ProtocolSettings.ExemptHandshake != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsExemptHandshake] = strconv.FormatBool(*s.Spec.ProtocolSettings.ExemptHandshake)
}
if src.Spec.ProtocolSettings.Qos != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsQos] = *src.Spec.ProtocolSettings.Qos
if s.Spec.ProtocolSettings.Qos != nil {
dst.Spec.Config[v1alpha2.ProtocolSettingsQos] = *s.Spec.ProtocolSettings.Qos
}
// webhookAuth fields
if src.Spec.ProtocolSettings.WebhookAuth != nil {
if src.Spec.ProtocolSettings.WebhookAuth.Type != "" {
dst.Spec.Config[v1alpha2.WebhookAuthType] = src.Spec.ProtocolSettings.WebhookAuth.Type
if s.Spec.ProtocolSettings.WebhookAuth != nil {
if s.Spec.ProtocolSettings.WebhookAuth.Type != "" {
dst.Spec.Config[v1alpha2.WebhookAuthType] = s.Spec.ProtocolSettings.WebhookAuth.Type
}
dst.Spec.Config[v1alpha2.WebhookAuthGrantType] = src.Spec.ProtocolSettings.WebhookAuth.GrantType
dst.Spec.Config[v1alpha2.WebhookAuthClientID] = src.Spec.ProtocolSettings.WebhookAuth.ClientID
dst.Spec.Config[v1alpha2.WebhookAuthClientSecret] = src.Spec.ProtocolSettings.WebhookAuth.ClientSecret
dst.Spec.Config[v1alpha2.WebhookAuthTokenURL] = src.Spec.ProtocolSettings.WebhookAuth.TokenURL
if src.Spec.ProtocolSettings.WebhookAuth.Scope != nil {
dst.Spec.Config[v1alpha2.WebhookAuthScope] = strings.Join(src.Spec.ProtocolSettings.WebhookAuth.Scope, ",")
dst.Spec.Config[v1alpha2.WebhookAuthGrantType] = s.Spec.ProtocolSettings.WebhookAuth.GrantType
dst.Spec.Config[v1alpha2.WebhookAuthClientID] = s.Spec.ProtocolSettings.WebhookAuth.ClientID
dst.Spec.Config[v1alpha2.WebhookAuthClientSecret] = s.Spec.ProtocolSettings.WebhookAuth.ClientSecret
dst.Spec.Config[v1alpha2.WebhookAuthTokenURL] = s.Spec.ProtocolSettings.WebhookAuth.TokenURL
if s.Spec.ProtocolSettings.WebhookAuth.Scope != nil {
dst.Spec.Config[v1alpha2.WebhookAuthScope] = strings.Join(s.Spec.ProtocolSettings.WebhookAuth.Scope, ",")
}
}
}

func (src *Subscription) initializeProtocolSettingsIfNil() {
if src.Spec.ProtocolSettings == nil {
src.Spec.ProtocolSettings = &ProtocolSettings{}
func (s *Subscription) initializeProtocolSettingsIfNil() {
if s.Spec.ProtocolSettings == nil {
s.Spec.ProtocolSettings = &ProtocolSettings{}
}
}

func (src *Subscription) initializeWebhookAuthIfNil() {
src.initializeProtocolSettingsIfNil()
if src.Spec.ProtocolSettings.WebhookAuth == nil {
src.Spec.ProtocolSettings.WebhookAuth = &WebhookAuth{}
func (s *Subscription) initializeWebhookAuthIfNil() {
s.initializeProtocolSettingsIfNil()
if s.Spec.ProtocolSettings.WebhookAuth == nil {
s.Spec.ProtocolSettings.WebhookAuth = &WebhookAuth{}
}
}

// setV1ProtocolFields converts the protocol-related fields from v1alpha1 to v1alpha2 Subscription version.
func (src *Subscription) setV1ProtocolFields(dst *v1alpha2.Subscription) {
func (s *Subscription) setV1ProtocolFields(dst *v1alpha2.Subscription) {
if protocol, ok := dst.Spec.Config[v1alpha2.Protocol]; ok {
src.Spec.Protocol = protocol
s.Spec.Protocol = protocol
}

if currentMode, ok := dst.Spec.Config[v1alpha2.ProtocolSettingsContentMode]; ok {
src.initializeProtocolSettingsIfNil()
src.Spec.ProtocolSettings.ContentMode = &currentMode
s.initializeProtocolSettingsIfNil()
s.Spec.ProtocolSettings.ContentMode = &currentMode
}
if qos, ok := dst.Spec.Config[v1alpha2.ProtocolSettingsQos]; ok {
src.initializeProtocolSettingsIfNil()
src.Spec.ProtocolSettings.Qos = &qos
s.initializeProtocolSettingsIfNil()
s.Spec.ProtocolSettings.Qos = &qos
}
if exemptHandshake, ok := dst.Spec.Config[v1alpha2.ProtocolSettingsExemptHandshake]; ok {
handshake, err := strconv.ParseBool(exemptHandshake)
if err != nil {
handshake = true
}
src.initializeProtocolSettingsIfNil()
src.Spec.ProtocolSettings.ExemptHandshake = &handshake
s.initializeProtocolSettingsIfNil()
s.Spec.ProtocolSettings.ExemptHandshake = &handshake
}

if authType, ok := dst.Spec.Config[v1alpha2.WebhookAuthType]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.Type = authType
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.Type = authType
}
if grantType, ok := dst.Spec.Config[v1alpha2.WebhookAuthGrantType]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.GrantType = grantType
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.GrantType = grantType
}
if clientID, ok := dst.Spec.Config[v1alpha2.WebhookAuthClientID]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.ClientID = clientID
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.ClientID = clientID
}
if secret, ok := dst.Spec.Config[v1alpha2.WebhookAuthClientSecret]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.ClientSecret = secret
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.ClientSecret = secret
}
if token, ok := dst.Spec.Config[v1alpha2.WebhookAuthTokenURL]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.TokenURL = token
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.TokenURL = token
}
if scope, ok := dst.Spec.Config[v1alpha2.WebhookAuthScope]; ok {
src.initializeWebhookAuthIfNil()
src.Spec.ProtocolSettings.WebhookAuth.Scope = strings.Split(scope, ",")
s.initializeWebhookAuthIfNil()
s.Spec.ProtocolSettings.WebhookAuth.Scope = strings.Split(scope, ",")
}
}

// setV2SpecTypes sets event types in the Subscription Spec in the v1alpha2 way.
func (src *Subscription) setV2SpecTypes(dst *v1alpha2.Subscription) error {
func (s *Subscription) setV2SpecTypes(dst *v1alpha2.Subscription) error {
if v1alpha1TypeCleaner == nil {
return errors.New("event type cleaner is not initialized")
}

if src.Spec.Filter != nil {
for _, filter := range src.Spec.Filter.Filters {
if s.Spec.Filter != nil {
for _, filter := range s.Spec.Filter.Filters {
if dst.Spec.Source == "" {
dst.Spec.Source = filter.EventSource.Value
}
Expand All @@ -251,38 +251,38 @@ func (src *Subscription) setV2SpecTypes(dst *v1alpha2.Subscription) error {
}

// natsSpecConfigToV2 converts the v1alpha2 Spec config to v1alpha1.
func (src *Subscription) natsSpecConfigToV1(dst *v1alpha2.Subscription) error {
func (s *Subscription) natsSpecConfigToV1(dst *v1alpha2.Subscription) error {
if maxInFlightMessages, ok := dst.Spec.Config[v1alpha2.MaxInFlightMessages]; ok {
intVal, err := strconv.Atoi(maxInFlightMessages)
if err != nil {
return err
}
src.Spec.Config = &SubscriptionConfig{
s.Spec.Config = &SubscriptionConfig{
MaxInFlightMessages: intVal,
}
}
return nil
}

// natsSpecConfigToV2 converts the hardcoded v1alpha1 Spec config to v1alpha2 generic config version.
func (src *Subscription) natsSpecConfigToV2(dst *v1alpha2.Subscription) {
if src.Spec.Config != nil {
func (s *Subscription) natsSpecConfigToV2(dst *v1alpha2.Subscription) {
if s.Spec.Config != nil {
if dst.Spec.Config == nil {
dst.Spec.Config = map[string]string{}
}
dst.Spec.Config[v1alpha2.MaxInFlightMessages] = strconv.Itoa(src.Spec.Config.MaxInFlightMessages)
dst.Spec.Config[v1alpha2.MaxInFlightMessages] = strconv.Itoa(s.Spec.Config.MaxInFlightMessages)
}
}

// setBEBBackendStatus moves the BEB-related to Backend fields of the Status in the v1alpha2.
func (src *Subscription) bebBackendStatusToV1(dst *v1alpha2.Subscription) {
src.Status.Ev2hash = dst.Status.Backend.Ev2hash
src.Status.Emshash = dst.Status.Backend.EventMeshHash
src.Status.ExternalSink = dst.Status.Backend.ExternalSink
src.Status.FailedActivation = dst.Status.Backend.FailedActivation
src.Status.APIRuleName = dst.Status.Backend.APIRuleName
func (s *Subscription) bebBackendStatusToV1(dst *v1alpha2.Subscription) {
s.Status.Ev2hash = dst.Status.Backend.Ev2hash
s.Status.Emshash = dst.Status.Backend.EventMeshHash
s.Status.ExternalSink = dst.Status.Backend.ExternalSink
s.Status.FailedActivation = dst.Status.Backend.FailedActivation
s.Status.APIRuleName = dst.Status.Backend.APIRuleName
if dst.Status.Backend.EventMeshSubscriptionStatus != nil {
src.Status.EmsSubscriptionStatus = &EmsSubscriptionStatus{
s.Status.EmsSubscriptionStatus = &EmsSubscriptionStatus{
SubscriptionStatus: dst.Status.Backend.EventMeshSubscriptionStatus.Status,
SubscriptionStatusReason: dst.Status.Backend.EventMeshSubscriptionStatus.StatusReason,
LastSuccessfulDelivery: dst.Status.Backend.EventMeshSubscriptionStatus.LastSuccessfulDelivery,
Expand All @@ -293,21 +293,21 @@ func (src *Subscription) bebBackendStatusToV1(dst *v1alpha2.Subscription) {
}

// natsBackendStatusToV1 moves the NATS-related to Backend fields of the Status in the v1alpha2.
func (src *Subscription) natsBackendStatusToV1(dst *v1alpha2.Subscription) {
func (s *Subscription) natsBackendStatusToV1(dst *v1alpha2.Subscription) {
if maxInFlightMessages, ok := dst.Spec.Config[v1alpha2.MaxInFlightMessages]; ok {
intVal, err := strconv.Atoi(maxInFlightMessages)
if err == nil {
src.Status.Config = &SubscriptionConfig{}
src.Status.Config.MaxInFlightMessages = intVal
s.Status.Config = &SubscriptionConfig{}
s.Status.Config.MaxInFlightMessages = intVal
}
}
}

// setV1CleanEvenTypes sets the clean event types to v1alpha1 Subscription Status.
func (src *Subscription) setV1CleanEvenTypes(dst *v1alpha2.Subscription) {
src.Status.InitializeCleanEventTypes()
func (s *Subscription) setV1CleanEvenTypes(dst *v1alpha2.Subscription) {
s.Status.InitializeCleanEventTypes()
for _, eventType := range dst.Status.Types {
src.Status.CleanEventTypes = append(src.Status.CleanEventTypes, eventType.CleanType)
s.Status.CleanEventTypes = append(s.Status.CleanEventTypes, eventType.CleanType)
}
}

Expand Down
Loading