Skip to content

Commit

Permalink
feat: Add golangci-lint linters - imports (openshift#1337)
Browse files Browse the repository at this point in the history
* feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

* fixup! feat: Add golangci-lint

Signed-off-by: Mateus Oliveira <[email protected]>

---------

Signed-off-by: Mateus Oliveira <[email protected]>
(cherry picked from commit 76f0049)
  • Loading branch information
mateusoliveira43 committed Mar 1, 2024
1 parent d19a993 commit 063bf8e
Show file tree
Hide file tree
Showing 45 changed files with 1,048 additions and 866 deletions.
126 changes: 126 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Documentation reference https://github.com/golangci/golangci-lint/blob/v1.55.2/.golangci.reference.yml
run:
skip-dirs-use-default: false
modules-download-mode: readonly
allow-parallel-runners: false

output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true
uniq-by-line: true
sort-results: true

linters-settings:
dogsled:
max-blank-identifiers: 2
errcheck:
check-type-assertions: true
check-blank: true
gci:
sections:
- standard
- default
- prefix(github.com/openshift/oadp-operator)
goconst:
min-len: 3
min-occurrences: 5
gofmt:
simplify: true
goheader:
# copy from ./hack/boilerplate.go.txt
template: |-
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
govet:
enable-all: true
misspell:
locale: US
nakedret:
max-func-lines: 30
nolintlint:
allow-unused: false
allow-no-explanation: []
require-explanation: true
require-specific: true
revive:
# TODO enable!
# enable-all-rules: true
rules:
- name: line-length-limit
disabled: true
# TODO delete after
- name: blank-imports
- name: dot-imports
- name: duplicated-imports
- name: import-alias-naming
- name: import-shadowing
- name: redundant-import-alias
unparam:
check-exported: true

linters:
disable-all: true
# TODO enable commented ones
enable:
- asasalint
- asciicheck
- bidichk
# - bodyclose
- dogsled
# - dupword
# - durationcheck
# - errcheck
- errchkjson
- exportloopref
- gci
# - ginkgolinter
# - goconst
- gofmt
# - goheader
- goprintffuncname
# - gosec
# - gosimple
# - govet
# - ineffassign
# - misspell
- nakedret
# - nilerr
# - noctx
- nolintlint
- nosprintfhostport
- revive
# - staticcheck
# - stylecheck
# - unconvert
# - unparam
# - unused
- usestdlibvars
fast: false

issues:
exclude-use-default: false
exclude-rules:
- linters:
- revive
text: "^struct-tag: unknown option 'inline' in JSON tag$"
- linters:
- stylecheck
text: "ST1000:|ST1020:|ST1021:|ST1022:"
max-issues-per-linter: 0
max-same-issues: 0

severity:
default-severity: error
case-sensitive: false
24 changes: 16 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,12 @@ envtest: $(ENVTEST)
# to login to registry cluster follow https://docs.ci.openshift.org/docs/how-tos/use-registries-in-build-farm/#how-do-i-log-in-to-pull-images-that-require-authentication
# If bin/ contains binaries of different arch, you may remove them so the container can install their arch.
.PHONY: test
test: vet envtest ## Run Go linter and unit tests and check Go code format and if api and bundle folders are up to date.
test: vet envtest ## Run unit tests; run Go linters checks; and check if api and bundle folders are up to date
KUBEBUILDER_ASSETS="$(ENVTESTPATH)" go test -mod=mod $(shell go list -mod=mod ./... | grep -v /tests/e2e) -coverprofile cover.out
@make fmt-isupdated
@make lint
@make api-isupdated
@make bundle-isupdated

.PHONY: fmt-isupdated
fmt-isupdated: TEMP:= $(shell mktemp -d)
fmt-isupdated:
@cp -r ./ $(TEMP) && cd $(TEMP) && make fmt && cd - && diff -ruN . $(TEMP)/ && echo "Go code is formatted" || (echo "Go code is not formatted, run 'make fmt' to format" && exit 1)
@chmod -R 777 $(TEMP) && rm -rf $(TEMP)

.PHONY: api-isupdated
api-isupdated: TEMP:= $(shell mktemp -d)
api-isupdated:
Expand Down Expand Up @@ -538,3 +532,17 @@ test-e2e-cleanup: login-required
$(OC_CLI) delete restore -n $(OADP_TEST_NAMESPACE) --all --wait=false
for restore_name in $(shell $(OC_CLI) get restore -n $(OADP_TEST_NAMESPACE) -o name);do $(OC_CLI) patch "$$restore_name" -n $(OADP_TEST_NAMESPACE) -p '{"metadata":{"finalizers":null}}' --type=merge;done
rm -rf $(SETTINGS_TMP)

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint

.PHONY: golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/[email protected])

.PHONY: lint
lint: golangci-lint ## Run Go linters checks against all project's Go files.
$(GOLANGCI_LINT) run

.PHONY: lint-fix
lint-fix: golangci-lint ## Fix Go linters issues.
$(GOLANGCI_LINT) run --fix
5 changes: 3 additions & 2 deletions api/v1alpha1/oadp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package v1alpha1
import (
"time"

"github.com/openshift/oadp-operator/pkg/common"
"github.com/openshift/oadp-operator/pkg/velero/server"
velero "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openshift/oadp-operator/pkg/common"
"github.com/openshift/oadp-operator/pkg/velero/server"
)

// Conditions
Expand Down
7 changes: 4 additions & 3 deletions controllers/bsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"

"github.com/go-logr/logr"
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
"github.com/openshift/oadp-operator/pkg/common"
"github.com/openshift/oadp-operator/pkg/storage/aws"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
"github.com/openshift/oadp-operator/pkg/common"
"github.com/openshift/oadp-operator/pkg/storage/aws"
)

func (r *DPAReconciler) ValidateBackupStorageLocations(dpa oadpv1alpha1.DataProtectionApplication) (bool, error) {
Expand Down
47 changes: 23 additions & 24 deletions controllers/bsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import (
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -21,6 +18,8 @@ import (
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
)

// A bucket that region can be automatically discovered
Expand Down Expand Up @@ -1636,7 +1635,7 @@ func TestDPAReconciler_ensureBackupLocationHasVeleroOrCloudStorage(t *testing.T)
Provider: "aws",
},
CloudStorage: &oadpv1alpha1.CloudStorageLocation{
CloudStorageRef: v1.LocalObjectReference{
CloudStorageRef: corev1.LocalObjectReference{
Name: "foo",
},
},
Expand Down Expand Up @@ -1903,8 +1902,8 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
Namespace: "test-ns",
},
Spec: oadpv1alpha1.CloudStorageSpec{
CreationSecret: v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
CreationSecret: corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand Down Expand Up @@ -1955,11 +1954,11 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
BackupLocations: []oadpv1alpha1.BackupLocation{
{
CloudStorage: &oadpv1alpha1.CloudStorageLocation{
CloudStorageRef: v1.LocalObjectReference{
CloudStorageRef: corev1.LocalObjectReference{
Name: "test-cs",
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Credential: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand Down Expand Up @@ -2054,8 +2053,8 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
Prefix: "test-prefix",
},
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Credential: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand Down Expand Up @@ -2087,8 +2086,8 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
Prefix: "test-prefix",
},
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Credential: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand All @@ -2108,11 +2107,11 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
BackupLocations: []oadpv1alpha1.BackupLocation{
{
CloudStorage: &oadpv1alpha1.CloudStorageLocation{
CloudStorageRef: v1.LocalObjectReference{
CloudStorageRef: corev1.LocalObjectReference{
Name: "test-cs",
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Credential: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand All @@ -2137,8 +2136,8 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
},
Spec: oadpv1alpha1.CloudStorageSpec{
Provider: "aws",
CreationSecret: v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
CreationSecret: corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand Down Expand Up @@ -2245,11 +2244,11 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
BackupLocations: []oadpv1alpha1.BackupLocation{
{
CloudStorage: &oadpv1alpha1.CloudStorageLocation{
CloudStorageRef: v1.LocalObjectReference{
CloudStorageRef: corev1.LocalObjectReference{
Name: "test-cs",
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Credential: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand All @@ -2275,8 +2274,8 @@ func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
},
Spec: oadpv1alpha1.CloudStorageSpec{
Provider: "aws",
CreationSecret: v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
CreationSecret: corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
Expand Down
Loading

0 comments on commit 063bf8e

Please sign in to comment.