Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linting issue #185

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ run:
tests: true
skip-dirs-use-default: true
modules-download-mode: readonly
skip-dirs:
- go/pkg # remove third party mod lib from scanning
- go/src # remove third party mod lib from scanning
- hostedtoolcache # remove the mod caches from scanning

issues:
max-issues-per-linter: 0
Expand Down
13 changes: 8 additions & 5 deletions cmd/podmon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"context"
"flag"
"fmt"
"podmon/internal/csiapi"
"podmon/internal/k8sapi"
"podmon/internal/monitor"
"strconv"
"strings"
"sync"
"time"

"podmon/internal/csiapi"
"podmon/internal/k8sapi"
"podmon/internal/monitor"

csiext "github.com/dell/dell-csi-extensions/podmon"
"github.com/fsnotify/fsnotify"
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
Expand Down Expand Up @@ -89,8 +90,10 @@ var ArrayConnMonitorFc = monitor.PodMonitor.ArrayConnectivityMonitor
var PodMonWait = podMonWait

// GetCSIClient is reference to a function that returns a new CSIClient
var GetCSIClient = csiapi.NewCSIClient
var createArgsOnce sync.Once
var (
GetCSIClient = csiapi.NewCSIClient
createArgsOnce sync.Once
)

func main() {
log.SetFormatter(&log.TextFormatter{
Expand Down
28 changes: 15 additions & 13 deletions cmd/podmon/main_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"context"
"fmt"
"os"
"podmon/internal/csiapi"
"podmon/internal/k8sapi"
"podmon/internal/monitor"
"strings"
"sync"
"time"

"podmon/internal/csiapi"
"podmon/internal/k8sapi"
"podmon/internal/monitor"

"github.com/cucumber/godog"
"github.com/dell/gofsutil"
logtest "github.com/sirupsen/logrus/hooks/test"
Expand All @@ -44,8 +45,10 @@ type mainFeature struct {
failStartAPIMonitor bool
}

var saveOriginalArgs sync.Once
var originalArgs []string
var (
saveOriginalArgs sync.Once
originalArgs []string
)

func (m *mainFeature) aPodmonInstance() error {
if m.loghook == nil {
Expand Down Expand Up @@ -84,21 +87,20 @@ func (le *mockLeaderElect) Run() error {
return nil
}

func (le *mockLeaderElect) WithNamespace(namespace string) {

func (le *mockLeaderElect) WithNamespace(_ string) {
}

func (m *mainFeature) mockGetCSIClient(csiSock string, clientOpts ...grpc.DialOption) (csiapi.CSIApi, error) {
func (m *mainFeature) mockGetCSIClient(_ string, _ ...grpc.DialOption) (csiapi.CSIApi, error) {
return m.csiapiMock, nil
}

func (m *mainFeature) mockStartPodMonitor(api k8sapi.K8sAPI, client kubernetes.Interface, labelKey, labelValue string, duration time.Duration) {
func (m *mainFeature) mockStartPodMonitor(_ k8sapi.K8sAPI, _ kubernetes.Interface, _, _ string, _ time.Duration) {
}

func (m *mainFeature) mockStartNodeMonitor(api k8sapi.K8sAPI, client kubernetes.Interface, labelKey, labelValue string, duration time.Duration) {
func (m *mainFeature) mockStartNodeMonitor(_ k8sapi.K8sAPI, _ kubernetes.Interface, _, _ string, _ time.Duration) {
}

func (m *mainFeature) mockStartAPIMonitor(api k8sapi.K8sAPI, first, retry, interval time.Duration, waiter func(interval time.Duration) bool) error {
func (m *mainFeature) mockStartAPIMonitor(_ k8sapi.K8sAPI, _, _, _ time.Duration, _ func(interval time.Duration) bool) error {
if m.failStartAPIMonitor {
return fmt.Errorf("induced StorageAPIMonitor failure")
}
Expand All @@ -109,7 +111,7 @@ func (m *mainFeature) mockPodMonWait() bool {
return true
}

func (m *mainFeature) mockLeaderElection(runFunc func(ctx context.Context)) leaderElection {
func (m *mainFeature) mockLeaderElection(_ func(ctx context.Context)) leaderElection {
return m.leaderElect
}

Expand Down Expand Up @@ -142,7 +144,7 @@ func (m *mainFeature) theLastLogMessageContains(errormsg string) error {
}

func (m *mainFeature) csiExtensionsPresentIsFalse(expectedStr string) error {
var expected = strings.ToLower(expectedStr) == "true"
expected := strings.ToLower(expectedStr) == "true"
return monitor.AssertExpectedAndActual(assert.Equal, expected, monitor.PodMonitor.CSIExtensionsPresent,
fmt.Sprintf("Expected CSIExtensionsPresent flag to be %s, but was %v",
expectedStr, monitor.PodMonitor.CSIExtensionsPresent))
Expand Down
6 changes: 3 additions & 3 deletions internal/criapi/criapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var CRIMaxConnectionRetry = 3
var CRINewClientTimeout = 90 * time.Second

// NewCRIClient returns a new client connection to the ContainerRuntimeInterface or an error
func NewCRIClient(criSock string, clientOpts ...grpc.DialOption) (*Client, error) {
func NewCRIClient(criSock string, _ ...grpc.DialOption) (*Client, error) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), CRINewClientTimeout)
defer cancel()
Expand Down Expand Up @@ -93,7 +93,7 @@ func (cri *Client) ListContainers(ctx context.Context, req *v1alpha2.ListContain
return CRIClient.RuntimeServiceClient.ListContainers(ctx, req)
}

var knownPaths [3]string = [3]string{"/var/run/dockershim.sock", "/run/containerd/containerd.sock", "/run/crio/crio.sock"}
var knownPaths = [3]string{"/var/run/dockershim.sock", "/run/containerd/containerd.sock", "/run/crio/crio.sock"}

// ChooseCRIPath chooses an appropriate unix domain socket path to the CRI interface.
// This is done according to the ordering described for the crictl command.
Expand All @@ -110,7 +110,7 @@ func (cri *Client) ChooseCRIPath() (string, error) {

// GetContainerInfo gets current status of all the containers on this server using CRI interface.
// The result is a map of ID to a structure containing the ID, Name, and State.
func (cri *Client) GetContainerInfo(ctx context.Context) (map[string]*ContainerInfo, error) {
func (cri *Client) GetContainerInfo(_ context.Context) (map[string]*ContainerInfo, error) {
result := make(map[string]*ContainerInfo)

path, err := cri.ChooseCRIPath()
Expand Down
3 changes: 2 additions & 1 deletion internal/criapi/criapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ package criapi
import (
"context"
"fmt"
"k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"testing"

"k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

func TestListContainers(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/criapi/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (mock *MockClient) Close() error {
}

// ListContainers would list individual containers but is not implemented for the mock client.
func (mock *MockClient) ListContainers(ctx context.Context, req *v1alpha2.ListContainersRequest) (*v1alpha2.ListContainersResponse, error) {
func (mock *MockClient) ListContainers(_ context.Context, _ *v1alpha2.ListContainersRequest) (*v1alpha2.ListContainersResponse, error) {
return nil, errors.New("unimplemented")
}

Expand All @@ -58,7 +58,7 @@ func (mock *MockClient) ChooseCRIPath() (string, error) {

// GetContainerInfo gets current status of all the containers on this server using CRI interface.
// The result is a map of ID to a structure containing the ID, Name, and State.
func (mock *MockClient) GetContainerInfo(ctx context.Context) (map[string]*ContainerInfo, error) {
func (mock *MockClient) GetContainerInfo(_ context.Context) (map[string]*ContainerInfo, error) {
if mock.InducedErrors.GetContainerInfo {
return mock.MockContainerInfos, errors.New("GetContainerInfo induced error")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/csiapi/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (mock *CSIMock) Close() error {
}

// ControllerUnpublishVolume is a mock implementation of csiapi.CSIApi.ControllerUnpublishVolume
func (mock *CSIMock) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
func (mock *CSIMock) ControllerUnpublishVolume(_ context.Context, _ *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
rep := &csi.ControllerUnpublishVolumeResponse{}
if mock.InducedErrors.ControllerUnpublishVolume {
return rep, errors.New("ControllerUnpublishedVolume induced error")
Expand All @@ -66,7 +66,7 @@ func (mock *CSIMock) ControllerUnpublishVolume(ctx context.Context, req *csi.Con
}

// NodeUnpublishVolume is a mock implementation of csiapi.CSIApi.NodeUnpublishVolume
func (mock *CSIMock) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
func (mock *CSIMock) NodeUnpublishVolume(_ context.Context, _ *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
rep := &csi.NodeUnpublishVolumeResponse{}
if mock.InducedErrors.NodeUnpublishVolume {
return rep, errors.New("NodeUnpublishedVolume induced error")
Expand All @@ -78,7 +78,7 @@ func (mock *CSIMock) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpub
}

// NodeUnstageVolume is a mock implementation of csiapi.CSIApi.NodeUnstageVolume
func (mock *CSIMock) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
func (mock *CSIMock) NodeUnstageVolume(_ context.Context, _ *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
rep := &csi.NodeUnstageVolumeResponse{}
if mock.InducedErrors.NodeUnstageVolume {
return rep, errors.New("NodeUnstageedVolume induced error")
Expand All @@ -90,7 +90,7 @@ func (mock *CSIMock) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstage
}

// ValidateVolumeHostConnectivity is a mock implementation of csiapi.CSIApi.ValidateVolumeHostConnectivity
func (mock *CSIMock) ValidateVolumeHostConnectivity(ctx context.Context, req *csiext.ValidateVolumeHostConnectivityRequest) (*csiext.ValidateVolumeHostConnectivityResponse, error) {
func (mock *CSIMock) ValidateVolumeHostConnectivity(_ context.Context, _ *csiext.ValidateVolumeHostConnectivityRequest) (*csiext.ValidateVolumeHostConnectivityResponse, error) {
rep := &csiext.ValidateVolumeHostConnectivityResponse{}
if mock.InducedErrors.ValidateVolumeHostConnectivity {
return rep, errors.New("ValidateVolumeHostConnectivity induced error")
Expand Down
4 changes: 2 additions & 2 deletions internal/k8sapi/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ type K8sAPI interface {
// SetupNodeWatch setups up a node watch.
SetupNodeWatch(ctx context.Context, listOptions metav1.ListOptions) (watch.Interface, error)

//TaintNode applies the specified 'taintKey' string and 'effect' to the node with 'nodeName'
//The 'remove' flag indicates if the taint should be removed from the node, if it exists.
// TaintNode applies the specified 'taintKey' string and 'effect' to the node with 'nodeName'
// The 'remove' flag indicates if the taint should be removed from the node, if it exists.
TaintNode(ctx context.Context, nodeName, taintKey string, effect v1.TaintEffect, remove bool) error

// CreateEvent creates an event on a runtime object.
Expand Down
26 changes: 13 additions & 13 deletions internal/k8sapi/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (mock *K8sMock) AddNode(node *v1.Node) {
}

// Connect connects to the Kubernetes system API
func (mock *K8sMock) Connect(kubeconfig *string) error {
func (mock *K8sMock) Connect(_ *string) error {
if mock.InducedErrors.Connect {
return errors.New("induced Connect error")
}
Expand All @@ -134,7 +134,7 @@ func (mock *K8sMock) GetContext(duration time.Duration) (context.Context, contex
}

// DeletePod deletes a pod of the given namespace and name, an optionally uses force deletion.
func (mock *K8sMock) DeletePod(ctx context.Context, namespace, name string, podUID types.UID, force bool) error {
func (mock *K8sMock) DeletePod(_ context.Context, namespace, name string, _ types.UID, _ bool) error {
if mock.InducedErrors.DeletePod {
return errors.New("induced DeletePod error")
}
Expand All @@ -144,7 +144,7 @@ func (mock *K8sMock) DeletePod(ctx context.Context, namespace, name string, podU
}

// GetPod retrieves a pod of the give namespace and name
func (mock *K8sMock) GetPod(ctx context.Context, namespace, name string) (*v1.Pod, error) {
func (mock *K8sMock) GetPod(_ context.Context, namespace, name string) (*v1.Pod, error) {
var pod *v1.Pod
if mock.InducedErrors.GetPod {
return pod, errors.New("induced GetPod error")
Expand All @@ -160,7 +160,7 @@ func (mock *K8sMock) GetPod(ctx context.Context, namespace, name string) (*v1.Po

// GetCachedVolumeAttachment will try to load the volumeattachment select by the persistent volume name and node name.
// If found it is returned from the cache. If not found, the cache is reloaded and the result returned from the reloaded data.
func (mock *K8sMock) GetCachedVolumeAttachment(ctx context.Context, pvName, nodeName string) (*storagev1.VolumeAttachment, error) {
func (mock *K8sMock) GetCachedVolumeAttachment(_ context.Context, pvName, nodeName string) (*storagev1.VolumeAttachment, error) {
valist := &storagev1.VolumeAttachmentList{}
if mock.InducedErrors.GetVolumeAttachments {
return nil, errors.New("induced GetVolumeAttachments error")
Expand All @@ -175,7 +175,7 @@ func (mock *K8sMock) GetCachedVolumeAttachment(ctx context.Context, pvName, node
}

// GetVolumeAttachments gets all the volume attachments in the K8S system
func (mock *K8sMock) GetVolumeAttachments(ctx context.Context) (*storagev1.VolumeAttachmentList, error) {
func (mock *K8sMock) GetVolumeAttachments(_ context.Context) (*storagev1.VolumeAttachmentList, error) {
valist := &storagev1.VolumeAttachmentList{}
if mock.InducedErrors.GetVolumeAttachments {
return valist, errors.New("induced GetVolumeAttachments error")
Expand All @@ -188,7 +188,7 @@ func (mock *K8sMock) GetVolumeAttachments(ctx context.Context) (*storagev1.Volum
}

// DeleteVolumeAttachment deletes a volume attachment by name.
func (mock *K8sMock) DeleteVolumeAttachment(ctx context.Context, va string) error {
func (mock *K8sMock) DeleteVolumeAttachment(_ context.Context, va string) error {
if mock.InducedErrors.DeleteVolumeAttachment {
return errors.New("induced DeleteVolumeAttachment error")
}
Expand All @@ -197,7 +197,7 @@ func (mock *K8sMock) DeleteVolumeAttachment(ctx context.Context, va string) erro
}

// GetPersistentVolumeClaimsInNamespace returns all the pvcs in a namespace.
func (mock *K8sMock) GetPersistentVolumeClaimsInNamespace(ctx context.Context, namespace string) (*v1.PersistentVolumeClaimList, error) {
func (mock *K8sMock) GetPersistentVolumeClaimsInNamespace(_ context.Context, _ string) (*v1.PersistentVolumeClaimList, error) {
pvclist := &v1.PersistentVolumeClaimList{}
if mock.InducedErrors.GetPersistentVolumeClaimsInNamespace {
return pvclist, errors.New("induced GetPersistentVolumeClaimsInNamespace error")
Expand Down Expand Up @@ -299,7 +299,7 @@ func (mock *K8sMock) GetPersistentVolumeClaimName(ctx context.Context, pvName st
}

// GetPersistentVolume retrieves a persistent volume given the pv name.
func (mock *K8sMock) GetPersistentVolume(ctx context.Context, pvName string) (*v1.PersistentVolume, error) {
func (mock *K8sMock) GetPersistentVolume(_ context.Context, pvName string) (*v1.PersistentVolume, error) {
var pv *v1.PersistentVolume
if mock.InducedErrors.GetPersistentVolume {
return pv, errors.New("induced GetPersistentVolume error")
Expand All @@ -309,7 +309,7 @@ func (mock *K8sMock) GetPersistentVolume(ctx context.Context, pvName string) (*v
}

// GetPersistentVolumeClaim returns the PVC of the given namespace/pvcName.
func (mock *K8sMock) GetPersistentVolumeClaim(ctx context.Context, namespace, pvcName string) (*v1.PersistentVolumeClaim, error) {
func (mock *K8sMock) GetPersistentVolumeClaim(_ context.Context, namespace, pvcName string) (*v1.PersistentVolumeClaim, error) {
var pvc *v1.PersistentVolumeClaim
if mock.InducedErrors.GetPersistentVolumeClaim {
return pvc, errors.New("induced GetPersistentVolumeClaim error")
Expand All @@ -320,7 +320,7 @@ func (mock *K8sMock) GetPersistentVolumeClaim(ctx context.Context, namespace, pv
}

// GetNode returns the node with the specified nodeName.
func (mock *K8sMock) GetNode(ctx context.Context, nodeName string) (*v1.Node, error) {
func (mock *K8sMock) GetNode(_ context.Context, nodeName string) (*v1.Node, error) {
var node *v1.Node
if mock.InducedErrors.GetNode {
return node, errors.New("induced GetNode error")
Expand Down Expand Up @@ -393,15 +393,15 @@ func (mock *K8sMock) getKey(namespace, name string) string {
}

// SetupPodWatch returns a mock watcher
func (mock *K8sMock) SetupPodWatch(ctx context.Context, namespace string, listOptions metav1.ListOptions) (watch.Interface, error) {
func (mock *K8sMock) SetupPodWatch(_ context.Context, _ string, _ metav1.ListOptions) (watch.Interface, error) {
if mock.InducedErrors.Watch {
return nil, errors.New("included Watch error")
}
return mock.Watcher, nil
}

// SetupNodeWatch returns a mock watcher
func (mock *K8sMock) SetupNodeWatch(ctx context.Context, listOptions metav1.ListOptions) (watch.Interface, error) {
func (mock *K8sMock) SetupNodeWatch(_ context.Context, _ metav1.ListOptions) (watch.Interface, error) {
if mock.InducedErrors.Watch {
return nil, errors.New("included Watch error")
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func (mock *K8sMock) TaintNode(ctx context.Context, nodeName, taintKey string, e
}

// CreateEvent creates an event for the specified object.
func (mock *K8sMock) CreateEvent(sourceComponent string, object runtime.Object, eventType, reason, messageFmt string, args ...interface{}) error {
func (mock *K8sMock) CreateEvent(_ string, _ runtime.Object, _, _, _ string, _ ...interface{}) error {
if mock.InducedErrors.CreateEvent {
return errors.New("induced CreateEvent error")
}
Expand Down
15 changes: 8 additions & 7 deletions internal/monitor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ type ControllerPodInfo struct { // information controller keeps on hand about a
PodAffinityLabels map[string]string // A list of pod affinity labels for the pod
}

const notFound = "not found"
const hostNameTopologyKey = "kubernetes.io/hostname"
const arrayIDVolumeAttribute = "arrayID"
const storageSystemVolumeAttribute = "StorageSystem"
const defaultArray = "default"
const (
notFound = "not found"
hostNameTopologyKey = "kubernetes.io/hostname"
arrayIDVolumeAttribute = "arrayID"
storageSystemVolumeAttribute = "StorageSystem"
defaultArray = "default"
)

// controllerModePodHandler handles controller mode functionality when a pod event happens
func (cm *PodMonitorType) controllerModePodHandler(pod *v1.Pod, eventType watch.EventType) error {
Expand Down Expand Up @@ -408,7 +410,6 @@ func (cm *PodMonitorType) podToArrayIDs(ctx context.Context, pod *v1.Pod) ([]str
// If connectivity is lost, will initiate cleanup of the pods.
// This is a never ending function, intended to be called as Go routine.
func (cm *PodMonitorType) ArrayConnectivityMonitor() {

// Loop through all the monitored Pods making sure they still have array access
for {
podKeysToClean := make([]string, 0)
Expand Down Expand Up @@ -703,7 +704,7 @@ func (cm *PodMonitorType) controllerModeDriverPodHandler(pod *v1.Pod, eventType
} else {
hasTaint := nodeHasTaint(node, PodmonDriverPodTaintKey, v1.TaintEffectNoSchedule)
log.Infof("Removing taint from node %s with %s", node.ObjectMeta.Name, PodmonDriverPodTaintKey)
//remove taint
// remove taint
if hasTaint {
err := taintNode(node.ObjectMeta.Name, PodmonDriverPodTaintKey, true)
if err != nil {
Expand Down
Loading
Loading