diff --git a/agent/handlers/task_server_setup.go b/agent/handlers/task_server_setup.go index b0255d471ac..a36d8cb8b3e 100644 --- a/agent/handlers/task_server_setup.go +++ b/agent/handlers/task_server_setup.go @@ -29,9 +29,11 @@ import ( "github.com/aws/amazon-ecs-agent/agent/stats" "github.com/aws/amazon-ecs-agent/ecs-agent/credentials" auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" "github.com/aws/amazon-ecs-agent/ecs-agent/tmds" tmdsv1 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v1" tmdsv2 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v2" + tmdsv4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" "github.com/cihub/seelog" "github.com/gorilla/mux" @@ -135,8 +137,11 @@ func v4HandlersSetup(muxRouter *mux.Router, cluster string, availabilityZone string, vpcID string, - containerInstanceArn string) { - muxRouter.HandleFunc(v4.ContainerMetadataPath, v4.ContainerMetadataHandler(state)) + containerInstanceArn string, +) { + tmdsAgentState := v4.NewTMDSAgentState(state) + metricsFactory := metrics.NewNopEntryFactory() + muxRouter.HandleFunc(tmdsv4.ContainerMetadataPath(), tmdsv4.ContainerMetadataHandler(tmdsAgentState, metricsFactory)) muxRouter.HandleFunc(v4.TaskMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, vpcID, containerInstanceArn, false)) muxRouter.HandleFunc(v4.TaskWithTagsMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, vpcID, containerInstanceArn, true)) muxRouter.HandleFunc(v4.ContainerStatsPath, v4.ContainerStatsHandler(state, statsEngine)) diff --git a/agent/handlers/v4/container_metadata_handler.go b/agent/handlers/v4/container_metadata_handler.go index c74580e38fb..6123ce266c8 100644 --- a/agent/handlers/v4/container_metadata_handler.go +++ b/agent/handlers/v4/container_metadata_handler.go @@ -14,72 +14,13 @@ package v4 import ( - "encoding/json" - "fmt" - "net/http" - "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" tmdsv4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state" - "github.com/cihub/seelog" "github.com/pkg/errors" ) -// ContainerMetadataPath specifies the relative URI path for serving container metadata. -var ContainerMetadataPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) - -// ContainerMetadataHandler returns the handler method for handling container metadata requests. -func ContainerMetadataHandler(state dockerstate.TaskEngineState) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - containerID, err := v3.GetContainerIDByRequest(r, state) - if err != nil { - responseJSON, err := json.Marshal( - fmt.Sprintf("V4 container metadata handler: unable to get container ID from request: %s", err.Error())) - if e := utils.WriteResponseIfMarshalError(w, err); e != nil { - return - } - utils.WriteJSONToResponse(w, http.StatusNotFound, responseJSON, utils.RequestTypeContainerMetadata) - return - } - containerResponse, err := GetContainerResponse(containerID, state) - if err != nil { - errResponseJSON, err := json.Marshal(err.Error()) - if e := utils.WriteResponseIfMarshalError(w, err); e != nil { - return - } - utils.WriteJSONToResponse(w, http.StatusInternalServerError, errResponseJSON, utils.RequestTypeContainerMetadata) - return - } - seelog.Infof("V4 container metadata handler: writing response for container '%s'", containerID) - - responseJSON, err := json.Marshal(containerResponse) - if e := utils.WriteResponseIfMarshalError(w, err); e != nil { - return - } - utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeContainerMetadata) - } -} - -// GetContainerResponse gets container response for v4 metadata -func GetContainerResponse(containerID string, state dockerstate.TaskEngineState) (*tmdsv4.ContainerResponse, error) { - containerResponse, err := NewContainerResponse(containerID, state) - if err != nil { - seelog.Errorf("Unable to get container metadata for container '%s'", containerID) - return nil, errors.Errorf("unable to generate metadata for container '%s'", containerID) - } - - // fill in network details if not set for NON AWSVPC Task - 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) ([]tmdsv4.Network, error) { dockerContainer, ok := state.ContainerByID(containerID) diff --git a/agent/handlers/v4/tmdsstate.go b/agent/handlers/v4/tmdsstate.go new file mode 100644 index 00000000000..46cceb572ef --- /dev/null +++ b/agent/handlers/v4/tmdsstate.go @@ -0,0 +1,59 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +package v4 + +import ( + "fmt" + + "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" + tmdsv4 "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state" + + "github.com/cihub/seelog" +) + +// Implements AgentState interface for TMDS v4. +type TMDSAgentState struct { + state dockerstate.TaskEngineState +} + +func NewTMDSAgentState(state dockerstate.TaskEngineState) *TMDSAgentState { + return &TMDSAgentState{state: state} +} + +// Returns container metadata in v4 format for the container identified by the provided +// v3EndpointID. +func (s *TMDSAgentState) GetContainerMetadata(v3EndpointID string) (tmdsv4.ContainerResponse, error) { + // Get docker ID from the v3 endpoint ID. + containerID, ok := s.state.DockerIDByV3EndpointID(v3EndpointID) + if !ok { + return tmdsv4.ContainerResponse{}, tmdsv4.NewErrorLookupFailure(fmt.Sprintf( + "unable to get container ID from request: unable to get docker ID from v3 endpoint ID: %s", + v3EndpointID)) + } + + containerResponse, err := NewContainerResponse(containerID, s.state) + if err != nil { + seelog.Errorf("Unable to get container metadata for container '%s'", containerID) + return tmdsv4.ContainerResponse{}, tmdsv4.NewErrorMetadataFetchFailure(fmt.Sprintf( + "unable to generate metadata for container '%s'", containerID)) + } + + // fill in network details if not set for NON AWSVPC Task + if containerResponse.Networks == nil { + if containerResponse.Networks, err = GetContainerNetworkMetadata(containerID, s.state); err != nil { + return tmdsv4.ContainerResponse{}, tmdsv4.NewErrorMetadataFetchFailure(err.Error()) + } + } + + return *containerResponse, nil +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/field/constants.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/field/constants.go index 8b154e1dbec..6a4279957e2 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/field/constants.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/logger/field/constants.go @@ -14,38 +14,39 @@ package field const ( - TaskID = "task" - TaskARN = "taskARN" - Container = "container" - DockerId = "dockerId" - ManagedAgent = "managedAgent" - KnownStatus = "knownStatus" - KnownSent = "knownSent" - DesiredStatus = "desiredStatus" - SentStatus = "sentStatus" - FailedStatus = "failedStatus" - Sequence = "seqnum" - Reason = "reason" - Status = "status" - RuntimeID = "runtimeID" - Elapsed = "elapsed" - Resource = "resource" - Error = "error" - Event = "event" - Image = "image" - Volume = "volume" - Time = "time" - NetworkMode = "networkMode" - Cluster = "cluster" - ServiceName = "ServiceName" - TaskProtection = "TaskProtection" - ImageID = "imageID" - ImageNames = "imageNames" - ImageSizeBytes = "imageSizeBytes" - ImagePulledAt = "imagePulledAt" - ImageLastUsedAt = "imageLastUsedAt" - ImagePullSucceeded = "imagePullSucceeded" - ContainerName = "containerName" - ContainerImage = "containerImage" - ContainerExitCode = "containerExitCode" + TaskID = "task" + TaskARN = "taskARN" + Container = "container" + DockerId = "dockerId" + ManagedAgent = "managedAgent" + KnownStatus = "knownStatus" + KnownSent = "knownSent" + DesiredStatus = "desiredStatus" + SentStatus = "sentStatus" + FailedStatus = "failedStatus" + Sequence = "seqnum" + Reason = "reason" + Status = "status" + RuntimeID = "runtimeID" + Elapsed = "elapsed" + Resource = "resource" + Error = "error" + Event = "event" + Image = "image" + Volume = "volume" + Time = "time" + NetworkMode = "networkMode" + Cluster = "cluster" + ServiceName = "ServiceName" + TaskProtection = "TaskProtection" + ImageID = "imageID" + ImageNames = "imageNames" + ImageSizeBytes = "imageSizeBytes" + ImagePulledAt = "imagePulledAt" + ImageLastUsedAt = "imageLastUsedAt" + ImagePullSucceeded = "imagePullSucceeded" + ContainerName = "containerName" + ContainerImage = "containerImage" + ContainerExitCode = "containerExitCode" + TMDSEndpointContainerID = "tmdsEndpointContainerID" ) diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/constants.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/constants.go new file mode 100644 index 00000000000..5dfe4bb5c21 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/constants.go @@ -0,0 +1,25 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +package metrics + +const ( + // MetadataServer + metadataServerMetricNamespace = "MetadataServer" + GetCredentialsMetricName = metadataServerMetricNamespace + ".GetCredentials" + CrashErrorMetricName = metadataServerMetricNamespace + ".Crash" + ShutdownErrorMetricName = metadataServerMetricNamespace + ".ShutdownError" + InternalServerErrorMetricName = metadataServerMetricNamespace + ".InternalServerError" + GetTaskProtectionMetricName = metadataServerMetricNamespace + ".GetTaskProtection" + UpdateTaskProtectionMetricName = metadataServerMetricNamespace + ".UpdateTaskProtection" + AuthConfigMetricName = metadataServerMetricNamespace + ".AuthConfig" +) diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/generate_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/generate_mocks.go new file mode 100644 index 00000000000..7d689316d02 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/generate_mocks.go @@ -0,0 +1,16 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +//go:generate mockgen -destination=mocks/mock_entry.go -copyright_file=../../scripts/copyright_file . EntryFactory,Entry + +package metrics diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/metrics.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/metrics.go new file mode 100644 index 00000000000..d0b98013460 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/metrics/metrics.go @@ -0,0 +1,76 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package metrics + +// EntryFactory specifies the factory interface for creating new Metric Entry objects. +type EntryFactory interface { + New(op string) Entry + Flush() +} + +// Entry specifies methods that need to be implemented by a metrics entry object. +type Entry interface { + // WithFields allows the caller to set metric metadata. Once an entry object is + // created (typically using a meaningful metrics name), its properties can be + // manipulated using WithFields. Example: + // + // m := m.WithFields(map[string]interface{"timestamp": time.Now()}) + WithFields(f map[string]interface{}) Entry + // WithCount allows the caller to set metric counts. This is useful for determining + // if a particular operation has occurred or not. Example: + // + // item, ok := lookup(key) + // if ok { + // lookupSuccessMetric = lookupSuccessMetric.WithCount(1) + // // Use item + // } else { + // lookupSuccessMetric = lookupSuccessMetric.WithCount(0)) + // } + WithCount(count int) Entry + // WithGauge allows the caller to associate a specific value to the metric. This is useful + // for reporting numerical values related to any operation, for instance the data transfer + // rate for an image pull operation. + WithGauge(value interface{}) Entry + // Done makes a metric operation as complete. It records the end time of the operation + // and returns a function pointer that can be used to flush the metrics to a + // persistent store. Callers can optionally defer the function pointer. Example: + // + // defer lookupMetric.Done(nil) () + Done(err error) func() +} + +// nopEntryFactory implements the EntryFactory interface with no-ops. +type nopEntryFactory struct{} + +// NewNopEntryFactory creates a metric log entry factory that doesn't log any metrics. +func NewNopEntryFactory() EntryFactory { + return &nopEntryFactory{} +} + +func (*nopEntryFactory) New(op string) Entry { + return newNopEntry(op) +} + +func (f *nopEntryFactory) Flush() {} + +type nopEntry struct{} + +func newNopEntry(op string) Entry { + return &nopEntry{} +} + +func (e *nopEntry) WithFields(f map[string]interface{}) Entry { return e } +func (e *nopEntry) WithCount(count int) Entry { return e } +func (e *nopEntry) WithGauge(value interface{}) Entry { return e } +func (e *nopEntry) Done(err error) func() { return func() {} } diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils/helpers.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils/helpers.go index 7e9b23b71cc..15fddb21f56 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils/helpers.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils/helpers.go @@ -14,6 +14,7 @@ package utils import ( + "encoding/json" "fmt" "net/http" @@ -69,6 +70,22 @@ type ErrorMessage struct { HTTPErrorCode int } +// Marshals the provided response to JSON and writes it to the ResponseWriter with the provided +// status code and application/json Content-Type header. +// Writes an empty JSON '{}' response if JSON marshaling fails and logs the error. +func WriteJSONResponse( + w http.ResponseWriter, + httpStatusCode int, + response interface{}, + requestType string, +) { + responseJSON, err := json.Marshal(response) + if e := WriteResponseIfMarshalError(w, err); e != nil { + return + } + WriteJSONToResponse(w, httpStatusCode, responseJSON, requestType) +} + // WriteJSONToResponse writes the header, JSON response to a ResponseWriter, and // log the error if necessary. func WriteJSONToResponse(w http.ResponseWriter, httpStatusCode int, responseJSON []byte, requestType string) { @@ -133,3 +150,7 @@ func LimitReachedHandler(auditLogger audit.AuditLogger) func(http.ResponseWriter auditLogger.Log(logRequest, http.StatusTooManyRequests, "") } } + +func Is5XXStatus(statusCode int) bool { + return 500 <= statusCode && statusCode <= 599 +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/handlers.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/handlers.go new file mode 100644 index 00000000000..edfa4de1e1e --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/handlers.go @@ -0,0 +1,88 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +package v4 + +import ( + "errors" + "fmt" + "net/http" + + "github.com/aws/amazon-ecs-agent/ecs-agent/logger" + "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" + "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils" + state "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state" + + "github.com/gorilla/mux" +) + +// v3EndpointIDMuxName is the key that's used in gorilla/mux to get the v3 endpoint ID. +const ( + EndpointContainerIDMuxName = "endpointContainerIDMuxName" + version = "v4" +) + +// ContainerMetadataPath specifies the relative URI path for serving container metadata. +func ContainerMetadataPath() string { + return "/v4/" + utils.ConstructMuxVar(EndpointContainerIDMuxName, utils.AnythingButSlashRegEx) +} + +// ContainerMetadataHandler returns the HTTP handler function for handling container metadata requests. +func ContainerMetadataHandler( + agentState state.AgentState, + metricsFactory metrics.EntryFactory, +) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + endpointContainerID := mux.Vars(r)[EndpointContainerIDMuxName] + containerMetadata, err := agentState.GetContainerMetadata(endpointContainerID) + if err != nil { + logger.Error("Failed to get v4 container metadata", logger.Fields{ + field.TMDSEndpointContainerID: endpointContainerID, + field.Error: err, + }) + + responseCode, responseBody := getContainerErrorResponse(endpointContainerID, err) + utils.WriteJSONResponse(w, responseCode, responseBody, utils.RequestTypeContainerMetadata) + + if utils.Is5XXStatus(responseCode) { + metricsFactory.New(metrics.InternalServerErrorMetricName).Done(err)() + } + + return + } + + logger.Info("Writing response for v4 container metadata", logger.Fields{ + field.TMDSEndpointContainerID: endpointContainerID, + field.Container: containerMetadata.ID, + }) + utils.WriteJSONResponse(w, http.StatusOK, containerMetadata, utils.RequestTypeContainerMetadata) + } +} + +// Returns an appropriate HTTP response status code and body for the error. +func getContainerErrorResponse(endpointContainerID string, err error) (int, string) { + var errLookupFailure *state.ErrorLookupFailure + if errors.As(err, &errLookupFailure) { + return http.StatusNotFound, fmt.Sprintf("V4 container metadata handler: %s", + errLookupFailure.ExternalReason()) + } + + var errMetadataFetchFailure *state.ErrorMetadataFetchFailure + if errors.As(err, &errMetadataFetchFailure) { + return http.StatusInternalServerError, errMetadataFetchFailure.ExternalReason() + } + + logger.Error("Unknown error encountered when handling container metadata fetch failure", + logger.Fields{field.Error: err}) + return http.StatusInternalServerError, "failed to get container metadata" +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/generate_mocks.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/generate_mocks.go new file mode 100644 index 00000000000..ea7567ec810 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/generate_mocks.go @@ -0,0 +1,15 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +//go:generate mockgen -destination=mocks/state_mock.go -copyright_file=../../../../../scripts/copyright_file . AgentState +package state diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/state.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/state.go new file mode 100644 index 00000000000..ff83d3ce445 --- /dev/null +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state/state.go @@ -0,0 +1,60 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package state + +import "fmt" + +// Error to be returned when container or task lookup failed +type ErrorLookupFailure struct { + externalReason string // Reason to be included in the response +} + +func NewErrorLookupFailure(externalReason string) *ErrorLookupFailure { + return &ErrorLookupFailure{externalReason: externalReason} +} + +func (e *ErrorLookupFailure) ExternalReason() string { + return e.externalReason +} + +func (e *ErrorLookupFailure) Error() string { + return fmt.Sprintf("container lookup failed: %s", e.externalReason) +} + +// General "catch-all" error to be returned when container metadata could not be +// fetched for some reason +type ErrorMetadataFetchFailure struct { + externalReason string // Reason to be included in the response +} + +func NewErrorMetadataFetchFailure(externalReason string) *ErrorMetadataFetchFailure { + return &ErrorMetadataFetchFailure{externalReason: externalReason} +} + +func (e *ErrorMetadataFetchFailure) Error() string { + return fmt.Sprintf("container lookup failed: %s", e.externalReason) +} + +func (e *ErrorMetadataFetchFailure) ExternalReason() string { + return e.externalReason +} + +// Interface for interacting with Agent State relevant to TMDS +type AgentState interface { + // Returns container metadata in v4 format for the container identified by the + // provided endpointContinerID. + // Returns ErrorLookupFailure if container lookup fails. + // Returns ErrorMetadataFetchFailure if something else goes wrong. + GetContainerMetadata(endpointContainerID string) (ContainerResponse, error) +} diff --git a/agent/vendor/modules.txt b/agent/vendor/modules.txt index 3f6db267e23..220b3a73dcb 100644 --- a/agent/vendor/modules.txt +++ b/agent/vendor/modules.txt @@ -22,11 +22,13 @@ github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/mocks github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request github.com/aws/amazon-ecs-agent/ecs-agent/logger/field +github.com/aws/amazon-ecs-agent/ecs-agent/metrics github.com/aws/amazon-ecs-agent/ecs-agent/tmds 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/amazon-ecs-agent/ecs-agent/tmds/handlers/v1 github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v2 +github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4 github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state github.com/aws/amazon-ecs-agent/ecs-agent/tmds/logging github.com/aws/amazon-ecs-agent/ecs-agent/tmds/utils/mux