From 1fdc46c1b9478256be744a2e22ccd154d0e3804b Mon Sep 17 00:00:00 2001 From: Peter Rifel Date: Fri, 29 Mar 2024 06:55:47 -0500 Subject: [PATCH] Add eventbridge AWSCloud client and v2 config + logger --- upup/pkg/fi/cloudup/awsup/aws_cloud.go | 37 +++++++++++++++---------- upup/pkg/fi/cloudup/awsup/logger.go | 38 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 upup/pkg/fi/cloudup/awsup/logger.go diff --git a/upup/pkg/fi/cloudup/awsup/aws_cloud.go b/upup/pkg/fi/cloudup/awsup/aws_cloud.go index 3c42b18e6bd10..b53e6de1772db 100644 --- a/upup/pkg/fi/cloudup/awsup/aws_cloud.go +++ b/upup/pkg/fi/cloudup/awsup/aws_cloud.go @@ -25,13 +25,15 @@ import ( "sync" "time" - "github.com/aws/aws-sdk-go/service/eventbridge" - "github.com/aws/aws-sdk-go/service/eventbridge/eventbridgeiface" + awsv2 "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/eventbridge" "github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sqs/sqsiface" "golang.org/x/sync/errgroup" "github.com/aws/aws-sdk-go-v2/aws/arn" + "github.com/aws/aws-sdk-go-v2/aws/retry" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" @@ -67,6 +69,7 @@ import ( identity_aws "k8s.io/kops/pkg/nodeidentity/aws" "k8s.io/kops/pkg/resources/spotinst" "k8s.io/kops/upup/pkg/fi" + "k8s.io/kops/util/pkg/awsinterfaces" ) // By default, aws-sdk-go only retries 3 times, which doesn't give @@ -134,7 +137,7 @@ type AWSCloud interface { Route53() route53iface.Route53API Spotinst() spotinst.Cloud SQS() sqsiface.SQSAPI - EventBridge() eventbridgeiface.EventBridgeAPI + EventBridge() awsinterfaces.EventBridgeAPI SSM() ssmiface.SSMAPI // TODO: Document and rationalize these tags/filters methods @@ -204,7 +207,7 @@ type awsCloudImplementation struct { spotinst spotinst.Cloud sts *sts.STS sqs *sqs.SQS - eventbridge *eventbridge.EventBridge + eventbridge *eventbridge.Client ssm *ssm.SSM region string @@ -281,6 +284,7 @@ func getCloudInstancesFromRegion(region string) AWSCloud { } func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) { + ctx := context.TODO() raw := getCloudInstancesFromRegion(region) if raw == nil { @@ -294,6 +298,18 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) { }, } + cfgV2, err := awsconfig.LoadDefaultConfig(ctx, + awsconfig.WithRegion(region), + awsconfig.WithClientLogMode(awsv2.LogRetries), + awsconfig.WithLogger(awsLogger{}), + awsconfig.WithRetryer(func() awsv2.Retryer { + return retry.NewStandard() + }), + ) + if err != nil { + return c, fmt.Errorf("failed to load default aws config: %w", err) + } + config := aws.NewConfig().WithRegion(region) config = setConfig(config) @@ -403,16 +419,7 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) { c.sqs.Handlers.Send.PushFront(requestLogger) c.addHandlers(region, &c.sqs.Handlers) - sess, err = session.NewSessionWithOptions(session.Options{ - Config: *config, - SharedConfigState: session.SharedConfigEnable, - }) - if err != nil { - return c, err - } - c.eventbridge = eventbridge.New(sess, config) - c.eventbridge.Handlers.Send.PushFront(requestLogger) - c.addHandlers(region, &c.eventbridge.Handlers) + c.eventbridge = eventbridge.NewFromConfig(cfgV2) sess, err = session.NewSessionWithOptions(session.Options{ Config: *config, @@ -2230,7 +2237,7 @@ func (c *awsCloudImplementation) SQS() sqsiface.SQSAPI { return c.sqs } -func (c *awsCloudImplementation) EventBridge() eventbridgeiface.EventBridgeAPI { +func (c *awsCloudImplementation) EventBridge() awsinterfaces.EventBridgeAPI { return c.eventbridge } diff --git a/upup/pkg/fi/cloudup/awsup/logger.go b/upup/pkg/fi/cloudup/awsup/logger.go new file mode 100644 index 0000000000000..59d2ee400f690 --- /dev/null +++ b/upup/pkg/fi/cloudup/awsup/logger.go @@ -0,0 +1,38 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package awsup + +import ( + "fmt" + + "github.com/aws/smithy-go/logging" + "k8s.io/klog/v2" +) + +type awsLogger struct{} + +var _ logging.Logger = awsLogger{} + +func (awsLogger) Logf(classification logging.Classification, format string, v ...interface{}) { + text := fmt.Sprintf("AWS request: %s", format) + switch classification { + case logging.Warn: + klog.Warningf(text, v...) + default: + klog.V(2).Infof(text, v...) + } +}