Skip to content

Commit

Permalink
bugfix for PR-3522: handle case when env-ECS_DYNAMIC_HOST_PORT_RANGE …
Browse files Browse the repository at this point in the history
…is not set
  • Loading branch information
SreeeS committed Jan 4, 2023
1 parent f96a061 commit d5232a9
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 18 deletions.
9 changes: 8 additions & 1 deletion agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ func TestParseDynamicHostPortRange(t *testing.T) {
expectedErrorDynamicHostPortRange: nil,
expectedErrorEphemeralHostPortRange: nil,
},
{
testName: "Parse DynamicHostPortRange for valid case when config option is not set or is empty",
testDynamicHostPortRangeVal: "",
expectedPortRangeVal: "300-400",
expectedErrorDynamicHostPortRange: nil,
expectedErrorEphemeralHostPortRange: nil,
},
{
testName: "Parse DynamicHostPortRange for Invalid DynamicHostPortRange value",
testDynamicHostPortRangeVal: "test1",
Expand All @@ -536,7 +543,7 @@ func TestParseDynamicHostPortRange(t *testing.T) {
defer setTestRegion()()
defer setTestEnv("ECS_DYNAMIC_HOST_PORT_RANGE", tc.testDynamicHostPortRangeVal)()

if tc.expectedErrorDynamicHostPortRange != nil {
if tc.testDynamicHostPortRangeVal == "" || tc.expectedErrorDynamicHostPortRange != nil {
getDynamicHostPortRange = func() (start int, end int, err error) {
return 300, 400, nil
}
Expand Down
24 changes: 16 additions & 8 deletions agent/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,24 @@ var getDynamicHostPortRange = utils.GetDynamicHostPortRange

func parseDynamicHostPortRange(dynamicHostPortRangeEnv string) string {
dynamicHostPortRange := os.Getenv(dynamicHostPortRangeEnv)
_, _, err := nat.ParsePortRangeToInt(dynamicHostPortRange)
if err != nil {
seelog.Warnf("Unable to read the dynamicHostPortRange value: %s", dynamicHostPortRange)
startHostPortRange, endHostPortRange, err := getDynamicHostPortRange()
if dynamicHostPortRange != "" {
_, _, err := nat.ParsePortRangeToInt(dynamicHostPortRange)
if err != nil {
seelog.Warnf("Unable to read the ephemeral host port range, "+
"falling back to the default range: %v-%v", utils.DefaultPortRangeStart, utils.DefaultPortRangeEnd)
return fmt.Sprintf("%d-%d", utils.DefaultPortRangeStart, utils.DefaultPortRangeEnd)
seelog.Warnf("Invalid dynamicHostPortRange value from config: %s, err: %v", dynamicHostPortRange, err)
return getDefaultDynamicHostPortRange()
}
return fmt.Sprintf("%d-%d", startHostPortRange, endHostPortRange)
} else {
return getDefaultDynamicHostPortRange()
}
return dynamicHostPortRange
}

func getDefaultDynamicHostPortRange() string {
startHostPortRange, endHostPortRange, err := getDynamicHostPortRange()
if err != nil {
seelog.Warnf("Unable to read the ephemeral host port range, "+
"falling back to the default range: %v-%v", utils.DefaultPortRangeStart, utils.DefaultPortRangeEnd)
return fmt.Sprintf("%d-%d", utils.DefaultPortRangeStart, utils.DefaultPortRangeEnd)
}
return fmt.Sprintf("%d-%d", startHostPortRange, endHostPortRange)
}
2 changes: 1 addition & 1 deletion ecs-init/cache/dependencies_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/docker/backoff_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/docker/dependencies_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/engine/dependencies_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/exec/iptables/cmd_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/exec/iptables/exec_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/exec/sysctl/cmd_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/exec/sysctl/exec_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecs-init/gpu/nvidia_gpu_manager_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5232a9

Please sign in to comment.