Skip to content

Commit

Permalink
Merge branch 'dev' into ecs-agent-wsclient-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
RichaGangwar authored May 18, 2023
2 parents 6cc9959 + edbeee3 commit f04f424
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 87 deletions.
7 changes: 4 additions & 3 deletions agent/containermetadata/parse_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"

tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/cihub/seelog"
"github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -142,17 +143,17 @@ func parseNetworkMetadata(settings *types.NetworkSettings, hostConfig *dockercon

// Extensive Network information is not available for Docker API versions 1.17-1.20
// Instead we only get the details of the first network
networkList := make([]Network, 0)
networkList := make([]tmdsresponse.Network, 0)
if len(settings.Networks) > 0 {
for modeFromSettings, containerNetwork := range settings.Networks {
networkMode := modeFromSettings
ipv4Addresses := []string{containerNetwork.IPAddress}
network := Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}
network := tmdsresponse.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}
networkList = append(networkList, network)
}
} else {
ipv4Addresses := []string{ipv4AddressFromSettings}
network := Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}
network := tmdsresponse.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}
networkList = append(networkList, network)
}

Expand Down
12 changes: 3 additions & 9 deletions agent/containermetadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/docker/docker/api/types"
)

Expand Down Expand Up @@ -82,19 +83,12 @@ type DockerMetadataClient interface {
InspectContainer(context.Context, string, time.Duration) (*types.ContainerJSON, error)
}

// Network is a struct that keeps track of metadata of a network interface
type Network struct {
NetworkMode string `json:"NetworkMode,omitempty"`
IPv4Addresses []string `json:"IPv4Addresses,omitempty"`
IPv6Addresses []string `json:"IPv6Addresses,omitempty"`
}

// NetworkMetadata keeps track of the data we parse from the Network Settings
// in docker containers. While most information is redundant with the internal
// Network struct, we keeps this wrapper in case we wish to add data specifically
// from the NetworkSettings
type NetworkMetadata struct {
networks []Network
networks []tmdsresponse.Network
}

// DockerContainerMetadata keeps track of all metadata acquired from Docker inspection
Expand Down Expand Up @@ -147,7 +141,7 @@ type metadataSerializer struct {
ImageID string `json:"ImageID,omitempty"`
ImageName string `json:"ImageName,omitempty"`
Ports []apicontainer.PortBinding `json:"PortMappings,omitempty"`
Networks []Network `json:"Networks,omitempty"`
Networks []tmdsresponse.Network `json:"Networks,omitempty"`
MetadataFileStatus MetadataStatus `json:"MetadataFileStatus,omitempty"`
AvailabilityZone string `json:"AvailabilityZone,omitempty"`
HostPrivateIPv4Address string `json:"HostPrivateIPv4Address,omitempty"`
Expand Down
19 changes: 9 additions & 10 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ import (
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"
apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
mock_dockerstate "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate/mocks"
task_protection_v1 "github.com/aws/amazon-ecs-agent/agent/handlers/agentapi/taskprotection/v1/handlers"
v1 "github.com/aws/amazon-ecs-agent/agent/handlers/v1"
v2 "github.com/aws/amazon-ecs-agent/agent/handlers/v2"
v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"
v4 "github.com/aws/amazon-ecs-agent/agent/handlers/v4"
Expand All @@ -50,6 +48,7 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks"
mock_audit "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/mocks"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
tmdsv1 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v1"
"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -242,14 +241,14 @@ var (
},
Type: containerType,
Labels: labels,
Ports: []v1.PortResponse{
Ports: []tmdsresponse.PortResponse{
{
ContainerPort: containerPort,
Protocol: containerPortProtocol,
HostPort: containerPort,
},
},
Networks: []containermetadata.Network{
Networks: []tmdsresponse.Network{
{
NetworkMode: utils.NetworkModeAWSVPC,
IPv4Addresses: []string{eniIPv4Address},
Expand Down Expand Up @@ -348,13 +347,13 @@ var (
},
Type: containerType,
Labels: labels,
Ports: []v1.PortResponse{
Ports: []tmdsresponse.PortResponse{
{
ContainerPort: containerPort,
Protocol: containerPortProtocol,
},
},
Networks: []containermetadata.Network{
Networks: []tmdsresponse.Network{
{
NetworkMode: bridgeMode,
IPv4Addresses: []string{bridgeIPAddr},
Expand Down Expand Up @@ -395,22 +394,22 @@ var (
},
Type: containerType,
Labels: labels,
Ports: []v1.PortResponse{
Ports: []tmdsresponse.PortResponse{
{
ContainerPort: containerPort,
Protocol: containerPortProtocol,
HostPort: containerPort,
},
},
Networks: []containermetadata.Network{
Networks: []tmdsresponse.Network{
{
NetworkMode: utils.NetworkModeAWSVPC,
IPv4Addresses: []string{eniIPv4Address},
},
},
},
Networks: []v4.Network{{
Network: containermetadata.Network{
Network: tmdsresponse.Network{
NetworkMode: utils.NetworkModeAWSVPC,
IPv4Addresses: []string{eniIPv4Address},
},
Expand Down Expand Up @@ -485,7 +484,7 @@ var (
expectedV4BridgeContainerResponse = v4.ContainerResponse{
ContainerResponse: &expectedBridgeContainerResponse,
Networks: []v4.Network{{
Network: containermetadata.Network{
Network: tmdsresponse.Network{
NetworkMode: bridgeMode,
IPv4Addresses: []string{bridgeIPAddr},
},
Expand Down
44 changes: 14 additions & 30 deletions agent/handlers/v1/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package v1
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/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
)

Expand Down Expand Up @@ -46,28 +46,12 @@ 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"`
Ports []PortResponse `json:"Ports,omitempty"`
Networks []containermetadata.Network `json:"Networks,omitempty"`
Volumes []VolumeResponse `json:"Volumes,omitempty"`
}

// VolumeResponse is the schema for the volume response JSON object
type VolumeResponse struct {
DockerName string `json:"DockerName,omitempty"`
Source string `json:"Source,omitempty"`
Destination string `json:"Destination,omitempty"`
}

// PortResponse defines the schema for portmapping response JSON
// object.
type PortResponse struct {
ContainerPort uint16 `json:"ContainerPort,omitempty"`
Protocol string `json:"Protocol,omitempty"`
HostPort uint16 `json:"HostPort,omitempty"`
HostIp string `json:"HostIp,omitempty"`
DockerID string `json:"DockerId"`
DockerName string `json:"DockerName"`
Name string `json:"Name"`
Ports []tmdsresponse.PortResponse `json:"Ports,omitempty"`
Networks []tmdsresponse.Network `json:"Networks,omitempty"`
Volumes []tmdsresponse.VolumeResponse `json:"Volumes,omitempty"`
}

// NewTaskResponse creates a TaskResponse for a task.
Expand Down Expand Up @@ -113,7 +97,7 @@ func NewContainerResponse(dockerContainer *apicontainer.DockerContainer, eni *ap
resp.Volumes = NewVolumesResponse(dockerContainer)

if eni != nil {
resp.Networks = []containermetadata.Network{
resp.Networks = []tmdsresponse.Network{
{
NetworkMode: utils.NetworkModeAWSVPC,
IPv4Addresses: eni.GetIPV4Addresses(),
Expand All @@ -125,9 +109,9 @@ func NewContainerResponse(dockerContainer *apicontainer.DockerContainer, eni *ap
}

// NewPortBindingsResponse creates PortResponse for a container.
func NewPortBindingsResponse(dockerContainer *apicontainer.DockerContainer, eni *apieni.ENI) []PortResponse {
func NewPortBindingsResponse(dockerContainer *apicontainer.DockerContainer, eni *apieni.ENI) []tmdsresponse.PortResponse {
container := dockerContainer.Container
resp := []PortResponse{}
resp := []tmdsresponse.PortResponse{}

bindings := container.GetKnownPortBindings()

Expand All @@ -138,7 +122,7 @@ func NewPortBindingsResponse(dockerContainer *apicontainer.DockerContainer, eni
}

for _, binding := range bindings {
port := PortResponse{
port := tmdsresponse.PortResponse{
ContainerPort: binding.ContainerPort,
Protocol: binding.Protocol.String(),
}
Expand All @@ -155,14 +139,14 @@ func NewPortBindingsResponse(dockerContainer *apicontainer.DockerContainer, eni
}

// NewVolumesResponse creates VolumeResponse for a container
func NewVolumesResponse(dockerContainer *apicontainer.DockerContainer) []VolumeResponse {
func NewVolumesResponse(dockerContainer *apicontainer.DockerContainer) []tmdsresponse.VolumeResponse {
container := dockerContainer.Container
var resp []VolumeResponse
var resp []tmdsresponse.VolumeResponse

volumes := container.GetVolumes()

for _, volume := range volumes {
volResp := VolumeResponse{
volResp := tmdsresponse.VolumeResponse{
DockerName: volume.Name,
Source: volume.Source,
Destination: volume.Destination,
Expand Down
48 changes: 24 additions & 24 deletions agent/handlers/v2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (

"github.com/aws/amazon-ecs-agent/agent/api"
apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
v1 "github.com/aws/amazon-ecs-agent/agent/handlers/v1"
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/cihub/seelog"
Expand Down Expand Up @@ -53,27 +53,27 @@ type TaskResponse struct {
// ContainerResponse defines the schema for the container response
// JSON object
type ContainerResponse struct {
ID string `json:"DockerId"`
Name string `json:"Name"`
DockerName string `json:"DockerName"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
Ports []v1.PortResponse `json:"Ports,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
ExitCode *int `json:"ExitCode,omitempty"`
Limits LimitsResponse `json:"Limits"`
CreatedAt *time.Time `json:"CreatedAt,omitempty"`
StartedAt *time.Time `json:"StartedAt,omitempty"`
FinishedAt *time.Time `json:"FinishedAt,omitempty"`
Type string `json:"Type"`
Networks []containermetadata.Network `json:"Networks,omitempty"`
Health *apicontainer.HealthStatus `json:"Health,omitempty"`
Volumes []v1.VolumeResponse `json:"Volumes,omitempty"`
LogDriver string `json:"LogDriver,omitempty"`
LogOptions map[string]string `json:"LogOptions,omitempty"`
ContainerARN string `json:"ContainerARN,omitempty"`
ID string `json:"DockerId"`
Name string `json:"Name"`
DockerName string `json:"DockerName"`
Image string `json:"Image"`
ImageID string `json:"ImageID"`
Ports []tmdsresponse.PortResponse `json:"Ports,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
ExitCode *int `json:"ExitCode,omitempty"`
Limits LimitsResponse `json:"Limits"`
CreatedAt *time.Time `json:"CreatedAt,omitempty"`
StartedAt *time.Time `json:"StartedAt,omitempty"`
FinishedAt *time.Time `json:"FinishedAt,omitempty"`
Type string `json:"Type"`
Networks []tmdsresponse.Network `json:"Networks,omitempty"`
Health *apicontainer.HealthStatus `json:"Health,omitempty"`
Volumes []tmdsresponse.VolumeResponse `json:"Volumes,omitempty"`
LogDriver string `json:"LogDriver,omitempty"`
LogOptions map[string]string `json:"LogOptions,omitempty"`
ContainerARN string `json:"ContainerARN,omitempty"`
}

// LimitsResponse defines the schema for task/cpu limits response
Expand Down Expand Up @@ -272,7 +272,7 @@ func NewContainerResponse(
}

for _, binding := range container.GetKnownPortBindings() {
port := v1.PortResponse{
port := tmdsresponse.PortResponse{
ContainerPort: binding.ContainerPort,
Protocol: binding.Protocol.String(),
}
Expand All @@ -289,7 +289,7 @@ func NewContainerResponse(
}

if eni != nil {
resp.Networks = []containermetadata.Network{
resp.Networks = []tmdsresponse.Network{
{
NetworkMode: utils.NetworkModeAWSVPC,
IPv4Addresses: eni.GetIPV4Addresses(),
Expand Down
10 changes: 5 additions & 5 deletions agent/handlers/v3/container_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"fmt"
"net/http"

"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
v2 "github.com/aws/amazon-ecs-agent/agent/handlers/v2"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
"github.com/cihub/seelog"
"github.com/pkg/errors"
Expand Down Expand Up @@ -78,7 +78,7 @@ func GetContainerResponse(containerID string, state dockerstate.TaskEngineState)
}

// GetContainerNetworkMetadata returns the network metadata for the container
func GetContainerNetworkMetadata(containerID string, state dockerstate.TaskEngineState) ([]containermetadata.Network, error) {
func GetContainerNetworkMetadata(containerID string, state dockerstate.TaskEngineState) ([]tmdsresponse.Network, error) {
dockerContainer, ok := state.ContainerByID(containerID)
if !ok {
return nil, errors.Errorf("Unable to find container '%s'", containerID)
Expand All @@ -98,17 +98,17 @@ func GetContainerNetworkMetadata(containerID string, state dockerstate.TaskEngin

// Extensive Network information is not available for Docker API versions 1.17-1.20
// Instead we only get the details of the first network
networks := make([]containermetadata.Network, 0)
networks := make([]tmdsresponse.Network, 0)
if len(settings.Networks) > 0 {
for modeFromSettings, containerNetwork := range settings.Networks {
networkMode := modeFromSettings
ipv4Addresses := []string{containerNetwork.IPAddress}
network := containermetadata.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}
network := tmdsresponse.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}
networks = append(networks, network)
}
} else {
ipv4Addresses := []string{ipv4AddressFromSettings}
network := containermetadata.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}
network := tmdsresponse.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}
networks = append(networks, network)
}
return networks, nil
Expand Down
6 changes: 3 additions & 3 deletions agent/handlers/v4/container_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"fmt"
"net/http"

"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"
tmdsresponse "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/response"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
"github.com/cihub/seelog"
"github.com/pkg/errors"
Expand Down Expand Up @@ -103,12 +103,12 @@ func GetContainerNetworkMetadata(containerID string, state dockerstate.TaskEngin
for modeFromSettings, containerNetwork := range settings.Networks {
networkMode := modeFromSettings
ipv4Addresses := []string{containerNetwork.IPAddress}
network := Network{Network: containermetadata.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}}
network := Network{Network: tmdsresponse.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses}}
networks = append(networks, network)
}
} else {
ipv4Addresses := []string{ipv4AddressFromSettings}
network := Network{Network: containermetadata.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}}
network := Network{Network: tmdsresponse.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses}}
networks = append(networks, network)
}
return networks, nil
Expand Down
Loading

0 comments on commit f04f424

Please sign in to comment.