Skip to content

Commit

Permalink
fix: better linting
Browse files Browse the repository at this point in the history
This commit enables additional linters and removes unused (commented) ones.

All issues reported by linters are resolved either with "make lint-fix" or
manually.
  • Loading branch information
Fedosin committed Jun 7, 2023
1 parent eeab2cb commit ebcce88
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 59 deletions.
37 changes: 22 additions & 15 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,29 @@ linters:
- typecheck
- unused
# Additional linters
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
# - cyclop
- dogsled
# - dupl
- durationcheck
- errname
# - errorlint
- errorlint
- exhaustive
# - exportloopref
- exportloopref
- forcetypeassert
# - funlen
# - gochecknoglobals
# - gocognit
- ginkgolinter
- goconst
- gocritic
- gocyclo
- godot
# - goerr113
- gofmt
- gofumpt
- goheader
- goimports
- goprintffuncname
# - gosec
- gosec
- importas
- makezero
- misspell
Expand All @@ -57,14 +54,23 @@ linters:
- nolintlint
- prealloc
- predeclared
# - revive
- promlinter
- reassign
- rowserrcheck
- sqlclosecheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
# - wrapcheck
- wsl

linters-settings:
goheader:
values:
Expand Down Expand Up @@ -141,8 +147,9 @@ issues:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- dupl
- gosec
- gochecknoglobals
- goerr113
- path: internal/envtest/environment.go
linters:
- dogsled
- gosec
- wsl
3 changes: 1 addition & 2 deletions api/v1alpha1/provider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlconfigv1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrlconfigv1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
)

const (
Expand Down
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"

// +kubebuilder:scaffold:imports

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
providercontroller "sigs.k8s.io/cluster-api-operator/internal/controller"
)
Expand Down Expand Up @@ -130,7 +128,12 @@ func main() {
klog.Infof("Profiler listening for requests at %s", profilerAddress)

go func() {
klog.Info(http.ListenAndServe(profilerAddress, nil))
server := &http.Server{
Addr: profilerAddress,
ReadHeaderTimeout: 3 * time.Second,
}

klog.Info(server.ListenAndServe())
}()
}

Expand Down
4 changes: 1 addition & 3 deletions internal/controller/component_customizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const (
defaultVerbosity = 1
)

var (
bool2Str = map[bool]string{true: "true", false: "false"}
)
var bool2Str = map[bool]string{true: "true", false: "false"}

// customizeObjectsFn apply provider specific customization to a list of manifests.
func customizeObjectsFn(provider genericprovider.GenericProvider) func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error) {
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/component_customizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
"k8s.io/utils/pointer"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
ctrlconfigv1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
)

func TestCustomizeDeployment(t *testing.T) {
Expand Down Expand Up @@ -479,7 +480,8 @@ func TestCustomizeDeployment(t *testing.T) {
"--sync-period=25200s",
"--profiler-address=localhost:1234",
"--v=5",
"--feature-gates=ANOTHER=false,TEST=true"},
"--feature-gates=ANOTHER=false,TEST=true",
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/genericprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"errors"
"fmt"
"reflect"

Expand Down Expand Up @@ -142,9 +143,9 @@ func (r *GenericProviderReconciler) reconcile(ctx context.Context, provider gene
for _, phase := range phases {
res, err = phase(ctx)
if err != nil {
se, ok := err.(*PhaseError)
if ok {
conditions.Set(provider, conditions.FalseCondition(se.Type, se.Reason, se.Severity, err.Error()))
var pe *PhaseError
if errors.As(err, &pe) {
conditions.Set(provider, conditions.FalseCondition(pe.Type, pe.Reason, pe.Severity, err.Error()))
}
}

Expand Down Expand Up @@ -174,9 +175,9 @@ func (r *GenericProviderReconciler) reconcileDelete(ctx context.Context, provide
for _, phase := range phases {
res, err = phase(ctx)
if err != nil {
se, ok := err.(*PhaseError)
if ok {
conditions.Set(provider, conditions.FalseCondition(se.Type, se.Reason, se.Severity, err.Error()))
var pe *PhaseError
if errors.As(err, &pe) {
conditions.Set(provider, conditions.FalseCondition(pe.Type, pe.Reason, pe.Severity, err.Error()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/controller/genericprovider_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"errors"
"fmt"
"net/url"
"strings"
Expand Down Expand Up @@ -410,7 +411,7 @@ func (p *phaseReconciler) install(ctx context.Context) (reconcile.Result, error)

if err := clusterClient.ProviderComponents().Create(p.components.Objs()); err != nil {
reason := "Install failed"
if err == wait.ErrWaitTimeout {
if errors.Is(err, wait.ErrWaitTimeout) {
reason = "Timed out waiting for deployment to become ready"
}

Expand Down
7 changes: 4 additions & 3 deletions internal/controller/preflight_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
)

func TestPreflightChecks(t *testing.T) {
Expand Down Expand Up @@ -650,7 +651,7 @@ func TestPreflightChecks(t *testing.T) {
}

// Check if proper condition is returned
gs.Expect(len(tc.providers[0].GetStatus().Conditions)).To(Equal(1))
gs.Expect(tc.providers[0].GetStatus().Conditions).To(HaveLen(1))
gs.Expect(tc.providers[0].GetStatus().Conditions[0].Type).To(Equal(tc.expectedCondition.Type))
gs.Expect(tc.providers[0].GetStatus().Conditions[0].Status).To(Equal(tc.expectedCondition.Status))
gs.Expect(tc.providers[0].GetStatus().Conditions[0].Message).To(Equal(tc.expectedCondition.Message))
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/envtest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
// +kubebuilder:scaffold:imports

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/envtest"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions internal/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type Environment struct {
// usually the environment is initialized in a suite_test.go file within a `BeforeSuite` ginkgo block.
func New(uncachedObjs ...client.Object) *Environment {
// Get the root of the current file to use in CRD paths.
_, filename, _, _ := goruntime.Caller(0) //nolint
_, filename, _, _ := goruntime.Caller(0)
root := path.Join(path.Dir(filename), "..", "..")
crdPaths := []string{
filepath.Join(root, "config", "crd", "bases"),
Expand All @@ -114,7 +114,7 @@ func New(uncachedObjs ...client.Object) *Environment {
env := &envtest.Environment{
Scheme: scheme.Scheme,
ErrorIfCRDPathMissing: true,
//CRDInstallOptions: envtest.CRDInstallOptions{CleanUpAfterUse: true},
// CRDInstallOptions: envtest.CRDInstallOptions{CleanUpAfterUse: true},
CRDDirectoryPaths: crdPaths,
}

Expand Down Expand Up @@ -232,7 +232,6 @@ func (e *Environment) CleanupAndWait(ctx context.Context, objs ...client.Object)

return false, nil
})

if err != nil {
errs = append(errs, fmt.Errorf("key %s, %s is not being deleted from the testenv client cache: %w", o.GetObjectKind().GroupVersionKind().String(), key, err))
}
Expand Down
3 changes: 1 addition & 2 deletions internal/webhook/bootstrapprovider_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
)

type BootstrapProviderWebhook struct {
}
type BootstrapProviderWebhook struct{}

func (r *BootstrapProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
3 changes: 1 addition & 2 deletions internal/webhook/controlplaneprovider_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
)

type ControlPlaneProviderWebhook struct {
}
type ControlPlaneProviderWebhook struct{}

func (r *ControlPlaneProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
3 changes: 1 addition & 2 deletions internal/webhook/coreprovider_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
)

type CoreProviderWebhook struct {
}
type CoreProviderWebhook struct{}

func (r *CoreProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
3 changes: 1 addition & 2 deletions internal/webhook/infrastructureprovider_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
)

type InfrastructureProviderWebhook struct {
}
type InfrastructureProviderWebhook struct{}

func (r *InfrastructureProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down
12 changes: 4 additions & 8 deletions webhook/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

type BootstrapProviderWebhook struct {
}
type BootstrapProviderWebhook struct{}

func (r *BootstrapProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return (&internalwebhook.BootstrapProviderWebhook{}).SetupWebhookWithManager(mgr)
}

type ControlPlaneProviderWebhook struct {
}
type ControlPlaneProviderWebhook struct{}

func (r *ControlPlaneProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return (&internalwebhook.ControlPlaneProviderWebhook{}).SetupWebhookWithManager(mgr)
}

type CoreProviderWebhook struct {
}
type CoreProviderWebhook struct{}

func (r *CoreProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return (&internalwebhook.CoreProviderWebhook{}).SetupWebhookWithManager(mgr)
}

type InfrastructureProviderWebhook struct {
}
type InfrastructureProviderWebhook struct{}

func (r *InfrastructureProviderWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
return (&internalwebhook.InfrastructureProviderWebhook{}).SetupWebhookWithManager(mgr)
Expand Down

0 comments on commit ebcce88

Please sign in to comment.