Skip to content

Commit

Permalink
Using env variable to set vmware timeout value (#1078)
Browse files Browse the repository at this point in the history
* using env variable to set timeout value

* checking for negative and 0

* adding debug

* lint workaround

* lint fix2

* another one
  • Loading branch information
bathina2 authored Aug 28, 2021
1 parent 261713c commit 49a8ce0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
32 changes: 26 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,15 @@ 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 {
iv, err := strconv.Atoi(v)
if err == nil && iv > 0 {
return iv
}
log.Debug().Print("Using default timeout value for vSphere because of invalid environment variable", field.M{"envVar": v})
}

return def
}
30 changes: 30 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,31 @@ 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)

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)

timeout = time.Duration(getEnvAsIntOrDefault("someotherenv", 5)) * time.Minute
c.Assert(timeout, Equals, 5*time.Minute)

os.Setenv(vmWareTimeoutMinEnv, tempEnv)
}

0 comments on commit 49a8ce0

Please sign in to comment.