Skip to content

Commit

Permalink
Enabled last batch of linters
Browse files Browse the repository at this point in the history
Signed-off-by: Kashif Khan <kashif.khan@est.tech>
  • Loading branch information
kashifest committed Mar 28, 2024
1 parent d46e201 commit 02af350
Show file tree
Hide file tree
Showing 55 changed files with 260 additions and 274 deletions.
26 changes: 20 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@ run:
linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
# - containedctx
- dogsled
# - errcheck
# - errorlint
- dupword
- durationcheck
- errcheck
- errchkjson
- exportloopref
- gci
- ginkgolinter
- goconst
- gocritic
- godot
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- gosec
- ineffassign
- loggercheck
- misspell
# - nakedret
# - nilerr
# - noctx
- nakedret
- nilerr
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- revive
Expand All @@ -38,7 +48,9 @@ linters:
- thelper
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
# Run with --fast=false for more extensive checks
fast: true
Expand Down Expand Up @@ -114,6 +126,8 @@ issues:
- unused
- revive
- stylecheck
- noctx
- unparam
# Specific exclude rules for deprecated fields that are still part of the codebase.
# These should be removed as the referenced deprecated item is removed from the project.
- linters:
Expand Down
3 changes: 1 addition & 2 deletions cmd/get-hardware-details/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
"strings"

"github.com/gophercloud/gophercloud/v2/openstack/baremetal/v1/nodes"
"k8s.io/klog/v2"

"github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/clients"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/hardwaredetails"
"k8s.io/klog/v2"
)

type options struct {
Expand Down
16 changes: 14 additions & 2 deletions cmd/make-virt-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ func main() {
}

domainResult := Domain{}
xml.Unmarshal(virshOut, &domainResult)
err = xml.Unmarshal(virshOut, &domainResult)
if err != nil {
fmt.Fprintf(os.Stderr,
"ERROR: Could not unmarshal details of domain %s: %s\n",
virshDomain, err)
os.Exit(1)
}

if *verbose {
fmt.Printf("%v\n", domainResult)
}
Expand Down Expand Up @@ -192,7 +199,12 @@ func main() {
}

var vbmcResult []VBMC
json.Unmarshal(vbmcOut, &vbmcResult)
_ = json.Unmarshal(vbmcOut, &vbmcResult)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not unmarshal details of vbmc: %s\n", err)
os.Exit(1)
}

nameToPort := make(map[string]int)
for _, vbmc := range vbmcResult {
if *verbose {
Expand Down
3 changes: 1 addition & 2 deletions controllers/metal3.io/action_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"math/rand"
"time"

"sigs.k8s.io/controller-runtime/pkg/reconcile"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// This is an upper limit for the ErrorCount, so that the max backoff
Expand Down
30 changes: 16 additions & 14 deletions controllers/metal3.io/baremetalhost_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ import (
"time"

"github.com/go-logr/logr"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1/profile"
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
"github.com/metal3-io/baremetal-operator/pkg/imageprovider"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/utils"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"

corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -39,14 +45,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1/profile"
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
"github.com/metal3-io/baremetal-operator/pkg/imageprovider"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/utils"
)

const (
Expand Down Expand Up @@ -224,7 +222,7 @@ func (r *BareMetalHostReconciler) Reconcile(ctx context.Context, request ctrl.Re

if err != nil {
err = errors.Wrap(err, fmt.Sprintf("action %q failed", initialState))
return
return result, err
}

// Only save status when we're told to, otherwise we
Expand Down Expand Up @@ -253,7 +251,7 @@ func (r *BareMetalHostReconciler) Reconcile(ctx context.Context, request ctrl.Re

logResult(info, result)

return
return result, nil
}

// Consume inspect.metal3.io/hardwaredetails when either
Expand Down Expand Up @@ -717,7 +715,11 @@ func (r *BareMetalHostReconciler) getPreprovImage(info *reconcileInfo, formats [
},
Spec: expectedSpec,
}
controllerutil.SetControllerReference(info.host, &preprovImage, r.Scheme())
err = controllerutil.SetControllerReference(info.host, &preprovImage, r.Scheme())
if err != nil {
return nil, errors.Wrap(err, "failed to set controller reference for PreprovisioningImage")
}

err = r.Create(info.ctx, &preprovImage)
return nil, err
}
Expand Down Expand Up @@ -1459,7 +1461,7 @@ func saveHostProvisioningSettings(host *metal3api.BareMetalHost, info *reconcile
// Root device hints may change as a result of RAID
dirty, err = updateRootDeviceHints(host, info)
if err != nil {
return
return dirty, err
}

// Copy RAID settings
Expand Down Expand Up @@ -1494,7 +1496,7 @@ func saveHostProvisioningSettings(host *metal3api.BareMetalHost, info *reconcile
dirty = true
}

return
return dirty, nil
}

func (r *BareMetalHostReconciler) createHostFirmwareSettings(info *reconcileInfo) error {
Expand Down
39 changes: 21 additions & 18 deletions controllers/metal3.io/baremetalhost_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import (
"fmt"
"testing"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/fixture"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/utils"
promutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"

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

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"

promutil "github.com/prometheus/client_golang/prometheus/testutil"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/fixture"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/utils"
)

const (
Expand Down Expand Up @@ -142,7 +138,7 @@ func newTestReconcilerWithFixture(fix *fixture.Fixture, initObjs ...runtime.Obje
c := clientBuilder.Build()
// Add a default secret that can be used by most hosts.
bmcSecret := newBMCCredsSecret(defaultSecretName, "User", "Pass")
c.Create(context.TODO(), bmcSecret)
_ = c.Create(context.TODO(), bmcSecret)

return &BareMetalHostReconciler{
Client: c,
Expand Down Expand Up @@ -551,15 +547,17 @@ func TestDoNotAddSecretFinalizersDuringDelete(t *testing.T) {
// The fake client will immediately remove the host
// from its cache, so let's keep the latest updated
// host
r.Get(context.TODO(), request.NamespacedName, host)
err = r.Get(context.TODO(), request.NamespacedName, host)
assert.NoError(t, err)
_, err = r.Reconcile(context.Background(), request)
assert.NoError(t, err)

// To simulate an immediate reconciliation loop due the
// secret update (and a slow host deletion), let's push
// back the host in the client cache.
host.ResourceVersion = ""
r.Client.Create(context.TODO(), host)
err = r.Client.Create(context.TODO(), host)
assert.NoError(t, err)
previousSecret := getHostSecret(t, r, host)
_, err = r.Reconcile(context.Background(), request)
assert.NoError(t, err)
Expand Down Expand Up @@ -776,7 +774,8 @@ func TestRebootWithSuffixedAnnotation(t *testing.T) {
)

delete(host.Annotations, annotation)
r.Update(context.TODO(), host)
err := r.Update(context.TODO(), host)
assert.NoError(t, err)

tryReconcile(t, r, host,
func(host *metal3api.BareMetalHost, result reconcile.Result) bool {
Expand Down Expand Up @@ -2683,7 +2682,8 @@ func TestHostFirmwareSettings(t *testing.T) {
i.request = newRequest(host)

hfs := newHostFirmwareSettings(host, tc.Conditions)
r.Create(context.TODO(), hfs)
err := r.Create(context.TODO(), hfs)
assert.NoError(t, err)

dirty, _, err := r.getHostFirmwareSettings(i)
if err != nil {
Expand Down Expand Up @@ -2751,7 +2751,8 @@ func TestHFSTransitionToPreparing(t *testing.T) {
},
}

r.Update(context.TODO(), hfs)
err := r.Update(context.TODO(), hfs)
assert.NoError(t, err)

waitForProvisioningState(t, r, host, metal3api.StatePreparing)
}
Expand Down Expand Up @@ -2783,7 +2784,8 @@ func TestHFSEmptyStatusSettings(t *testing.T) {
},
}

r.Update(context.TODO(), hfs)
err := r.Update(context.TODO(), hfs)
assert.NoError(t, err)

tryReconcile(t, r, host,
func(host *metal3api.BareMetalHost, result reconcile.Result) bool {
Expand All @@ -2799,7 +2801,8 @@ func TestHFSEmptyStatusSettings(t *testing.T) {
},
}

r.Update(context.TODO(), hfs)
err = r.Update(context.TODO(), hfs)
assert.NoError(t, err)
tryReconcile(t, r, host,
func(host *metal3api.BareMetalHost, result reconcile.Result) bool {
return host.Status.Provisioning.State == metal3api.StateAvailable
Expand Down
10 changes: 4 additions & 6 deletions controllers/metal3.io/bmceventsubscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"time"

"github.com/go-logr/logr"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/metal3-io/baremetal-operator/pkg/utils"
"github.com/pkg/errors"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -34,10 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/metal3-io/baremetal-operator/pkg/utils"
)

const (
Expand Down Expand Up @@ -125,7 +123,7 @@ func (r *BMCEventSubscriptionReconciler) Reconcile(ctx context.Context, request
return ctrl.Result{}, nil
}

return
return result, nil
}

func (r *BMCEventSubscriptionReconciler) handleError(ctx context.Context, subscription *metal3api.BMCEventSubscription, e error, message string, requeue bool) (ctrl.Result, error) {
Expand Down
6 changes: 3 additions & 3 deletions controllers/metal3.io/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package controllers

import (
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"
logf "sigs.k8s.io/controller-runtime/pkg/log"
logz "sigs.k8s.io/controller-runtime/pkg/log/zap"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
)

func init() {
logf.SetLogger(logz.New(logz.UseDevMode(true)))
// Register our package types with the global scheme
metal3api.AddToScheme(scheme.Scheme)
err := metal3api.AddToScheme(scheme.Scheme)
logf.Log.Error(err, "Cannot Add scheme into metal3api")
}
11 changes: 4 additions & 7 deletions controllers/metal3.io/demo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
goctx "context"
"testing"

"sigs.k8s.io/controller-runtime/pkg/client"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/demo"
"k8s.io/apimachinery/pkg/runtime"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/demo"
)

func newDemoReconciler(initObjs ...runtime.Object) *BareMetalHostReconciler {
Expand All @@ -25,7 +22,7 @@ func newDemoReconciler(initObjs ...runtime.Object) *BareMetalHostReconciler {

// Add a default secret that can be used by most hosts.
bmcSecret := newSecret(defaultSecretName, map[string]string{"username": "User", "password": "Pass"})
c.Create(goctx.TODO(), bmcSecret)
_ = c.Create(goctx.TODO(), bmcSecret)

return &BareMetalHostReconciler{
Client: c,
Expand Down
Loading

0 comments on commit 02af350

Please sign in to comment.