Skip to content

Commit

Permalink
Merge pull request #16432 from rifelpet/aws-sdk-go-v2-eventbridge
Browse files Browse the repository at this point in the history
Update EventBridge to aws-sdk-go-v2
  • Loading branch information
k8s-ci-robot authored Mar 30, 2024
2 parents 2d04e0a + 9187953 commit 1c0c5a5
Show file tree
Hide file tree
Showing 93 changed files with 37,605 additions and 18,819 deletions.
40 changes: 21 additions & 19 deletions cloudmock/aws/mockeventbridge/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,41 @@ limitations under the License.
package mockeventbridge

import (
"context"
"sync"

"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/eventbridge/eventbridgeiface"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
eventbridgetypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
"k8s.io/kops/util/pkg/awsinterfaces"
)

type MockEventBridge struct {
eventbridgeiface.EventBridgeAPI
awsinterfaces.EventBridgeAPI
mutex sync.Mutex

Rules map[string]*eventbridge.Rule
TagsByArn map[string][]*eventbridge.Tag
TargetsByRule map[string][]*eventbridge.Target
Rules map[string]*eventbridgetypes.Rule
TagsByArn map[string][]eventbridgetypes.Tag
TargetsByRule map[string][]eventbridgetypes.Target
}

var _ eventbridgeiface.EventBridgeAPI = &MockEventBridge{}
var _ awsinterfaces.EventBridgeAPI = &MockEventBridge{}

func (m *MockEventBridge) PutRule(input *eventbridge.PutRuleInput) (*eventbridge.PutRuleOutput, error) {
func (m *MockEventBridge) PutRule(ctx context.Context, input *eventbridge.PutRuleInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutRuleOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

name := *input.Name
arn := "arn:aws-test:events:us-east-1:012345678901:rule/" + name

rule := &eventbridge.Rule{
rule := &eventbridgetypes.Rule{
Arn: &arn,
EventPattern: input.EventPattern,
}
if m.Rules == nil {
m.Rules = make(map[string]*eventbridge.Rule)
m.Rules = make(map[string]*eventbridgetypes.Rule)
}
if m.TagsByArn == nil {
m.TagsByArn = make(map[string][]*eventbridge.Tag)
m.TagsByArn = make(map[string][]eventbridgetypes.Tag)
}
m.Rules[name] = rule
m.TagsByArn[arn] = input.Tags
Expand All @@ -60,7 +62,7 @@ func (m *MockEventBridge) PutRule(input *eventbridge.PutRuleInput) (*eventbridge
return response, nil
}

func (m *MockEventBridge) ListRules(input *eventbridge.ListRulesInput) (*eventbridge.ListRulesOutput, error) {
func (m *MockEventBridge) ListRules(ctx context.Context, input *eventbridge.ListRulesInput, optFns ...func(*eventbridge.Options)) (*eventbridge.ListRulesOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -70,15 +72,15 @@ func (m *MockEventBridge) ListRules(input *eventbridge.ListRulesInput) (*eventbr
if rule == nil {
return response, nil
}
response.Rules = []*eventbridge.Rule{rule}
response.Rules = []eventbridgetypes.Rule{*rule}
return response, nil
}

func (m *MockEventBridge) DeleteRule(*eventbridge.DeleteRuleInput) (*eventbridge.DeleteRuleOutput, error) {
func (m *MockEventBridge) DeleteRule(ctx context.Context, input *eventbridge.DeleteRuleInput, optFns ...func(*eventbridge.Options)) (*eventbridge.DeleteRuleOutput, error) {
panic("Not implemented")
}

func (m *MockEventBridge) ListTagsForResource(input *eventbridge.ListTagsForResourceInput) (*eventbridge.ListTagsForResourceOutput, error) {
func (m *MockEventBridge) ListTagsForResource(ctx context.Context, input *eventbridge.ListTagsForResourceInput, optFns ...func(*eventbridge.Options)) (*eventbridge.ListTagsForResourceOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -88,19 +90,19 @@ func (m *MockEventBridge) ListTagsForResource(input *eventbridge.ListTagsForReso
return response, nil
}

func (m *MockEventBridge) PutTargets(input *eventbridge.PutTargetsInput) (*eventbridge.PutTargetsOutput, error) {
func (m *MockEventBridge) PutTargets(ctx context.Context, input *eventbridge.PutTargetsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.PutTargetsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

if m.TargetsByRule == nil {
m.TargetsByRule = make(map[string][]*eventbridge.Target)
m.TargetsByRule = make(map[string][]eventbridgetypes.Target)
}
m.TargetsByRule[*input.Rule] = input.Targets

return &eventbridge.PutTargetsOutput{}, nil
}

func (m *MockEventBridge) ListTargetsByRule(input *eventbridge.ListTargetsByRuleInput) (*eventbridge.ListTargetsByRuleOutput, error) {
func (m *MockEventBridge) ListTargetsByRule(ctx context.Context, input *eventbridge.ListTargetsByRuleInput, optFns ...func(*eventbridge.Options)) (*eventbridge.ListTargetsByRuleOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -110,6 +112,6 @@ func (m *MockEventBridge) ListTargetsByRule(input *eventbridge.ListTargetsByRule
return response, nil
}

func (m *MockEventBridge) RemoveTargets(*eventbridge.RemoveTargetsInput) (*eventbridge.RemoveTargetsOutput, error) {
func (m *MockEventBridge) RemoveTargets(ctx context.Context, input *eventbridge.RemoveTargetsInput, optFns ...func(*eventbridge.Options)) (*eventbridge.RemoveTargetsOutput, error) {
panic("Not implemented")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.9
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0
github.com/aws/aws-sdk-go-v2/service/ec2 v1.155.0
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.30.3
github.com/aws/aws-sdk-go-v2/service/kms v1.30.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.0
github.com/aws/smithy-go v1.20.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.4 h1:SIkD6T4zGQ+1YIit22wi37CGNkrE
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.4/go.mod h1:XfeqbsG0HNedNs0GT+ju4Bs+pFAwsrlzcRdMvdNVf5s=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.155.0 h1:MuQr3lq2n/5lAdDcIYMANNpYNkFo6HDGq7S9+aRy9uc=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.155.0/go.mod h1:TeZ9dVQzGaLG+SBIgdLIDbJ6WmfFvksLeG3EHGnNfZM=
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.30.3 h1:XHY0q3eoA3d4YAm8AhUI1Swi79Io6rLEbKuIgkhCcqA=
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.30.3/go.mod h1:z2ST+IAJHUpgqAPJPsDs44wypEizBT0kekjWNfjQJ6M=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 h1:EyBZibRTVAs6ECHZOw5/wlylS9OcTzwyjeQMudmREjE=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1/go.mod h1:JKpmtYhhPs7D97NL/ltqz7yCkERFW5dOlHyVl66ZYF8=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.6 h1:NkHCgg0Ck86c5PTOzBZ0JRccI51suJDg5lgFtxBu1ek=
Expand Down
8 changes: 4 additions & 4 deletions pkg/model/awsmodel/nodeterminationhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package awsmodel
import (
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/model/iam"
Expand Down Expand Up @@ -200,7 +200,7 @@ func (b *NodeTerminationHandlerBuilder) FindDeletions(c *fi.CloudupModelBuilderC
request := &eventbridge.ListRulesInput{
NamePrefix: ruleName,
}
response, err := eventBridge.ListRules(request)
response, err := eventBridge.ListRules(c.Context(), request)
if err != nil {
return fmt.Errorf("listing EventBridge rules: %w", err)
}
Expand All @@ -213,7 +213,7 @@ func (b *NodeTerminationHandlerBuilder) FindDeletions(c *fi.CloudupModelBuilderC

rule := response.Rules[0]

tagResponse, err := eventBridge.ListTagsForResource(&eventbridge.ListTagsForResourceInput{ResourceARN: rule.Arn})
tagResponse, err := eventBridge.ListTagsForResource(c.Context(), &eventbridge.ListTagsForResourceInput{ResourceARN: rule.Arn})
if err != nil {
return fmt.Errorf("listing tags for EventBridge rule: %w", err)
}
Expand Down
20 changes: 11 additions & 9 deletions pkg/resources/aws/eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package aws

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"k8s.io/klog/v2"

"k8s.io/kops/pkg/resources"
Expand All @@ -44,22 +45,22 @@ func EventBridgeRuleDeleter(cloud fi.Cloud, r *resources.Resource) error {
}

func DeleteEventBridgeRule(cloud fi.Cloud, ruleName string) error {

ctx := context.TODO()
c := cloud.(awsup.AWSCloud)

targets, err := c.EventBridge().ListTargetsByRule(&eventbridge.ListTargetsByRuleInput{
targets, err := c.EventBridge().ListTargetsByRule(ctx, &eventbridge.ListTargetsByRuleInput{
Rule: aws.String(ruleName),
})
if err != nil {
return fmt.Errorf("listing targets for EventBridge rule %q: %w", ruleName, err)
}
if len(targets.Targets) > 0 {
var ids []*string
var ids []string
for _, target := range targets.Targets {
ids = append(ids, target.Id)
ids = append(ids, aws.ToString(target.Id))
}
klog.V(2).Infof("Removing EventBridge Targets for rule %q", ruleName)
_, err = c.EventBridge().RemoveTargets(&eventbridge.RemoveTargetsInput{
_, err = c.EventBridge().RemoveTargets(ctx, &eventbridge.RemoveTargetsInput{
Ids: ids,
Rule: aws.String(ruleName),
})
Expand All @@ -72,14 +73,15 @@ func DeleteEventBridgeRule(cloud fi.Cloud, ruleName string) error {
request := &eventbridge.DeleteRuleInput{
Name: aws.String(ruleName),
}
_, err = c.EventBridge().DeleteRule(request)
_, err = c.EventBridge().DeleteRule(ctx, request)
if err != nil {
return fmt.Errorf("deleting EventBridge rule %q: %w", ruleName, err)
}
return nil
}

func ListEventBridgeRules(cloud fi.Cloud, vpcID, clusterName string) ([]*resources.Resource, error) {
ctx := context.TODO()
c := cloud.(awsup.AWSCloud)

klog.V(2).Infof("Listing EventBridge rules")
Expand All @@ -91,7 +93,7 @@ func ListEventBridgeRules(cloud fi.Cloud, vpcID, clusterName string) ([]*resourc
Limit: nil,
NamePrefix: aws.String(clusterNamePrefix),
}
response, err := c.EventBridge().ListRules(request)
response, err := c.EventBridge().ListRules(ctx, request)
if err != nil {
return nil, fmt.Errorf("error listing Eventbridge rules: %v", err)
}
Expand Down
16 changes: 9 additions & 7 deletions upup/pkg/fi/cloudup/awstasks/eventbridgerule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package awstasks

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
eventbridgetypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
"k8s.io/apimachinery/pkg/util/validation/field"
awsResources "k8s.io/kops/pkg/resources/aws"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -57,7 +59,7 @@ func (eb *EventBridgeRule) Find(c *fi.CloudupContext) (*EventBridgeRule, error)
request := &eventbridge.ListRulesInput{
NamePrefix: eb.Name,
}
response, err := cloud.EventBridge().ListRules(request)
response, err := cloud.EventBridge().ListRules(c.Context(), request)
if err != nil {
return nil, fmt.Errorf("error listing EventBridge rules: %v", err)
}
Expand All @@ -70,7 +72,7 @@ func (eb *EventBridgeRule) Find(c *fi.CloudupContext) (*EventBridgeRule, error)

rule := response.Rules[0]

tagResponse, err := cloud.EventBridge().ListTagsForResource(&eventbridge.ListTagsForResourceInput{ResourceARN: rule.Arn})
tagResponse, err := cloud.EventBridge().ListTagsForResource(c.Context(), &eventbridge.ListTagsForResourceInput{ResourceARN: rule.Arn})
if err != nil {
return nil, fmt.Errorf("error listing tags for EventBridge rule: %v", err)
}
Expand Down Expand Up @@ -106,9 +108,9 @@ func (eb *EventBridgeRule) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Event
}

if a == nil {
var tags []*eventbridge.Tag
var tags []eventbridgetypes.Tag
for k, v := range eb.Tags {
tags = append(tags, &eventbridge.Tag{
tags = append(tags, eventbridgetypes.Tag{
Key: aws.String(k),
Value: aws.String(v),
})
Expand All @@ -120,7 +122,7 @@ func (eb *EventBridgeRule) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Event
Tags: tags,
}

_, err := t.Cloud.EventBridge().PutRule(request)
_, err := t.Cloud.EventBridge().PutRule(context.TODO(), request)
if err != nil {
return fmt.Errorf("error creating EventBridge rule: %v", err)
}
Expand Down
14 changes: 8 additions & 6 deletions upup/pkg/fi/cloudup/awstasks/eventbridgetarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package awstasks

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
eventbridgetypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
Expand Down Expand Up @@ -65,7 +67,7 @@ func (eb *EventBridgeTarget) Find(c *fi.CloudupContext) (*EventBridgeTarget, err
Rule: eb.Rule.Name,
}

response, err := cloud.EventBridge().ListTargetsByRule(request)
response, err := cloud.EventBridge().ListTargetsByRule(c.Context(), request)
if err != nil {
return nil, fmt.Errorf("error listing EventBridge targets: %v", err)
}
Expand Down Expand Up @@ -107,17 +109,17 @@ func (_ *EventBridgeTarget) CheckChanges(a, e, changes *EventBridgeTarget) error

func (eb *EventBridgeTarget) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *EventBridgeTarget) error {
if a == nil {
target := &eventbridge.Target{
target := eventbridgetypes.Target{
Arn: eb.SQSQueue.ARN,
Id: aws.String("1"),
}

request := &eventbridge.PutTargetsInput{
Rule: eb.Rule.Name,
Targets: []*eventbridge.Target{target},
Targets: []eventbridgetypes.Target{target},
}

_, err := t.Cloud.EventBridge().PutTargets(request)
_, err := t.Cloud.EventBridge().PutTargets(context.TODO(), request)
if err != nil {
return fmt.Errorf("error creating EventBridge target: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions upup/pkg/fi/cloudup/awstasks/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package awstasks
import (
"strings"

eventbridgetypes "github.com/aws/aws-sdk-go-v2/service/eventbridge/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/iam"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ func mapToIAMTags(tags map[string]string) []*iam.Tag {
return m
}

func mapEventBridgeTagsToMap(tags []*eventbridge.Tag) map[string]string {
func mapEventBridgeTagsToMap(tags []eventbridgetypes.Tag) map[string]string {
if tags == nil {
return nil
}
Expand Down
Loading

0 comments on commit 1c0c5a5

Please sign in to comment.