Skip to content

Commit

Permalink
Add eventbridge AWSCloud client and v2 config + logger
Browse files Browse the repository at this point in the history
  • Loading branch information
rifelpet committed Mar 29, 2024
1 parent 94909f5 commit 9187953
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
37 changes: 22 additions & 15 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down
38 changes: 38 additions & 0 deletions upup/pkg/fi/cloudup/awsup/logger.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}

0 comments on commit 9187953

Please sign in to comment.