Skip to content

Commit 3c75edb

Browse files
authored
add load balancer attributes support for IngressClassParams (#2190)
* add load balancer attributes support for IngressClassParams * format
1 parent 248971a commit 3c75edb

14 files changed

+707
-153
lines changed

apis/elbv2/v1beta1/ingressclassparams_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ type Tag struct {
5656
Value string `json:"value"`
5757
}
5858

59+
// Attributes defines custom attributes on resources.
60+
type Attribute struct {
61+
// The key of the attribute.
62+
Key string `json:"key"`
63+
64+
// The value of the attribute.
65+
Value string `json:"value"`
66+
}
67+
5968
// IngressClassParamsSpec defines the desired state of IngressClassParams
6069
type IngressClassParamsSpec struct {
6170
// NamespaceSelector restrict the namespaces of Ingresses that are allowed to specify the IngressClass with this IngressClassParams.
@@ -77,6 +86,10 @@ type IngressClassParamsSpec struct {
7786

7887
// Tags defines list of Tags on AWS resources provisioned for Ingresses that belong to IngressClass with this IngressClassParams.
7988
Tags []Tag `json:"tags,omitempty"`
89+
90+
// LoadBalancerAttributes define the custom attributes to LoadBalancers for all Ingress that that belong to IngressClass with this IngressClassParams.
91+
// +optional
92+
LoadBalancerAttributes []Attribute `json:"loadBalancerAttributes,omitempty"`
8093
}
8194

8295
// +kubebuilder:object:root=true

apis/elbv2/v1beta1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/elbv2.k8s.aws_ingressclassparams.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ spec:
6363
- ipv4
6464
- dualstack
6565
type: string
66+
loadBalancerAttributes:
67+
description: LoadBalancerAttributes define the custom attributes to LoadBalancers for all Ingress that that belong to IngressClass with this IngressClassParams.
68+
items:
69+
description: Attributes defines custom attributes on resources.
70+
properties:
71+
key:
72+
description: The key of the attribute.
73+
type: string
74+
value:
75+
description: The value of the attribute.
76+
type: string
77+
required:
78+
- key
79+
- value
80+
type: object
81+
type: array
6682
namespaceSelector:
6783
description: NamespaceSelector restrict the namespaces of Ingresses that are allowed to specify the IngressClass with this IngressClassParams. * if absent or present but empty, it selects all namespaces.
6884
properties:

pkg/deploy/elbv2/load_balancer_synthesizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *loadBalancerSynthesizer) Synthesize(ctx context.Context) error {
6363
for _, sdkLB := range unmatchedSDKLBs {
6464
if err := s.lbManager.Delete(ctx, sdkLB); err != nil {
6565
errMessage := err.Error()
66-
if strings.Contains(errMessage,"OperationNotPermitted") && strings.Contains(errMessage, "deletion protection") {
66+
if strings.Contains(errMessage, "OperationNotPermitted") && strings.Contains(errMessage, "deletion protection") {
6767
s.disableDeletionProtection(sdkLB.LoadBalancer)
6868
if err = s.lbManager.Delete(ctx, sdkLB); err != nil {
6969
return err

pkg/ingress/model_build_load_balancer.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
resourceIDLoadBalancer = "LoadBalancer"
27+
resourceIDLoadBalancer = "LoadBalancer"
2828
minimalAvailableIPAddressCount = int64(8)
2929
)
3030

@@ -297,21 +297,12 @@ func (t *defaultModelBuildTask) buildLoadBalancerCOIPv4Pool(_ context.Context) (
297297
}
298298

299299
func (t *defaultModelBuildTask) buildLoadBalancerAttributes(_ context.Context) ([]elbv2model.LoadBalancerAttribute, error) {
300-
mergedAttributes := make(map[string]string)
301-
for _, member := range t.ingGroup.Members {
302-
var rawAttributes map[string]string
303-
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixLoadBalancerAttributes, &rawAttributes, member.Ing.Annotations); err != nil {
304-
return nil, err
305-
}
306-
for attrKey, attrValue := range rawAttributes {
307-
if existingAttrValue, exists := mergedAttributes[attrKey]; exists && existingAttrValue != attrValue {
308-
return nil, errors.Errorf("conflicting loadBalancerAttribute %v: %v | %v", attrKey, existingAttrValue, attrValue)
309-
}
310-
mergedAttributes[attrKey] = attrValue
311-
}
300+
ingGroupAttributes, err := t.buildIngressGroupLoadBalancerAttributes(t.ingGroup.Members)
301+
if err != nil {
302+
return nil, err
312303
}
313-
attributes := make([]elbv2model.LoadBalancerAttribute, 0, len(mergedAttributes))
314-
for attrKey, attrValue := range mergedAttributes {
304+
attributes := make([]elbv2model.LoadBalancerAttribute, 0, len(ingGroupAttributes))
305+
for attrKey, attrValue := range ingGroupAttributes {
315306
attributes = append(attributes, elbv2model.LoadBalancerAttribute{
316307
Key: attrKey,
317308
Value: attrValue,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ingress
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
6+
"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
7+
)
8+
9+
// buildIngressGroupLoadBalancerAttributes builds the LB attributes for a group of Ingresses.
10+
func (t *defaultModelBuildTask) buildIngressGroupLoadBalancerAttributes(ingList []ClassifiedIngress) (map[string]string, error) {
11+
ingGroupAttributes := make(map[string]string)
12+
for _, ing := range ingList {
13+
ingAttributes, err := t.buildIngressLoadBalancerAttributes(ing)
14+
if err != nil {
15+
return nil, err
16+
}
17+
// check for conflict attribute values
18+
for attrKey, attrValue := range ingAttributes {
19+
existingAttrValue, exists := ingGroupAttributes[attrKey]
20+
if exists && existingAttrValue != attrValue {
21+
return nil, errors.Errorf("conflicting attributes %v: %v | %v", attrKey, existingAttrValue, attrValue)
22+
}
23+
ingGroupAttributes[attrKey] = attrValue
24+
}
25+
}
26+
if len(ingList) > 0 {
27+
ingClassAttributes, err := t.buildIngressClassLoadBalancerAttributes(ingList[0].IngClassConfig)
28+
if err != nil {
29+
return nil, err
30+
}
31+
return algorithm.MergeStringMap(ingClassAttributes, ingGroupAttributes), nil
32+
}
33+
return ingGroupAttributes, nil
34+
}
35+
36+
// buildIngressLoadBalancerAttributes builds the LB attributes used for a single Ingress
37+
// Note: the Attributes specified via IngressClass takes higher priority than the attributes specified via annotation on Ingress or Service.
38+
func (t *defaultModelBuildTask) buildIngressLoadBalancerAttributes(ing ClassifiedIngress) (map[string]string, error) {
39+
var annotationAttributes map[string]string
40+
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixLoadBalancerAttributes, &annotationAttributes, ing.Ing.Annotations); err != nil {
41+
return nil, err
42+
}
43+
return annotationAttributes, nil
44+
}
45+
46+
// buildIngressClassLoadBalancerAttributes builds the LB attributes for an IngressClass.
47+
func (t *defaultModelBuildTask) buildIngressClassLoadBalancerAttributes(ingClassConfig ClassConfiguration) (map[string]string, error) {
48+
if ingClassConfig.IngClassParams == nil || len(ingClassConfig.IngClassParams.Spec.LoadBalancerAttributes) == 0 {
49+
return nil, nil
50+
}
51+
ingClassAttributes := make(map[string]string, len(ingClassConfig.IngClassParams.Spec.LoadBalancerAttributes))
52+
for _, attr := range ingClassConfig.IngClassParams.Spec.LoadBalancerAttributes {
53+
ingClassAttributes[attr.Key] = attr.Value
54+
}
55+
return ingClassAttributes, nil
56+
}

0 commit comments

Comments
 (0)