agent/handlers/v3/container_metadata_handler.go
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
}
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
}
func GetContainerNetworkMetadata ( containerID string , state dockerstate . TaskEngineState ) ( [ ] containermetadata . Network , error ) { dockerContainer , ok := state . ContainerByID ( containerID ) if ! ok { return nil , errors . Errorf ( " " , 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 ( " " , 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 }
// GetContainerNetworkMetadata returns the network metadata for the container
GetContainerNetworkMetadata returns the network metadata for the container