Skip to content

Address various linter (staticcheck, modernize, etc.) comments. #517

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,31 @@ func NewResizeController(
return ctrl
}

func (ctrl *resizeController) addPVC(obj interface{}) {
func (ctrl *resizeController) addPVC(obj any) {
objKey, err := util.GetObjectKey(obj)
if err != nil {
return
}
ctrl.claimQueue.Add(objKey)
}

func (ctrl *resizeController) addPod(obj interface{}) {
func (ctrl *resizeController) addPod(obj any) {
pod := parsePod(obj)
if pod == nil {
return
}
ctrl.usedPVCs.addPod(pod)
}

func (ctrl *resizeController) deletePod(obj interface{}) {
func (ctrl *resizeController) deletePod(obj any) {
pod := parsePod(obj)
if pod == nil {
return
}
ctrl.usedPVCs.removePod(pod)
}

func (ctrl *resizeController) updatePod(oldObj, newObj interface{}) {
func (ctrl *resizeController) updatePod(oldObj, newObj any) {
pod := parsePod(newObj)
if pod == nil {
return
Expand All @@ -182,7 +182,7 @@ func (ctrl *resizeController) updatePod(oldObj, newObj interface{}) {
}
}

func (ctrl *resizeController) updatePVC(oldObj, newObj interface{}) {
func (ctrl *resizeController) updatePVC(oldObj, newObj any) {
oldPVC, ok := oldObj.(*v1.PersistentVolumeClaim)
if !ok || oldPVC == nil {
return
Expand Down Expand Up @@ -261,7 +261,7 @@ func (ctrl *resizeController) updatePVC(oldObj, newObj interface{}) {
}
}

func (ctrl *resizeController) deletePVC(obj interface{}) {
func (ctrl *resizeController) deletePVC(obj any) {
objKey, err := util.GetObjectKey(obj)
if err != nil {
return
Expand Down Expand Up @@ -291,7 +291,7 @@ func (ctrl *resizeController) Run(workers int, ctx context.Context) {
go ctrl.slowSet.Run(stopCh)
}

for i := 0; i < workers; i++ {
for range workers {
go wait.Until(ctrl.syncPVCs, 0, stopCh)
}

Expand Down Expand Up @@ -566,7 +566,7 @@ func (ctrl *resizeController) markPVCResizeInProgress(pvc *v1.PersistentVolumeCl

updatedPVC, err := util.PatchClaim(ctrl.kubeClient, pvc, newPVC, true /* addResourceVersionCheck */)
if err != nil {
return updatedPVC, fmt.Errorf("Mark PVC %q as resize as in progress failed: %v", klog.KObj(pvc), err)
return updatedPVC, fmt.Errorf("mark PVC %q as resize as in progress failed: %v", klog.KObj(pvc), err)
}
err = ctrl.claims.Update(updatedPVC)
if err != nil {
Expand All @@ -584,7 +584,7 @@ func (ctrl *resizeController) markPVCResizeFinished(

updatedPVC, err := util.PatchClaim(ctrl.kubeClient, pvc, newPVC, true /* addResourceVersionCheck */)
if err != nil {
return fmt.Errorf("Mark PVC %q as resize finished failed: %w", klog.KObj(pvc), err)
return fmt.Errorf("mark PVC %q as resize finished failed: %w", klog.KObj(pvc), err)
}

err = ctrl.claims.Update(updatedPVC)
Expand Down Expand Up @@ -636,7 +636,7 @@ func (ctrl *resizeController) updatePVCapacity(
return updatedPV, nil
}

func parsePod(obj interface{}) *v1.Pod {
func parsePod(obj any) *v1.Pod {
if obj == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/csi/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *MockClient) Expand(
additionalInfoVal := additionalInfo.(connection.AdditionalInfo)
migrated := additionalInfoVal.Migrated
if migrated != "true" {
err := fmt.Errorf("Expected value of migrated label: true, Actual value: %s", migrated)
err := fmt.Errorf("migrated label expected value: true, actual value: %s", migrated)
return requestBytes, c.supportsNodeResize, err
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/modifier/csi_modifier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modifier

import (
"errors"
"testing"

"github.com/kubernetes-csi/external-resizer/pkg/csi"
Expand All @@ -10,10 +9,6 @@ import (
"k8s.io/client-go/kubernetes/fake"
)

var (
controllerServiceNotSupportErr = errors.New("CSI driver does not support controller service")
)

func TestNewModifier(t *testing.T) {
for i, c := range []struct {
SupportsControllerModify bool
Expand Down
10 changes: 5 additions & 5 deletions pkg/modifycontroller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ func (ctrl *modifyController) initUncertainPVCs() error {
return nil
}

func (ctrl *modifyController) addPVC(obj interface{}) {
func (ctrl *modifyController) addPVC(obj any) {
objKey, err := util.GetObjectKey(obj)
if err != nil {
return
}
ctrl.claimQueue.Add(objKey)
}

func (ctrl *modifyController) updatePVC(oldObj, newObj interface{}) {
func (ctrl *modifyController) updatePVC(oldObj, newObj any) {
oldPVC, ok := oldObj.(*v1.PersistentVolumeClaim)
if !ok || oldPVC == nil {
return
Expand Down Expand Up @@ -178,7 +178,7 @@ func (ctrl *modifyController) updatePVC(oldObj, newObj interface{}) {
}
}

func (ctrl *modifyController) deletePVC(obj interface{}) {
func (ctrl *modifyController) deletePVC(obj any) {
objKey, err := util.GetObjectKey(obj)
if err != nil {
return
Expand Down Expand Up @@ -220,7 +220,7 @@ func (ctrl *modifyController) Run(
// Starts go-routine that deletes expired slowSet entries.
go ctrl.slowSet.Run(stopCh)

for i := 0; i < workers; i++ {
for range workers {
go wait.Until(ctrl.sync, 0, stopCh)
}

Expand Down Expand Up @@ -265,7 +265,7 @@ func (ctrl *modifyController) syncPVC(key string) error {

pv, err := ctrl.pvLister.Get(pvc.Spec.VolumeName)
if err != nil {
return fmt.Errorf("Get PV %q of pvc %q in PVInformer cache failed: %v", pvc.Spec.VolumeName, klog.KObj(pvc), err)
return fmt.Errorf("get pv %q of pvc %q in PVInformer cache failed: %v", pvc.Spec.VolumeName, klog.KObj(pvc), err)
}

// Only trigger modify volume if the following conditions are met
Expand Down
2 changes: 1 addition & 1 deletion pkg/modifycontroller/modify_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (ctrl *modifyController) markControllerModifyVolumeStatus(
case v1.PersistentVolumeClaimModifyVolumeInProgress:
conditionMessage = "ModifyVolume operation in progress."
case v1.PersistentVolumeClaimModifyVolumeInfeasible:
conditionMessage = "ModifyVolume failed with error" + err.Error() + ". Waiting for retry."
conditionMessage = "ModifyVolume failed with error: " + err.Error() + ". Waiting for retry."
}
pvcCondition.Message = conditionMessage
// Do not change conditions for pending modifications and keep existing conditions
Expand Down
31 changes: 14 additions & 17 deletions pkg/modifycontroller/modify_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package modifycontroller

import (
"context"
"reflect"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -37,7 +37,6 @@ var (
targetVac = "target-vac"
testDriverName = "mock"
infeasibleErr = status.Errorf(codes.InvalidArgument, "Parameters in VolumeAttributesClass is invalid")
finalErr = status.Errorf(codes.Internal, "Final error")
pvcConditionInProgress = v1.PersistentVolumeClaimCondition{
Type: v1.PersistentVolumeClaimVolumeModifyingVolume,
Status: v1.ConditionTrue,
Expand All @@ -47,7 +46,7 @@ var (
pvcConditionInfeasible = v1.PersistentVolumeClaimCondition{
Type: v1.PersistentVolumeClaimVolumeModifyingVolume,
Status: v1.ConditionTrue,
Message: "ModifyVolume failed with errorrpc error: code = InvalidArgument desc = Parameters in VolumeAttributesClass is invalid. Waiting for retry.",
Message: "ModifyVolume failed with error: rpc error: code = InvalidArgument desc = Parameters in VolumeAttributesClass is invalid. Waiting for retry.",
}

pvcConditionError = v1.PersistentVolumeClaimCondition{
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestMarkControllerModifyVolumeStatus(t *testing.T) {
pvc: basePVC.Get(),
expectedPVC: basePVC.WithModifyVolumeStatus(v1.PersistentVolumeClaimModifyVolumeInfeasible).Get(),
expectedConditions: []v1.PersistentVolumeClaimCondition{pvcConditionInfeasible},
expectedErr: infeasibleErr,
expectedErr: nil,
testFunc: func(pvc *v1.PersistentVolumeClaim, ctrl *modifyController) (*v1.PersistentVolumeClaim, error) {
return ctrl.markControllerModifyVolumeStatus(pvc, v1.PersistentVolumeClaimModifyVolumeInfeasible, infeasibleErr)
},
Expand Down Expand Up @@ -126,14 +125,14 @@ func TestMarkControllerModifyVolumeStatus(t *testing.T) {
ctrlInstance, _ := controller.(*modifyController)

pvc, err = tc.testFunc(pvc, ctrlInstance)
if err != nil && !reflect.DeepEqual(tc.expectedErr, err) {
t.Errorf("Expected error to be %v but got %v", tc.expectedErr, err)
if !errors.Is(err, tc.expectedErr) {
t.Errorf("expected error: %v, got: %v", tc.expectedErr, err)
}

realStatus := pvc.Status.ModifyVolumeStatus.Status
expectedStatus := tc.expectedPVC.Status.ModifyVolumeStatus.Status
if !reflect.DeepEqual(realStatus, expectedStatus) {
t.Errorf("expected modify volume status %+v got %+v", expectedStatus, realStatus)
if diff := cmp.Diff(expectedStatus, realStatus); diff != "" {
t.Errorf("unexpected modify volume status (-want +got):\n%s", diff)
}

realConditions := pvc.Status.Conditions
Expand Down Expand Up @@ -167,8 +166,6 @@ func TestUpdateConditionBasedOnError(t *testing.T) {
client := csi.NewMockClient("foo", true, true, true, true, true, false)
driverName, _ := client.GetDriverName(context.TODO())

pvc := test.pvc

var initialObjects []runtime.Object
initialObjects = append(initialObjects, test.pvc)

Expand All @@ -185,9 +182,9 @@ func TestUpdateConditionBasedOnError(t *testing.T) {

ctrlInstance, _ := controller.(*modifyController)

pvc, err = ctrlInstance.updateConditionBasedOnError(tc.pvc, err)
if err != nil && !reflect.DeepEqual(tc.expectedErr, err) {
t.Errorf("Expected error to be %v but got %v", tc.expectedErr, err)
pvc, err := ctrlInstance.updateConditionBasedOnError(tc.pvc, err)
if !errors.Is(err, tc.expectedErr) {
t.Errorf("expected error: %v, got: %v", tc.expectedErr, err)
}

if !testutil.CompareConditions(pvc.Status.Conditions, tc.expectedConditions) {
Expand Down Expand Up @@ -254,20 +251,20 @@ func TestMarkControllerModifyVolumeCompleted(t *testing.T) {
ctrlInstance, _ := controller.(*modifyController)

actualPVC, pv, err := ctrlInstance.markControllerModifyVolumeCompleted(tc.pvc, tc.pv)
if err != nil && !reflect.DeepEqual(tc.expectedErr, err) {
t.Errorf("Expected error to be %v but got %v", tc.expectedErr, err)
if !errors.Is(err, tc.expectedErr) {
t.Errorf("expected error: %v, got: %v", tc.expectedErr, err)
}

if len(actualPVC.Status.Conditions) == 0 {
actualPVC.Status.Conditions = []v1.PersistentVolumeClaimCondition{}
}

if diff := cmp.Diff(tc.expectedPVC, actualPVC); diff != "" {
t.Errorf("expected pvc %+v got %+v, diff is: %v", tc.expectedPVC, actualPVC, diff)
t.Errorf("unexpected pvc (-want +got):\n%s", diff)
}

if diff := cmp.Diff(tc.expectedPV, pv); diff != "" {
t.Errorf("expected pvc %+v got %+v, diff is: %v", tc.expectedPV, pv, diff)
t.Errorf("unexpected pv (-want +got):\n%s", diff)
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/resizer/csi_resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (
)

var (
controllerServiceNotSupportErr = errors.New("CSI driver does not support controller service")
resizeNotSupportErr = errors.New("CSI driver neither supports controller resize nor node resize")
errControllerServiceNotSupport = errors.New("CSI driver does not support controller service")
errResizeNotSupport = errors.New("CSI driver neither supports controller resize nor node resize")
)

func NewResizerFromClient(
Expand All @@ -53,7 +53,7 @@ func NewResizerFromClient(
}

if !supportControllerService {
return nil, controllerServiceNotSupportErr
return nil, errControllerServiceNotSupport
}

supportControllerResize, err := supportsControllerResize(csiClient, timeout)
Expand All @@ -70,7 +70,7 @@ func NewResizerFromClient(
klog.InfoS("The CSI driver supports node resize only, using trivial resizer to handle resize requests")
return newTrivialResizer(driverName), nil
}
return nil, resizeNotSupportErr
return nil, errResizeNotSupport
}

_, err = supportsControllerSingleNodeMultiWriter(csiClient, timeout)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resizer/csi_resizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNewResizer(t *testing.T) {
SupportsPluginControllerService: false,
SupportsControllerSingleNodeMultiWriter: true,

Error: controllerServiceNotSupportErr,
Error: errControllerServiceNotSupport,
},
// Controller modify not supported.
{
Expand All @@ -69,7 +69,7 @@ func TestNewResizer(t *testing.T) {
SupportsPluginControllerService: true,
SupportsControllerSingleNodeMultiWriter: true,

Error: resizeNotSupportErr,
Error: errResizeNotSupport,
},
} {
client := csi.NewMockClient("mock", c.SupportsNodeResize, c.SupportsControllerResize, false, c.SupportsPluginControllerService, c.SupportsControllerSingleNodeMultiWriter, false)
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func GetPVCPatchData(oldPVC, newPVC *v1.PersistentVolumeClaim, addResourceVersio
}

func addResourceVersion(patchBytes []byte, resourceVersion string) ([]byte, error) {
var patchMap map[string]interface{}
var patchMap map[string]any
err := json.Unmarshal(patchBytes, &patchMap)
if err != nil {
return nil, fmt.Errorf("error unmarshalling patch with %v", err)
Expand All @@ -157,7 +157,7 @@ func addResourceVersion(patchBytes []byte, resourceVersion string) ([]byte, erro
return versionBytes, nil
}

func GetPatchData(oldObj, newObj interface{}) ([]byte, error) {
func GetPatchData(oldObj, newObj any) ([]byte, error) {
oldData, err := json.Marshal(oldObj)
if err != nil {
return nil, fmt.Errorf("marshal old object failed: %v", err)
Expand Down Expand Up @@ -195,7 +195,7 @@ func SanitizeName(name string) string {
return name
}

func GetObjectKey(obj interface{}) (string, error) {
func GetObjectKey(obj any) (string, error) {
if unknown, ok := obj.(cache.DeletedFinalStateUnknown); ok && unknown.Obj != nil {
obj = unknown.Obj
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func TestGetPVCPatchData(t *testing.T) {
t.Errorf("Case %d: Get patch data failed: %v", i, err)
}

var patchMap map[string]interface{}
var patchMap map[string]any
err = json.Unmarshal(patchBytes, &patchMap)
if err != nil {
t.Errorf("Case %d: unmarshalling json patch failed: %v", i, err)
}

metadata, exist := patchMap["metadata"].(map[string]interface{})
metadata, exist := patchMap["metadata"].(map[string]any)
if !exist {
t.Errorf("Case %d: ResourceVersion should exist in patch data", i)
}
Expand Down