/
vpc_endpoint.go
208 lines (175 loc) · 5.96 KB
/
vpc_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2022.
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 aws_client
import (
"context"
"errors"
"fmt"
"math"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go"
avov1alpha2 "github.com/openshift/aws-vpce-operator/api/v1alpha2"
"github.com/openshift/aws-vpce-operator/pkg/util"
)
// SelectVPCForVPCEndpoint uses a "least connection" strategy to place a VPC Endpoint in the provided VPC ID with the
// fewest existing VPC Endpoints in it to balance out quota usage.
// https://docs.aws.amazon.com/vpc/latest/userguide/amazon-vpc-limits.html#vpc-limits-endpoints
func (c *AWSClient) SelectVPCForVPCEndpoint(ctx context.Context, ids ...string) (string, error) {
if len(ids) == 0 {
return "", errors.New("must specify vpc id when counting VPC Endpoints per VPC")
}
input := &ec2.DescribeVpcEndpointsInput{
Filters: []types.Filter{
{
Name: aws.String("vpc-id"),
Values: ids,
},
},
}
minVpcId := ""
minVpceConsumed := math.MaxInt
vpcePerVpc := map[string]int{}
for _, id := range ids {
vpcePerVpc[id] = 0
}
paginator := ec2.NewDescribeVpcEndpointsPaginator(c.ec2Client, input)
for paginator.HasMorePages() {
resp, err := paginator.NextPage(ctx)
if err != nil {
return "", err
}
for _, vpce := range resp.VpcEndpoints {
vpcePerVpc[*vpce.VpcId]++
}
}
for vpcId, vpceCount := range vpcePerVpc {
if vpceCount < minVpceConsumed {
minVpceConsumed = vpceCount
minVpcId = vpcId
}
}
if minVpcId == "" {
return "", errors.New("unexpectedly did not select a VPC for the VPC Endpoint")
}
return minVpcId, nil
}
// FilterVpcIdsByTags tags in a list of tags and returns a list of AWS VPC Ids that have all of the provided tags
func (c *AWSClient) FilterVpcIdsByTags(ctx context.Context, tags []avov1alpha2.Tag) ([]string, error) {
if len(tags) == 0 {
return nil, errors.New("must specify tags when filtering VPCs by tags")
}
filters := make([]types.Filter, len(tags))
for i, tag := range tags {
filters[i] = types.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", tag.Key)),
Values: []string{tag.Value},
}
}
resp, err := c.ec2Client.DescribeVpcs(ctx, &ec2.DescribeVpcsInput{
Filters: filters,
})
if err != nil {
return nil, err
}
if len(resp.Vpcs) == 0 {
return nil, fmt.Errorf("no VPCs found when filtering by tags: %v", tags)
}
ids := make([]string, len(resp.Vpcs))
for i, vpc := range resp.Vpcs {
ids[i] = *vpc.VpcId
}
return ids, nil
}
// DescribeSingleVPCEndpointById returns information about a VPC endpoint with a given id.
func (c *AWSClient) DescribeSingleVPCEndpointById(ctx context.Context, id string) (*ec2.DescribeVpcEndpointsOutput, error) {
if id == "" {
// Otherwise, AWS will return all VPC endpoints (interpreting as no specified filter)
return &ec2.DescribeVpcEndpointsOutput{}, nil
}
input := &ec2.DescribeVpcEndpointsInput{
VpcEndpointIds: []string{id},
}
resp, err := c.ec2Client.DescribeVpcEndpoints(ctx, input)
if err != nil {
var ae smithy.APIError
if errors.As(err, &ae) {
// Don't return an error if the VPC endpoint with the specified ID doesn't exist
if ae.ErrorCode() == "InvalidVpcEndpointId.NotFound" {
return nil, nil
}
}
return nil, err
}
if len(resp.VpcEndpoints) != 1 {
return nil, fmt.Errorf("expected 1 VPC endpoint, got %d", len(resp.VpcEndpoints))
}
return resp, err
}
// FilterVPCEndpointByDefaultTags returns information about a VPC endpoint with the default expected tags.
func (c *AWSClient) FilterVPCEndpointByDefaultTags(ctx context.Context, clusterTag, vpceNameTag string) (*ec2.DescribeVpcEndpointsOutput, error) {
if clusterTag == "" {
return &ec2.DescribeVpcEndpointsOutput{}, nil
}
return c.ec2Client.DescribeVpcEndpoints(ctx, &ec2.DescribeVpcEndpointsInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{vpceNameTag},
},
{
Name: aws.String("tag-key"),
Values: []string{clusterTag},
},
{
Name: aws.String("tag:" + util.OperatorTagKey),
Values: []string{util.OperatorTagValue},
},
},
})
}
// CreateDefaultInterfaceVPCEndpoint creates an interface VPC endpoint with
// the default (open to all) VPC Endpoint policy. It attaches no security groups
// nor associates the VPC Endpoint with any subnets.
func (c *AWSClient) CreateDefaultInterfaceVPCEndpoint(ctx context.Context, name, vpcId, serviceName, tagKey string) (*ec2.CreateVpcEndpointOutput, error) {
tags, err := util.GenerateAwsTags(name, tagKey)
if err != nil {
return nil, err
}
input := &ec2.CreateVpcEndpointInput{
// TODO: Implement ClientToken for idempotency guarantees
// ClientToken: "token",
VpcId: &vpcId,
ServiceName: &serviceName,
VpcEndpointType: types.VpcEndpointTypeInterface,
TagSpecifications: []types.TagSpecification{
{
ResourceType: types.ResourceTypeVpcEndpoint,
Tags: tags,
},
},
}
return c.ec2Client.CreateVpcEndpoint(ctx, input)
}
// DeleteVPCEndpoint deletes a VPC endpoint with the given id.
func (c *AWSClient) DeleteVPCEndpoint(ctx context.Context, id string) (*ec2.DeleteVpcEndpointsOutput, error) {
input := &ec2.DeleteVpcEndpointsInput{
VpcEndpointIds: []string{id},
}
return c.ec2Client.DeleteVpcEndpoints(ctx, input)
}
// ModifyVpcEndpoint modifies a VPC endpoint
func (c *AWSClient) ModifyVpcEndpoint(ctx context.Context, input *ec2.ModifyVpcEndpointInput) (*ec2.ModifyVpcEndpointOutput, error) {
return c.ec2Client.ModifyVpcEndpoint(ctx, input)
}