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

Failing closed after maximum retry is achieved to avoid inf recursion #336

Merged
merged 1 commit into from
Oct 28, 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
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)
}

knabben marked this conversation as resolved.
Show resolved Hide resolved
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.
knabben marked this conversation as resolved.
Show resolved Hide resolved
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) {
mauriciopoppe marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
Loading