Skip to content

Commit

Permalink
Add ecs-agent container healthcheck localhost ip override env var
Browse files Browse the repository at this point in the history
  • Loading branch information
fierlion committed Mar 18, 2021
1 parent 5be7aa0 commit 1230b58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
17 changes: 17 additions & 0 deletions agent/app/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand All @@ -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, ok := os.LookupEnv(testEnvVar); ok {
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) {
Expand Down
15 changes: 14 additions & 1 deletion agent/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package app

import (
"fmt"
"os"
"time"

"github.com/aws/amazon-ecs-agent/agent/app/args"
Expand All @@ -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 {
Expand All @@ -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, ok := os.LookupEnv(ECS_AGENT_HEALTHCHECK_HOST_ENV_VAR); ok {
localhost = localhostOverride
}
healthcheckUrl := fmt.Sprintf("http://%s:51678/v1/metadata", localhost)
return runHealthcheck(healthcheckUrl, time.Second*25)
}

if *parsedArgs.LogLevel != "" {
Expand Down

0 comments on commit 1230b58

Please sign in to comment.