Skip to content

Commit

Permalink
refactor IMDS code
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanthvn committed Jun 22, 2021
1 parent 83f15fb commit 96dd1f6
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 194 deletions.
82 changes: 33 additions & 49 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)

if err != nil {
awsAPIErrInc("GetMetadata", err)
return "", err
return "", newIMDSRequestError(p, err)
}

return result, nil
Expand Down Expand Up @@ -407,52 +407,51 @@ func New(useCustomNetworking, enableIpv4PrefixDelegation bool) (*EC2InstanceMeta
// InitWithEC2metadata initializes the EC2InstanceMetadataCache with the data retrieved from EC2 metadata service
func (cache *EC2InstanceMetadataCache) initWithEC2Metadata(ctx context.Context) error {
var err error
var imdsError IMDSRequestError
// retrieve availability-zone
cache.availabilityZone, imdsError = cache.imds.GetAZ(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.availabilityZone, err = cache.imds.GetAZ(ctx)
if err != nil {
return err
}
log.Debugf("Found availability zone: %s ", cache.availabilityZone)

// retrieve eth0 local-ipv4
cache.localIPv4, imdsError = cache.imds.GetLocalIPv4(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.localIPv4, err = cache.imds.GetLocalIPv4(ctx)
if err != nil {
return err
}
log.Debugf("Discovered the instance primary ip address: %s", cache.localIPv4)

// retrieve instance-id
cache.instanceID, imdsError = cache.imds.GetInstanceID(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.instanceID, err = cache.imds.GetInstanceID(ctx)
if err != nil {
return err
}
log.Debugf("Found instance-id: %s ", cache.instanceID)

// retrieve instance-type
cache.instanceType, imdsError = cache.imds.GetInstanceType(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.instanceType, err = cache.imds.GetInstanceType(ctx)
if err != nil {
return err
}
log.Debugf("Found instance-type: %s ", cache.instanceType)

// retrieve primary interface's mac
mac, imdsError := cache.imds.GetMAC(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
mac, err := cache.imds.GetMAC(ctx)
if err != nil {
return err
}
cache.primaryENImac = mac
log.Debugf("Found primary interface's MAC address: %s", mac)

cache.primaryENI, imdsError = cache.imds.GetInterfaceID(ctx, mac)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.primaryENI, err = cache.imds.GetInterfaceID(ctx, mac)
if err != nil {
return errors.Wrap(err, "get instance metadata: failed to find primary ENI")
}
log.Debugf("%s is the primary ENI of this instance", cache.primaryENI)

// retrieve sub-id
cache.subnetID, imdsError = cache.imds.GetSubnetID(ctx, mac)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cache.subnetID, err = cache.imds.GetSubnetID(ctx, mac)
if err != nil {
return err
}
log.Debugf("Found subnet-id: %s ", cache.subnetID)
Expand All @@ -470,8 +469,8 @@ func (cache *EC2InstanceMetadataCache) initWithEC2Metadata(ctx context.Context)
func (cache *EC2InstanceMetadataCache) RefreshSGIDs(mac string) error {
ctx := context.TODO()

sgIDs, imdsError := cache.imds.GetSecurityGroupIDs(ctx, mac)
if err := IMDSResponseErrorHandler(imdsError); err != nil {
sgIDs, err := cache.imds.GetSecurityGroupIDs(ctx, mac)
if err != nil {
return err
}

Expand Down Expand Up @@ -543,8 +542,8 @@ func (cache *EC2InstanceMetadataCache) GetAttachedENIs() (eniList []ENIMetadata,
ctx := context.TODO()

// retrieve number of interfaces
macs, imdsError := cache.imds.GetMACs(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
macs, err := cache.imds.GetMACs(ctx)
if err != nil {
return nil, err
}
log.Debugf("Total number of interfaces found: %d ", len(macs))
Expand All @@ -567,18 +566,18 @@ func (cache *EC2InstanceMetadataCache) getENIMetadata(eniMAC string) (ENIMetadat
var err error
var deviceNum int

eniID, imdsError := cache.imds.GetInterfaceID(ctx, eniMAC)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
eniID, err := cache.imds.GetInterfaceID(ctx, eniMAC)
if err != nil {
return ENIMetadata{}, err
}

deviceNum, imdsError = cache.imds.GetDeviceNumber(ctx, eniMAC)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
deviceNum, err = cache.imds.GetDeviceNumber(ctx, eniMAC)
if err != nil {
return ENIMetadata{}, err
}

primaryMAC, imdsError := cache.imds.GetMAC(ctx)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
primaryMAC, err := cache.imds.GetMAC(ctx)
if err != nil {
return ENIMetadata{}, err
}
if eniMAC == primaryMAC && deviceNum != 0 {
Expand All @@ -589,13 +588,13 @@ func (cache *EC2InstanceMetadataCache) getENIMetadata(eniMAC string) (ENIMetadat

log.Debugf("Found ENI: %s, MAC %s, device %d", eniID, eniMAC, deviceNum)

cidr, imdsError := cache.imds.GetSubnetIPv4CIDRBlock(ctx, eniMAC)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
cidr, err := cache.imds.GetSubnetIPv4CIDRBlock(ctx, eniMAC)
if err != nil {
return ENIMetadata{}, err
}

imdsIPv4s, imdsError := cache.imds.GetLocalIPv4s(ctx, eniMAC)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
imdsIPv4s, err := cache.imds.GetLocalIPv4s(ctx, eniMAC)
if err != nil {
return ENIMetadata{}, err
}

Expand All @@ -613,8 +612,8 @@ func (cache *EC2InstanceMetadataCache) getENIMetadata(eniMAC string) (ENIMetadat
//call attached ENIs, the call will return prefix not found in the logs and that will pollute
//ipamd.log hence skipping.
if (eniMAC == primaryMAC && !cache.useCustomNetworking) || (eniMAC != primaryMAC) {
imdsIPv4Prefixes, imdsError := cache.imds.GetLocalIPv4Prefixes(ctx, eniMAC)
if err = IMDSResponseErrorHandler(imdsError); err != nil {
imdsIPv4Prefixes, err := cache.imds.GetLocalIPv4Prefixes(ctx, eniMAC)
if err != nil {
return ENIMetadata{}, err
}
for _, ipv4prefix := range imdsIPv4Prefixes {
Expand Down Expand Up @@ -1592,8 +1591,8 @@ func (cache *EC2InstanceMetadataCache) getLeakedENIs() ([]*ec2.NetworkInterface,
func (cache *EC2InstanceMetadataCache) GetVPCIPv4CIDRs() ([]string, error) {
ctx := context.TODO()

ipnets, imdsError := cache.imds.GetVPCIPv4CIDRBlocks(ctx, cache.primaryENImac)
if err := IMDSResponseErrorHandler(imdsError); err != nil {
ipnets, err := cache.imds.GetVPCIPv4CIDRBlocks(ctx, cache.primaryENImac)
if err != nil {
return nil, err
}

Expand Down Expand Up @@ -1672,18 +1671,3 @@ func (cache *EC2InstanceMetadataCache) IsCNIUnmanagedENI(eniID string) bool {
}
return false
}

func IMDSResponseErrorHandler(imdsError IMDSRequestError) error {
if imdsError == (IMDSRequestError{}) {
return nil
}
err := imdsError.err
key := imdsError.requestKey

if err == nil {
return nil
}

log.Warnf("Failed to retrieve %s from instance metadata %v", key, err)
return err
}
Loading

0 comments on commit 96dd1f6

Please sign in to comment.