-
Notifications
You must be signed in to change notification settings - Fork 618
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
v3 metadata networks response for non-awsvpc tasks #1833
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,12 @@ import ( | |
"fmt" | ||
"net/http" | ||
|
||
"github.com/aws/amazon-ecs-agent/agent/containermetadata" | ||
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" | ||
"github.com/aws/amazon-ecs-agent/agent/handlers/utils" | ||
"github.com/aws/amazon-ecs-agent/agent/handlers/v2" | ||
"github.com/cihub/seelog" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ContainerMetadataPath specifies the relative URI path for serving container metadata. | ||
|
@@ -37,10 +39,66 @@ func ContainerMetadataHandler(state dockerstate.TaskEngineState) func(http.Respo | |
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerMetadata) | ||
return | ||
} | ||
|
||
containerResponse, err := GetContainerResponse(containerID, state) | ||
if err != nil { | ||
errResponseJSON, _ := json.Marshal(err.Error()) | ||
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerMetadata) | ||
return | ||
} | ||
seelog.Infof("V3 container metadata handler: writing response for container '%s'", containerID) | ||
|
||
// v3 handler shares the same container metadata response format with v2 handler. | ||
v2.WriteContainerMetadataResponse(w, containerID, state) | ||
responseJSON, _ := json.Marshal(containerResponse) | ||
utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeContainerMetadata) | ||
} | ||
} | ||
|
||
// GetContainerResponse gets container response for v3 metadata | ||
func GetContainerResponse(containerID string, state dockerstate.TaskEngineState) (*v2.ContainerResponse, error) { | ||
containerResponse, err := v2.NewContainerResponse(containerID, state) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for my understanding - why aren't we keeping the logic to build up the responses in same question for the task response structure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the network information is populated in v2's |
||
if err != nil { | ||
return nil, errors.Errorf("Unable to generate metadata for container '%s'", containerID) | ||
} | ||
// fill in network details if not set | ||
if containerResponse.Networks == nil { | ||
if containerResponse.Networks, err = GetContainerNetworkMetadata(containerID, state); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return containerResponse, nil | ||
} | ||
|
||
// GetContainerNetworkMetadata returns the network metadata for the container | ||
func GetContainerNetworkMetadata(containerID string, state dockerstate.TaskEngineState) ([]containermetadata.Network, error) { | ||
dockerContainer, ok := state.ContainerByID(containerID) | ||
if !ok { | ||
return nil, errors.Errorf("Unable to find container '%s'", containerID) | ||
} | ||
// the logic here has been reused from | ||
// https://github.com/aws/amazon-ecs-agent/blob/0c8913ba33965cf6ffdd6253fad422458d9346bd/agent/containermetadata/parse_metadata.go#L123 | ||
settings := dockerContainer.Container.GetNetworkSettings() | ||
if settings == nil { | ||
return nil, errors.Errorf("Unable to generate network response for container '%s'", containerID) | ||
} | ||
// This metadata is the information provided in older versions of the API | ||
// We get the NetworkMode (Network interface name) from the HostConfig because this | ||
// this is the network with which the container is created | ||
ipv4AddressFromSettings := settings.IPAddress | ||
networkModeFromHostConfig := dockerContainer.Container.GetNetworkMode() | ||
|
||
// 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) | ||
if len(settings.Networks) > 0 { | ||
for modeFromSettings, containerNetwork := range settings.Networks { | ||
networkMode := modeFromSettings | ||
ipv4Addresses := []string{containerNetwork.IPAddress} | ||
network := containermetadata.Network{NetworkMode: networkMode, IPv4Addresses: ipv4Addresses} | ||
networks = append(networks, network) | ||
} | ||
} else { | ||
ipv4Addresses := []string{ipv4AddressFromSettings} | ||
network := containermetadata.Network{NetworkMode: networkModeFromHostConfig, IPv4Addresses: ipv4Addresses} | ||
networks = append(networks, network) | ||
} | ||
return networks, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: did you run
go fmt
on this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did and doesn't report of anything different