Skip to content

Commit 4ece0e9

Browse files
authored
Merge pull request #1049 from nixpanic/golang/slices
replace custom ContainsString() with standard slices.Contains()
2 parents b19b28b + a1868b5 commit 4ece0e9

File tree

6 files changed

+26
-38
lines changed

6 files changed

+26
-38
lines changed

pkg/common-controller/framework_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424
"reflect"
2525
sysruntime "runtime"
26+
"slices"
2627
"strconv"
2728
"strings"
2829
"sync"
@@ -1496,7 +1497,7 @@ func evaluateFinalizerTests(ctrl *csiSnapshotCommonController, reactor *snapshot
14961497
if funcName == "testAddPVCFinalizer" {
14971498
for _, pvc := range reactor.claims {
14981499
if test.initialClaims[0].Name == pvc.Name {
1499-
if !utils.ContainsString(test.initialClaims[0].ObjectMeta.Finalizers, utils.PVCFinalizer) && utils.ContainsString(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
1500+
if !slices.Contains(test.initialClaims[0].ObjectMeta.Finalizers, utils.PVCFinalizer) && slices.Contains(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
15001501
klog.V(4).Infof("test %q succeeded. PVCFinalizer is added to PVC %s", test.name, pvc.Name)
15011502
bHasPVCFinalizer = true
15021503
}
@@ -1511,7 +1512,7 @@ func evaluateFinalizerTests(ctrl *csiSnapshotCommonController, reactor *snapshot
15111512
if funcName == "testRemovePVCFinalizer" {
15121513
for _, pvc := range reactor.claims {
15131514
if test.initialClaims[0].Name == pvc.Name {
1514-
if utils.ContainsString(test.initialClaims[0].ObjectMeta.Finalizers, utils.PVCFinalizer) && !utils.ContainsString(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
1515+
if slices.Contains(test.initialClaims[0].ObjectMeta.Finalizers, utils.PVCFinalizer) && !slices.Contains(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
15151516
klog.V(4).Infof("test %q succeeded. PVCFinalizer is removed from PVC %s", test.name, pvc.Name)
15161517
bHasPVCFinalizer = false
15171518
}
@@ -1526,10 +1527,10 @@ func evaluateFinalizerTests(ctrl *csiSnapshotCommonController, reactor *snapshot
15261527
if funcName == "testAddSnapshotFinalizer" {
15271528
for _, snapshot := range reactor.snapshots {
15281529
if test.initialSnapshots[0].Name == snapshot.Name {
1529-
if !utils.ContainsString(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1530-
utils.ContainsString(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1531-
!utils.ContainsString(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) &&
1532-
utils.ContainsString(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) {
1530+
if !slices.Contains(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1531+
slices.Contains(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1532+
!slices.Contains(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) &&
1533+
slices.Contains(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) {
15331534
klog.V(4).Infof("test %q succeeded. Finalizers are added to snapshot %s", test.name, snapshot.Name)
15341535
bHasSnapshotFinalizer = true
15351536
}
@@ -1544,10 +1545,10 @@ func evaluateFinalizerTests(ctrl *csiSnapshotCommonController, reactor *snapshot
15441545
if funcName == "testRemoveSnapshotFinalizer" {
15451546
for _, snapshot := range reactor.snapshots {
15461547
if test.initialSnapshots[0].Name == snapshot.Name {
1547-
if utils.ContainsString(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1548-
!utils.ContainsString(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1549-
utils.ContainsString(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) &&
1550-
!utils.ContainsString(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) {
1548+
if slices.Contains(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1549+
!slices.Contains(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) &&
1550+
slices.Contains(test.initialSnapshots[0].ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) &&
1551+
!slices.Contains(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) {
15511552

15521553
klog.V(4).Infof("test %q succeeded. SnapshotFinalizer is removed from Snapshot %s", test.name, snapshot.Name)
15531554
bHasSnapshotFinalizer = false

pkg/common-controller/snapshot_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package common_controller
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324
"time"
2425

@@ -921,7 +922,7 @@ func (ctrl *csiSnapshotCommonController) ensurePVCFinalizer(snapshot *crdv1.Volu
921922
return newControllerUpdateError(snapshot.Name, "cannot get claim from snapshot")
922923
}
923924

924-
if utils.ContainsString(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
925+
if slices.Contains(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
925926
klog.Infof("Protection finalizer already exists for persistent volume claim %s/%s", pvc.Namespace, pvc.Name)
926927
return nil
927928
}
@@ -1012,7 +1013,7 @@ func (ctrl *csiSnapshotCommonController) checkandRemovePVCFinalizer(snapshot *cr
10121013
klog.V(5).Infof("checkandRemovePVCFinalizer for snapshot [%s]: snapshot status [%#v]", snapshot.Name, snapshot.Status)
10131014

10141015
// Check if there is a Finalizer on PVC to be removed
1015-
if utils.ContainsString(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
1016+
if slices.Contains(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer) {
10161017
// There is a Finalizer on PVC. Check if PVC is used
10171018
// and remove finalizer if it's not used.
10181019
inUse := ctrl.isPVCBeingUsed(pvc, snapshot, skipCurrentSnapshot)

pkg/sidecar-controller/groupsnapshot_helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"crypto/sha256"
2222
"fmt"
23+
"slices"
2324
"strings"
2425
"time"
2526

@@ -202,7 +203,7 @@ func (ctrl *csiSnapshotSideCarController) syncGroupSnapshotContent(groupSnapshot
202203
// removeGroupSnapshotContentFinalizer removes the VolumeGroupSnapshotContentFinalizer from a
203204
// group snapshot content if there exists one.
204205
func (ctrl csiSnapshotSideCarController) removeGroupSnapshotContentFinalizer(groupSnapshotContent *crdv1alpha1.VolumeGroupSnapshotContent) error {
205-
if !utils.ContainsString(groupSnapshotContent.ObjectMeta.Finalizers, utils.VolumeGroupSnapshotContentFinalizer) {
206+
if !slices.Contains(groupSnapshotContent.ObjectMeta.Finalizers, utils.VolumeGroupSnapshotContentFinalizer) {
206207
// the finalizer does not exit, return directly
207208
return nil
208209
}

pkg/sidecar-controller/snapshot_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package sidecar_controller
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324
"time"
2425

@@ -565,7 +566,7 @@ func (ctrl *csiSnapshotSideCarController) GetCredentialsFromAnnotation(content *
565566
// removeContentFinalizer removes the VolumeSnapshotContentFinalizer from a
566567
// content if there exists one.
567568
func (ctrl csiSnapshotSideCarController) removeContentFinalizer(content *crdv1.VolumeSnapshotContent) error {
568-
if !utils.ContainsString(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer) {
569+
if !slices.Contains(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer) {
569570
// the finalizer does not exit, return directly
570571
return nil
571572
}

pkg/utils/util.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"slices"
2324
"strconv"
2425
"strings"
2526
"time"
@@ -185,16 +186,6 @@ func MapContainsKey(m map[string]string, s string) bool {
185186
return r
186187
}
187188

188-
// ContainsString checks if a given slice of strings contains the provided string.
189-
func ContainsString(slice []string, s string) bool {
190-
for _, item := range slice {
191-
if item == s {
192-
return true
193-
}
194-
}
195-
return false
196-
}
197-
198189
// RemoveString returns a newly created []string that contains all items from slice that
199190
// are not equal to s.
200191
func RemoveString(slice []string, s string) []string {
@@ -502,39 +493,39 @@ func NoResyncPeriodFunc() time.Duration {
502493

503494
// NeedToAddContentFinalizer checks if a Finalizer needs to be added for the volume snapshot content.
504495
func NeedToAddContentFinalizer(content *crdv1.VolumeSnapshotContent) bool {
505-
return content.ObjectMeta.DeletionTimestamp == nil && !ContainsString(content.ObjectMeta.Finalizers, VolumeSnapshotContentFinalizer)
496+
return content.ObjectMeta.DeletionTimestamp == nil && !slices.Contains(content.ObjectMeta.Finalizers, VolumeSnapshotContentFinalizer)
506497
}
507498

508499
// NeedToAddGroupSnapshotContentFinalizer checks if a Finalizer needs to be added for the volume group snapshot content.
509500
func NeedToAddGroupSnapshotContentFinalizer(groupSnapshotContent *crdv1alpha1.VolumeGroupSnapshotContent) bool {
510-
return groupSnapshotContent.ObjectMeta.DeletionTimestamp == nil && !ContainsString(groupSnapshotContent.ObjectMeta.Finalizers, VolumeGroupSnapshotContentFinalizer)
501+
return groupSnapshotContent.ObjectMeta.DeletionTimestamp == nil && !slices.Contains(groupSnapshotContent.ObjectMeta.Finalizers, VolumeGroupSnapshotContentFinalizer)
511502
}
512503

513504
// IsSnapshotDeletionCandidate checks if a volume snapshot deletionTimestamp
514505
// is set and any finalizer is on the snapshot.
515506
func IsSnapshotDeletionCandidate(snapshot *crdv1.VolumeSnapshot) bool {
516-
return snapshot.ObjectMeta.DeletionTimestamp != nil && (ContainsString(snapshot.ObjectMeta.Finalizers, VolumeSnapshotAsSourceFinalizer) || ContainsString(snapshot.ObjectMeta.Finalizers, VolumeSnapshotBoundFinalizer))
507+
return snapshot.ObjectMeta.DeletionTimestamp != nil && (slices.Contains(snapshot.ObjectMeta.Finalizers, VolumeSnapshotAsSourceFinalizer) || slices.Contains(snapshot.ObjectMeta.Finalizers, VolumeSnapshotBoundFinalizer))
517508
}
518509

519510
// IsGroupSnapshotDeletionCandidate checks if a volume group snapshot deletionTimestamp
520511
// is set and any finalizer is on the group snapshot.
521512
func IsGroupSnapshotDeletionCandidate(groupSnapshot *crdv1alpha1.VolumeGroupSnapshot) bool {
522-
return groupSnapshot.ObjectMeta.DeletionTimestamp != nil && ContainsString(groupSnapshot.ObjectMeta.Finalizers, VolumeGroupSnapshotBoundFinalizer)
513+
return groupSnapshot.ObjectMeta.DeletionTimestamp != nil && slices.Contains(groupSnapshot.ObjectMeta.Finalizers, VolumeGroupSnapshotBoundFinalizer)
523514
}
524515

525516
// NeedToAddSnapshotAsSourceFinalizer checks if a Finalizer needs to be added for the volume snapshot as a source for PVC.
526517
func NeedToAddSnapshotAsSourceFinalizer(snapshot *crdv1.VolumeSnapshot) bool {
527-
return snapshot.ObjectMeta.DeletionTimestamp == nil && !ContainsString(snapshot.ObjectMeta.Finalizers, VolumeSnapshotAsSourceFinalizer)
518+
return snapshot.ObjectMeta.DeletionTimestamp == nil && !slices.Contains(snapshot.ObjectMeta.Finalizers, VolumeSnapshotAsSourceFinalizer)
528519
}
529520

530521
// NeedToAddSnapshotBoundFinalizer checks if a Finalizer needs to be added for the bound volume snapshot.
531522
func NeedToAddSnapshotBoundFinalizer(snapshot *crdv1.VolumeSnapshot) bool {
532-
return snapshot.ObjectMeta.DeletionTimestamp == nil && !ContainsString(snapshot.ObjectMeta.Finalizers, VolumeSnapshotBoundFinalizer) && IsBoundVolumeSnapshotContentNameSet(snapshot)
523+
return snapshot.ObjectMeta.DeletionTimestamp == nil && !slices.Contains(snapshot.ObjectMeta.Finalizers, VolumeSnapshotBoundFinalizer) && IsBoundVolumeSnapshotContentNameSet(snapshot)
533524
}
534525

535526
// NeedToAddGroupSnapshotBoundFinalizer checks if a Finalizer needs to be added for the bound volume group snapshot.
536527
func NeedToAddGroupSnapshotBoundFinalizer(groupSnapshot *crdv1alpha1.VolumeGroupSnapshot) bool {
537-
return groupSnapshot.ObjectMeta.DeletionTimestamp == nil && !ContainsString(groupSnapshot.ObjectMeta.Finalizers, VolumeGroupSnapshotBoundFinalizer) && IsBoundVolumeGroupSnapshotContentNameSet(groupSnapshot)
528+
return groupSnapshot.ObjectMeta.DeletionTimestamp == nil && !slices.Contains(groupSnapshot.ObjectMeta.Finalizers, VolumeGroupSnapshotBoundFinalizer) && IsBoundVolumeGroupSnapshotContentNameSet(groupSnapshot)
538529
}
539530

540531
func deprecationWarning(deprecatedParam, newParam, removalVersion string) string {

pkg/utils/util_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
2727

28-
func TestContainsString(t *testing.T) {
29-
src := []string{"aa", "bb", "cc"}
30-
if !ContainsString(src, "bb") {
31-
t.Errorf("ContainsString didn't find the string as expected")
32-
}
33-
}
34-
3528
func TestRemoveString(t *testing.T) {
3629
tests := []struct {
3730
testName string

0 commit comments

Comments
 (0)