diff --git a/README.md b/README.md index 3517c4f815..461ea1f963 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ additional details on each available environment variable. | `ECS_ENABLE_TASK_CPU_MEM_LIMIT` | `true` | Whether to enable task-level cpu and memory limits | `true` | `false` | | `ECS_CGROUP_PATH` | `/sys/fs/cgroup` | The root cgroup path that is expected by the ECS agent. This is the path that accessible from the agent mount. | `/sys/fs/cgroup` | Not applicable | | `ECS_CGROUP_CPU_PERIOD` | `10ms` | CGroups CPU period for task level limits. This value should be between 8ms to 100ms | `100ms` | Not applicable | +| `ECS_AGENT_HEALTHCHECK_HOST` | `localhost` | Override for the ecs-agent container's healthcheck localhost ip address| `localhost` | `localhost` | | `ECS_ENABLE_CPU_UNBOUNDED_WINDOWS_WORKAROUND` | `true` | When `true`, ECS will allow CPU unbounded(CPU=`0`) tasks to run along with CPU bounded tasks in Windows. | Not applicable | `false` | | `ECS_ENABLE_MEMORY_UNBOUNDED_WINDOWS_WORKAROUND` | `true` | When `true`, ECS will ignore the memory reservation parameter (soft limit) to run along with memory bounded tasks in Windows. To run a memory unbounded task, omit the memory hard limit and set any memory reservation, it will be ignored. | Not applicable | `false` | | `ECS_TASK_METADATA_RPS_LIMIT` | `100,150` | Comma separated integer values for steady state and burst throttle limits for task metadata endpoint | `40,60` | `40,60` | diff --git a/agent/app/healthcheck_test.go b/agent/app/healthcheck_test.go index 5f142ea1ab..7000bcd96c 100644 --- a/agent/app/healthcheck_test.go +++ b/agent/app/healthcheck_test.go @@ -17,6 +17,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "os" "testing" "time" @@ -39,6 +40,22 @@ func TestHealthcheck_InvalidURL2(t *testing.T) { require.Equal(t, 1, rc) } +func TestHealthcheck_EnvvarConfig(t *testing.T) { + testEnvVar := "TEST_AGENT_HEALTHCHECK_ENV_VAR" + defer os.Unsetenv(testEnvVar) + os.Setenv(testEnvVar, "127.0.0.1") + + // testing healthcheck url setup from app/run.go + localhost := "localhost" + if localhostOverride := os.Getenv(testEnvVar); localhostOverride != "" { + localhost = localhostOverride + } + healthcheckUrl := fmt.Sprintf("http://%s:51678/v1/metadata", localhost) + expectedUrl := "http://127.0.0.1:51678/v1/metadata" + + require.Equal(t, healthcheckUrl, expectedUrl) +} + func TestHealthcheck_Timeout(t *testing.T) { sema := make(chan int) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/agent/app/run.go b/agent/app/run.go index 3aa509b42a..8af36bfe3c 100644 --- a/agent/app/run.go +++ b/agent/app/run.go @@ -14,6 +14,8 @@ package app import ( + "fmt" + "os" "time" "github.com/aws/amazon-ecs-agent/agent/app/args" @@ -24,6 +26,10 @@ import ( log "github.com/cihub/seelog" ) +const ( + ECS_AGENT_HEALTHCHECK_HOST_ENV_VAR = "ECS_AGENT_HEALTHCHECK_HOST" +) + // Run runs the ECS Agent App. It returns an exit code, which is used by // main() to set the status code for the program func Run(arguments []string) int { @@ -43,7 +49,14 @@ func Run(arguments []string) int { // timeout of 30s. This is so that we can catch any http timeout and log the // issue within agent logs. // see https://docs.docker.com/engine/reference/builder/#healthcheck - return runHealthcheck("http://localhost:51678/v1/metadata", time.Second*25) + // if ECS_AGENT_HEALTHCHECK_HOST env var is set, it will override + // the healthcheck's localhost ip address inside the ecs-agent container + localhost := "localhost" + if localhostOverride := os.Getenv(ECS_AGENT_HEALTHCHECK_HOST_ENV_VAR); localhostOverride != "" { + localhost = localhostOverride + } + healthcheckUrl := fmt.Sprintf("http://%s:51678/v1/metadata", localhost) + return runHealthcheck(healthcheckUrl, time.Second*25) } if *parsedArgs.LogLevel != "" {