Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifically set the CW endpoint when using awslogs as log driver for specifc regions #4143

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions agent/engine/docker_task_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime"
"github.com/aws/aws-sdk-go/aws"
ep "github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/pkg/errors"
Expand Down Expand Up @@ -84,6 +85,7 @@ const (
// logDriverTypeFirelens is the log driver type for containers that want to use the firelens container to send logs.
logDriverTypeFirelens = "awsfirelens"
logDriverTypeFluentd = "fluentd"
logDriverTypeAwslogs = "awslogs"
logDriverTag = "tag"
logDriverFluentdAddress = "fluentd-address"
dataLogDriverPath = "/data/firelens/"
Expand Down Expand Up @@ -1645,6 +1647,34 @@ func (engine *DockerTaskEngine) createContainer(task *apitask.Task, container *a
}
}

// This is a short term solution only for specific regions until AWS SDK Go is upgraded to V2
if hostConfig.LogConfig.Type == logDriverTypeAwslogs {
region := engine.cfg.AWSRegion
if region == "eu-isoe-west-1" || region == "us-isof-south-1" || region == "us-isof-east-1" {
endpoint := ""
dnsSuffix := ""
partition, ok := ep.PartitionForRegion(ep.DefaultPartitions(), region)
if !ok {
logger.Warn("No partition resolved for region. Using AWS default", logger.Fields{
"region": region,
"defaultDNSSuffix": ep.AwsPartition().DNSSuffix(),
})
dnsSuffix = ep.AwsPartition().DNSSuffix()
} else {
resolvedEndpoint, err := partition.EndpointFor("logs", region)
if err == nil {
endpoint = resolvedEndpoint.URL
} else {
dnsSuffix = partition.DNSSuffix()
}
}
if endpoint == "" {
endpoint = fmt.Sprintf("https://logs.%s.%s", region, dnsSuffix)
}
hostConfig.LogConfig.Config["awslogs-endpoint"] = endpoint
}
}

//Apply the log driver secret into container's LogConfig and Env secrets to container.Environment
hasSecretAsEnvOrLogDriver := func(s apicontainer.Secret) bool {
return s.Type == apicontainer.SecretTypeEnv || s.Target == apicontainer.SecretTargetLogDriver
Expand Down
77 changes: 77 additions & 0 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,83 @@ func TestTaskSecretsEnvironmentVariables(t *testing.T) {
}
}

// This is a short term solution only for specific regions until AWS SDK Go is upgraded to V2
func TestCreateContainerAwslogsLogDriver(t *testing.T) {
testCases := []struct {
name string
region string
expectedLogConfigEndpoint string
}{
{
name: "test container that uses awslogs log driver in IAD",
region: "us-east-1",
expectedLogConfigEndpoint: "",
},
{
name: "test container that uses awslogs log driver in NCL",
region: "eu-isoe-west-1",
expectedLogConfigEndpoint: "https://logs.eu-isoe-west-1.cloud.adc-e.uk",
},
{
name: "test container that uses awslogs log driver in ALE",
region: "us-isof-south-1",
expectedLogConfigEndpoint: "https://logs.us-isof-south-1.csp.hci.ic.gov",
},
{
name: "test container that uses awslogs log driver in LTW",
region: "us-isof-east-1",
expectedLogConfigEndpoint: "https://logs.us-isof-east-1.csp.hci.ic.gov",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, taskEngine, _, _, _, _ := mocks(t, ctx, &defaultConfig)
defer ctrl.Finish()

taskEngine.(*DockerTaskEngine).cfg.AWSRegion = tc.region

rawHostConfigInput := dockercontainer.HostConfig{
LogConfig: dockercontainer.LogConfig{
Type: "awslogs",
Config: map[string]string{},
},
}
rawHostConfig, err := json.Marshal(&rawHostConfigInput)
require.NoError(t, err)
testTask := &apitask.Task{
Arn: "arn:aws:ecs:region:account-id:task/test-task-arn",
Containers: []*apicontainer.Container{
{
Name: "test-container",
DockerConfig: apicontainer.DockerConfig{
HostConfig: func() *string {
s := string(rawHostConfig)
return &s
}(),
},
},
},
}

client.EXPECT().APIVersion().Return(defaultDockerClientAPIVersion, nil).AnyTimes()
client.EXPECT().CreateContainer(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(
func(ctx context.Context,
config *dockercontainer.Config,
hostConfig *dockercontainer.HostConfig,
name string,
timeout time.Duration) {
assert.Equal(t, tc.expectedLogConfigEndpoint, hostConfig.LogConfig.Config["awslogs-endpoint"])
})

ret := taskEngine.(*DockerTaskEngine).createContainer(testTask, testTask.Containers[0])
assert.NoError(t, ret.Error)
})
}

}

// TestCreateContainerAddFirelensLogDriverConfig tests that in createContainer, when the
// container is using firelens log driver, its logConfig is properly set.
func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
Expand Down
Loading