Skip to content

Commit

Permalink
Merge pull request #38872 from t0yv0/t0yv0/normalize-container-def-he…
Browse files Browse the repository at this point in the history
…althcheck

Normalize aws_ecs_task_definition healthCheck
  • Loading branch information
ewbankkit authored Aug 15, 2024
2 parents 57d9295 + fc2d2cb commit 1b0ede5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .changelog/38870.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Remove `null`s from `container_definition` array fields
resource/aws_ecs_task_definition: Remove `null`s from `container_definition` array fields
```
3 changes: 3 additions & 0 deletions .changelog/38872.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Fix perpetual `container_definitions` diffs on `healthCheck`'s default values
```
16 changes: 15 additions & 1 deletion internal/service/ecs/container_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,25 @@ func (cd containerDefinitions) reduce(isAWSVPC bool) {
// Compact any sparse lists.
cd.compactArrays()

// Deal with special fields which have defaults.
// See https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definitions.
for i, def := range cd {
// Deal with special fields which have defaults.
if def.Essential == nil {
cd[i].Essential = aws.Bool(true)
}

if hc := def.HealthCheck; hc != nil {
if hc.Interval == nil {
hc.Interval = aws.Int32(30)
}
if hc.Retries == nil {
hc.Retries = aws.Int32(3)
}
if hc.Timeout == nil {
hc.Timeout = aws.Int32(5)
}
}

for j, pm := range def.PortMappings {
if pm.Protocol == awstypes.TransportProtocolTcp {
cd[i].PortMappings[j].Protocol = ""
Expand Down
73 changes: 73 additions & 0 deletions internal/service/ecs/container_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,76 @@ func TestContainerDefinitionsAreEquivalent_sparseArrays(t *testing.T) {
t.Fatal("Expected definitions to be equal.")
}
}

func TestContainerDefinitionsAreEquivalent_healthCheck(t *testing.T) {
t.Parallel()

cfgRepresentation := `
[
{
"cpu": 512,
"environment": [],
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost:8080/health || exit 1"
]
},
"image": "nginx",
"memory": 2048,
"name": "nginx",
"startTimeout": 10,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "foo-bar-e196c99",
"awslogs-region": "region-1",
"awslogs-stream-prefix": "nginx"
}
}
}
]`

apiRepresentation := `
[
{
"cpu": 512,
"environment": [],
"essential": true,
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost:8080/health || exit 1"
],
"interval": 30,
"retries": 3,
"timeout": 5
},
"image": "nginx",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "foo-bar-e196c99",
"awslogs-region": "region-1",
"awslogs-stream-prefix": "nginx"
}
},
"memory": 2048,
"mountPoints": [],
"name": "nginx",
"portMappings": [],
"startTimeout": 10,
"systemControls": [],
"volumesFrom": []
}
]
`

equal, err := containerDefinitionsAreEquivalent(cfgRepresentation, apiRepresentation, false)
if err != nil {
t.Fatal(err)
}
if !equal {
t.Fatal("Expected definitions to be equal.")
}
}

0 comments on commit 1b0ede5

Please sign in to comment.