@@ -2,18 +2,20 @@ package endpointvpc
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"reflect"
7
8
"sort"
8
9
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"
12
14
13
15
hivev1 "github.com/openshift/hive/apis/hive/v1"
14
16
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
15
17
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
16
- "github.com/openshift/hive/pkg/awsclient "
18
+ awsclient "github.com/openshift/hive/pkg/awsclientv2 "
17
19
18
20
log "github.com/sirupsen/logrus"
19
21
"github.com/spf13/cobra"
@@ -108,15 +110,15 @@ func (o *endpointVPCAddOptions) Complete(cmd *cobra.Command, args []string) erro
108
110
func (o * endpointVPCAddOptions ) Validate (cmd * cobra.Command , args []string ) error {
109
111
// Check if the endpoint VPC exists
110
112
if _ , err := o .endpointVpcClients .DescribeVpcs (& ec2.DescribeVpcsInput {
111
- VpcIds : []* string {aws . String ( o .endpointVpcId ) },
113
+ VpcIds : []string {o .endpointVpcId },
112
114
}); err != nil {
113
115
log .WithError (err ).Fatal ("Failed to describe endpoint VPC" )
114
116
}
115
117
116
118
// Check if the endpoint subnets belong to the endpoint VPC
117
119
err := o .endpointVpcClients .DescribeSubnetsPages (
118
120
& ec2.DescribeSubnetsInput {
119
- SubnetIds : aws . StringSlice ( o .endpointSubnetIds ) ,
121
+ SubnetIds : o .endpointSubnetIds ,
120
122
},
121
123
func (page * ec2.DescribeSubnetsOutput , lastPage bool ) bool {
122
124
for _ , subnet := range page .Subnets {
@@ -136,7 +138,7 @@ func (o *endpointVPCAddOptions) Validate(cmd *cobra.Command, args []string) erro
136
138
137
139
func (o * endpointVPCAddOptions ) Run (cmd * cobra.Command , args []string ) error {
138
140
// 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 )
140
142
if err != nil {
141
143
log .WithError (err ).Fatal ("Failed to get default SG of the endpoint VPC" )
142
144
}
@@ -169,30 +171,27 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
169
171
log .Info ("Adding route to private route tables of the associated VPC" )
170
172
if err = addRouteToRouteTables (
171
173
associatedVpcClients ,
172
- aws . String ( associatedVpcId ) ,
174
+ associatedVpcId ,
173
175
endpointVpcCIDR ,
174
176
vpcPeeringConnectionId ,
175
- & ec2 .Filter {Name : aws .String ("tag:Name" ), Values : []* string {aws . String ( "*private*" ) }},
177
+ ec2types .Filter {Name : aws .String ("tag:Name" ), Values : []string {"*private*" }},
176
178
); err != nil {
177
179
log .WithError (err ).Fatal ("Failed to add route to private route tables of the associated VPC" )
178
180
}
179
181
180
182
log .Info ("Adding route to route tables of the endpoint subnets" )
181
183
if err = addRouteToRouteTables (
182
184
o .endpointVpcClients ,
183
- aws . String ( o .endpointVpcId ) ,
185
+ o .endpointVpcId ,
184
186
associatedVpcCIDR ,
185
187
vpcPeeringConnectionId ,
186
- & ec2 .Filter {Name : aws .String ("association.subnet-id" ), Values : aws . StringSlice ( o .endpointSubnetIds ) },
188
+ ec2types .Filter {Name : aws .String ("association.subnet-id" ), Values : o .endpointSubnetIds },
187
189
); err != nil {
188
190
log .WithError (err ).Fatal ("Failed to add route to route tables of the endpoint subnets" )
189
191
}
190
192
191
193
// Update SGs
192
- associatedVpcWorkerSG , err := awsutils .GetWorkerSGFromVpcId (
193
- associatedVpcClients ,
194
- aws .String (associatedVpcId ),
195
- )
194
+ associatedVpcWorkerSG , err := awsutils .GetWorkerSGFromVpcId (associatedVpcClients , associatedVpcId )
196
195
if err != nil {
197
196
log .WithError (err ).Fatal ("Failed to get worker SG of the associated VPC" )
198
197
}
@@ -210,8 +209,9 @@ func (o *endpointVPCAddOptions) Run(cmd *cobra.Command, args []string) error {
210
209
aws .String (fmt .Sprintf ("Access from worker SG of associated VPC %s" , associatedVpcId )),
211
210
); err != nil {
212
211
// 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" :
215
215
log .Warnf ("Traffic from the associated VPC's worker SG to the endpoint VPC's default SG is already authorized" )
216
216
default :
217
217
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 {
226
226
aws .String (fmt .Sprintf ("Access from default SG of endpoint VPC %s" , o .endpointVpcId )),
227
227
); err != nil {
228
228
// 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" :
231
232
log .Warnf ("Traffic from the endpoint VPC's default SG to the associated VPC's worker SG is already authorized" )
232
233
default :
233
234
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 {
244
245
aws .String (fmt .Sprintf ("Access from CIDR block of associated VPC %s" , associatedVpcId )),
245
246
); err != nil {
246
247
// 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" :
249
251
log .Warnf ("Traffic from the associated VPC's CIDR block to the endpoint VPC's default SG is already authorized" )
250
252
default :
251
253
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 {
260
262
aws .String (fmt .Sprintf ("Access from CIDR block of endpoint VPC %s" , o .endpointVpcId )),
261
263
); err != nil {
262
264
// 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" :
265
268
log .Warnf ("Traffic from the endpoint VPC's CIDR block to the associated VPC's worker SG is already authorized" )
266
269
default :
267
270
log .WithError (err ).Fatal ("Failed to authorize traffic from the endpoint VPC's CIDR block to the associated VPC's worker SG" )
@@ -283,7 +286,7 @@ func (o *endpointVPCAddOptions) addEndpointVpcToHiveConfig() {
283
286
var endpointSubnets []hivev1.AWSPrivateLinkSubnet
284
287
if err := o .endpointVpcClients .DescribeSubnetsPages (
285
288
& ec2.DescribeSubnetsInput {
286
- SubnetIds : aws . StringSlice ( o .endpointSubnetIds ) ,
289
+ SubnetIds : o .endpointSubnetIds ,
287
290
},
288
291
func (page * ec2.DescribeSubnetsOutput , lastPage bool ) bool {
289
292
for _ , subnet := range page .Subnets {
@@ -335,13 +338,13 @@ func (o *endpointVPCAddOptions) addEndpointVpcToHiveConfig() {
335
338
336
339
func addRouteToRouteTables (
337
340
vpcClients awsclient.Client ,
338
- vpcId , peerCIDR , VpcPeeringConnectionId * string ,
339
- additionalFiltersForRouteTables ... * ec2 .Filter ,
341
+ vpcId string , peerCIDR , VpcPeeringConnectionId * string ,
342
+ additionalFiltersForRouteTables ... ec2types .Filter ,
340
343
) error {
341
- filters := append ([]* ec2 .Filter {
344
+ filters := append ([]ec2types .Filter {
342
345
{
343
346
Name : aws .String ("vpc-id" ),
344
- Values : []* string {vpcId },
347
+ Values : []string {vpcId },
345
348
},
346
349
}, additionalFiltersForRouteTables ... )
347
350
@@ -358,8 +361,9 @@ func addRouteToRouteTables(
358
361
})
359
362
if err != nil {
360
363
// 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" :
363
367
log .Warnf ("Route already exists in route table %v" , * routeTable .RouteTableId )
364
368
default :
365
369
log .WithError (err ).Fatalf ("Failed to create route for route table %v" , * routeTable .RouteTableId )
@@ -389,10 +393,11 @@ func setupVpcPeeringConnection(
389
393
if err != nil {
390
394
return nil , err
391
395
}
396
+ // TODO: Nil pointer check?
392
397
log .Debugf ("VPC peering connection %v requested" , * createVpcPeeringConnectionOutput .VpcPeeringConnection .VpcPeeringConnectionId )
393
398
394
399
err = endpointVpcClients .WaitUntilVpcPeeringConnectionExists (& ec2.DescribeVpcPeeringConnectionsInput {
395
- VpcPeeringConnectionIds : []* string {createVpcPeeringConnectionOutput .VpcPeeringConnection .VpcPeeringConnectionId },
400
+ VpcPeeringConnectionIds : []string {* createVpcPeeringConnectionOutput .VpcPeeringConnection .VpcPeeringConnectionId },
396
401
})
397
402
if err != nil {
398
403
return nil , err
0 commit comments