Skip to content

Commit

Permalink
Adding debug logs and improving comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
orsenthil committed Dec 2, 2024
1 parent ef1abe2 commit 7a9a7a7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 25 deletions.
4 changes: 1 addition & 3 deletions pkg/awsutils/awssession/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func getHTTPTimeout() time.Duration {
return httpTimeoutValue
}

// New will return an session for service clients
// New will return aws.Config to be used by Service Clients.
func New(ctx context.Context) (aws.Config, error) {
customHTTPClient := &http.Client{
Timeout: getHTTPTimeout()}
Expand All @@ -76,8 +76,6 @@ func New(ctx context.Context) (aws.Config, error) {
}

endpoint := os.Getenv("AWS_EC2_ENDPOINT")

//TODO (senthilx) - The endpoint resolver is using deprecated method, this should be moved to the services.
if endpoint != "" {
optFns = append(optFns, config.WithEndpointResolver(aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
Expand Down
7 changes: 3 additions & 4 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ type ENIMetadata struct {
// PrimaryIPv4Address returns the primary IPv4 address of this node
func (eni ENIMetadata) PrimaryIPv4Address() string {
for _, addr := range eni.IPv4Addresses {
if addr.Primary != nil && *addr.Primary {
return *addr.PrivateIpAddress
if addr.Primary != nil && aws.ToBool(addr.Primary) {
return aws.ToString(addr.PrivateIpAddress)
}
}
return ""
Expand Down Expand Up @@ -357,7 +357,6 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)
return "", newIMDSRequestError(p, err)
}

// Read the content
defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand Down Expand Up @@ -400,7 +399,6 @@ func New(useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Ena
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %v", err)
}
// TODO (senthilx) - Revisit this.
ec2SVC := ec2wrapper.New(awsCfg)
cache.ec2SVC = ec2SVC
err = cache.initWithEC2Metadata(ctx)
Expand Down Expand Up @@ -2138,6 +2136,7 @@ func (cache *EC2InstanceMetadataCache) IsPrimaryENI(eniID string) bool {
}

func checkAPIErrorAndBroadcastEvent(err error, api string) {
log.Debugf("checkAPIErrorAndBroadcastEvent resulted in %v", err)
if errors.As(err, &awsAPIError) {
if awsAPIError.ErrorCode() == "UnauthorizedOperation" {
if eventRecorder := eventrecorder.Get(); eventRecorder != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/awsutils/imds.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func (typedimds TypedIMDS) getList(ctx context.Context, key string) ([]string, e
return nil, newIMDSRequestError(key, fmt.Errorf("empty response"))
}

// Read the content
defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand All @@ -135,7 +134,7 @@ func (typedimds TypedIMDS) GetAZ(ctx context.Context) (string, error) {
if output == nil || output.Content == nil {
return "", newIMDSRequestError("placement/availability-zone", fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand All @@ -151,10 +150,11 @@ func (typedimds TypedIMDS) GetInstanceType(ctx context.Context) (string, error)
if err != nil {
return "", err
}

if output == nil || output.Content == nil {
return "", newIMDSRequestError("instance-type", fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand All @@ -175,10 +175,11 @@ func (typedimds TypedIMDS) GetInstanceID(ctx context.Context) (string, error) {
if err != nil {
return "", err
}

if output == nil || output.Content == nil {
return "", newIMDSRequestError("instance-id", fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand All @@ -197,7 +198,7 @@ func (typedimds TypedIMDS) GetMAC(ctx context.Context) (string, error) {
if output == nil || output.Content == nil {
return "", newIMDSRequestError("mac", fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand Down Expand Up @@ -256,7 +257,7 @@ func (typedimds TypedIMDS) GetInterfaceID(ctx context.Context, mac string) (stri
if output == nil || output.Content == nil {
return "", newIMDSRequestError(key, fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand All @@ -274,7 +275,7 @@ func (typedimds TypedIMDS) getInt(ctx context.Context, key string) (int, error)
if output == nil || output.Content == nil {
return 0, newIMDSRequestError(key, fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand Down Expand Up @@ -379,7 +380,7 @@ func (typedimds TypedIMDS) getIP(ctx context.Context, key string) (net.IP, error
if output == nil || output.Content == nil {
return nil, newIMDSRequestError(key, fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand Down Expand Up @@ -419,7 +420,7 @@ func (typedimds TypedIMDS) getCIDR(ctx context.Context, key string) (net.IPNet,
if output == nil || output.Content == nil {
return net.IPNet{}, newIMDSRequestError(key, fmt.Errorf("empty response"))
}
// Read the content

defer output.Content.Close()
bytes, err := io.ReadAll(output.Content)
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions pkg/ec2wrapper/ec2wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ec2wrapper
import (
"context"

"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/awssession"
"github.com/aws/amazon-vpc-cni-k8s/pkg/ec2metadatawrapper"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -33,12 +32,6 @@ type EC2Wrapper struct {
// NewMetricsClient returns an instance of the EC2 wrapper
func NewMetricsClient() (*EC2Wrapper, error) {
ctx := context.TODO()
// TODO (senthilx) - how do I use this awsconfig at line 50
awsconfig, err := awssession.New(ctx)
log.Infof("loading default config %v", awsconfig)
if err != nil {
return &EC2Wrapper{}, err
}
ec2MetadataClient, err := ec2metadatawrapper.New(ctx)
if err != nil {
return &EC2Wrapper{}, err
Expand Down
2 changes: 2 additions & 0 deletions pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func prometheusRegister() {

// containsInsufficientCIDRsOrSubnetIPs returns whether a CIDR cannot be carved in the subnet or subnet is running out of IP addresses
func containsInsufficientCIDRsOrSubnetIPs(err error) bool {
log.Debugf("containsInsufficientCIDRsOrSubnetIPs encountered %v", err)
var apiErr smithy.APIError
// IP exhaustion can be due to Insufficient Cidr blocks or Insufficient Free Address in a Subnet
// In these 2 cases we will back off for 2 minutes before retrying
Expand All @@ -319,6 +320,7 @@ func containsInsufficientCIDRsOrSubnetIPs(err error) bool {

// containsPrivateIPAddressLimitExceededError returns whether exceeds ENI's IP address limit
func containsPrivateIPAddressLimitExceededError(err error) bool {
log.Debugf("containsPrivateIPAddressLimitExceededError encountered %v", err)
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
return apiErr.ErrorCode() == "PrivateIpAddressLimitExceeded"
Expand Down
2 changes: 1 addition & 1 deletion pkg/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
}
)

// cloudWatchAPI is a custom interface that includes only the methods we need from the CloudWatch API
// cloudWatchAPI defines the interface with methods required from CloudWatch Service
type cloudWatchAPI interface {
PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error)
}
Expand Down
2 changes: 1 addition & 1 deletion test/framework/resources/aws/services/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *defaultCloudFormation) WaitTillStackCreated(ctx context.Context, stackN
}

var describeStackOutput *cloudformation.DescribeStacksOutput
// Using the provided ctx, ctx.Done() allows wait.PollImmediateUtil to cancel if required.
// Using the provided ctx, ctx.Done() allows wait.PollImmediateUtil to cancel
err = wait.PollImmediateUntil(utils.PollIntervalLong, func() (done bool, err error) {
describeStackOutput, err = d.client.DescribeStacks(ctx, describeStackInput)
if err != nil {
Expand Down

0 comments on commit 7a9a7a7

Please sign in to comment.