@@ -2,15 +2,17 @@ package endpointvpc
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
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"
9
11
10
12
hivev1 "github.com/openshift/hive/apis/hive/v1"
11
13
"github.com/openshift/hive/contrib/pkg/awsprivatelink/common"
12
14
awsutils "github.com/openshift/hive/contrib/pkg/utils/aws"
13
- "github.com/openshift/hive/pkg/awsclient "
15
+ awsclient "github.com/openshift/hive/pkg/awsclientv2 "
14
16
15
17
log "github.com/sirupsen/logrus"
16
18
"github.com/spf13/cobra"
@@ -111,7 +113,7 @@ func (o *endpointVPCRemoveOptions) Validate(cmd *cobra.Command, args []string) e
111
113
112
114
func (o * endpointVPCRemoveOptions ) Run (cmd * cobra.Command , args []string ) error {
113
115
// 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 )
115
117
if err != nil {
116
118
log .WithError (err ).Fatal ("Failed to get default SG of the endpoint VPC" )
117
119
}
@@ -124,12 +126,12 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
124
126
associatedVpcId := associatedVpc .AWSPrivateLinkVPC .VPCID
125
127
log .Infof ("Removing networking elements between associated VPC %v and endpoint VPC %v" , associatedVpcId , o .endpointVpcId )
126
128
127
- associatedVpcCIDR , err := awsutils .GetCIDRFromVpcId (associatedVpcClients , aws . String ( associatedVpcId ) )
129
+ associatedVpcCIDR , err := awsutils .GetCIDRFromVpcId (associatedVpcClients , associatedVpcId )
128
130
if err != nil {
129
131
log .Fatal ("Failed to get CIDR of associated VPC" )
130
132
}
131
133
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 )
133
135
if err != nil {
134
136
log .Fatal ("Failed to get CIDR of endpoint VPC" )
135
137
}
@@ -138,8 +140,8 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
138
140
// Delete VPC peering connection
139
141
if err = deleteVpcPeeringConnection (
140
142
associatedVpcClients ,
141
- aws . String ( associatedVpcId ) ,
142
- aws . String ( o .endpointVpcId ) ,
143
+ associatedVpcId ,
144
+ o .endpointVpcId ,
143
145
); err != nil {
144
146
log .WithError (err ).Fatal ("Failed to delete VPC peering connection" )
145
147
}
@@ -148,25 +150,25 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
148
150
log .Info ("Deleting route from private route tables of the associated VPC" )
149
151
if err = deleteRouteFromRouteTables (
150
152
associatedVpcClients ,
151
- aws . String ( associatedVpcId ) ,
153
+ associatedVpcId ,
152
154
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*" }},
154
156
); err != nil {
155
157
log .WithError (err ).Fatal ("Failed to delete route from private route tables of the associated VPC" )
156
158
}
157
159
158
160
log .Info ("Deleting route from route tables of the endpoint subnets" )
159
161
if err = deleteRouteFromRouteTables (
160
162
o .endpointVpcClients ,
161
- aws . String ( o .endpointVpcId ) ,
163
+ o .endpointVpcId ,
162
164
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 },
164
166
); err != nil {
165
167
log .WithError (err ).Fatal ("Failed to delete route from route tables of the endpoint subnets" )
166
168
}
167
169
168
170
// Update SGs
169
- associatedVpcWorkerSG , err := awsutils .GetWorkerSGFromVpcId (associatedVpcClients , aws . String ( associatedVpcId ) )
171
+ associatedVpcWorkerSG , err := awsutils .GetWorkerSGFromVpcId (associatedVpcClients , associatedVpcId )
170
172
if err != nil {
171
173
log .WithError (err ).Fatal ("Failed to get worker SG of the associated Hive cluster" )
172
174
}
@@ -183,8 +185,9 @@ func (o *endpointVPCRemoveOptions) Run(cmd *cobra.Command, args []string) error
183
185
aws .String (endpointVPCDefaultSG ),
184
186
); err != nil {
185
187
// 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" :
188
191
log .Warnf ("Access from the endpoint VPC's default SG to the associated VPC's worker SG is not enabled" )
189
192
default :
190
193
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
198
201
aws .String (associatedVpcWorkerSG ),
199
202
); err != nil {
200
203
// 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" :
203
207
log .Warnf ("Access from the associated VPC's worker SG to the endpoint VPC's default SG is not enabled" )
204
208
default :
205
209
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
215
219
aws .String (endpointVpcCIDR ),
216
220
); err != nil {
217
221
// 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" :
220
225
log .Warnf ("Access from the endpoint VPC's CIDR block to the associated VPC's worker SG is not enabled" )
221
226
default :
222
227
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
230
235
aws .String (associatedVpcCIDR ),
231
236
); err != nil {
232
237
// 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" :
235
241
log .Warnf ("Access from the associated VPC's CIDR block to the endpoint VPC's default SG is not enabled" )
236
242
default :
237
243
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() {
259
265
}
260
266
}
261
267
262
- func deleteVpcPeeringConnection (awsClients awsclient.Client , VpcId1 , VpcId2 * string ) error {
268
+ func deleteVpcPeeringConnection (awsClients awsclient.Client , VpcId1 , VpcId2 string ) error {
263
269
log .Info ("Deleting VPC peering connection between the associated VPC and the endpoint VPC" )
264
270
265
271
describeVpcPeeringConnectionsOutput , err := awsClients .DescribeVpcPeeringConnections (& ec2.DescribeVpcPeeringConnectionsInput {
266
- Filters : []* ec2 .Filter {
272
+ Filters : []ec2types .Filter {
267
273
{
268
274
Name : aws .String ("requester-vpc-info.vpc-id" ),
269
- Values : []* string {VpcId1 , VpcId2 },
275
+ Values : []string {VpcId1 , VpcId2 },
270
276
},
271
277
{
272
278
Name : aws .String ("accepter-vpc-info.vpc-id" ),
273
- Values : []* string {VpcId1 , VpcId2 },
279
+ Values : []string {VpcId1 , VpcId2 },
274
280
},
275
281
// Only one peering connection can be active at any given time between a pair of VPCs
276
282
{
277
283
Name : aws .String ("status-code" ),
278
- Values : []* string {aws . String ( "active" ) },
284
+ Values : []string {"active" },
279
285
},
280
286
},
281
287
})
@@ -296,7 +302,7 @@ func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 *str
296
302
log .Debugf ("The deletion of VPC peering connection %v has been initiated" , * VpcPeeringConnectionId )
297
303
298
304
if err = awsClients .WaitUntilVpcPeeringConnectionDeleted (& ec2.DescribeVpcPeeringConnectionsInput {
299
- VpcPeeringConnectionIds : []* string {VpcPeeringConnectionId },
305
+ VpcPeeringConnectionIds : []string {aws . ToString ( VpcPeeringConnectionId ) },
300
306
}); err != nil {
301
307
return err
302
308
}
@@ -307,13 +313,13 @@ func deleteVpcPeeringConnection(awsClients awsclient.Client, VpcId1, VpcId2 *str
307
313
308
314
func deleteRouteFromRouteTables (
309
315
vpcClients awsclient.Client ,
310
- vpcId , peerCIDR * string ,
311
- additionalFiltersForRouteTables ... * ec2 .Filter ,
316
+ vpcId string , peerCIDR * string ,
317
+ additionalFiltersForRouteTables ... ec2types .Filter ,
312
318
) error {
313
- filters := append ([]* ec2 .Filter {
319
+ filters := append ([]ec2types .Filter {
314
320
{
315
321
Name : aws .String ("vpc-id" ),
316
- Values : []* string {vpcId },
322
+ Values : []string {vpcId },
317
323
},
318
324
}, additionalFiltersForRouteTables ... )
319
325
@@ -329,8 +335,9 @@ func deleteRouteFromRouteTables(
329
335
})
330
336
if err != nil {
331
337
// 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" :
334
341
log .Warnf ("Route not found in route table %v" , * routeTable .RouteTableId )
335
342
default :
336
343
log .WithError (err ).Fatalf ("Failed to delete route from route table %v" , * routeTable .RouteTableId )
0 commit comments