Skip to content

Commit

Permalink
feat(lint): enabe magic number detector (mld)
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed May 11, 2024
1 parent 043b4ea commit 8fa8846
Show file tree
Hide file tree
Showing 74 changed files with 414 additions and 120 deletions.
37 changes: 36 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,47 @@
linters-settings:
lll:
line-length: 170
mnd:
checks:
- argument
- case
- operation
- return
- assign
ignored-functions:
- '^len\.'
- '^strings\.SplitN$'
- '^matchOrEmpty$'
- '^make$'
ignored-files:
# ignore install files that are part of features being deprecated
# such ass install anc the kamel cli
#
# files are individually listed as if any new file is added or those not listed here are amended
# then best practices must be followed
- 'pkg/install/cluster.go'
- 'pkg/cmd/install.go'
- 'pkg/cmd/debug.go'
- 'pkg/cmd/describe.go'
- 'pkg/cmd/describe_integration.go'
- 'pkg/cmd/describe_kamelet.go'
- 'pkg/cmd/dump.go'
- 'pkg/cmd/get.go'
- 'pkg/cmd/install.go'
- 'pkg/cmd/log.go'
- 'pkg/cmd/kamelet_get.go'
- 'pkg/cmd/kit_get.go'
- 'pkg/cmd/uninstall.go'
- 'pkg/cmd/run_support.go'
# seems to be used only by the "kamel cli" command
- 'pkg/util/kubernetes/log/pod_scraper.go'
- 'pkg/util/indentedwriter/writer.go'
run:
tests: false
issues:
exclude-files:
- test_support.go

linters:
enable-all: true
disable:
Expand Down Expand Up @@ -62,6 +98,5 @@ linters:
- execinquery
# TODO: too much work at this stage as many files are impacted by the lint suggestions, however the reported
# lint violation make a lot of sense so we should re-enable the lints below and work to fix the findings
- mnd
- perfsprint
- goconst
2 changes: 1 addition & 1 deletion addons/keda/duck/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 3 additions & 1 deletion cmd/util/json-schema-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"reflect"
"strings"

"github.com/apache/camel-k/v2/pkg/util/io"

"github.com/apache/camel-k/v2/pkg/util"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -99,7 +101,7 @@ func generate(crdFilename, dslFilename, path string, isArray bool, destination s
if err != nil {
return err
}
return os.WriteFile(destination, result, 0o600)
return os.WriteFile(destination, result, io.FilePerm600)
}

func remapRef(ref string) string {
Expand Down
3 changes: 2 additions & 1 deletion e2e/advanced/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
"context"
"testing"

"os/exec"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"os/exec"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
Expand Down
3 changes: 2 additions & 1 deletion e2e/advanced/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ package advanced
import (
"context"
"fmt"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"testing"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
Expand Down
5 changes: 3 additions & 2 deletions e2e/install/cli/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ package cli
import (
"context"
"fmt"
. "github.com/onsi/gomega"
"testing"

. "github.com/onsi/gomega"

. "github.com/apache/camel-k/v2/e2e/support"
"github.com/apache/camel-k/v2/pkg/apis/camel/v1"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/olm"
)

Expand Down
3 changes: 2 additions & 1 deletion e2e/knative/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ package knative

import (
"context"
"testing"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
corev1 "k8s.io/api/core/v1"
"testing"

. "github.com/apache/camel-k/v2/e2e/support"
. "github.com/onsi/gomega"
Expand Down
3 changes: 2 additions & 1 deletion e2e/knative/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (
"context"
"testing"

"io/ioutil"

. "github.com/apache/camel-k/v2/e2e/support"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/require"
"io/ioutil"
)

func TestOpenAPIService(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion e2e/native/native_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ package native

import (
"context"
"testing"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"testing"
)

func TestNativeBinding(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion e2e/support/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ package support

import (
"context"
"github.com/apache/camel-k/v2/pkg/util/log"
"os"
"os/exec"
"strings"
"testing"

"github.com/apache/camel-k/v2/pkg/util/log"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
. "github.com/onsi/gomega/gstruct"
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/camel/v1/trait/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions pkg/apis/camel/v1/zz_generated.deepcopy.go

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

3 changes: 2 additions & 1 deletion pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go

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

8 changes: 5 additions & 3 deletions pkg/builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path"
"path/filepath"

"github.com/apache/camel-k/v2/pkg/util/io"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"

Expand Down Expand Up @@ -88,7 +90,7 @@ func executableDockerfile(ctx *builderContext) error {
USER nonroot
`)

err := os.WriteFile(filepath.Join(ctx.Path, ContextDir, "Dockerfile"), dockerfile, 0o400)
err := os.WriteFile(filepath.Join(ctx.Path, ContextDir, "Dockerfile"), dockerfile, io.FilePerm400)
if err != nil {
return err
}
Expand All @@ -112,7 +114,7 @@ func jvmDockerfile(ctx *builderContext) error {
USER 1000
`)

err := os.WriteFile(filepath.Join(ctx.Path, ContextDir, "Dockerfile"), dockerfile, 0o400)
err := os.WriteFile(filepath.Join(ctx.Path, ContextDir, "Dockerfile"), dockerfile, io.FilePerm400)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,7 +156,7 @@ func imageContext(ctx *builderContext, selector artifactsSelector) error {

contextDir := filepath.Join(ctx.Path, ContextDir)

err = os.MkdirAll(contextDir, 0o700)
err = os.MkdirAll(contextDir, io.FilePerm700)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/builder/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"regexp"
"strings"

"github.com/apache/camel-k/v2/pkg/util/io"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/jvm"
Expand Down Expand Up @@ -57,6 +59,7 @@ type projectSteps struct {
CommonSteps []Step
}

//nolint:mnd
var Project = projectSteps{
CleanUpBuildDir: NewStep(ProjectGenerationPhase-1, cleanUpBuildDir),
GenerateJavaKeystore: NewStep(ProjectGenerationPhase, generateJavaKeystore),
Expand All @@ -76,7 +79,7 @@ func cleanUpBuildDir(ctx *builderContext) error {
return err
}

return os.MkdirAll(ctx.Build.BuildDir, 0o700)
return os.MkdirAll(ctx.Build.BuildDir, io.FilePerm700)
}

func generateJavaKeystore(ctx *builderContext) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/builder/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"

"github.com/apache/camel-k/v2/pkg/util/io"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/defaults"
Expand Down Expand Up @@ -221,7 +223,7 @@ func BuildQuarkusRunnerCommon(ctx context.Context, mc maven.Context, project mav
}

func computeApplicationProperties(appPropertiesPath string, applicationProperties map[string]string) error {
f, err := os.OpenFile(appPropertiesPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(appPropertiesPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, io.FilePerm644)
if err != nil {
return fmt.Errorf("failure while opening/creating application.properties: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"runtime"
"syscall"

"github.com/apache/camel-k/v2/pkg/util/io"

"k8s.io/apimachinery/pkg/types"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -104,7 +106,7 @@ func exitOnError(err error, msg string) {

func writeTerminationMessage(message string) {
// #nosec G306
err := os.WriteFile(terminationMessagePath, []byte(message), 0o644)
err := os.WriteFile(terminationMessagePath, []byte(message), io.FilePerm644)
if err != nil {
log.Error(err, "cannot write termination message")
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"github.com/spf13/cobra"
)

const operatorCommand = "operator"
const (
operatorCommand = "operator"
defaultHealthPort = 8081
defaultMonitoringPort = 8080
)

func newCmdOperator(rootCmdOptions *RootCmdOptions) (*cobra.Command, *operatorCmdOptions) {
options := operatorCmdOptions{}
Expand All @@ -38,8 +42,8 @@ func newCmdOperator(rootCmdOptions *RootCmdOptions) (*cobra.Command, *operatorCm
Run: options.run,
}

cmd.Flags().Int32("health-port", 8081, "The port of the health endpoint")
cmd.Flags().Int32("monitoring-port", 8080, "The port of the metrics endpoint")
cmd.Flags().Int32("health-port", defaultHealthPort, "The port of the health endpoint")
cmd.Flags().Int32("monitoring-port", defaultMonitoringPort, "The port of the metrics endpoint")
cmd.Flags().Bool("leader-election", true, "Use leader election")
cmd.Flags().String("leader-election-id", "", "Use the given ID as the leader election Lease name")

Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/util_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"reflect"
"strings"

"github.com/apache/camel-k/v2/pkg/util/io"

"github.com/apache/camel-k/v2/pkg/util"
p "github.com/gertd/go-pluralize"

Expand Down Expand Up @@ -147,7 +149,7 @@ func (cfg *Config) Delete(path string) {
func (cfg *Config) Save() error {
root := filepath.Dir(cfg.location)
if _, err := os.Stat(root); os.IsNotExist(err) {
if e := os.MkdirAll(root, 0o600); e != nil {
if e := os.MkdirAll(root, io.FilePerm600); e != nil {
return e
}
}
Expand All @@ -156,7 +158,7 @@ func (cfg *Config) Save() error {
if err != nil {
return err
}
return os.WriteFile(cfg.location, data, 0o600)
return os.WriteFile(cfg.location, data, io.FilePerm600)
}

func (cfg *Config) navigate(values map[string]interface{}, prefix string, create bool) map[string]interface{} {
Expand Down
9 changes: 7 additions & 2 deletions pkg/controller/build/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import (
"github.com/apache/camel-k/v2/pkg/util/monitoring"
)

const (
requeueAfterDuration = 5 * time.Second
podRequeueAfterDuration = 1 * time.Second
)

// Add creates a new Build Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
Expand Down Expand Up @@ -224,13 +229,13 @@ func (r *reconcileBuild) Reconcile(ctx context.Context, request reconcile.Reques

if target.Status.Phase == v1.BuildPhaseScheduling || target.Status.Phase == v1.BuildPhaseFailed {
// Requeue scheduling (resp. failed) build so that it re-enters the build (resp. recovery) working queue
return reconcile.Result{RequeueAfter: 5 * time.Second}, nil
return reconcile.Result{RequeueAfter: requeueAfterDuration}, nil
}

if target.BuilderConfiguration().Strategy == v1.BuildStrategyPod &&
(target.Status.Phase == v1.BuildPhasePending || target.Status.Phase == v1.BuildPhaseRunning) {
// Requeue running Build to poll Pod and signal timeout
return reconcile.Result{RequeueAfter: 1 * time.Second}, nil
return reconcile.Result{RequeueAfter: podRequeueAfterDuration}, nil
}

return reconcile.Result{}, nil
Expand Down
Loading

0 comments on commit 8fa8846

Please sign in to comment.