Skip to content

Commit

Permalink
Failing closed after maximum retry is achieved to avoid inifite recur…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
knabben committed May 23, 2024
1 parent 07be14d commit 6cf566e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/os/volume/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (VolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {

// GetVolumeIDFromTargetPath - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out
func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
volumeString, err := getTarget(mount)
volumeString, err := getTarget(mount, 0)

if err != nil {
return "", fmt.Errorf("error getting the volume for the mount %s, internal error %v", mount, err)
Expand All @@ -277,22 +277,28 @@ func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
return volumeString, nil
}

func getTarget(mount string) (string, error) {
func getTarget(mount string, retry int) (string, error) {
cmd := `(Get-Item -Path $Env:mountpath).Target`
cmdEnv := fmt.Sprintf("mountpath=%s", mount)
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
if err != nil || len(out) == 0 {
return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}
if retry >= 256 {
return "", fmt.Errorf("maximum recursion reached, cmd: %s, output: %s, :retry %d", cmd, string(out), retry)
}

volumeString := strings.TrimSpace(string(out))
klog.V(8).Infof("retry: %d, volumeString: %s", retry, volumeString)

if !strings.HasPrefix(volumeString, "Volume") {
return getTarget(volumeString)
return getTarget(volumeString, retry+1)
}

return ensureVolumePrefix(volumeString), nil
}

// GetVolumeIDFromTargetPath returns the volume id of a given target path.
// GetClosestVolumeIDFromTargetPath returns the volume id of a given target path.
func (VolumeAPI) GetClosestVolumeIDFromTargetPath(targetPath string) (string, error) {
volumeString, err := findClosestVolume(targetPath)

Expand Down
32 changes: 32 additions & 0 deletions pkg/os/volume/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package volume

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetTarget(t *testing.T) {
tests := []struct {
mountpath string
expectedResult string
expectError bool
counter int
}{
{
"c:\\",
"",
true,
1,
},
}
for _, test := range tests {
target, err := getTarget(test.mountpath, test.counter)
if test.expectError {
assert.NotNil(t, err, "Expect error during getTarget(%s)", test.mountpath)
} else {
assert.Nil(t, err, "Expect error is nil during getTarget(%s)", test.mountpath)
}
assert.Equal(t, target, test.expectedResult, "Expect result not equal with getTarget(%s) return: %q, expected: %s, error: %v",
test.mountpath, target, test.expectedResult, err)
}
}

0 comments on commit 6cf566e

Please sign in to comment.