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

Add RestartCount to introspection endpoint container response #4190

Merged
merged 1 commit into from
May 23, 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
25 changes: 15 additions & 10 deletions agent/handlers/v1/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ type TasksResponse struct {

// ContainerResponse is the schema for the container response JSON object
type ContainerResponse struct {
DockerID string `json:"DockerId"`
DockerName string `json:"DockerName"`
Name string `json:"Name"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
CreatedAt *time.Time `json:"CreatedAt,omitempty"`
StartedAt *time.Time `json:"StartedAt,omitempty"`
Ports []tmdsresponse.PortResponse `json:"Ports,omitempty"`
Networks []tmdsresponse.Network `json:"Networks,omitempty"`
Volumes []tmdsresponse.VolumeResponse `json:"Volumes,omitempty"`
DockerID string `json:"DockerId"`
DockerName string `json:"DockerName"`
Name string `json:"Name"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
CreatedAt *time.Time `json:"CreatedAt,omitempty"`
StartedAt *time.Time `json:"StartedAt,omitempty"`
Ports []tmdsresponse.PortResponse `json:"Ports,omitempty"`
Networks []tmdsresponse.Network `json:"Networks,omitempty"`
Volumes []tmdsresponse.VolumeResponse `json:"Volumes,omitempty"`
RestartCount *int `json:"RestartCount,omitempty"`
}

// NewTaskResponse creates a TaskResponse for a task.
Expand Down Expand Up @@ -121,6 +122,10 @@ func NewContainerResponse(dockerContainer *apicontainer.DockerContainer, eni *ni
startedAt = startedAt.UTC()
resp.StartedAt = &startedAt
}
if container.RestartPolicyEnabled() {
restartCount := container.RestartTracker.GetRestartCount()
resp.RestartCount = &restartCount
}
return resp
}

Expand Down
56 changes: 56 additions & 0 deletions agent/handlers/v1/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"
"github.com/aws/amazon-ecs-agent/ecs-agent/api/container/restart"
apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status"
ni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/networkinterface"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
Expand Down Expand Up @@ -197,6 +198,61 @@ func TestContainerResponse(t *testing.T) {
assert.Equal(t, expectedContainerResponse, containerResponse)
}

func TestContainerResponseWithRestartPolicy(t *testing.T) {
restartPolicy := &restart.RestartPolicy{
Enabled: true,
}
container := &apicontainer.Container{
Name: containerName,
Image: imageName,
ImageID: imageID,
Ports: []apicontainer.PortBinding{
{
ContainerPort: 80,
Protocol: apicontainer.TransportProtocolTCP,
},
},
VolumesUnsafe: []types.MountPoint{
{
Name: volName,
Source: volSource,
Destination: volDestination,
},
},
RestartPolicy: restartPolicy,
RestartTracker: restart.NewRestartTracker(*restartPolicy),
}

container.SetCreatedAt(containerTime)
container.SetStartedAt(containerTime)
container.RestartTracker.RecordRestart()

dockerContainer := &apicontainer.DockerContainer{
DockerID: containerID,
DockerName: containerName,
Container: container,
}

eni := &ni.NetworkInterface{
IPV4Addresses: []*ni.IPV4Address{
{
Address: eniIPv4Address,
},
},
}

containerResponse := NewContainerResponse(dockerContainer, eni)

_, err := json.Marshal(containerResponse)
assert.NoError(t, err)

expectedContainerResponseWithRestartPolicy := expectedContainerResponse
rc := int(1)
expectedContainerResponseWithRestartPolicy.RestartCount = &rc

assert.Equal(t, expectedContainerResponseWithRestartPolicy, containerResponse)
}

func TestPortBindingsResponse(t *testing.T) {
container := &apicontainer.Container{
Name: containerName,
Expand Down
Loading