Skip to content

Commit

Permalink
set firelens mem_buf_limit
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyic committed Oct 22, 2024
1 parent 79f17a5 commit 8bef39f
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 85 deletions.
19 changes: 19 additions & 0 deletions agent/api/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,25 @@ func (c *Container) GetNetworkModeFromHostConfig() string {
return hostConfig.NetworkMode.NetworkName()
}

// GetMemoryReservationFromHostConfig returns the container memory reservation
func (c *Container) GetMemoryReservationFromHostConfig() int64 {
c.lock.RLock()
defer c.lock.RUnlock()

if c.DockerConfig.HostConfig == nil {
return 0
}

hostConfig := &dockercontainer.HostConfig{}
err := json.Unmarshal([]byte(*c.DockerConfig.HostConfig), hostConfig)
if err != nil {
seelog.Warnf("Encountered error when trying to get memory reservation for container %s: %v", c.RuntimeID, err)
return 0
}

return int64(hostConfig.MemoryReservation)
}

// GetHostConfig returns the container's host config.
func (c *Container) GetHostConfig() *string {
c.lock.RLock()
Expand Down
4 changes: 2 additions & 2 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ func (task *Task) initializeFirelensResource(config *config.Config, resourceFiel

for _, container := range task.Containers {
firelensConfig := container.GetFirelensConfig()
if firelensConfig != nil {
if firelensConfig != nil { // is Firelens container
var ec2InstanceID string
if container.Environment != nil && container.Environment[awsExecutionEnvKey] == ec2ExecutionEnv {
ec2InstanceID = resourceFields.EC2InstanceID
Expand All @@ -1262,7 +1262,7 @@ func (task *Task) initializeFirelensResource(config *config.Config, resourceFiel
}
firelensResource, err := firelens.NewFirelensResource(config.Cluster, task.Arn, task.Family+":"+task.Version,
ec2InstanceID, config.DataDir, firelensConfig.Type, config.AWSRegion, networkMode, firelensConfig.Options, containerToLogOptions,
credentialsManager, task.ExecutionCredentialsID)
credentialsManager, task.ExecutionCredentialsID, container.GetMemoryReservationFromHostConfig())
if err != nil {
return errors.Wrap(err, "unable to initialize firelens resource")
}
Expand Down
2 changes: 1 addition & 1 deletion agent/taskresource/firelens/firelens_unimplemented.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type FirelensResource struct{}
// NewFirelensResource returns a new FirelensResource.
func NewFirelensResource(cluster, taskARN, taskDefinition, ec2InstanceID, dataDir, firelensConfigType, region, networkMode string,
firelensOptions map[string]string, containerToLogOptions map[string]map[string]string, credentialsManager credentials.Manager,
executionCredentialsID string) (*FirelensResource, error) {
executionCredentialsID string, containerMemoryReservation int64) (*FirelensResource, error) {
return nil, errors.New("not implemented")
}

Expand Down
60 changes: 31 additions & 29 deletions agent/taskresource/firelens/firelens_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,23 @@ const (
// FirelensResource models fluentd/fluentbit firelens container related resources as a task resource.
type FirelensResource struct {
// Fields that are specific to firelens resource. They are only set at initialization so are not protected by lock.
cluster string
taskARN string
taskDefinition string
ec2InstanceID string
resourceDir string
firelensConfigType string
region string
ecsMetadataEnabled bool
containerToLogOptions map[string]map[string]string
credentialsManager credentials.Manager
executionCredentialsID string
externalConfigType string
externalConfigValue string
networkMode string
ioutil ioutilwrapper.IOUtil
s3ClientCreator factory.S3ClientCreator
cluster string
taskARN string
taskDefinition string
ec2InstanceID string
resourceDir string
firelensConfigType string
region string
ecsMetadataEnabled bool
containerToLogOptions map[string]map[string]string
credentialsManager credentials.Manager
executionCredentialsID string
externalConfigType string
externalConfigValue string
networkMode string
ioutil ioutilwrapper.IOUtil
s3ClientCreator factory.S3ClientCreator
containerMemoryReservation int64

// Fields for the common functionality of task resource. Access to these fields are protected by lock.
createdAtUnsafe time.Time
Expand All @@ -98,20 +99,21 @@ type FirelensResource struct {
// NewFirelensResource returns a new FirelensResource.
func NewFirelensResource(cluster, taskARN, taskDefinition, ec2InstanceID, dataDir, firelensConfigType, region, networkMode string,
firelensOptions map[string]string, containerToLogOptions map[string]map[string]string, credentialsManager credentials.Manager,
executionCredentialsID string) (*FirelensResource, error) {
executionCredentialsID string, containerMemoryReservation int64) (*FirelensResource, error) {
firelensResource := &FirelensResource{
cluster: cluster,
taskARN: taskARN,
taskDefinition: taskDefinition,
ec2InstanceID: ec2InstanceID,
firelensConfigType: firelensConfigType,
region: region,
networkMode: networkMode,
containerToLogOptions: containerToLogOptions,
ioutil: ioutilwrapper.NewIOUtil(),
s3ClientCreator: factory.NewS3ClientCreator(),
executionCredentialsID: executionCredentialsID,
credentialsManager: credentialsManager,
cluster: cluster,
taskARN: taskARN,
taskDefinition: taskDefinition,
ec2InstanceID: ec2InstanceID,
firelensConfigType: firelensConfigType,
region: region,
networkMode: networkMode,
containerToLogOptions: containerToLogOptions,
ioutil: ioutilwrapper.NewIOUtil(),
s3ClientCreator: factory.NewS3ClientCreator(),
executionCredentialsID: executionCredentialsID,
credentialsManager: credentialsManager,
containerMemoryReservation: containerMemoryReservation,
}

fields := strings.Split(taskARN, "/")
Expand Down
78 changes: 40 additions & 38 deletions agent/taskresource/firelens/firelens_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ import (
)

const (
testCluster = "mycluster"
testTaskARN = "arn:aws:ecs:us-east-2:01234567891011:task/mycluster/3de392df-6bfa-470b-97ed-aa6f482cd7a"
testTaskDefinition = "taskdefinition:1"
testEC2InstanceID = "i-123456789a"
testDataDir = "testdatadir"
testResourceDir = "testresourcedir"
testTerminalResason = "testterminalreason"
testTempFile = "testtempfile"
testRegion = "us-west-2"
testExecutionCredentialsID = "testexecutioncredentialsid"
testExternalConfigType = "testexternalconfigtype"
testExternalConfigValue = "testexternalconfigvalue"
testCluster = "mycluster"
testTaskARN = "arn:aws:ecs:us-east-2:01234567891011:task/mycluster/3de392df-6bfa-470b-97ed-aa6f482cd7a"
testTaskDefinition = "taskdefinition:1"
testEC2InstanceID = "i-123456789a"
testDataDir = "testdatadir"
testResourceDir = "testresourcedir"
testTerminalResason = "testterminalreason"
testTempFile = "testtempfile"
testRegion = "us-west-2"
testExecutionCredentialsID = "testexecutioncredentialsid"
testExternalConfigType = "testexternalconfigtype"
testExternalConfigValue = "testexternalconfigvalue"
testContainerMemoryReservation = 100
)

var (
Expand Down Expand Up @@ -105,7 +106,7 @@ func mockMkdirAllError() func() {

func newMockFirelensResource(firelensConfigType, networkMode string, lopOptions map[string]string,
mockIOUtil *mock_ioutilwrapper.MockIOUtil, mockCredentialsManager *mock_credentials.MockManager,
mockS3ClientCreator *mock_factory.MockS3ClientCreator) *FirelensResource {
mockS3ClientCreator *mock_factory.MockS3ClientCreator, containerMemoryReservation int64) *FirelensResource {
return &FirelensResource{
cluster: testCluster,
taskARN: testTaskARN,
Expand All @@ -118,10 +119,11 @@ func newMockFirelensResource(firelensConfigType, networkMode string, lopOptions
containerToLogOptions: map[string]map[string]string{
"container": lopOptions,
},
executionCredentialsID: testExecutionCredentialsID,
credentialsManager: mockCredentialsManager,
ioutil: mockIOUtil,
s3ClientCreator: mockS3ClientCreator,
executionCredentialsID: testExecutionCredentialsID,
credentialsManager: mockCredentialsManager,
ioutil: mockIOUtil,
s3ClientCreator: mockS3ClientCreator,
containerMemoryReservation: containerMemoryReservation,
}
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func TestCreateFirelensResourceFluentdBridgeMode(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockRename()()
gomock.InOrder(
Expand All @@ -173,7 +175,7 @@ func TestCreateFirelensResourceFluentdAWSVPCMode(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, awsvpcNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockRename()()
gomock.InOrder(
Expand All @@ -188,7 +190,7 @@ func TestCreateFirelensResourceFluentdDefaultMode(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, "", testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockRename()()
gomock.InOrder(
Expand All @@ -203,7 +205,7 @@ func TestCreateFirelensResourceFluentbit(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentbit, bridgeNetworkMode, testFluentbitOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockRename()()
gomock.InOrder(
Expand All @@ -218,7 +220,7 @@ func TestCreateFirelensResourceInvalidType(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)
firelensResource.firelensConfigType = "invalid"

assert.Error(t, firelensResource.Create())
Expand All @@ -230,7 +232,7 @@ func TestCreateFirelensResourceCreateConfigDirError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockMkdirAllError()()

Expand All @@ -243,7 +245,7 @@ func TestCreateFirelensResourceCreateSocketDirError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

defer mockMkdirAllError()()

Expand All @@ -256,7 +258,7 @@ func TestCreateFirelensResourceGenerateConfigError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)
firelensResource.containerToLogOptions = map[string]map[string]string{
"container": {
"invalid": "invalid",
Expand All @@ -272,7 +274,7 @@ func TestCreateFirelensResourceCreateTempFileError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

gomock.InOrder(
mockIOUtil.EXPECT().TempFile(testResourceDir, tempFile).Return(nil, errors.New("test error")),
Expand All @@ -287,7 +289,7 @@ func TestCreateFirelensResourceWriteConfigFileError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

mockFile.(*mock_oswrapper.MockFile).WriteImpl = func(bytes []byte) (i int, e error) {
return 0, errors.New("test error")
Expand All @@ -306,7 +308,7 @@ func TestCreateFirelensResourceChmodError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

mockFile.(*mock_oswrapper.MockFile).ChmodImpl = func(mode os.FileMode) error {
return errors.New("test error")
Expand All @@ -325,7 +327,7 @@ func TestCreateFirelensResourceRenameError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

gomock.InOrder(
mockIOUtil.EXPECT().TempFile(testResourceDir, tempFile).Return(mockFile, nil),
Expand All @@ -347,7 +349,7 @@ func TestCreateFirelensResourceWithS3Config(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

err := firelensResource.parseOptions(testFirelensOptionsS3)
require.NoError(t, err)
Expand Down Expand Up @@ -385,7 +387,7 @@ func TestCreateFirelensResourceWithS3ConfigMissingCredentials(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

err := firelensResource.parseOptions(testFirelensOptionsS3)
require.NoError(t, err)
Expand All @@ -403,7 +405,7 @@ func TestCreateFirelensResourceWithS3ConfigInvalidS3ARN(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

err := firelensResource.parseOptions(testFirelensOptionsS3)
require.NoError(t, err)
Expand All @@ -422,7 +424,7 @@ func TestCreateFirelensResourceWithS3ConfigDownloadFailure(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

err := firelensResource.parseOptions(testFirelensOptionsS3)
require.NoError(t, err)
Expand Down Expand Up @@ -450,7 +452,7 @@ func TestCleanupFirelensResource(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

assert.NoError(t, firelensResource.Cleanup())
}
Expand All @@ -460,7 +462,7 @@ func TestCleanupFirelensResourceError(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, mockIOUtil,
mockCredentialsManager, mockS3ClientCreator)
mockCredentialsManager, mockS3ClientCreator, testContainerMemoryReservation)

removeAll = func(path string) error {
return errors.New("test error")
Expand All @@ -478,7 +480,7 @@ func TestInitializeFirelensResource(t *testing.T) {
defer done()

firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, nil, nil,
nil)
nil, testContainerMemoryReservation)
firelensResource.Initialize(&taskresource.ResourceFields{
ResourceFieldsCommon: &taskresource.ResourceFieldsCommon{
CredentialsManager: mockCredentialsManager,
Expand All @@ -494,7 +496,7 @@ func TestInitializeFirelensResource(t *testing.T) {

func TestSetKnownStatus(t *testing.T) {
firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, nil, nil,
nil)
nil, testContainerMemoryReservation)
firelensResource.appliedStatusUnsafe = resourcestatus.ResourceStatus(FirelensCreated)

firelensResource.SetKnownStatus(resourcestatus.ResourceStatus(FirelensCreated))
Expand All @@ -504,7 +506,7 @@ func TestSetKnownStatus(t *testing.T) {

func TestSetKnownStatusNoAppliedStatusUpdate(t *testing.T) {
firelensResource := newMockFirelensResource(FirelensConfigTypeFluentd, bridgeNetworkMode, testFluentdOptions, nil, nil,
nil)
nil, testContainerMemoryReservation)
firelensResource.appliedStatusUnsafe = resourcestatus.ResourceStatus(FirelensCreated)

firelensResource.SetKnownStatus(resourcestatus.ResourceStatus(FirelensStatusNone))
Expand Down
Loading

0 comments on commit 8bef39f

Please sign in to comment.