Skip to content

Commit

Permalink
fixup! fix: Enable more linters
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Mar 5, 2024
1 parent 32722ec commit d8e62c1
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 116 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ jobs:
# TODO coverage report

- name: Go linters
run: make lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2

container-check:
runs-on: ubuntu-latest
Expand Down
17 changes: 14 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
run:
skip-dirs-use-default: false
modules-download-mode: readonly
allow-parallel-runners: false
allow-parallel-runners: true
skip-dirs:
- test/*
skip-files:
- internal/controller/velerobackup_controller.go
- internal/controller/velerobackup_controller_test.go

output:
format: colored-line-number
Expand Down Expand Up @@ -107,8 +112,14 @@ issues:
- revive
text: "^struct-tag: unknown option 'inline' in JSON tag$"
- linters:
- stylecheck
text: "ST1000:|ST1020:|ST1021:|ST1022:"
- revive
text: "^add-constant: avoid magic numbers like '0', create a named constant for it$"
- linters:
- revive
text: "^add-constant: avoid magic numbers like '1', create a named constant for it$"
# - linters:
# - stylecheck
# text: "ST1000:|ST1020:|ST1021:|ST1022:"
max-issues-per-linter: 0
max-same-issues: 0

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
GOLANGCI_LINT_VERSION ?= v1.56.2
GOLANGCI_LINT_VERSION ?= v1.55.2

.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
Expand Down
16 changes: 8 additions & 8 deletions api/v1alpha1/nonadminbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ import (

// NonAdminBackupSpec defines the desired state of NonAdminBackup
type NonAdminBackupSpec struct {
// NonAdminBackup log level (use debug for the most logging, leave unset for default)
// +optional
// +kubebuilder:validation:Enum=trace;debug;info;warning;error;fatal;panic
LogLevel string `json:"logLevel,omitempty"`

// https://github.com/vmware-tanzu/velero/blob/main/pkg/apis/velero/v1/backup_types.go

// BackupSpec defines the specification for a Velero backup.
BackupSpec *velerov1api.BackupSpec `json:"backupSpec,omitempty"`

// BackupStatus captures the current status of a Velero backup.
BackupStatus *velerov1api.BackupStatus `json:"backupStatus,omitempty"`

// NonAdminBackup log level (use debug for the most logging, leave unset for default)
// +optional
// +kubebuilder:validation:Enum=trace;debug;info;warning;error;fatal;panic
LogLevel string `json:"logLevel,omitempty"`
}

// NonAdminBackupStatus defines the observed state of NonAdminBackup
type NonAdminBackupStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// NonAdminBackup is the Schema for the nonadminbackups API
type NonAdminBackup struct {
Expand All @@ -54,7 +54,7 @@ type NonAdminBackup struct {
Status NonAdminBackupStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// NonAdminBackupList contains a list of NonAdminBackup
type NonAdminBackupList struct {
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {
utilruntime.Must(nacv1alpha1.AddToScheme(scheme))

utilruntime.Must(velerov1api.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme
}

func main() {

Check failure on line 55 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golang-check (1.20)

function-length: maximum number of lines per function exceeded; max 75 but got 101 (revive)

Check failure on line 55 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golang-check (1.21)

function-length: maximum number of lines per function exceeded; max 75 but got 101 (revive)

Check failure on line 55 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golang-check (1.22)

function-length: maximum number of lines per function exceeded; max 75 but got 101 (revive)
Expand Down Expand Up @@ -130,7 +130,7 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "NonAdminBackup")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
// +kubebuilder:scaffold:builder

if err = (&controller.VeleroBackupReconciler{
Client: mgr.GetClient(),
Expand Down
28 changes: 23 additions & 5 deletions internal/controller/common_k8s.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024.
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.
*/

package controller

Check failure on line 17 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.20)

package-comments: should have a package comment (revive)

Check failure on line 17 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.21)

package-comments: should have a package comment (revive)

Check failure on line 17 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.22)

package-comments: should have a package comment (revive)

import (
Expand All @@ -14,7 +30,7 @@ const (
ManagedByLabelValue = "oadp-nac-controller"
NabOriginNameAnnotation = "openshift.io/oadp-nab-origin-name"
NabOriginNamespaceAnnotation = "openshift.io/oadp-nab-origin-namespace"
NabOriginUuidAnnotation = "openshift.io/oadp-nab-origin-uuid"
NabOriginUUIDAnnotation = "openshift.io/oadp-nab-origin-uuid"
)

const (
Expand All @@ -29,22 +45,24 @@ func CreateLabelsForNac(labels map[string]string) map[string]string {

mergedLabels, err := mergeUniqueKeyTOfTMaps(defaultLabels, labels)
if err != nil {
fmt.Println("Error merging labels:", err)
// TODO logger
_, _ = fmt.Println("Error merging labels:", err)
return defaultLabels
}
return mergedLabels
}

func CreateAnnotationsForNac(ownerNamespace string, ownerName string, ownerUuid string, existingAnnotations map[string]string) map[string]string {
func CreateAnnotationsForNac(ownerNamespace string, ownerName string, ownerUUID string, existingAnnotations map[string]string) map[string]string {

Check failure on line 55 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.20)

exported: exported function CreateAnnotationsForNac should have comment or be unexported (revive)

Check failure on line 55 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.21)

exported: exported function CreateAnnotationsForNac should have comment or be unexported (revive)

Check failure on line 55 in internal/controller/common_k8s.go

View workflow job for this annotation

GitHub Actions / golang-check (1.22)

exported: exported function CreateAnnotationsForNac should have comment or be unexported (revive)
defaultAnnotations := map[string]string{
NabOriginNamespaceAnnotation: ownerNamespace,
NabOriginNameAnnotation: ownerName,
NabOriginUuidAnnotation: ownerUuid,
NabOriginUUIDAnnotation: ownerUUID,
}

mergedAnnotations, err := mergeUniqueKeyTOfTMaps(defaultAnnotations, existingAnnotations)
if err != nil {
fmt.Println("Error merging annotations:", err)
// TODO logger
_, _ = fmt.Println("Error merging annotations:", err)
return defaultAnnotations
}
return mergedAnnotations
Expand Down
85 changes: 47 additions & 38 deletions internal/controller/common_k8s_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024.
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.
*/

package controller

import (
Expand All @@ -8,71 +24,64 @@ import (
)

func TestMergeUniqueKeyTOfTMaps(t *testing.T) {

Check failure on line 26 in internal/controller/common_k8s_test.go

View workflow job for this annotation

GitHub Actions / golang-check (1.20)

cognitive-complexity: function TestMergeUniqueKeyTOfTMaps has cognitive complexity 8 (> max enabled 7) (revive)

Check failure on line 26 in internal/controller/common_k8s_test.go

View workflow job for this annotation

GitHub Actions / golang-check (1.21)

cognitive-complexity: function TestMergeUniqueKeyTOfTMaps has cognitive complexity 8 (> max enabled 7) (revive)

Check failure on line 26 in internal/controller/common_k8s_test.go

View workflow job for this annotation

GitHub Actions / golang-check (1.22)

cognitive-complexity: function TestMergeUniqueKeyTOfTMaps has cognitive complexity 8 (> max enabled 7) (revive)
type args struct {
userLabels []map[string]string
}
const (
d = "d"
delta = "delta"
)
tests := []struct {
name string
args args
want map[string]string
args []map[string]string
wantErr bool
}{
{
name: "append unique labels together",
args: args{
userLabels: []map[string]string{
{"a": "a"},
{"b": "b"},
},
args: []map[string]string{
{"a": "alpha"},
{"b": "beta"},
},
want: map[string]string{
"a": "a",
"b": "b",
"a": "alpha",
"b": "beta",
},
},
{
name: "append unique labels together, with valid duplicates",
args: args{
userLabels: []map[string]string{
{"a": "a"},
{"b": "b"},
{"b": "b"},
},
args: []map[string]string{
{"c": "gamma"},
{d: delta},
{d: delta},
},
want: map[string]string{
"a": "a",
"b": "b",
"c": "gamma",
d: delta,
},
},
{
name: "append unique labels together - nil sandwich",
args: args{
userLabels: []map[string]string{
{"a": "a"},
nil,
{"b": "b"},
},
args: []map[string]string{
{"x": "chi"},
nil,
{"y": "psi"},
},
want: map[string]string{
"a": "a",
"b": "b",
"x": "chi",
"y": "psi",
},
},
{
name: "should error when append duplicate label keys with different value together",
args: args{
userLabels: []map[string]string{
{"a": "a"},
{"a": "b"},
},
args: []map[string]string{
{"key": "value-1"},
{"key": "value-2"},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := mergeUniqueKeyTOfTMaps(tt.args.userLabels...)
got, err := mergeUniqueKeyTOfTMaps(tt.args...)
if (err != nil) != tt.wantErr {
t.Errorf("mergeUniqueKeyTOfTMaps() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -111,17 +120,17 @@ func TestCreateAnnotationsForNac(t *testing.T) {

ownerName := "testOwner"
ownerNamespace := "testNamespace"
ownerUuid := "f2c4d2c3-58d3-46ec-bf03-5940f567f7f8"
ownerUUID := "f2c4d2c3-58d3-46ec-bf03-5940f567f7f8"

expectedAnnotations := map[string]string{
NabOriginNamespaceAnnotation: ownerNamespace,
NabOriginNameAnnotation: ownerName,
NabOriginUuidAnnotation: ownerUuid,
NabOriginUUIDAnnotation: ownerUUID,
"existingKey1": "existingValue1",
"existingKey2": "existingValue2",
}

mergedAnnotations := CreateAnnotationsForNac(ownerNamespace, ownerName, ownerUuid, existingAnnotations)
mergedAnnotations := CreateAnnotationsForNac(ownerNamespace, ownerName, ownerUUID, existingAnnotations)
assert.Equal(t, expectedAnnotations, mergedAnnotations, "Merged annotations should match expected annotations")

// Merging annotations with conflicts
Expand All @@ -132,9 +141,9 @@ func TestCreateAnnotationsForNac(t *testing.T) {
expectedAnnotationsWithConflict := map[string]string{
NabOriginNameAnnotation: ownerName,
NabOriginNamespaceAnnotation: ownerNamespace,
NabOriginUuidAnnotation: ownerUuid,
NabOriginUUIDAnnotation: ownerUUID,
}

mergedAnnotationsWithConflict := CreateAnnotationsForNac(ownerNamespace, ownerName, ownerUuid, existingAnnotationsWithConflict)
mergedAnnotationsWithConflict := CreateAnnotationsForNac(ownerNamespace, ownerName, ownerUUID, existingAnnotationsWithConflict)
assert.Equal(t, expectedAnnotationsWithConflict, mergedAnnotationsWithConflict, "Merged annotations should match expected annotations with conflict")
}
17 changes: 16 additions & 1 deletion internal/controller/common_nab.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024.
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.
*/

package controller

Check failure on line 17 in internal/controller/common_nab.go

View workflow job for this annotation

GitHub Actions / golang-check (1.20)

ST1000: at least one file in a package should have a package comment (stylecheck)

Check failure on line 17 in internal/controller/common_nab.go

View workflow job for this annotation

GitHub Actions / golang-check (1.21)

ST1000: at least one file in a package should have a package comment (stylecheck)

Check failure on line 17 in internal/controller/common_nab.go

View workflow job for this annotation

GitHub Actions / golang-check (1.22)

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
Expand All @@ -11,7 +27,6 @@ import (
)

func GetVeleroBackupSpecFromNonAdminBackup(nonAdminBackup *nacv1alpha1.NonAdminBackup) (*velerov1api.BackupSpec, error) {

if nonAdminBackup == nil {
return nil, fmt.Errorf("nonAdminBackup is nil")
}
Expand Down
16 changes: 16 additions & 0 deletions internal/controller/common_nab_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024.
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.
*/

package controller

import (
Expand Down
Loading

0 comments on commit d8e62c1

Please sign in to comment.