Skip to content

Commit 3749bc4

Browse files
committed
HIVE-2849: Migrate AWS SDK to v2
1 parent 7a7ba3a commit 3749bc4

File tree

19 files changed

+869
-180
lines changed

19 files changed

+869
-180
lines changed

contrib/pkg/awsprivatelink/enable.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"os/user"
77
"path/filepath"
88

9-
"github.com/aws/aws-sdk-go/aws"
10-
"github.com/aws/aws-sdk-go/service/ec2"
9+
"github.com/aws/aws-sdk-go-v2/aws"
10+
"github.com/aws/aws-sdk-go-v2/service/ec2"
11+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1112

1213
configv1 "github.com/openshift/api/config/v1"
1314
hivev1 "github.com/openshift/hive/apis/hive/v1"
1415
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
1516
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
16-
"github.com/openshift/hive/pkg/awsclient"
17+
awsclient "github.com/openshift/hive/pkg/awsclientv2"
1718
operatorutils "github.com/openshift/hive/pkg/operator/hive"
1819

1920
log "github.com/sirupsen/logrus"
@@ -119,10 +120,10 @@ func (o *enableOptions) Run(cmd *cobra.Command, args []string) error {
119120
// Get active cluster's VPC, filtering by infra-id
120121
targetTagKey := "kubernetes.io/cluster/" + o.infraId
121122
describeVPCsOutput, err := o.awsClients.DescribeVpcs(&ec2.DescribeVpcsInput{
122-
Filters: []*ec2.Filter{
123+
Filters: []ec2types.Filter{
123124
{
124125
Name: aws.String("tag-key"),
125-
Values: []*string{aws.String(targetTagKey)},
126+
Values: []string{targetTagKey},
126127
},
127128
},
128129
})

contrib/pkg/awsprivatelink/endpointvpc/add.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ package endpointvpc
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"sort"
89

9-
"github.com/aws/aws-sdk-go/aws"
10-
"github.com/aws/aws-sdk-go/aws/awserr"
11-
"github.com/aws/aws-sdk-go/service/ec2"
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/ec2"
12+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
13+
"github.com/aws/smithy-go"
1214

1315
hivev1 "github.com/openshift/hive/apis/hive/v1"
1416
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
1517
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
16-
"github.com/openshift/hive/pkg/awsclient"
18+
awsclient "github.com/openshift/hive/pkg/awsclientv2"
1719

1820
log "github.com/sirupsen/logrus"
1921
"github.com/spf13/cobra"
@@ -107,7 +109,7 @@ func (o *endpointVPCAddOptions) Complete(cmd *cobra.Command, args []string) erro
107109

108110
func (o *endpointVPCAddOptions) Validate(cmd *cobra.Command, args []string) error {
109111
// Check if the endpoint VPC exists
110-
if _, err := o.endpointVpcClients.DescribeVpcs(&ec2.DescribeVpcsInput{
112+
if _, err := o.endpointVpcClients.DescribeVpcs(&ec2types.DescribeVpcsInput{
111113
VpcIds: []*string{aws.String(o.endpointVpcId)},
112114
}); err != nil {
113115
log.WithError(err).Fatal("Failed to describe endpoint VPC")
@@ -136,7 +138,7 @@ func (o *endpointVPCAddOptions) Validate(cmd *cobra.Command, args []string) erro
136138

137139
func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
138140
// Get default SG of the endpoint VPC
139-
endpointVPCDefaultSG, err := awsutils.GetDefaultSGOfVpc(o.endpointVpcClients, aws.String(o.endpointVpcId))
141+
endpointVPCDefaultSG, err := awsutils.GetDefaultSGOfVpc(o.endpointVpcClients, o.endpointVpcId)
140142
if err != nil {
141143
log.WithError(err).Fatal("Failed to get default SG of the endpoint VPC")
142144
}
@@ -189,10 +191,7 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
189191
}
190192

191193
// Update SGs
192-
associatedVpcWorkerSG, err := awsutils.GetWorkerSGFromVpcId(
193-
associatedVpcClients,
194-
aws.String(associatedVpcId),
195-
)
194+
associatedVpcWorkerSG, err := awsutils.GetWorkerSGFromVpcId(associatedVpcClients, associatedVpcId)
196195
if err != nil {
197196
log.WithError(err).Fatal("Failed to get worker SG of the associated VPC")
198197
}
@@ -210,8 +209,9 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
210209
aws.String(fmt.Sprintf("Access from worker SG of associated VPC %s", associatedVpcId)),
211210
); err != nil {
212211
// Proceed if ingress already authorized, fail otherwise
213-
switch aerr, ok := err.(awserr.Error); {
214-
case ok && aerr.Code() == "InvalidPermission.Duplicate":
212+
var aerr smithy.APIError
213+
switch ok := errors.As(err, &aerr); {
214+
case ok && aerr.ErrorCode() == "InvalidPermission.Duplicate":
215215
log.Warnf("Traffic from the associated VPC's worker SG to the endpoint VPC's default SG is already authorized")
216216
default:
217217
log.WithError(err).Fatal("Failed to authorize traffic from the associated VPC's worker SG to the endpoint VPC's default SG")
@@ -226,8 +226,9 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
226226
aws.String(fmt.Sprintf("Access from default SG of endpoint VPC %s", o.endpointVpcId)),
227227
); err != nil {
228228
// Proceed if ingress already authorized, fail otherwise
229-
switch aerr, ok := err.(awserr.Error); {
230-
case ok && aerr.Code() == "InvalidPermission.Duplicate":
229+
var aerr smithy.APIError
230+
switch ok := errors.As(err, &aerr); {
231+
case ok && aerr.ErrorCode() == "InvalidPermission.Duplicate":
231232
log.Warnf("Traffic from the endpoint VPC's default SG to the associated VPC's worker SG is already authorized")
232233
default:
233234
log.WithError(err).Fatal("Failed to authorize traffic from the endpoint VPC's default SG to the associated VPC's worker SG")
@@ -244,8 +245,9 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
244245
aws.String(fmt.Sprintf("Access from CIDR block of associated VPC %s", associatedVpcId)),
245246
); err != nil {
246247
// Proceed if ingress already authorized, fail otherwise
247-
switch aerr, ok := err.(awserr.Error); {
248-
case ok && aerr.Code() == "InvalidPermission.Duplicate":
248+
var aerr smithy.APIError
249+
switch ok := errors.As(err, &aerr); {
250+
case ok && aerr.ErrorCode() == "InvalidPermission.Duplicate":
249251
log.Warnf("Traffic from the associated VPC's CIDR block to the endpoint VPC's default SG is already authorized")
250252
default:
251253
log.WithError(err).Fatal("Failed to authorize traffic from the associated VPC's CIDR block to the endpoint VPC's default SG")
@@ -260,8 +262,9 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
260262
aws.String(fmt.Sprintf("Access from CIDR block of endpoint VPC %s", o.endpointVpcId)),
261263
); err != nil {
262264
// Proceed if ingress already authorized, fail otherwise
263-
switch aerr, ok := err.(awserr.Error); {
264-
case ok && aerr.Code() == "InvalidPermission.Duplicate":
265+
var aerr smithy.APIError
266+
switch ok := errors.As(err, &aerr); {
267+
case ok && aerr.ErrorCode() == "InvalidPermission.Duplicate":
265268
log.Warnf("Traffic from the endpoint VPC's CIDR block to the associated VPC's worker SG is already authorized")
266269
default:
267270
log.WithError(err).Fatal("Failed to authorize traffic from the endpoint VPC's CIDR block to the associated VPC's worker SG")
@@ -358,8 +361,9 @@ func addRouteToRouteTables(
358361
})
359362
if err != nil {
360363
// Proceed if route already exists, fail otherwise
361-
switch aerr, ok := err.(awserr.Error); {
362-
case ok && aerr.Code() == "RouteAlreadyExists":
364+
var aerr smithy.APIError
365+
switch ok := errors.As(err, &aerr); {
366+
case ok && aerr.ErrorCode() == "RouteAlreadyExists":
363367
log.Warnf("Route already exists in route table %v", *routeTable.RouteTableId)
364368
default:
365369
log.WithError(err).Fatalf("Failed to create route for route table %v", *routeTable.RouteTableId)

contrib/pkg/awsprivatelink/endpointvpc/remove.go

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package endpointvpc
22

33
import (
44
"context"
5+
"errors"
56

6-
"github.com/aws/aws-sdk-go/aws"
7-
"github.com/aws/aws-sdk-go/aws/awserr"
8-
"github.com/aws/aws-sdk-go/service/ec2"
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/service/ec2"
9+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
10+
"github.com/aws/smithy-go"
911

1012
hivev1 "github.com/openshift/hive/apis/hive/v1"
1113
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
1214
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
13-
"github.com/openshift/hive/pkg/awsclient"
15+
awsclient "github.com/openshift/hive/pkg/awsclientv2"
1416

1517
log "github.com/sirupsen/logrus"
1618
"github.com/spf13/cobra"
@@ -111,7 +113,7 @@ func (o *endpointVPCRemoveOptions) Validate(cmd *cobra.Command, args []string) e
111113

112114
func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error {
113115
// Get default SG of the endpoint VPC
114-
endpointVPCDefaultSG, err := awsutils.GetDefaultSGOfVpc(o.endpointVpcClients, aws.String(o.endpointVpcId))
116+
endpointVPCDefaultSG, err := awsutils.GetDefaultSGOfVpc(o.endpointVpcClients, o.endpointVpcId)
115117
if err != nil {
116118
log.WithError(err).Fatal("Failed to get default SG of the endpoint VPC")
117119
}
@@ -124,12 +126,12 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
124126
associatedVpcId := associatedVpc.AWSPrivateLinkVPC.VPCID
125127
log.Infof("Removing networking elements between associated VPC %v and endpoint VPC %v", associatedVpcId, o.endpointVpcId)
126128

127-
associatedVpcCIDR, err := awsutils.GetCIDRFromVpcId(associatedVpcClients, aws.String(associatedVpcId))
129+
associatedVpcCIDR, err := awsutils.GetCIDRFromVpcId(associatedVpcClients, associatedVpcId)
128130
if err != nil {
129131
log.Fatal("Failed to get CIDR of associated VPC")
130132
}
131133
log.Debugf("Found associated VPC CIDR = %v", associatedVpcCIDR)
132-
endpointVpcCIDR, err := awsutils.GetCIDRFromVpcId(o.endpointVpcClients, aws.String(o.endpointVpcId))
134+
endpointVpcCIDR, err := awsutils.GetCIDRFromVpcId(o.endpointVpcClients, o.endpointVpcId)
133135
if err != nil {
134136
log.Fatal("Failed to get CIDR of endpoint VPC")
135137
}
@@ -138,8 +140,8 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
138140
// Delete VPC peering connection
139141
if err = deleteVpcPeeringConnection(
140142
associatedVpcClients,
141-
aws.String(associatedVpcId),
142-
aws.String(o.endpointVpcId),
143+
associatedVpcId,
144+
o.endpointVpcId,
143145
); err != nil {
144146
log.WithError(err).Fatal("Failed to delete VPC peering connection")
145147
}
@@ -148,25 +150,25 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
148150
log.Info("Deleting route from private route tables of the associated VPC")
149151
if err = deleteRouteFromRouteTables(
150152
associatedVpcClients,
151-
aws.String(associatedVpcId),
153+
associatedVpcId,
152154
aws.String(endpointVpcCIDR),
153-
&ec2.Filter{Name: aws.String("tag:Name"), Values: []*string{aws.String("*private*")}},
155+
ec2types.Filter{Name: aws.String("tag:Name"), Values: []string{"*private*"}},
154156
); err != nil {
155157
log.WithError(err).Fatal("Failed to delete route from private route tables of the associated VPC")
156158
}
157159

158160
log.Info("Deleting route from route tables of the endpoint subnets")
159161
if err = deleteRouteFromRouteTables(
160162
o.endpointVpcClients,
161-
aws.String(o.endpointVpcId),
163+
o.endpointVpcId,
162164
aws.String(associatedVpcCIDR),
163-
&ec2.Filter{Name: aws.String("association.subnet-id"), Values: aws.StringSlice(o.endpointSubnetIds)},
165+
ec2types.Filter{Name: aws.String("association.subnet-id"), Values: o.endpointSubnetIds},
164166
); err != nil {
165167
log.WithError(err).Fatal("Failed to delete route from route tables of the endpoint subnets")
166168
}
167169

168170
// Update SGs
169-
associatedVpcWorkerSG, err := awsutils.GetWorkerSGFromVpcId(associatedVpcClients, aws.String(associatedVpcId))
171+
associatedVpcWorkerSG, err := awsutils.GetWorkerSGFromVpcId(associatedVpcClients, associatedVpcId)
170172
if err != nil {
171173
log.WithError(err).Fatal("Failed to get worker SG of the associated Hive cluster")
172174
}
@@ -183,8 +185,9 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
183185
aws.String(endpointVPCDefaultSG),
184186
); err != nil {
185187
// Proceed if ingress not found, fail otherwise
186-
switch aerr, ok := err.(awserr.Error); {
187-
case ok && aerr.Code() == "InvalidPermission.NotFound":
188+
var aerr smithy.APIError
189+
switch ok := errors.As(err, &aerr); {
190+
case ok && aerr.ErrorCode() == "InvalidPermission.NotFound":
188191
log.Warnf("Access from the endpoint VPC's default SG to the associated VPC's worker SG is not enabled")
189192
default:
190193
log.WithError(err).Fatal("Failed to revoke access from the endpoint VPC's default SG to the associated VPC's worker SG")
@@ -198,8 +201,9 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
198201
aws.String(associatedVpcWorkerSG),
199202
); err != nil {
200203
// Proceed if ingress not found, fail otherwise
201-
switch aerr, ok := err.(awserr.Error); {
202-
case ok && aerr.Code() == "InvalidPermission.NotFound":
204+
var aerr smithy.APIError
205+
switch ok := errors.As(err, &aerr); {
206+
case ok && aerr.ErrorCode() == "InvalidPermission.NotFound":
203207
log.Warnf("Access from the associated VPC's worker SG to the endpoint VPC's default SG is not enabled")
204208
default:
205209
log.WithError(err).Fatal("Failed to revoke access from the associated VPC's worker SG to the endpoint VPC's default SG")
@@ -215,8 +219,9 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
215219
aws.String(endpointVpcCIDR),
216220
); err != nil {
217221
// Proceed if ingress not found, fail otherwise
218-
switch aerr, ok := err.(awserr.Error); {
219-
case ok && aerr.Code() == "InvalidPermission.NotFound":
222+
var aerr smithy.APIError
223+
switch ok := errors.As(err, &aerr); {
224+
case ok && aerr.ErrorCode() == "InvalidPermission.NotFound":
220225
log.Warnf("Access from the endpoint VPC's CIDR block to the associated VPC's worker SG is not enabled")
221226
default:
222227
log.WithError(err).Fatal("Failed to revoke access from the endpoint VPC's CIDR block to the associated VPC's worker SG")
@@ -230,8 +235,9 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
230235
aws.String(associatedVpcCIDR),
231236
); err != nil {
232237
// Proceed if ingress not found, fail otherwise
233-
switch aerr, ok := err.(awserr.Error); {
234-
case ok && aerr.Code() == "InvalidPermission.NotFound":
238+
var aerr smithy.APIError
239+
switch ok := errors.As(err, &aerr); {
240+
case ok && aerr.ErrorCode() == "InvalidPermission.NotFound":
235241
log.Warnf("Access from the associated VPC's CIDR block to the endpoint VPC's default SG is not enabled")
236242
default:
237243
log.WithError(err).Fatal("Failed to revoke access from the associated VPC's CIDR block to the endpoint VPC's default SG")
@@ -259,23 +265,23 @@ func (o *endpointVPCRemoveOptions) removeEndpointVpcFromHiveConfig() {
259265
}
260266
}
261267

262-
func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 *string) error {
268+
func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 string) error {
263269
log.Info("Deleting VPC peering connection between the associated VPC and the endpoint VPC")
264270

265271
describeVpcPeeringConnectionsOutput, err := awsClients.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{
266-
Filters: []*ec2.Filter{
272+
Filters: []ec2types.Filter{
267273
{
268274
Name: aws.String("requester-vpc-info.vpc-id"),
269-
Values: []*string{VpcId1, VpcId2},
275+
Values: []string{VpcId1, VpcId2},
270276
},
271277
{
272278
Name: aws.String("accepter-vpc-info.vpc-id"),
273-
Values: []*string{VpcId1, VpcId2},
279+
Values: []string{VpcId1, VpcId2},
274280
},
275281
// Only one peering connection can be active at any given time between a pair of VPCs
276282
{
277283
Name: aws.String("status-code"),
278-
Values: []*string{aws.String("active")},
284+
Values: []string{"active"},
279285
},
280286
},
281287
})
@@ -296,7 +302,7 @@ func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 *str
296302
log.Debugf("The deletion of VPC peering connection %v has been initiated", *VpcPeeringConnectionId)
297303

298304
if err = awsClients.WaitUntilVpcPeeringConnectionDeleted(&ec2.DescribeVpcPeeringConnectionsInput{
299-
VpcPeeringConnectionIds: []*string{VpcPeeringConnectionId},
305+
VpcPeeringConnectionIds: []string{aws.ToString(VpcPeeringConnectionId)},
300306
}); err != nil {
301307
return err
302308
}
@@ -307,13 +313,13 @@ func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 *str
307313

308314
func deleteRouteFromRouteTables(
309315
vpcClients awsclient.Client,
310-
vpcId, peerCIDR *string,
311-
additionalFiltersForRouteTables ...*ec2.Filter,
316+
vpcId string, peerCIDR *string,
317+
additionalFiltersForRouteTables ...ec2types.Filter,
312318
) error {
313-
filters := append([]*ec2.Filter{
319+
filters := append([]ec2types.Filter{
314320
{
315321
Name: aws.String("vpc-id"),
316-
Values: []*string{vpcId},
322+
Values: []string{vpcId},
317323
},
318324
}, additionalFiltersForRouteTables...)
319325

@@ -329,8 +335,9 @@ func deleteRouteFromRouteTables(
329335
})
330336
if err != nil {
331337
// Proceed if route not found, fail otherwise
332-
switch aerr, ok := err.(awserr.Error); {
333-
case ok && aerr.Code() == "InvalidRoute.NotFound":
338+
var aerr smithy.APIError
339+
switch ok := errors.As(err, aerr); {
340+
case ok && aerr.ErrorCode() == "InvalidRoute.NotFound":
334341
log.Warnf("Route not found in route table %v", *routeTable.RouteTableId)
335342
default:
336343
log.WithError(err).Fatalf("Failed to delete route from route table %v", *routeTable.RouteTableId)

0 commit comments

Comments
 (0)