Skip to content

Commit

Permalink
Update the logic and the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghui12 committed Jul 21, 2021
1 parent 35b82a7 commit 7da6f38
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
11 changes: 4 additions & 7 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ const (
firelensSocketBindFormat = "%s/data/firelens/%s/socket/:/var/run/"
// firelensDriverName is the log driver name for containers that want to use the firelens container to send logs.
firelensDriverName = "awsfirelens"
// firelensLogDriverBufferLimitSize is the option for customers who want to specify the buffer limit size in FireLens.
firelensLogDriverBufferLimitOption = "log-driver-buffer-limit"
// firelensLogDriverBufferLimitOption is the option for customers who want to specify the buffer limit size in FireLens.
FirelensLogDriverBufferLimitOption = "log-driver-buffer-limit"

// firelensConfigVarFmt specifies the format for firelens config variable name. The first placeholder
// is option name. The second placeholder is the index of the container in the task's container list, appended
Expand Down Expand Up @@ -149,9 +149,6 @@ const (
sysctlValueOff = "0"
)

// LogDriverBufferLimit is the value of the user-defined option log-driver-buffer-limit
var LogDriverBufferLimit = ""

// TaskOverrides are the overrides applied to a task
type TaskOverrides struct{}

Expand Down Expand Up @@ -1074,8 +1071,8 @@ func (task *Task) collectFirelensLogOptions(containerToLogOptions map[string]map
containerToLogOptions[container.Name] = make(map[string]string)
}
for k, v := range hostConfig.LogConfig.Config {
if k == firelensLogDriverBufferLimitOption {
LogDriverBufferLimit = v
if k == FirelensLogDriverBufferLimitOption {
continue
}
containerToLogOptions[container.Name][k] = v
}
Expand Down
30 changes: 12 additions & 18 deletions agent/api/task/task_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,9 @@ func TestInitializeFirelensResource(t *testing.T) {
shouldHaveInstanceID: true,
expectedLogOptions: map[string]map[string]string{
"logsender": {
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
"secret-name": "\"#{ENV['secret-name_0']}\"",
"key1": "value1",
"key2": "value2",
"secret-name": "\"#{ENV['secret-name_0']}\"",
},
},
},
Expand All @@ -730,10 +729,9 @@ func TestInitializeFirelensResource(t *testing.T) {
shouldHaveInstanceID: true,
expectedLogOptions: map[string]map[string]string{
"logsender": {
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
"secret-name": "${secret-name_0}",
"key1": "value1",
"key2": "value2",
"secret-name": "${secret-name_0}",
},
},
},
Expand All @@ -746,10 +744,9 @@ func TestInitializeFirelensResource(t *testing.T) {
}(),
expectedLogOptions: map[string]map[string]string{
"logsender": {
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
"secret-name": "\"#{ENV['secret-name_0']}\"",
"key1": "value1",
"key2": "value2",
"secret-name": "\"#{ENV['secret-name_0']}\"",
},
},
},
Expand All @@ -764,10 +761,9 @@ func TestInitializeFirelensResource(t *testing.T) {
shouldDisableMetadata: true,
expectedLogOptions: map[string]map[string]string{
"logsender": {
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
"secret-name": "\"#{ENV['secret-name_0']}\"",
"key1": "value1",
"key2": "value2",
"secret-name": "\"#{ENV['secret-name_0']}\"",
},
},
},
Expand Down Expand Up @@ -913,8 +909,6 @@ func TestCollectFirelensLogOptions(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "value1", containerToLogOptions["logsender"]["key1"])
assert.Equal(t, "value2", containerToLogOptions["logsender"]["key2"])
assert.Equal(t, "10000", containerToLogOptions["logsender"]["log-driver-buffer-limit"])
assert.Equal(t, "10000", LogDriverBufferLimit)
}

func TestCollectFirelensLogOptionsInvalidOptions(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion agent/engine/docker_task_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,13 +1280,14 @@ func getFirelensLogConfig(task *apitask.Task, container *apicontainer.Container,
tag := fmt.Sprintf(fluentTagDockerFormat, container.Name, taskID)
fluentd := socketPathPrefix + filepath.Join(cfg.DataDirOnHost, dataLogDriverPath, taskID, dataLogDriverSocketPath)
logConfig := hostConfig.LogConfig
bufferLimit := logConfig.Config[apitask.FirelensLogDriverBufferLimitOption]
logConfig.Type = logDriverTypeFluentd
logConfig.Config = make(map[string]string)
logConfig.Config[logDriverTag] = tag
logConfig.Config[logDriverFluentdAddress] = fluentd
logConfig.Config[logDriverAsyncConnect] = strconv.FormatBool(true)
logConfig.Config[logDriverSubSecondPrecision] = strconv.FormatBool(true)
logConfig.Config[logDriverBufferLimit] = apitask.LogDriverBufferLimit
logConfig.Config[logDriverBufferLimit] = bufferLimit
seelog.Debugf("Applying firelens log config for container %s: %v", container.Name, logConfig)
return logConfig
}
Expand Down
16 changes: 9 additions & 7 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2668,8 +2668,9 @@ func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
LogConfig: dockercontainer.LogConfig{
Type: logDriverType,
Config: map[string]string{
"key1": "value1",
"key2": "value2",
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
},
},
}
Expand Down Expand Up @@ -2710,8 +2711,9 @@ func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
LogConfig: dockercontainer.LogConfig{
Type: logDriverType,
Config: map[string]string{
"key1": "value1",
"key2": "value2",
"key1": "value1",
"key2": "value2",
"log-driver-buffer-limit": "10000",
},
},
}
Expand Down Expand Up @@ -2775,7 +2777,7 @@ func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
expectedLogConfigTag: taskName + "-firelens-" + taskID,
expectedFluentdAsyncConnect: strconv.FormatBool(true),
expectedSubSecondPrecision: strconv.FormatBool(true),
expectedBufferLimit: "",
expectedBufferLimit: "10000",
expectedLogConfigFluentAddress: socketPathPrefix + filepath.Join(defaultConfig.DataDirOnHost, dataLogDriverPath, taskID, dataLogDriverSocketPath),
expectedIPAddress: envVarBridgeMode,
expectedPort: envVarPort,
Expand All @@ -2787,7 +2789,7 @@ func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
expectedLogConfigTag: taskName + "-firelens-" + taskID,
expectedFluentdAsyncConnect: strconv.FormatBool(true),
expectedSubSecondPrecision: strconv.FormatBool(true),
expectedBufferLimit: "",
expectedBufferLimit: "10000",
expectedLogConfigFluentAddress: socketPathPrefix + filepath.Join(defaultConfig.DataDirOnHost, dataLogDriverPath, taskID, dataLogDriverSocketPath),
expectedIPAddress: envVarBridgeMode,
expectedPort: envVarPort,
Expand All @@ -2799,7 +2801,7 @@ func TestCreateContainerAddFirelensLogDriverConfig(t *testing.T) {
expectedLogConfigTag: taskName + "-firelens-" + taskID,
expectedFluentdAsyncConnect: strconv.FormatBool(true),
expectedSubSecondPrecision: strconv.FormatBool(true),
expectedBufferLimit: "",
expectedBufferLimit: "10000",
expectedLogConfigFluentAddress: socketPathPrefix + filepath.Join(defaultConfig.DataDirOnHost, dataLogDriverPath, taskID, dataLogDriverSocketPath),
expectedIPAddress: envVarAWSVPCMode,
expectedPort: envVarPort,
Expand Down

0 comments on commit 7da6f38

Please sign in to comment.