forked from kubernetes-sigs/aws-load-balancer-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_build_load_balancer.go
412 lines (385 loc) · 15.8 KB
/
model_build_load_balancer.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package ingress
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"regexp"
awssdk "github.com/aws/aws-sdk-go/aws"
ec2sdk "github.com/aws/aws-sdk-go/service/ec2"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/sonal-chauhan/aws-load-balancer-controller/apis/elbv2/v1beta1"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/algorithm"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/annotations"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/config"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/deploy/tracking"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/equality"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/k8s"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/model/core"
elbv2model "github.com/sonal-chauhan/aws-load-balancer-controller/pkg/model/elbv2"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/networking"
)
const (
resourceIDLoadBalancer = "LoadBalancer"
minimalAvailableIPAddressCount = int64(8)
)
func (t *defaultModelBuildTask) buildLoadBalancer(ctx context.Context, listenPortConfigByPort map[int64]listenPortConfig) (*elbv2model.LoadBalancer, error) {
lbSpec, err := t.buildLoadBalancerSpec(ctx, listenPortConfigByPort)
if err != nil {
return nil, err
}
lb := elbv2model.NewLoadBalancer(t.stack, resourceIDLoadBalancer, lbSpec)
t.loadBalancer = lb
return lb, nil
}
func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, listenPortConfigByPort map[int64]listenPortConfig) (elbv2model.LoadBalancerSpec, error) {
scheme, err := t.buildLoadBalancerScheme(ctx)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
ipAddressType, err := t.buildLoadBalancerIPAddressType(ctx)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
subnetMappings, err := t.buildLoadBalancerSubnetMappings(ctx, scheme)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
securityGroups, err := t.buildLoadBalancerSecurityGroups(ctx, listenPortConfigByPort, ipAddressType)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
coIPv4Pool, err := t.buildLoadBalancerCOIPv4Pool(ctx)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
loadBalancerAttributes, err := t.buildLoadBalancerAttributes(ctx)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
tags, err := t.buildLoadBalancerTags(ctx)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
name, err := t.buildLoadBalancerName(ctx, scheme)
if err != nil {
return elbv2model.LoadBalancerSpec{}, err
}
return elbv2model.LoadBalancerSpec{
Name: name,
Type: elbv2model.LoadBalancerTypeApplication,
Scheme: &scheme,
IPAddressType: &ipAddressType,
SubnetMappings: subnetMappings,
SecurityGroups: securityGroups,
CustomerOwnedIPv4Pool: coIPv4Pool,
LoadBalancerAttributes: loadBalancerAttributes,
Tags: tags,
}, nil
}
var invalidLoadBalancerNamePattern = regexp.MustCompile("[[:^alnum:]]")
func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme elbv2model.LoadBalancerScheme) (string, error) {
explicitNames := sets.String{}
for _, member := range t.ingGroup.Members {
rawName := ""
if exists := t.annotationParser.ParseStringAnnotation(annotations.IngressSuffixLoadBalancerName, &rawName, member.Ing.Annotations); !exists {
continue
}
explicitNames.Insert(rawName)
}
if len(explicitNames) == 1 {
name, _ := explicitNames.PopAny()
// The name of the loadbalancer can only have up to 32 characters
if len(name) > 32 {
return "", errors.New("load balancer name cannot be longer than 32 characters")
}
return name, nil
}
if len(explicitNames) > 1 {
return "", errors.Errorf("conflicting load balancer name: %v", explicitNames)
}
uuidHash := sha256.New()
_, _ = uuidHash.Write([]byte(t.clusterName))
_, _ = uuidHash.Write([]byte(t.ingGroup.ID.String()))
_, _ = uuidHash.Write([]byte(scheme))
uuid := hex.EncodeToString(uuidHash.Sum(nil))
if t.ingGroup.ID.IsExplicit() {
payload := invalidLoadBalancerNamePattern.ReplaceAllString(t.ingGroup.ID.Name, "")
return fmt.Sprintf("k8s-%.17s-%.10s", payload, uuid), nil
}
sanitizedNamespace := invalidLoadBalancerNamePattern.ReplaceAllString(t.ingGroup.ID.Namespace, "")
sanitizedName := invalidLoadBalancerNamePattern.ReplaceAllString(t.ingGroup.ID.Name, "")
return fmt.Sprintf("k8s-%.8s-%.8s-%.10s", sanitizedNamespace, sanitizedName, uuid), nil
}
func (t *defaultModelBuildTask) buildLoadBalancerScheme(_ context.Context) (elbv2model.LoadBalancerScheme, error) {
explicitSchemes := sets.String{}
for _, member := range t.ingGroup.Members {
if member.IngClassConfig.IngClassParams != nil && member.IngClassConfig.IngClassParams.Spec.Scheme != nil {
scheme := string(*member.IngClassConfig.IngClassParams.Spec.Scheme)
explicitSchemes.Insert(scheme)
continue
}
rawSchema := ""
if exists := t.annotationParser.ParseStringAnnotation(annotations.IngressSuffixScheme, &rawSchema, member.Ing.Annotations); !exists {
continue
}
explicitSchemes.Insert(rawSchema)
}
if len(explicitSchemes) == 0 {
return t.defaultScheme, nil
}
if len(explicitSchemes) > 1 {
return "", errors.Errorf("conflicting scheme: %v", explicitSchemes)
}
rawScheme, _ := explicitSchemes.PopAny()
switch rawScheme {
case string(elbv2model.LoadBalancerSchemeInternetFacing):
return elbv2model.LoadBalancerSchemeInternetFacing, nil
case string(elbv2model.LoadBalancerSchemeInternal):
return elbv2model.LoadBalancerSchemeInternal, nil
default:
return "", errors.Errorf("unknown scheme: %v", rawScheme)
}
}
// buildLoadBalancerIPAddressType builds the LoadBalancer IPAddressType.
func (t *defaultModelBuildTask) buildLoadBalancerIPAddressType(_ context.Context) (elbv2model.IPAddressType, error) {
explicitIPAddressTypes := sets.NewString()
for _, member := range t.ingGroup.Members {
if member.IngClassConfig.IngClassParams != nil && member.IngClassConfig.IngClassParams.Spec.IPAddressType != nil {
ipAddressType := string(*member.IngClassConfig.IngClassParams.Spec.IPAddressType)
explicitIPAddressTypes.Insert(ipAddressType)
continue
}
rawIPAddressType := ""
if exists := t.annotationParser.ParseStringAnnotation(annotations.IngressSuffixIPAddressType, &rawIPAddressType, member.Ing.Annotations); !exists {
continue
}
explicitIPAddressTypes.Insert(rawIPAddressType)
}
if len(explicitIPAddressTypes) == 0 {
return t.defaultIPAddressType, nil
}
if len(explicitIPAddressTypes) > 1 {
return "", errors.Errorf("conflicting IPAddressType: %v", explicitIPAddressTypes.List())
}
rawIPAddressType, _ := explicitIPAddressTypes.PopAny()
switch rawIPAddressType {
case string(elbv2model.IPAddressTypeIPV4):
return elbv2model.IPAddressTypeIPV4, nil
case string(elbv2model.IPAddressTypeDualStack):
return elbv2model.IPAddressTypeDualStack, nil
default:
return "", errors.Errorf("unknown IPAddressType: %v", rawIPAddressType)
}
}
func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Context, scheme elbv2model.LoadBalancerScheme) ([]elbv2model.SubnetMapping, error) {
var explicitSubnetSelectorList []*v1beta1.SubnetSelector
var explicitSubnetNameOrIDsList [][]string
for _, member := range t.ingGroup.Members {
if member.IngClassConfig.IngClassParams != nil && member.IngClassConfig.IngClassParams.Spec.Subnets != nil {
explicitSubnetSelectorList = append(explicitSubnetSelectorList, member.IngClassConfig.IngClassParams.Spec.Subnets)
continue
}
var rawSubnetNameOrIDs []string
if exists := t.annotationParser.ParseStringSliceAnnotation(annotations.IngressSuffixSubnets, &rawSubnetNameOrIDs, member.Ing.Annotations); !exists {
continue
}
explicitSubnetNameOrIDsList = append(explicitSubnetNameOrIDsList, rawSubnetNameOrIDs)
}
if len(explicitSubnetSelectorList) != 0 {
if len(explicitSubnetNameOrIDsList) != 0 {
return nil, errors.Errorf("conflicting subnet specifications: IngressClassParams versus annotation")
}
chosenSubnetSelector := explicitSubnetSelectorList[0]
for _, subnetSelector := range explicitSubnetSelectorList[1:] {
if !cmp.Equal(*chosenSubnetSelector, *subnetSelector) {
return nil, errors.Errorf("conflicting IngressClassParams subnet specifications")
}
}
chosenSubnets, err := t.subnetsResolver.ResolveViaSelector(ctx, chosenSubnetSelector,
networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication),
networking.WithSubnetsResolveLBScheme(scheme),
networking.WithSubnetsClusterTagCheck(t.featureGates.Enabled(config.SubnetsClusterTagCheck)),
)
if err != nil {
return nil, err
}
return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil
}
if len(explicitSubnetNameOrIDsList) != 0 {
chosenSubnetNameOrIDs := explicitSubnetNameOrIDsList[0]
for _, subnetNameOrIDs := range explicitSubnetNameOrIDsList[1:] {
// subnetNameOrIDs order doesn't matter
if !cmp.Equal(chosenSubnetNameOrIDs, subnetNameOrIDs, equality.IgnoreStringSliceOrder()) {
return nil, errors.Errorf("conflicting subnets: %v | %v", chosenSubnetNameOrIDs, subnetNameOrIDs)
}
}
chosenSubnets, err := t.subnetsResolver.ResolveViaNameOrIDSlice(ctx, chosenSubnetNameOrIDs,
networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication),
networking.WithSubnetsResolveLBScheme(scheme),
)
if err != nil {
return nil, err
}
return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil
}
stackTags := t.trackingProvider.StackTags(t.stack)
sdkLBs, err := t.elbv2TaggingManager.ListLoadBalancers(ctx, tracking.TagsAsTagFilter(stackTags))
if err != nil {
return nil, err
}
if len(sdkLBs) == 0 || (string(scheme) != awssdk.StringValue(sdkLBs[0].LoadBalancer.Scheme)) {
chosenSubnets, err := t.subnetsResolver.ResolveViaDiscovery(ctx,
networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication),
networking.WithSubnetsResolveLBScheme(scheme),
networking.WithSubnetsResolveAvailableIPAddressCount(minimalAvailableIPAddressCount),
networking.WithSubnetsClusterTagCheck(t.featureGates.Enabled(config.SubnetsClusterTagCheck)),
)
if err != nil {
return nil, errors.Wrap(err, "couldn't auto-discover subnets")
}
return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil
}
availabilityZones := sdkLBs[0].LoadBalancer.AvailabilityZones
subnetIDs := make([]string, 0, len(availabilityZones))
for _, availabilityZone := range availabilityZones {
subnetID := awssdk.StringValue(availabilityZone.SubnetId)
subnetIDs = append(subnetIDs, subnetID)
}
return buildLoadBalancerSubnetMappingsWithSubnetIDs(subnetIDs), nil
}
func (t *defaultModelBuildTask) buildLoadBalancerSecurityGroups(ctx context.Context, listenPortConfigByPort map[int64]listenPortConfig, ipAddressType elbv2model.IPAddressType) ([]core.StringToken, error) {
sgNameOrIDsViaAnnotation, err := t.buildFrontendSGNameOrIDsFromAnnotation(ctx)
if err != nil {
return nil, err
}
var lbSGTokens []core.StringToken
if len(sgNameOrIDsViaAnnotation) == 0 {
managedSG, err := t.buildManagedSecurityGroup(ctx, listenPortConfigByPort, ipAddressType)
if err != nil {
return nil, err
}
lbSGTokens = append(lbSGTokens, managedSG.GroupID())
if !t.enableBackendSG {
t.backendSGIDToken = managedSG.GroupID()
} else {
backendSGID, err := t.backendSGProvider.Get(ctx, networking.ResourceTypeIngress, k8s.ToSliceOfNamespacedNames(t.ingGroup.Members))
if err != nil {
return nil, err
}
t.backendSGIDToken = core.LiteralStringToken((backendSGID))
t.backendSGAllocated = true
lbSGTokens = append(lbSGTokens, t.backendSGIDToken)
}
t.logger.Info("Auto Create SG", "LB SGs", lbSGTokens, "backend SG", t.backendSGIDToken)
} else {
manageBackendSGRules, err := t.buildManageSecurityGroupRulesFlag(ctx)
if err != nil {
return nil, err
}
frontendSGIDs, err := t.sgResolver.ResolveViaNameOrID(ctx, sgNameOrIDsViaAnnotation)
if err != nil {
return nil, err
}
for _, sgID := range frontendSGIDs {
lbSGTokens = append(lbSGTokens, core.LiteralStringToken(sgID))
}
if manageBackendSGRules {
if !t.enableBackendSG {
return nil, errors.New("backendSG feature is required to manage worker node SG rules when frontendSG manually specified")
}
backendSGID, err := t.backendSGProvider.Get(ctx, networking.ResourceTypeIngress, k8s.ToSliceOfNamespacedNames(t.ingGroup.Members))
if err != nil {
return nil, err
}
t.backendSGIDToken = core.LiteralStringToken(backendSGID)
t.backendSGAllocated = true
lbSGTokens = append(lbSGTokens, t.backendSGIDToken)
}
t.logger.Info("SG configured via annotation", "LB SGs", lbSGTokens, "backend SG", t.backendSGIDToken)
}
return lbSGTokens, nil
}
func (t *defaultModelBuildTask) buildFrontendSGNameOrIDsFromAnnotation(ctx context.Context) ([]string, error) {
var explicitSGNameOrIDsList [][]string
for _, member := range t.ingGroup.Members {
var rawSGNameOrIDs []string
if exists := t.annotationParser.ParseStringSliceAnnotation(annotations.IngressSuffixSecurityGroups, &rawSGNameOrIDs, member.Ing.Annotations); !exists {
continue
}
explicitSGNameOrIDsList = append(explicitSGNameOrIDsList, rawSGNameOrIDs)
}
if len(explicitSGNameOrIDsList) == 0 {
return nil, nil
}
chosenSGNameOrIDs := explicitSGNameOrIDsList[0]
for _, sgNameOrIDs := range explicitSGNameOrIDsList[1:] {
if !cmp.Equal(chosenSGNameOrIDs, sgNameOrIDs) {
return nil, errors.Errorf("conflicting securityGroups: %v | %v", chosenSGNameOrIDs, sgNameOrIDs)
}
}
return chosenSGNameOrIDs, nil
}
func (t *defaultModelBuildTask) buildLoadBalancerCOIPv4Pool(_ context.Context) (*string, error) {
explicitCOIPv4Pools := sets.NewString()
for _, member := range t.ingGroup.Members {
rawCOIPv4Pool := ""
if exists := t.annotationParser.ParseStringAnnotation(annotations.IngressSuffixCustomerOwnedIPv4Pool, &rawCOIPv4Pool, member.Ing.Annotations); !exists {
continue
}
if len(rawCOIPv4Pool) == 0 {
return nil, errors.Errorf("cannot use empty value for %s annotation, ingress: %v",
annotations.IngressSuffixCustomerOwnedIPv4Pool, k8s.NamespacedName(member.Ing))
}
explicitCOIPv4Pools.Insert(rawCOIPv4Pool)
}
if len(explicitCOIPv4Pools) == 0 {
return nil, nil
}
if len(explicitCOIPv4Pools) > 1 {
return nil, errors.Errorf("conflicting CustomerOwnedIPv4Pool: %v", explicitCOIPv4Pools.List())
}
rawCOIPv4Pool, _ := explicitCOIPv4Pools.PopAny()
return &rawCOIPv4Pool, nil
}
func (t *defaultModelBuildTask) buildLoadBalancerAttributes(_ context.Context) ([]elbv2model.LoadBalancerAttribute, error) {
ingGroupAttributes, err := t.buildIngressGroupLoadBalancerAttributes(t.ingGroup.Members)
if err != nil {
return nil, err
}
attributes := make([]elbv2model.LoadBalancerAttribute, 0, len(ingGroupAttributes))
for attrKey, attrValue := range ingGroupAttributes {
attributes = append(attributes, elbv2model.LoadBalancerAttribute{
Key: attrKey,
Value: attrValue,
})
}
return attributes, nil
}
func (t *defaultModelBuildTask) buildLoadBalancerTags(_ context.Context) (map[string]string, error) {
ingGroupTags, err := t.buildIngressGroupResourceTags(t.ingGroup.Members)
if err != nil {
return nil, err
}
return algorithm.MergeStringMap(t.defaultTags, ingGroupTags), nil
}
func buildLoadBalancerSubnetMappingsWithSubnets(subnets []*ec2sdk.Subnet) []elbv2model.SubnetMapping {
subnetMappings := make([]elbv2model.SubnetMapping, 0, len(subnets))
for _, subnet := range subnets {
subnetMappings = append(subnetMappings, elbv2model.SubnetMapping{
SubnetID: awssdk.StringValue(subnet.SubnetId),
})
}
return subnetMappings
}
func buildLoadBalancerSubnetMappingsWithSubnetIDs(subnetIDs []string) []elbv2model.SubnetMapping {
subnetMappings := make([]elbv2model.SubnetMapping, 0, len(subnetIDs))
for _, subnetID := range subnetIDs {
subnetMappings = append(subnetMappings, elbv2model.SubnetMapping{
SubnetID: subnetID,
})
}
return subnetMappings
}