Skip to content

Commit

Permalink
app: use sdk awserr for imds
Browse files Browse the repository at this point in the history
  • Loading branch information
adnxn committed Oct 21, 2019
1 parent ead37eb commit 6125b11
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 50 deletions.
12 changes: 5 additions & 7 deletions agent/app/agent_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions agent/app/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"sync"
"testing"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 0 additions & 40 deletions agent/ec2/error.go

This file was deleted.

0 comments on commit 6125b11

Please sign in to comment.