Skip to content

Commit

Permalink
Error Handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
orsenthil committed Nov 28, 2024
1 parent 13cd8b3 commit ef1abe2
Showing 1 changed file with 47 additions and 67 deletions.
114 changes: 47 additions & 67 deletions pkg/awsutils/imds.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ func newIMDSRequestError(requestKey string, err error) *imdsRequestError {
}
}

// Constructor with explicit code and fault
func newIMDSRequestErrorWithDetails(requestKey string, err error, code string, fault smithy.ErrorFault) *imdsRequestError {
return &imdsRequestError{
requestKey: requestKey,
err: err,
code: code,
fault: fault,
}
}

func (e *imdsRequestError) Error() string {
return fmt.Sprintf("failed to retrieve %s from instance metadata %v", e.requestKey, e.err)
}
Expand Down Expand Up @@ -220,9 +210,11 @@ func (typedimds TypedIMDS) GetMAC(ctx context.Context) (string, error) {
func (typedimds TypedIMDS) GetMACs(ctx context.Context) ([]string, error) {
list, err := typedimds.getList(ctx, "network/interfaces/macs")
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -238,7 +230,9 @@ func (typedimds TypedIMDS) GetMACImdsFields(ctx context.Context, mac string) ([]
key := fmt.Sprintf("network/interfaces/macs/%s", mac)
list, err := typedimds.getList(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("%v", err)
return nil, imdsErr.err
}
Expand Down Expand Up @@ -318,9 +312,11 @@ func (typedimds TypedIMDS) GetSubnetID(ctx context.Context, mac string) (string,

// Now handle any errors, but return subnetID if it was read
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("Warning: %v", err)
return subnetID, imdsErr.err
return subnetID, newIMDSRequestError(err.Error(), err)
}
return "", err
}
Expand All @@ -346,9 +342,11 @@ func (typedimds TypedIMDS) GetVpcID(ctx context.Context, mac string) (string, er

// Handle errors but preserve any partial vpcID data
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("Warning: %v", err)
return vpcID, imdsErr.err
return vpcID, newIMDSRequestError(err.Error(), err)
}
return "", err
}
Expand All @@ -361,9 +359,11 @@ func (typedimds TypedIMDS) GetSecurityGroupIDs(ctx context.Context, mac string)
key := fmt.Sprintf("network/interfaces/macs/%s/security-group-ids", mac)
sgs, err := typedimds.getList(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("%v", err)
return sgs, imdsErr.err
return sgs, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand Down Expand Up @@ -459,9 +459,11 @@ func (typedimds TypedIMDS) GetLocalIPv4s(ctx context.Context, mac string) ([]net
key := fmt.Sprintf("network/interfaces/macs/%s/local-ipv4s", mac)
ips, err := typedimds.getIPs(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -472,22 +474,16 @@ func (typedimds TypedIMDS) GetLocalIPv4s(ctx context.Context, mac string) ([]net
func (typedimds TypedIMDS) GetIPv4Prefixes(ctx context.Context, mac string) ([]net.IPNet, error) {
key := fmt.Sprintf("network/interfaces/macs/%s/ipv4-prefix", mac)
prefixes, err := typedimds.getCIDRs(ctx, key)
var oe *smithy.OperationError
oe = new(smithy.OperationError)

if err != nil {
if errors.As(err, &oe) {
if IsNotFound(oe) {
return nil, nil
}
}

if imdsErr, ok := err.(*imdsRequestError); ok {
if IsNotFound(imdsErr.err) {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
if IsNotFound(err) {
return nil, nil
}
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -499,12 +495,14 @@ func (typedimds TypedIMDS) GetIPv6Prefixes(ctx context.Context, mac string) ([]n
key := fmt.Sprintf("network/interfaces/macs/%s/ipv6-prefix", mac)
prefixes, err := typedimds.getCIDRs(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
if IsNotFound(imdsErr.err) {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
if IsNotFound(err) {
return nil, nil
}
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -516,13 +514,15 @@ func (typedimds TypedIMDS) GetIPv6s(ctx context.Context, mac string) ([]net.IP,
key := fmt.Sprintf("network/interfaces/macs/%s/ipv6s", mac)
ips, err := typedimds.getIPs(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
if IsNotFound(imdsErr.err) {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
if IsNotFound(err) {
// No IPv6. Not an error, just a disappointment :(
return nil, nil
}
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -540,9 +540,11 @@ func (typedimds TypedIMDS) GetVPCIPv4CIDRBlocks(ctx context.Context, mac string)
key := fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv4-cidr-blocks", mac)
cidrs, err := typedimds.getCIDRs(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
log.Warnf("%v", err)
return cidrs, imdsErr.err
return cidrs, newIMDSRequestError(err.Error(), err)
}
return nil, err
}
Expand All @@ -554,13 +556,15 @@ func (typedimds TypedIMDS) GetVPCIPv6CIDRBlocks(ctx context.Context, mac string)
key := fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv6-cidr-blocks", mac)
ipnets, err := typedimds.getCIDRs(ctx, key)
if err != nil {
if imdsErr, ok := err.(*imdsRequestError); ok {
if IsNotFound(imdsErr.err) {
imdsErr := new(imdsRequestError)
oe := new(smithy.OperationError)
if errors.As(err, &imdsErr) || errors.As(err, &oe) {
if IsNotFound(err) {
// No IPv6. Not an error, just a disappointment :(
return nil, nil
}
log.Warnf("%v", err)
return nil, imdsErr.err
return nil, newIMDSRequestError(err.Error(), err)
}
return nil, nil
}
Expand All @@ -573,30 +577,6 @@ func (typedimds TypedIMDS) GetSubnetIPv6CIDRBlocks(ctx context.Context, mac stri
return typedimds.getCIDR(ctx, key)
}

func IsNotFoundResponse(err error) bool {
log.Warnf("IsNotFound 1: %v", err)
if err == nil {
return false
}

var re *awshttp.ResponseError
var oe *smithy.OperationError
var ae smithy.APIError
if errors.As(err, &oe) {
log.Warnf("IsNotFound 2: failed to call service: %s, operation: %s, error: %v", oe.Service(), oe.Operation(), oe.Unwrap())
}
if errors.As(err, &ae) {
log.Warnf("IsNotFound 3: code: %s, message: %s, fault: %s", ae.ErrorCode(), ae.ErrorMessage(), ae.ErrorFault().String())
}

if errors.As(err, &re) {
log.Warnf("IsNotFound: %v %d", re, re.Response.StatusCode)
return re.Response.StatusCode == http.StatusNotFound
}

return false
}

// IsNotFound returns true if the error was caused by an AWS API 404 response.
// We implement a Custom IMDS Error, so need to use APIError instead of HTTP Response Error
func IsNotFound(err error) bool {
Expand Down

0 comments on commit ef1abe2

Please sign in to comment.