Skip to content

Commit

Permalink
Bump linter to v1.55 (#258)
Browse files Browse the repository at this point in the history
* format github workflow codequality.yml

Replace doublequotes with singlequotes in codequality.yml.

* bump golangcli-lint version

Bump the golangcli-lint version from v1.52 to v1.55 for increased stability. https://github.com/golangci/golangci-lint/releases/tag/v1.55.2

* fix comments

Fix some formating issues and typos in the comments.

* regenerate mocks

* move message to const

* whitelist dotimports and set lll to warning

Allow dotimports for ginkgo and gomega.

* fix lll
  • Loading branch information
friedrichwilken authored Dec 19, 2023
1 parent b295ce5 commit ee97c1a
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 50 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/codequality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ on:
branches:
- main
paths-ignore:
- 'docs/**'
- '**.md'
- 'sec-scanners-config.yaml'
- "docs/**"
- "**.md"
- "sec-scanners-config.yaml"
push:
branches:
- main
paths-ignore:
- 'docs/**'
- '**.md'
- 'sec-scanners-config.yaml'
- "docs/**"
- "**.md"
- "sec-scanners-config.yaml"

permissions:
contents: read
Expand All @@ -27,12 +27,12 @@ jobs:
steps:
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: "1.21"
cache: false
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52
version: v1.55
args: --timeout=5m
30 changes: 22 additions & 8 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ run:
# Default: 1m
timeout: 3m


# This file contains only configs which differ from defaults.
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
linters-settings:
stylecheck:
dot-import-whitelist:
- github.com/onsi/ginkgo/v2
- github.com/onsi/gomega
revive:
enable-all-rules: false
severity: error
rules:
- name: comment-spacings
disabled: true
- name: dot-imports
severity: warning
disabled: true
- name: line-length-limit
severity: warning
disabled: true
arguments: [120]
cyclop:
# The maximal code complexity to report.
# Default: 10
Expand Down Expand Up @@ -121,7 +137,7 @@ linters-settings:
nolintlint:
# Exclude following linters from requiring an explanation.
# Default: []
allow-no-explanation: [ funlen, gocognit, lll ]
allow-no-explanation: [funlen, gocognit, lll]
# Enable to require an explanation of nonzero length after each nolint directive.
# Default: false
require-explanation: true
Expand All @@ -141,7 +157,6 @@ linters-settings:
# Default: false
all: true


linters:
disable-all: true
enable:
Expand Down Expand Up @@ -252,7 +267,6 @@ linters:
#- structcheck # [deprecated, replaced by unused] finds unused struct fields
#- varcheck # [deprecated, replaced by unused] finds unused global variables and constants


issues:
# Maximum count of issues with the same text.
# Set to 0 to disable.
Expand All @@ -261,13 +275,13 @@ issues:

exclude-rules:
- source: "^//\\s*go:generate\\s"
linters: [ lll ]
linters: [lll]
- source: "(noinspection|TODO)"
linters: [ godot ]
linters: [godot]
- source: "//noinspection"
linters: [ gocritic ]
linters: [gocritic]
- source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
linters: [ errorlint ]
linters: [errorlint]
- path: "_test\\.go"
linters:
- bodyclose
Expand Down
47 changes: 25 additions & 22 deletions internal/controller/nats/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ import (
)

const (
NATSFinalizerName = "nats.operator.kyma-project.io/finalizer"
ControllerName = "nats-manager"
ManagedByLabelKey = "app.kubernetes.io/managed-by"
ManagedByLabelValue = ControllerName
NATSFinalizerName = "nats.operator.kyma-project.io/finalizer"
ControllerName = "nats-manager"
ManagedByLabelKey = "app.kubernetes.io/managed-by"
ManagedByLabelValue = ControllerName
CreationNotAllowedMsg = "Only a single NATS CR with name: %s and namespace: " +
"%s is allowed to be created in a Kyma cluster."
)

// Reconciler reconciles a Nats object.
// Reconciler reconciles a NATS object.
//
//go:generate go run github.com/vektra/mockery/v2 --name=Controller --dir=../../../vendor/sigs.k8s.io/controller-runtime/pkg/controller --outpkg=mocks --case=underscore
//go:generate go run github.com/vektra/mockery/v2 --name=Manager --dir=../../../vendor/sigs.k8s.io/controller-runtime/pkg/manager --outpkg=mocks --case=underscore
Expand Down Expand Up @@ -125,44 +127,44 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// copy the object, so we don't modify the source object
// Copy the object, so we don't modify the source object.
nats := currentNats.DeepCopy()

// logger with nats details
// Create a logger with NATS details.
log := r.loggerWithNATS(nats)

// check if nats is in deletion state
// Check if nats is in deletion state.
if nats.IsInDeletion() {
return r.handleNATSDeletion(ctx, nats, log)
}

// check if the NATS CR is allowed to be created.
// Check if the NATS CR is allowed to be created.
if r.allowedNATSCR != nil {
if result, err := r.handleNATSCRAllowedCheck(ctx, nats, log); !result || err != nil {
return ctrl.Result{}, err
}
}

// handle reconciliation
// Handle reconciliation.
return r.handleNATSReconcile(ctx, nats, log)
}

// handleNATSCRAllowedCheck checks if NATS CR is allowed to be created or not.
// returns true if the NATS CR is allowed.
func (r *Reconciler) handleNATSCRAllowedCheck(ctx context.Context, nats *natsv1alpha1.NATS,
log *zap.SugaredLogger) (bool, error) {
// if the name and namespace matches with allowed NATS CR then allow the CR to be reconciled.
log *zap.SugaredLogger,
) (bool, error) {
// If the name and namespace matches with allowed NATS CR then allow the CR to be reconciled.
if nats.Name == r.allowedNATSCR.Name && nats.Namespace == r.allowedNATSCR.Namespace {
return true, nil
}

// set error state in status.
// Set error state in status.
nats.Status.SetStateError()
// update conditions in status.
// Update conditions in status.
nats.Status.UpdateConditionStatefulSet(metav1.ConditionFalse,
natsv1alpha1.ConditionReasonForbidden, "")
errorMessage := fmt.Sprintf("Only a single NATS CR with name: %s and namespace: %s "+
"is allowed to be created in a Kyma cluster.", r.allowedNATSCR.Name, r.allowedNATSCR.Namespace)
errorMessage := fmt.Sprintf(CreationNotAllowedMsg, r.allowedNATSCR.Name, r.allowedNATSCR.Namespace)
nats.Status.UpdateConditionAvailable(metav1.ConditionFalse,
natsv1alpha1.ConditionReasonForbidden, errorMessage)
events.Warn(r.recorder, nats, natsv1alpha1.ConditionReasonForbidden, errorMessage)
Expand All @@ -173,7 +175,7 @@ func (r *Reconciler) handleNATSCRAllowedCheck(ctx context.Context, nats *natsv1a
// generateNatsResources renders the NATS chart with provided overrides.
// It puts results into ReleaseInstance.
func (r *Reconciler) generateNatsResources(nats *natsv1alpha1.NATS, instance *chart.ReleaseInstance) error {
// generate Nats resources from chart
// Generate Nats resources from chart.
natsResources, err := r.natsManager.GenerateNATSResources(
instance,
manager.WithOwnerReference(*nats), // add owner references to all resources
Expand All @@ -183,15 +185,16 @@ func (r *Reconciler) generateNatsResources(nats *natsv1alpha1.NATS, instance *ch
return err
}

// update manifests in instance
// Update manifests in instance.
instance.SetRenderedManifests(*natsResources)
return nil
}

// initNATSInstance initializes a new NATS release instance based on NATS CR.
func (r *Reconciler) initNATSInstance(ctx context.Context, nats *natsv1alpha1.NATS,
log *zap.SugaredLogger) (*chart.ReleaseInstance, error) {
// Check if istio is enabled in cluster
log *zap.SugaredLogger,
) (*chart.ReleaseInstance, error) {
// Check if istio is enabled in cluster.
istioExists, err := r.kubeClient.DestinationRuleCRDExists(ctx)
if err != nil {
return nil, err
Expand All @@ -208,11 +211,11 @@ func (r *Reconciler) initNATSInstance(ctx context.Context, nats *natsv1alpha1.NA
}
log.Infof("NATS account secret (name: %s) exists: %t", accountSecretName, accountSecret != nil)

// generate overrides for helm chart
// Generate overrides for helm chart.
overrides := r.natsManager.GenerateOverrides(&nats.Spec, istioExists, accountSecret == nil)
log.Debugw("using overrides", "overrides", overrides)

// Init a release instance
// Init a release instance.
instance := chart.NewReleaseInstance(nats.Name, nats.Namespace, istioExists, overrides)

if err = r.generateNatsResources(nats, instance); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions internal/controller/nats/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,14 @@ func Test_handleNATSCRAllowedCheck(t *testing.T) {
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: string(natsv1alpha1.ConditionReasonForbidden),
Message: fmt.Sprintf("Only a single NATS CR with name: %s and namespace: %s "+
"is allowed to be created in a Kyma cluster.", givenAllowedNATS.Name,
givenAllowedNATS.Namespace),
Message: fmt.Sprintf(CreationNotAllowedMsg, givenAllowedNATS.Name, givenAllowedNATS.Namespace),
},
}
require.True(t, natsv1alpha1.ConditionsEquals(wantConditions, gotNATS.Status.Conditions))

wantK8sEventMsg := fmt.Sprintf("Warning Forbidden %s", CreationNotAllowedMsg)
wantK8sEvent := []string{
fmt.Sprintf("Warning Forbidden Only a single NATS CR with name: %s and namespace: %s "+
"is allowed to be created in a Kyma cluster.", givenAllowedNATS.Name,
givenAllowedNATS.Namespace),
fmt.Sprintf(wantK8sEventMsg, givenAllowedNATS.Name, givenAllowedNATS.Namespace),
}

// check k8s events
Expand Down
18 changes: 17 additions & 1 deletion internal/controller/nats/mocks/controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ee97c1a

Please sign in to comment.