Skip to content

Commit dcd7162

Browse files
committed
Remove unwanted code conditions
Signed-off-by: Humble Chirammal <[email protected]>
1 parent 859696c commit dcd7162

File tree

4 files changed

+63
-64
lines changed

4 files changed

+63
-64
lines changed

pkg/common-controller/snapshot_controller.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"strings"
2323
"time"
2424

25-
crdv1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
26-
"github.com/kubernetes-csi/external-snapshotter/v2/pkg/utils"
2725
v1 "k8s.io/api/core/v1"
2826
storagev1 "k8s.io/api/storage/v1"
2927
apierrs "k8s.io/apimachinery/pkg/api/errors"
@@ -34,6 +32,9 @@ import (
3432
ref "k8s.io/client-go/tools/reference"
3533
"k8s.io/klog"
3634
"k8s.io/kubernetes/pkg/util/slice"
35+
36+
crdv1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
37+
"github.com/kubernetes-csi/external-snapshotter/v2/pkg/utils"
3738
)
3839

3940
// ==================================================================
@@ -334,8 +335,8 @@ func (ctrl *csiSnapshotCommonController) checkandRemoveSnapshotFinalizersAndChec
334335
func (ctrl *csiSnapshotCommonController) checkandAddSnapshotFinalizers(snapshot *crdv1.VolumeSnapshot) error {
335336
// get the content for this Snapshot
336337
var (
337-
content *crdv1.VolumeSnapshotContent = nil
338-
err error = nil
338+
content *crdv1.VolumeSnapshotContent
339+
err error
339340
)
340341
if snapshot.Spec.Source.VolumeSnapshotContentName != nil {
341342
content, err = ctrl.getPreprovisionedContentFromStore(snapshot)
@@ -374,7 +375,7 @@ func (ctrl *csiSnapshotCommonController) checkandAddSnapshotFinalizers(snapshot
374375
// If there is any problem with the binding (e.g., snapshot points to a non-existent snapshot content), update the snapshot status and emit event.
375376
func (ctrl *csiSnapshotCommonController) syncReadySnapshot(snapshot *crdv1.VolumeSnapshot) error {
376377
if !utils.IsBoundVolumeSnapshotContentNameSet(snapshot) {
377-
return fmt.Errorf("snapshot %s is not bound to a content.", utils.SnapshotKey(snapshot))
378+
return fmt.Errorf("snapshot %s is not bound to a content", utils.SnapshotKey(snapshot))
378379
}
379380
content, err := ctrl.getContentFromStore(*snapshot.Status.BoundVolumeSnapshotContentName)
380381
if err != nil {
@@ -464,30 +465,29 @@ func (ctrl *csiSnapshotCommonController) syncUnreadySnapshot(snapshot *crdv1.Vol
464465
if snapshot.Spec.Source.PersistentVolumeClaimName == nil {
465466
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotPVCSourceMissing", fmt.Sprintf("PVC source for snapshot %s is missing", uniqueSnapshotName))
466467
return fmt.Errorf("expected PVC source for snapshot %s but got nil", uniqueSnapshotName)
467-
} else {
468-
var err error
469-
var content *crdv1.VolumeSnapshotContent
470-
if content, err = ctrl.createSnapshotContent(snapshot); err != nil {
471-
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotContentCreationFailed", fmt.Sprintf("Failed to create snapshot content with error %v", err))
472-
return err
473-
}
468+
}
469+
var err error
470+
var content *crdv1.VolumeSnapshotContent
471+
if content, err = ctrl.createSnapshotContent(snapshot); err != nil {
472+
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotContentCreationFailed", fmt.Sprintf("Failed to create snapshot content with error %v", err))
473+
return err
474+
}
474475

475-
// Update snapshot status with BoundVolumeSnapshotContentName
476-
for i := 0; i < ctrl.createSnapshotContentRetryCount; i++ {
477-
klog.V(5).Infof("syncUnreadySnapshot [%s]: trying to update snapshot status", utils.SnapshotKey(snapshot))
478-
_, err = ctrl.updateSnapshotStatus(snapshot, content)
479-
if err == nil {
480-
break
481-
}
482-
klog.V(4).Infof("failed to update snapshot %s status: %v", utils.SnapshotKey(snapshot), err)
483-
time.Sleep(ctrl.createSnapshotContentInterval)
476+
// Update snapshot status with BoundVolumeSnapshotContentName
477+
for i := 0; i < ctrl.createSnapshotContentRetryCount; i++ {
478+
klog.V(5).Infof("syncUnreadySnapshot [%s]: trying to update snapshot status", utils.SnapshotKey(snapshot))
479+
_, err = ctrl.updateSnapshotStatus(snapshot, content)
480+
if err == nil {
481+
break
484482
}
483+
klog.V(4).Infof("failed to update snapshot %s status: %v", utils.SnapshotKey(snapshot), err)
484+
time.Sleep(ctrl.createSnapshotContentInterval)
485+
}
485486

486-
if err != nil {
487-
// update snapshot status failed
488-
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotStatusUpdateFailed", fmt.Sprintf("Snapshot status update failed, %v", err))
489-
return err
490-
}
487+
if err != nil {
488+
// update snapshot status failed
489+
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotStatusUpdateFailed", fmt.Sprintf("Snapshot status update failed, %v", err))
490+
return err
491491
}
492492
}
493493
return nil
@@ -1356,14 +1356,14 @@ func (ctrl *csiSnapshotCommonController) getSnapshotFromStore(snapshotName strin
13561356
klog.V(4).Infof("getSnapshotFromStore: snapshot %s not found", snapshotName)
13571357
// Fall through with snapshot = nil
13581358
return nil, nil
1359-
} else {
1360-
var ok bool
1361-
snapshot, ok = obj.(*crdv1.VolumeSnapshot)
1362-
if !ok {
1363-
return nil, fmt.Errorf("cannot convert object from snapshot cache to snapshot %q!?: %#v", snapshotName, obj)
1364-
}
1365-
klog.V(4).Infof("getSnapshotFromStore: snapshot %s found", snapshotName)
13661359
}
1360+
var ok bool
1361+
snapshot, ok = obj.(*crdv1.VolumeSnapshot)
1362+
if !ok {
1363+
return nil, fmt.Errorf("cannot convert object from snapshot cache to snapshot %q!?: %#v", snapshotName, obj)
1364+
}
1365+
klog.V(4).Infof("getSnapshotFromStore: snapshot %s found", snapshotName)
1366+
13671367
return snapshot, nil
13681368
}
13691369

pkg/sidecar-controller/snapshot_controller.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,33 @@ func (ctrl *csiSnapshotSideCarController) syncContent(content *crdv1.VolumeSnaps
6767
// update content SnapshotHandle to nil upon a successful deletion. At this
6868
// point, the finalizer on content should NOT be removed to avoid leaking.
6969
return ctrl.deleteCSISnapshot(content)
70-
} else {
71-
// otherwise, either the snapshot has been deleted from the underlying
72-
// storage system, or the deletion policy is Retain, remove the finalizer
73-
// if there is one so that API server could delete the object if there is
74-
// no other finalizer.
75-
return ctrl.removeContentFinalizer(content)
7670
}
71+
// otherwise, either the snapshot has been deleted from the underlying
72+
// storage system, or the deletion policy is Retain, remove the finalizer
73+
// if there is one so that API server could delete the object if there is
74+
// no other finalizer.
75+
return ctrl.removeContentFinalizer(content)
76+
77+
}
78+
if content.Spec.Source.VolumeHandle != nil && content.Status == nil {
79+
klog.V(5).Infof("syncContent: Call CreateSnapshot for content %s", content.Name)
80+
ctrl.createSnapshot(content)
7781
} else {
78-
if content.Spec.Source.VolumeHandle != nil && content.Status == nil {
79-
klog.V(5).Infof("syncContent: Call CreateSnapshot for content %s", content.Name)
80-
ctrl.createSnapshot(content)
81-
} else {
82-
// Skip checkandUpdateContentStatus() if ReadyToUse is
83-
// already true. We don't want to keep calling CreateSnapshot
84-
// or ListSnapshots CSI methods over and over again for
85-
// performance reasons.
86-
if content.Status != nil && content.Status.ReadyToUse != nil && *content.Status.ReadyToUse == true {
87-
// Try to remove AnnVolumeSnapshotBeingCreated if it is not removed yet for some reason
88-
err := ctrl.removeAnnVolumeSnapshotBeingCreated(content)
89-
if err != nil {
90-
return fmt.Errorf("failed to remove VolumeSnapshotBeingCreated annotation from the content %s: %q", content.Name, err)
91-
}
92-
return nil
82+
// Skip checkandUpdateContentStatus() if ReadyToUse is
83+
// already true. We don't want to keep calling CreateSnapshot
84+
// or ListSnapshots CSI methods over and over again for
85+
// performance reasons.
86+
if content.Status != nil && content.Status.ReadyToUse != nil && *content.Status.ReadyToUse == true {
87+
// Try to remove AnnVolumeSnapshotBeingCreated if it is not removed yet for some reason
88+
err := ctrl.removeAnnVolumeSnapshotBeingCreated(content)
89+
if err != nil {
90+
return fmt.Errorf("failed to remove VolumeSnapshotBeingCreated annotation from the content %s: %q", content.Name, err)
9391
}
94-
ctrl.checkandUpdateContentStatus(content)
92+
return nil
9593
}
94+
ctrl.checkandUpdateContentStatus(content)
9695
}
96+
9797
return nil
9898
}
9999

@@ -296,9 +296,9 @@ func (ctrl *csiSnapshotSideCarController) checkandUpdateContentStatusOperation(c
296296
return nil, err
297297
}
298298
return updatedContent, nil
299-
} else {
300-
return ctrl.createSnapshotWrapper(content)
301299
}
300+
return ctrl.createSnapshotWrapper(content)
301+
302302
}
303303

304304
// This is a wrapper function for the snapshot creation process.
@@ -347,10 +347,9 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotWrapper(content *crdv1.V
347347
newContent, err := ctrl.updateSnapshotContentStatus(content, snapshotID, readyToUse, creationTime.UnixNano(), size)
348348
if err != nil {
349349
klog.Errorf("error updating status for volume snapshot content %s: %v.", content.Name, err)
350-
return nil, fmt.Errorf("error updating status for volume snapshot content %s: %v.", content.Name, err)
351-
} else {
352-
content = newContent
350+
return nil, fmt.Errorf("error updating status for volume snapshot content %s: %v", content.Name, err)
353351
}
352+
content = newContent
354353

355354
// NOTE(xyang): handle create timeout
356355
// Remove annotation to indicate storage system has successfully

pkg/sidecar-controller/snapshot_delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
var defaultSize int64 = 1000
33-
var emptySize int64 = 0
33+
var emptySize int64
3434
var deletePolicy = crdv1.VolumeSnapshotContentDelete
3535
var retainPolicy = crdv1.VolumeSnapshotContentRetain
3636
var timeNow = time.Now()

pkg/utils/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ func verifyAndGetSecretNameAndNamespaceTemplate(secret secretParamsMap, snapshot
215215
} else if numName == 0 {
216216
// No secrets specified
217217
return "", "", nil
218-
} else {
219-
// THIS IS NOT A VALID CASE
220-
return "", "", fmt.Errorf("unknown error with getting secret name and namespace templates")
221218
}
219+
// THIS IS NOT A VALID CASE
220+
return "", "", fmt.Errorf("unknown error with getting secret name and namespace templates")
221+
222222
}
223223

224224
// getSecretReference returns a reference to the secret specified in the given nameTemplate

0 commit comments

Comments
 (0)