Skip to content

Commit

Permalink
Consume v4 container metadata handler from ecs-agent module (#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
amogh09 committed Jun 2, 2023
1 parent 682a8dd commit 780efae
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 95 deletions.
9 changes: 7 additions & 2 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
59 changes: 0 additions & 59 deletions agent/handlers/v4/container_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions agent/handlers/v4/tmdsstate.go
Original file line number Diff line number Diff line change
@@ -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
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 780efae

Please sign in to comment.