Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate route53 to aws-sdk-go-v2 #16448

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cloudmock/aws/mockroute53/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ import (
"strings"
"sync"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
"github.com/aws/aws-sdk-go-v2/aws"
route53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
"k8s.io/kops/util/pkg/awsinterfaces"
)

type zoneInfo struct {
ID string
hostedZone *route53.HostedZone
records []*route53.ResourceRecordSet
vpcs []*route53.VPC
hostedZone *route53types.HostedZone
records []*route53types.ResourceRecordSet
vpcs []*route53types.VPC
}

type MockRoute53 struct {
// Mock out interface
route53iface.Route53API
awsinterfaces.Route53API

mutex sync.Mutex
Zones []*zoneInfo
}

var _ route53iface.Route53API = &MockRoute53{}
var _ awsinterfaces.Route53API = &MockRoute53{}

func (m *MockRoute53) findZone(hostedZoneId string) *zoneInfo {
if !strings.Contains(hostedZoneId, "/") {
Expand All @@ -55,12 +55,12 @@ func (m *MockRoute53) findZone(hostedZoneId string) *zoneInfo {
return nil
}

func (m *MockRoute53) MockCreateZone(z *route53.HostedZone, vpcs []*route53.VPC) {
func (m *MockRoute53) MockCreateZone(z *route53types.HostedZone, vpcs []*route53types.VPC) {
m.mutex.Lock()
defer m.mutex.Unlock()

zi := &zoneInfo{
ID: aws.StringValue(z.Id),
ID: aws.ToString(z.Id),
hostedZone: z,
vpcs: vpcs,
}
Expand Down
66 changes: 20 additions & 46 deletions cloudmock/aws/mockroute53/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,44 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
route53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
"k8s.io/klog/v2"
)

func (m *MockRoute53) ListResourceRecordSetsRequest(*route53.ListResourceRecordSetsInput) (*request.Request, *route53.ListResourceRecordSetsOutput) {
panic("MockRoute53 ListResourceRecordSetsRequest not implemented")
}

func (m *MockRoute53) ListResourceRecordSetsWithContext(aws.Context, *route53.ListResourceRecordSetsInput, ...request.Option) (*route53.ListResourceRecordSetsOutput, error) {
panic("Not implemented")
}

func (m *MockRoute53) ListResourceRecordSets(*route53.ListResourceRecordSetsInput) (*route53.ListResourceRecordSetsOutput, error) {
panic("MockRoute53 ListResourceRecordSets not implemented")
}

func (m *MockRoute53) ListResourceRecordSetsPagesWithContext(ctx aws.Context, request *route53.ListResourceRecordSetsInput, callback func(*route53.ListResourceRecordSetsOutput, bool) bool, opts ...request.Option) error {
func (m *MockRoute53) ListResourceRecordSets(ctx context.Context, request *route53.ListResourceRecordSetsInput, optFns ...func(*route53.Options)) (*route53.ListResourceRecordSetsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("ListResourceRecordSetsPages %v", request)

if request.HostedZoneId == nil {
// TODO: Use correct error
return fmt.Errorf("HostedZoneId required")
return nil, fmt.Errorf("HostedZoneId required")
}

if request.StartRecordIdentifier != nil || request.StartRecordName != nil || request.StartRecordType != nil || request.MaxItems != nil {
if request.StartRecordIdentifier != nil || request.StartRecordName != nil || len(request.StartRecordType) > 0 || request.MaxItems != nil {
klog.Fatalf("Unsupported options: %v", request)
}

zone := m.findZone(*request.HostedZoneId)

if zone == nil {
// TODO: Use correct error
return fmt.Errorf("NOT FOUND")
return nil, fmt.Errorf("NOT FOUND")
}

page := &route53.ListResourceRecordSetsOutput{}
for _, r := range zone.records {
copy := *r
page.ResourceRecordSets = append(page.ResourceRecordSets, &copy)
page.ResourceRecordSets = append(page.ResourceRecordSets, copy)
}
lastPage := true
callback(page, lastPage)

return nil
}

func (m *MockRoute53) ListResourceRecordSetsPages(request *route53.ListResourceRecordSetsInput, callback func(*route53.ListResourceRecordSetsOutput, bool) bool) error {
return m.ListResourceRecordSetsPagesWithContext(context.TODO(), request, callback)
}

func (m *MockRoute53) ChangeResourceRecordSetsRequest(*route53.ChangeResourceRecordSetsInput) (*request.Request, *route53.ChangeResourceRecordSetsOutput) {
panic("MockRoute53 ChangeResourceRecordSetsRequest not implemented")
}

func (m *MockRoute53) ChangeResourceRecordSetsWithContext(aws.Context, *route53.ChangeResourceRecordSetsInput, ...request.Option) (*route53.ChangeResourceRecordSetsOutput, error) {
panic("Not implemented")
return page, nil
}

func (m *MockRoute53) ChangeResourceRecordSets(request *route53.ChangeResourceRecordSetsInput) (*route53.ChangeResourceRecordSetsOutput, error) {
func (m *MockRoute53) ChangeResourceRecordSets(ctx context.Context, request *route53.ChangeResourceRecordSetsInput, optFns ...func(*route53.Options)) (*route53.ChangeResourceRecordSetsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -100,41 +74,41 @@ func (m *MockRoute53) ChangeResourceRecordSets(request *route53.ChangeResourceRe
}

response := &route53.ChangeResourceRecordSetsOutput{
ChangeInfo: &route53.ChangeInfo{},
ChangeInfo: &route53types.ChangeInfo{},
}
for _, change := range request.ChangeBatch.Changes {
changeType := aws.StringValue(change.ResourceRecordSet.Type)
changeName := aws.StringValue(change.ResourceRecordSet.Name)
changeType := change.ResourceRecordSet.Type
changeName := aws.ToString(change.ResourceRecordSet.Name)

foundIndex := -1
for i, rr := range zone.records {
if aws.StringValue(rr.Type) != changeType {
if rr.Type != changeType {
continue
}
if aws.StringValue(rr.Name) != changeName {
if aws.ToString(rr.Name) != changeName {
continue
}
foundIndex = i
break
}

switch aws.StringValue(change.Action) {
case "UPSERT":
switch change.Action {
case route53types.ChangeActionUpsert:
if foundIndex == -1 {
zone.records = append(zone.records, change.ResourceRecordSet)
} else {
zone.records[foundIndex] = change.ResourceRecordSet
}

case "CREATE":
case route53types.ChangeActionCreate:
if foundIndex == -1 {
zone.records = append(zone.records, change.ResourceRecordSet)
} else {
// TODO: Use correct error
return nil, fmt.Errorf("duplicate record %s %q", changeType, changeName)
}

case "DELETE":
case route53types.ChangeActionDelete:
if foundIndex == -1 {
// TODO: Use correct error
return nil, fmt.Errorf("record not found %s %q", changeType, changeName)
Expand All @@ -143,7 +117,7 @@ func (m *MockRoute53) ChangeResourceRecordSets(request *route53.ChangeResourceRe

default:
// TODO: Use correct error
return nil, fmt.Errorf("Unsupported action: %q", aws.StringValue(change.Action))
return nil, fmt.Errorf("Unsupported action: %q", change.Action)
}
}

Expand Down
72 changes: 15 additions & 57 deletions cloudmock/aws/mockroute53/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@ limitations under the License.
package mockroute53

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53"
route53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
"k8s.io/klog/v2"
)

func (m *MockRoute53) GetHostedZoneRequest(*route53.GetHostedZoneInput) (*request.Request, *route53.GetHostedZoneOutput) {
panic("MockRoute53 GetHostedZoneRequest not implemented")
}

func (m *MockRoute53) GetHostedZoneWithContext(aws.Context, *route53.GetHostedZoneInput, ...request.Option) (*route53.GetHostedZoneOutput, error) {
panic("Not implemented")
}

func (m *MockRoute53) GetHostedZone(request *route53.GetHostedZoneInput) (*route53.GetHostedZoneOutput, error) {
func (m *MockRoute53) GetHostedZone(ctx context.Context, request *route53.GetHostedZoneInput, optFns ...func(*route53.Options)) (*route53.GetHostedZoneOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -48,45 +40,21 @@ func (m *MockRoute53) GetHostedZone(request *route53.GetHostedZoneInput) (*route
// TODO: Use correct error
return nil, fmt.Errorf("NOT FOUND")
}
vpcs := make([]route53types.VPC, len(zone.vpcs))
for i := range zone.vpcs {
vpcs[i] = *zone.vpcs[i]
}

copy := *zone.hostedZone
response := &route53.GetHostedZoneOutput{
// DelegationSet ???
HostedZone: &copy,
VPCs: zone.vpcs,
VPCs: vpcs,
}
return response, nil
}

func (m *MockRoute53) GetHostedZoneCountRequest(*route53.GetHostedZoneCountInput) (*request.Request, *route53.GetHostedZoneCountOutput) {
panic("MockRoute53 GetHostedZoneCountRequest not implemented")
}

func (m *MockRoute53) GetHostedZoneCountWithContext(aws.Context, *route53.GetHostedZoneCountInput, ...request.Option) (*route53.GetHostedZoneCountOutput, error) {
panic("Not implemented")
}

func (m *MockRoute53) GetHostedZoneCount(*route53.GetHostedZoneCountInput) (*route53.GetHostedZoneCountOutput, error) {
panic("MockRoute53 GetHostedZoneCount not implemented")
}

func (m *MockRoute53) ListHostedZonesRequest(*route53.ListHostedZonesInput) (*request.Request, *route53.ListHostedZonesOutput) {
panic("MockRoute53 ListHostedZonesRequest not implemented")
}

func (m *MockRoute53) ListHostedZonesWithContext(aws.Context, *route53.ListHostedZonesInput, ...request.Option) (*route53.ListHostedZonesOutput, error) {
panic("Not implemented")
}

func (m *MockRoute53) ListHostedZones(*route53.ListHostedZonesInput) (*route53.ListHostedZonesOutput, error) {
panic("MockRoute53 ListHostedZones not implemented")
}

func (m *MockRoute53) ListHostedZonesPagesWithContext(aws.Context, *route53.ListHostedZonesInput, func(*route53.ListHostedZonesOutput, bool) bool, ...request.Option) error {
panic("Not implemented")
}

func (m *MockRoute53) ListHostedZonesPages(request *route53.ListHostedZonesInput, callback func(*route53.ListHostedZonesOutput, bool) bool) error {
func (m *MockRoute53) ListHostedZones(ctx context.Context, request *route53.ListHostedZonesInput, optFns ...func(*route53.Options)) (*route53.ListHostedZonesOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -95,30 +63,20 @@ func (m *MockRoute53) ListHostedZonesPages(request *route53.ListHostedZonesInput
page := &route53.ListHostedZonesOutput{}
for _, zone := range m.Zones {
copy := *zone.hostedZone
page.HostedZones = append(page.HostedZones, &copy)
page.HostedZones = append(page.HostedZones, copy)
}
lastPage := true
callback(page, lastPage)

return nil
}

func (m *MockRoute53) ListHostedZonesByNameRequest(*route53.ListHostedZonesByNameInput) (*request.Request, *route53.ListHostedZonesByNameOutput) {
panic("MockRoute53 ListHostedZonesByNameRequest not implemented")
}

func (m *MockRoute53) ListHostedZonesByNameWithContext(aws.Context, *route53.ListHostedZonesByNameInput, ...request.Option) (*route53.ListHostedZonesByNameOutput, error) {
panic("Not implemented")
return page, nil
}

func (m *MockRoute53) ListHostedZonesByName(*route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesByNameOutput, error) {
func (m *MockRoute53) ListHostedZonesByName(ctx context.Context, request *route53.ListHostedZonesByNameInput, optFns ...func(*route53.Options)) (*route53.ListHostedZonesByNameOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

var zones []*route53.HostedZone
var zones []route53types.HostedZone

for _, z := range m.Zones {
zones = append(zones, z.hostedZone)
zones = append(zones, *z.hostedZone)
}

return &route53.ListHostedZonesByNameOutput{
Expand Down
56 changes: 18 additions & 38 deletions dnsprovider/pkg/dnsprovider/providers/aws/route53/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ limitations under the License.
package route53

import (
"context"
"fmt"
"io"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"k8s.io/klog/v2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/route53"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/util/pkg/awslog"
)

const (
Expand All @@ -37,48 +39,26 @@ var MaxBatchSize = 900

func init() {
dnsprovider.RegisterDNSProvider(ProviderName, func(config io.Reader) (dnsprovider.Interface, error) {
return newRoute53(config)
return newRoute53()
})
}

// route53HandlerLogger is a request handler for aws-sdk-go that logs route53 requests
func route53HandlerLogger(req *request.Request) {
service := req.ClientInfo.ServiceName

name := "?"
if req.Operation != nil {
name = req.Operation.Name
}

klog.V(4).Infof("AWS request: %s %s", service, name)
}

// newRoute53 creates a new instance of an AWS Route53 DNS Interface.
func newRoute53(config io.Reader) (*Interface, error) {
func newRoute53() (*Interface, error) {
ctx := context.TODO()
// Connect to AWS Route53 - TODO: Do more sophisticated auth

awsConfig := aws.NewConfig()

// This avoids a confusing error message when we fail to get credentials
// e.g. https://github.com/kubernetes/kops/issues/605
awsConfig = awsConfig.WithCredentialsChainVerboseErrors(true)

// To avoid API throttling on busier accounts
awsConfig = awsConfig.WithMaxRetries(5)

sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
cfg, err := awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithClientLogMode(aws.LogRetries),
awslog.WithAWSLogger(),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 5)
}))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load default aws config: %w", err)
}
svc := route53.New(sess, awsConfig)

// Add our handler that will log requests
svc.Handlers.Sign.PushFrontNamed(request.NamedHandler{
Name: "k8s/logger",
Fn: route53HandlerLogger,
})
svc := route53.NewFromConfig(cfg)

return New(svc), nil
}
Loading
Loading