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

Using env variable to set vmware timeout value #1078

Merged
merged 6 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 25 additions & 6 deletions pkg/blockstorage/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/url"
"os"
"strconv"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -38,8 +40,14 @@ const (
VSpherePasswordKey = "VSpherePasswordKey"

noDescription = ""
defaultWaitTime = 10 * time.Minute
defaultWaitTime = 60 * time.Minute
defaultRetryLimit = 30 * time.Minute

vmWareTimeoutMinEnv = "VMWARE_GOM_TIMEOUT_MIN"
)

var (
vmWareTimeout = time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
)

// FcdProvider provides blockstorage.Provider
Expand Down Expand Up @@ -118,7 +126,7 @@ func (p *FcdProvider) VolumeCreateFromSnapshot(ctx context.Context, snapshot blo
return nil, errors.Wrap(err, "Failed to create disk from snapshot")
}
log.Debug().Print("Started CreateDiskFromSnapshot task", field.M{"VolumeID": volID, "SnapshotID": snapshotID})
res, err := task.Wait(ctx, defaultWaitTime)
res, err := task.Wait(ctx, vmWareTimeout)
if err != nil {
return nil, errors.Wrap(err, "Failed to wait on task")
}
Expand Down Expand Up @@ -151,7 +159,7 @@ func (p *FcdProvider) VolumeDelete(ctx context.Context, volume *blockstorage.Vol
if err != nil {
return errors.Wrap(err, "Failed to delete the disk")
}
_, err = task.Wait(ctx, defaultWaitTime)
_, err = task.Wait(ctx, vmWareTimeout)
return err
}

Expand Down Expand Up @@ -193,7 +201,7 @@ func (p *FcdProvider) SnapshotCreate(ctx context.Context, volume blockstorage.Vo
return false, errors.Wrap(lerr, "Failed to create snapshot")
}
log.Debug().Print("Started CreateSnapshot task", field.M{"VolumeID": volume.ID})
res, lerr = task.Wait(ctx, defaultWaitTime)
res, lerr = task.Wait(ctx, vmWareTimeout)
if lerr != nil {
if soap.IsVimFault(lerr) {
switch soap.ToVimFault(lerr).(type) {
Expand Down Expand Up @@ -252,7 +260,7 @@ func (p *FcdProvider) SnapshotDelete(ctx context.Context, snapshot *blockstorage
return false, errors.Wrap(lerr, "Failed to delete snapshot")
}
log.Debug().Print("Started SnapshotDelete task", field.M{"VolumeID": volID, "SnapshotID": snapshotID})
_, lerr = task.Wait(ctx, defaultWaitTime)
_, lerr = task.Wait(ctx, vmWareTimeout)
if lerr != nil {
// The following error handling was pulled from https://github.com/vmware-tanzu/astrolabe/blob/91eeed4dcf77edd1387a25e984174f159d66fedb/pkg/ivd/ivd_protected_entity.go#L433
if soap.IsVimFault(lerr) {
Expand Down Expand Up @@ -334,7 +342,7 @@ func (p *FcdProvider) setTagsVolume(ctx context.Context, volume *blockstorage.Vo
if err != nil {
return errors.Wrap(err, "Failed to update metadata")
}
_, err = task.Wait(ctx, defaultWaitTime)
_, err = task.Wait(ctx, vmWareTimeout)
if err != nil {
return errors.Wrap(err, "Failed to wait on task")
}
Expand All @@ -350,3 +358,14 @@ func (p *FcdProvider) VolumesList(ctx context.Context, tags map[string]string, z
func (p *FcdProvider) SnapshotsList(ctx context.Context, tags map[string]string) ([]*blockstorage.Snapshot, error) {
return nil, errors.New("Not implemented")
}

func getEnvAsIntOrDefault(envKey string, def int) int {
if v, ok := os.LookupEnv(envKey); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Personal bias toward less nesting. if ...; !ok { return def }, then return def again below if you have a reason to.

iv, err := strconv.Atoi(v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log error. Defaulting on hidden failures are difficult to diagnose. It will help if you log a warning, or similar, to stderr
Another option - log Info to stdout whenever you default.
Message must include the environment variable and the value you are defaulting to.

If checking error, then return non-default value whenever err == nil, otherwise you are ignoring legitimate integer values - this all relates to my first point: someone set a legitimate integer value, but its silently being ignored when supplied, why?

While the code will work for the application, it seems as though the current implementation could cause frustration in operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaron-kasten logging the case where an invalid option was provided

if err == nil && iv > 0 {
return iv
}
}

return def
}
27 changes: 27 additions & 0 deletions pkg/blockstorage/vmware/vmware_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package vmware

import (
"os"
"testing"
"time"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -55,3 +57,28 @@ func (s *VMWareSuite) TestURLParse(c *C) {
}
}
}

func (s *VMWareSuite) TestTimeoutEnvSetting(c *C) {
tempEnv := os.Getenv(vmWareTimeoutMinEnv)
os.Unsetenv(vmWareTimeoutMinEnv)
timeout := time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
c.Assert(timeout, Equals, defaultWaitTime)

os.Setenv(vmWareTimeoutMinEnv, "7")
timeout = time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
c.Assert(timeout, Equals, 7*time.Minute)

Comment on lines +67 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test for "0" or "-1". What values would you expect?

os.Setenv(vmWareTimeoutMinEnv, "badValue")
timeout = time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
c.Assert(timeout, Equals, defaultWaitTime)

os.Setenv(vmWareTimeoutMinEnv, "-1")
timeout = time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
c.Assert(timeout, Equals, defaultWaitTime)

os.Setenv(vmWareTimeoutMinEnv, "0")
timeout = time.Duration(getEnvAsIntOrDefault(vmWareTimeoutMinEnv, int(defaultWaitTime/time.Minute))) * time.Minute
c.Assert(timeout, Equals, defaultWaitTime)

os.Setenv(vmWareTimeoutMinEnv, tempEnv)
}