Skip to content

Commit

Permalink
rename GetIntFromStringEnvVar to GetEnvVar, rename output var
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviassss authored and jdn5126 committed Dec 11, 2023
1 parent 71235d6 commit 0dc4cd7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions cmd/aws-vpc-cni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,33 +380,33 @@ func validateEnvVars() bool {
}

// Validate that IP_COOLDOWN_PERIOD is a valid integer
ipCooldownPeriod, err, input := utils.GetIntFromStringEnvVar(envIPCooldownPeriod, defaultIPCooldownPeriod)
ipCooldownPeriod, err, ipCooldownPeriodStr := utils.GetEnvVar(envIPCooldownPeriod, defaultIPCooldownPeriod)
if err != nil || ipCooldownPeriod < 0 {
log.Errorf("IP_COOLDOWN_PERIOD MUST be a valid positive integer. %s is invalid", input)
log.Errorf("IP_COOLDOWN_PERIOD MUST be a valid positive integer. %s is invalid", ipCooldownPeriodStr)
return false
}

prefixDelegationEn := utils.GetBoolAsStringEnvVar(envEnPrefixDelegation, defaultEnPrefixDelegation)
warmIPTarget, err, inputWarmIP := utils.GetIntFromStringEnvVar(envWarmIPTarget, defaultWarmIPTarget)
warmIPTarget, err, warmIPTargetStr := utils.GetEnvVar(envWarmIPTarget, defaultWarmIPTarget)
if err != nil {
log.Errorf("error when trying to get env WARM_IP_TARGET: %s; input is %v", err, inputWarmIP)
log.Errorf("error when trying to get env WARM_IP_TARGET: %s; input is %v", err, warmIPTargetStr)
return false
}
warmPrefixTarget, err, inputWarmPrefix := utils.GetIntFromStringEnvVar(envWarmPrefixTarget, defaultWarmPrefixTarget)
warmPrefixTarget, err, warmPrefixTargetStr := utils.GetEnvVar(envWarmPrefixTarget, defaultWarmPrefixTarget)
if err != nil {
log.Errorf("error when trying to get env WARM_PREFIX_TARGET: %s; input is %v", err, inputWarmPrefix)
log.Errorf("error when trying to get env WARM_PREFIX_TARGET: %s; input is %v", err, warmPrefixTargetStr)
return false
}
minimumIPTarget, err, inputMinIP := utils.GetIntFromStringEnvVar(envMinIPTarget, defaultMinIPTarget)
minimumIPTarget, err, minimumIPTargetStr := utils.GetEnvVar(envMinIPTarget, defaultMinIPTarget)
if err != nil {
log.Errorf("error when trying to get env MINIMUM_IP_TARGET: %s; input is %v", err, inputMinIP)
log.Errorf("error when trying to get env MINIMUM_IP_TARGET: %s; input is %v", err, minimumIPTargetStr)
return false
}

if prefixDelegationEn && (warmIPTarget == 0 && warmPrefixTarget == 0 && minimumIPTarget == 0) {
log.Errorf("Setting WARM_PREFIX_TARGET = 0 is not supported while WARM_IP_TARGET/MINIMUM_IP_TARGET is not set. Please configure either one of the WARM_{PREFIX/IP}_TARGET or MINIMUM_IP_TARGET env variables")
return false
} else if inputWarmIP == "" && inputMinIP != "" {
} else if warmIPTargetStr == "" && minimumIPTargetStr != "" {
log.Errorf("WARM_IP_TARGET MUST be set when MINIMUM_IP_TARGET is set. Please configure both environment variables.")
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ipamd/datastore/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (addr AddressInfo) Assigned() bool {

// getCooldownPeriod returns the time duration in seconds configured by the IP_COOLDOWN_PERIOD env variable
func getCooldownPeriod() time.Duration {
cooldownVal, err, _ := utils.GetIntFromStringEnvVar(envIPCooldownPeriod, 30)
cooldownVal, err, _ := utils.GetEnvVar(envIPCooldownPeriod, 30)
if err != nil {
return 30 * time.Second
}
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GetBoolAsStringEnvVar(env string, defaultVal bool) bool {
}

// Parse environment variable and return integer representation of string, or default value if environment variable is unset
func GetIntFromStringEnvVar(env string, defaultVal int) (int, error, string) {
func GetEnvVar(env string, defaultVal int) (int, error, string) {
if val, ok := os.LookupEnv(env); ok {
parsedVal, err := strconv.Atoi(val)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ func TestGetEnv(t *testing.T) {
// Validate that GetIntFromStringEnvVar runs against acceptable format input without error
func TestGetIntFromStringEnvVar(t *testing.T) {
// Test environment flag variable not set
tmp, _, _ := GetIntFromStringEnvVar(envInt, defaultIntEnv)
tmp, _, _ := GetEnvVar(envInt, defaultIntEnv)
assert.Equal(t, tmp, defaultIntEnv)

// Test basic Integer as string set with acceptable format
os.Setenv(envInt, "20")
tmp, _, _ = GetIntFromStringEnvVar(envInt, defaultIntEnv)
tmp, _, _ = GetEnvVar(envInt, defaultIntEnv)
assert.Equal(t, tmp, 20)

// Test basic Integer as string set with unacceptable format
os.Setenv(envInt, "2O")
defer os.Unsetenv(envInt)
tmp, _, _ = GetIntFromStringEnvVar(envInt, defaultIntEnv)
tmp, _, _ = GetEnvVar(envInt, defaultIntEnv)
assert.Equal(t, tmp, -1)
}

0 comments on commit 0dc4cd7

Please sign in to comment.