Skip to content

Commit

Permalink
Migrate aws sdk go v1 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddhabang committed Sep 5, 2024
1 parent e1d32f4 commit 3a5ffdb
Show file tree
Hide file tree
Showing 164 changed files with 6,380 additions and 46,083 deletions.
4 changes: 2 additions & 2 deletions controllers/elbv2/targetgroupbinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
discv1 "k8s.io/api/discovery/v1"
Expand Down Expand Up @@ -138,7 +138,7 @@ func (r *targetGroupBindingReconciler) cleanupTargetGroupBinding(ctx context.Con
}

func (r *targetGroupBindingReconciler) updateTargetGroupBindingStatus(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
if aws.Int64Value(tgb.Status.ObservedGeneration) == tgb.Generation {
if aws.ToInt64(tgb.Status.ObservedGeneration) == tgb.Generation {
return nil
}
tgbOld := tgb.DeepCopy()
Expand Down
23 changes: 21 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module sigs.k8s.io/aws-load-balancer-controller

go 1.22.3
go 1.22.4

require (
github.com/aws/aws-sdk-go v1.50.8
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11
github.com/aws/aws-sdk-go-v2/service/acm v1.28.4
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.23.3
github.com/aws/aws-sdk-go-v2/service/shield v1.27.3
github.com/aws/aws-sdk-go-v2/service/wafregional v1.23.3
github.com/aws/aws-sdk-go-v2/service/wafv2 v1.51.4
github.com/aws/smithy-go v1.20.3
github.com/evanphx/json-patch v5.7.0+incompatible
github.com/gavv/httpexpect/v2 v2.9.0
github.com/go-logr/logr v1.4.1
Expand Down Expand Up @@ -41,6 +51,15 @@ require (
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.27 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand Down
42 changes: 40 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,46 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/aws/aws-sdk-go v1.50.8 h1:gY0WoOW+/Wz6XmYSgDH9ge3wnAevYDSQWPxxJvqAkP4=
github.com/aws/aws-sdk-go v1.50.8/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90=
github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg=
github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI=
github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/acm v1.28.4 h1:wiW1Y6/1lysA0eJZRq0I53YYKuV9MNAzL15z2eZRlEE=
github.com/aws/aws-sdk-go-v2/service/acm v1.28.4/go.mod h1:bzjymHHRhexkSMIvUHMpKydo9U82bmqQ5ru0IzYM8m8=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0 h1:ta62lid9JkIpKZtZZXSj6rP2AqY5x1qYGq53ffxqD9Q=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0/go.mod h1:o6QDjdVKpP5EF0dp/VlvqckzuSDATr1rLdHt3A5m0YY=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0 h1:8rDRtPOu3ax8jEctw7G926JQlnFdhZZA4KJzQ+4ks3Q=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0/go.mod h1:L5bVuO4PeXuDuMYZfL3IW69E6mz6PDCYpp6IKDlcLMA=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII=
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.23.3 h1:ByynKMsGZGmpUpnQ99y+lS7VxZrNt3mdagCnHd011Kk=
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.23.3/go.mod h1:ZR4h87npHPuVQ2SEeoWMe+CO/HcS9g2iYMLnT5HawW8=
github.com/aws/aws-sdk-go-v2/service/shield v1.27.3 h1:SfjI6FuphzspGPvcRD8hjMD6wLUAE6vtJLGrui19j2s=
github.com/aws/aws-sdk-go-v2/service/shield v1.27.3/go.mod h1:JpxjPa91y1hRb3G8xxzhOQFcK/r90it41jA/hD0q+Gg=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ=
github.com/aws/aws-sdk-go-v2/service/wafregional v1.23.3 h1:7dr6En0/6KRFoz8VmnYks9dVvL+tkL5RjRrxqGzr1zI=
github.com/aws/aws-sdk-go-v2/service/wafregional v1.23.3/go.mod h1:24TtlRsv4LKAE3VnRJQhpatr8cpX0yj8NSzg8/lxOCw=
github.com/aws/aws-sdk-go-v2/service/wafv2 v1.51.4 h1:1khBA5uryBRJoCb4G2iR5RT06BkfPEjjDCHAiRb8P3Q=
github.com/aws/aws-sdk-go-v2/service/wafv2 v1.51.4/go.mod h1:QpFImaPGKNwa+MiZ+oo6LbV1PVQBapc0CnrAMRScoxM=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
18 changes: 18 additions & 0 deletions pkg/annotations/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Parser interface {
// returns whether annotation exists and parser error if any.
ParseInt64Annotation(annotation string, value *int64, annotations map[string]string, opts ...ParseOption) (bool, error)

// ParseInt32Annotation parses annotation into int32 value,
// returns whether annotation exists and parser error if any.
ParseInt32Annotation(annotation string, value *int32, annotations map[string]string, opts ...ParseOption) (bool, error)

// ParseStringSliceAnnotation parses comma separated values from the annotation into string slice
// returns true if the annotation exists
ParseStringSliceAnnotation(annotation string, value *[]string, annotations map[string]string, opts ...ParseOption) bool
Expand Down Expand Up @@ -106,6 +110,20 @@ func (p *suffixAnnotationParser) ParseInt64Annotation(annotation string, value *
return true, nil
}

func (p *suffixAnnotationParser) ParseInt32Annotation(annotation string, value *int32, annotations map[string]string, opts ...ParseOption) (bool, error) {
raw := ""
exists, matchedKey := p.parseStringAnnotation(annotation, &raw, annotations, opts...)
if !exists {
return false, nil
}
i, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return true, errors.Wrapf(err, "failed to parse int32 annotation, %v: %v", matchedKey, raw)
}
*value = int32(i)
return true, nil
}

func (p *suffixAnnotationParser) ParseStringSliceAnnotation(annotation string, value *[]string, annotations map[string]string, opts ...ParseOption) bool {
raw := ""
if exists, _ := p.parseStringAnnotation(annotation, &raw, annotations, opts...); !exists {
Expand Down
119 changes: 62 additions & 57 deletions pkg/aws/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package aws
import (
"context"
"fmt"
awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-sdk-go-v2/config"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
smithymiddleware "github.com/aws/smithy-go/middleware"
"net"
"os"
"sigs.k8s.io/aws-load-balancer-controller/pkg/version"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
amerrors "k8s.io/apimachinery/pkg/util/errors"
epresolver "sigs.k8s.io/aws-load-balancer-controller/pkg/aws/endpoints"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/metrics"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/throttle"
)

const userAgent = "elbv2.k8s.aws"

type Cloud interface {
// EC2 provides API to AWS EC2
EC2() services.EC2
Expand Down Expand Up @@ -64,17 +67,18 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer, logger l
}
}
}

endpointsResolver := epresolver.NewResolver(cfg.AWSEndpoints)
metadataCFG := aws.NewConfig().WithEndpointResolver(endpointsResolver)
opts := session.Options{}
opts.Config.MergeIn(metadataCFG)
var ec2IMDSEndpointMode imds.EndpointModeState
if !hasIPv4 {
opts.EC2IMDSEndpointMode = endpoints.EC2IMDSEndpointModeStateIPv6
ec2IMDSEndpointMode = imds.EndpointModeStateIPv6
} else {
ec2IMDSEndpointMode = imds.EndpointModeStateIPv4
}

metadataSess := session.Must(session.NewSessionWithOptions(opts))
metadata := services.NewEC2Metadata(metadataSess)
ec2MetadataCfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRetryMaxAttempts(cfg.MaxRetries),
config.WithEC2IMDSEndpointMode(ec2IMDSEndpointMode),
)
ec2Metadata := services.NewEC2Metadata(ec2MetadataCfg)

if len(cfg.Region) == 0 {
region := os.Getenv("AWS_DEFAULT_REGION")
Expand All @@ -84,54 +88,56 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer, logger l

if region == "" {
err := (error)(nil)
region, err = metadata.Region()
region, err = ec2Metadata.Region()
if err != nil {
return nil, errors.Wrap(err, "failed to introspect region from EC2Metadata, specify --aws-region instead if EC2Metadata is unavailable")
}
}
cfg.Region = region
}
awsCFG := aws.NewConfig().WithRegion(cfg.Region).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint).WithMaxRetries(cfg.MaxRetries).WithEndpointResolver(endpointsResolver)
opts = session.Options{}
opts.Config.MergeIn(awsCFG)
if !hasIPv4 {
opts.EC2IMDSEndpointMode = endpoints.EC2IMDSEndpointModeStateIPv6
}
sess := session.Must(session.NewSessionWithOptions(opts))
injectUserAgent(&sess.Handlers)

if cfg.ThrottleConfig != nil {
throttler := throttle.NewThrottler(cfg.ThrottleConfig)
throttler.InjectHandlers(&sess.Handlers)
}
if metricsRegisterer != nil {
metricsCollector, err := metrics.NewCollector(metricsRegisterer)
if err != nil {
return nil, errors.Wrapf(err, "failed to initialize sdk metrics collector")
}
metricsCollector.InjectHandlers(&sess.Handlers)
}

ec2Service := services.NewEC2(sess)

vpcID, err := getVpcID(cfg, ec2Service, metadata, logger)
awsConfig, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(cfg.Region),
config.WithRetryMaxAttempts(cfg.MaxRetries),
config.WithEC2IMDSEndpointMode(ec2IMDSEndpointMode),
config.WithAPIOptions([]func(stack *smithymiddleware.Stack) error{
awsmiddleware.AddUserAgentKeyValue(userAgent, version.GitVersion),
}),
)

//TODO: ADD metric collection and throttle configuration later
//if cfg.ThrottleConfig != nil {
// throttler := throttle.NewThrottler(cfg.ThrottleConfig)
// throttler.InjectHandlers(&sess.Handlers)
//}

//if metricsRegisterer != nil {
// metricsCollector, err := metrics.NewCollector(metricsRegisterer)
// if err != nil {
// return nil, errors.Wrapf(err, "failed to initialize sdk metrics collector")
// }
// awsConfig.APIOptions = append(awsConfig.APIOptions, metricsCollector.CollectAPICallMetricMiddleware())
//}

ec2Service := services.NewEC2(awsConfig)

vpcID, err := getVpcID(cfg, ec2Service, ec2Metadata, logger)
if err != nil {
return nil, errors.Wrap(err, "failed to get VPC ID")
}
cfg.VpcID = vpcID
return &defaultCloud{
cfg: cfg,
ec2: ec2Service,
elbv2: services.NewELBV2(sess),
acm: services.NewACM(sess),
wafv2: services.NewWAFv2(sess),
wafRegional: services.NewWAFRegional(sess, cfg.Region),
shield: services.NewShield(sess),
rgt: services.NewRGT(sess),
elbv2: services.NewELBV2(awsConfig),
acm: services.NewACM(awsConfig),
wafv2: services.NewWAFv2(awsConfig),
wafRegional: services.NewWAFRegional(awsConfig, cfg.Region),
shield: services.NewShield(awsConfig), //done
rgt: services.NewRGT(awsConfig),
}, nil
}

func getVpcID(cfg CloudConfig, ec2Service services.EC2, metadata services.EC2Metadata, logger logr.Logger) (string, error) {
func getVpcID(cfg CloudConfig, ec2Service services.EC2, ec2Metadata services.EC2Metadata, logger logr.Logger) (string, error) {

if cfg.VpcID != "" {
logger.V(1).Info("vpcid is specified using flag --aws-vpc-id, controller will use the value", "vpc: ", cfg.VpcID)
Expand All @@ -142,12 +148,12 @@ func getVpcID(cfg CloudConfig, ec2Service services.EC2, metadata services.EC2Met
return inferVPCIDFromTags(ec2Service, cfg.VpcNameTagKey, cfg.VpcTags[cfg.VpcNameTagKey])
}

return inferVPCID(metadata, ec2Service)
return inferVPCID(ec2Metadata, ec2Service)
}

func inferVPCID(metadata services.EC2Metadata, ec2Service services.EC2) (string, error) {
func inferVPCID(ec2Metadata services.EC2Metadata, ec2Service services.EC2) (string, error) {
var errList []error
vpcId, err := metadata.VpcID()
vpcId, err := ec2Metadata.VpcID()
if err == nil {
return vpcId, nil
} else {
Expand All @@ -156,8 +162,8 @@ func inferVPCID(metadata services.EC2Metadata, ec2Service services.EC2) (string,

nodeName := os.Getenv("NODENAME")
if strings.HasPrefix(nodeName, "i-") {
output, err := ec2Service.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{&nodeName},
output, err := ec2Service.DescribeInstancesWithContext(context.Background(), &ec2.DescribeInstancesInput{
InstanceIds: []string{nodeName},
})
if err != nil {
errList = append(errList, errors.Wrapf(err, "failed to describe instance %q", nodeName))
Expand All @@ -183,10 +189,10 @@ func inferVPCID(metadata services.EC2Metadata, ec2Service services.EC2) (string,

func inferVPCIDFromTags(ec2Service services.EC2, VpcNameTagKey string, VpcNameTagValue string) (string, error) {
vpcs, err := ec2Service.DescribeVPCsAsList(context.Background(), &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
Filters: []ec2types.Filter{
{
Name: aws.String("tag:" + VpcNameTagKey),
Values: []*string{aws.String(VpcNameTagValue)},
Values: []string{VpcNameTagValue},
},
},
})
Expand All @@ -208,9 +214,8 @@ var _ Cloud = &defaultCloud{}
type defaultCloud struct {
cfg CloudConfig

ec2 services.EC2
elbv2 services.ELBV2

ec2 services.EC2
elbv2 services.ELBV2
acm services.ACM
wafv2 services.WAFv2
wafRegional services.WAFRegional
Expand Down
30 changes: 0 additions & 30 deletions pkg/aws/endpoints/resolver.go

This file was deleted.

Loading

0 comments on commit 3a5ffdb

Please sign in to comment.