diff --git a/agent/app/agent_unix.go b/agent/app/agent_unix.go index cc4971b1948..b609b4c6631 100644 --- a/agent/app/agent_unix.go +++ b/agent/app/agent_unix.go @@ -17,12 +17,10 @@ package app import ( "fmt" - "net/http" asmfactory "github.com/aws/amazon-ecs-agent/agent/asm/factory" "github.com/aws/amazon-ecs-agent/agent/config" "github.com/aws/amazon-ecs-agent/agent/credentials" - "github.com/aws/amazon-ecs-agent/agent/ec2" "github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs" "github.com/aws/amazon-ecs-agent/agent/ecscni" "github.com/aws/amazon-ecs-agent/agent/engine" @@ -36,6 +34,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/taskresource" cgroup "github.com/aws/amazon-ecs-agent/agent/taskresource/cgroup/control" "github.com/aws/amazon-ecs-agent/agent/utils/ioutilwrapper" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/cihub/seelog" "github.com/pkg/errors" ) @@ -133,14 +132,13 @@ func (agent *ecsAgent) setVPCSubnet() (error, bool) { return nil, false } -// isInstanceLaunchedInVPC returns false when the http status code is set to -// 'not found' (404) when querying the vpc id from instance metadata +// isInstanceLaunchedInVPC returns false when the awserr returned is an EC2MetadataError +// when querying the vpc id from instance metadata func isInstanceLaunchedInVPC(err error) bool { - if metadataErr, ok := err.(*ec2.MetadataError); ok && - metadataErr.GetStatusCode() == http.StatusNotFound { + if aerr, ok := err.(awserr.Error); ok && + aerr.Code() == "EC2MetadataError" { return false } - return true } diff --git a/agent/app/agent_unix_test.go b/agent/app/agent_unix_test.go index 4ecf82ee519..c20d818e38c 100644 --- a/agent/app/agent_unix_test.go +++ b/agent/app/agent_unix_test.go @@ -19,7 +19,6 @@ import ( "context" "errors" "fmt" - "net/http" "reflect" "sync" "testing" @@ -28,7 +27,6 @@ import ( mock_oswrapper "github.com/aws/amazon-ecs-agent/agent/app/oswrapper/mocks" "github.com/aws/amazon-ecs-agent/agent/config" "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi" - "github.com/aws/amazon-ecs-agent/agent/ec2" mock_ec2 "github.com/aws/amazon-ecs-agent/agent/ec2/mocks" "github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs" "github.com/aws/amazon-ecs-agent/agent/ecscni" @@ -46,6 +44,7 @@ import ( "github.com/aws/amazon-ecs-agent/agent/taskresource/cgroup/control/mock_control" mock_mobypkgwrapper "github.com/aws/amazon-ecs-agent/agent/utils/mobypkgwrapper/mocks" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -271,7 +270,7 @@ func TestSetVPCSubnetClassicEC2(t *testing.T) { mockMetadata := mock_ec2.NewMockEC2MetadataClient(ctrl) gomock.InOrder( mockMetadata.EXPECT().PrimaryENIMAC().Return(mac, nil), - mockMetadata.EXPECT().VPCID(mac).Return("", ec2.NewMetadataError(http.StatusNotFound)), + mockMetadata.EXPECT().VPCID(mac).Return("", awserr.New("EC2MetadataError", "failed to make EC2Metadata request", nil)), ) agent := &ecsAgent{ec2MetadataClient: mockMetadata} err, ok := agent.setVPCSubnet() diff --git a/agent/ec2/error.go b/agent/ec2/error.go deleted file mode 100644 index 2937b9fe8ff..00000000000 --- a/agent/ec2/error.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017 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 ec2 - -import "fmt" - -// MetadataError is used to encapsulate the error returned because of non OK HTTP -// status codes in the response when querying the Instance Metadata Service -type MetadataError struct { - error - statusCode int -} - -// NewMetadataError returns a new MetadataError object -func NewMetadataError(statusCode int) *MetadataError { - return &MetadataError{ - statusCode: statusCode, - } -} - -// Error returns the error string -func (err *MetadataError) Error() string { - return fmt.Sprintf("ec2 metadata client: unsuccessful response from Metadata service: %v", err.statusCode) -} - -// GetStatusCode returns the http status code for the error from metadata -func (err *MetadataError) GetStatusCode() int { - return err.statusCode -}