Skip to content

Commit

Permalink
Support listener attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
wweiwei-li committed Sep 24, 2024
1 parent 356c904 commit a8a4a41
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 21 deletions.
20 changes: 20 additions & 0 deletions apis/elbv2/v1beta1/ingressclassparams_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ type Attribute struct {
Value string `json:"value"`
}

type ListenerProtocol string

const (
ListenerProtocolHTTP ListenerProtocol = "HTTP"
ListenerProtocolHTTPS ListenerProtocol = "HTTPS"
)

type Listener struct {
// The protocol of the listener
Protocol ListenerProtocol `json:"protocol,omitempty"`
// The port of the listener
Port int32 `json:"port,omitempty"`
// The attributes of the listener
ListenerAttributes []Attribute `json:"listenerAttributes,omitempty"`
}

// IngressClassParamsSpec defines the desired state of IngressClassParams
type IngressClassParamsSpec struct {
// CertificateArn specifies the ARN of the certificates for all Ingresses that belong to IngressClass with this IngressClassParams.
Expand Down Expand Up @@ -126,6 +142,10 @@ type IngressClassParamsSpec struct {
// LoadBalancerAttributes define the custom attributes to LoadBalancers for all Ingress that that belong to IngressClass with this IngressClassParams.
// +optional
LoadBalancerAttributes []Attribute `json:"loadBalancerAttributes,omitempty"`

// Listeners define a list of listeners with their protocol, port and attributes.
// +optional
Listeners []Listener `json:"listeners,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
27 changes: 27 additions & 0 deletions apis/elbv2/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions config/crd/bases/elbv2.k8s.aws_ingressclassparams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,27 @@ spec:
- value
type: object
type: array
listeners:
type: array
items:
type: object
properties:
protocol:
enum:
- HTTPS
- HTTP
type: string
port:
type: integer
listenerAttributes:
type: array
items:
type: object
properties:
key:
type: string
value:
type: string
type: object
type: object
served: true
Expand Down
6 changes: 4 additions & 2 deletions docs/install/iam_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"elasticloadbalancing:DescribeTargetGroupAttributes",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:DescribeTags",
"elasticloadbalancing:DescribeTrustStores"
"elasticloadbalancing:DescribeTrustStores",
"elasticloadbalancing:DescribeListenerAttributes"
],
"Resource": "*"
},
Expand Down Expand Up @@ -188,7 +189,8 @@
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:ModifyTargetGroupAttributes",
"elasticloadbalancing:DeleteTargetGroup"
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:ModifyListenerAttributes"
],
"Resource": "*",
"Condition": {
Expand Down
6 changes: 4 additions & 2 deletions docs/install/iam_policy_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"elasticloadbalancing:DescribeTargetGroupAttributes",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:DescribeTags",
"elasticloadbalancing:DescribeTrustStores"
"elasticloadbalancing:DescribeTrustStores",
"elasticloadbalancing:DescribeListenerAttributes"
],
"Resource": "*"
},
Expand Down Expand Up @@ -210,7 +211,8 @@
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:ModifyTargetGroupAttributes",
"elasticloadbalancing:DeleteTargetGroup"
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:ModifyListenerAttributes"
],
"Resource": "*",
"Condition": {
Expand Down
6 changes: 4 additions & 2 deletions docs/install/iam_policy_us-gov.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"elasticloadbalancing:DescribeTargetGroupAttributes",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:DescribeTags",
"elasticloadbalancing:DescribeTrustStores"
"elasticloadbalancing:DescribeTrustStores",
"elasticloadbalancing:DescribeListenerAttributes"
],
"Resource": "*"
},
Expand Down Expand Up @@ -210,7 +211,8 @@
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:ModifyTargetGroupAttributes",
"elasticloadbalancing:DeleteTargetGroup"
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:ModifyListenerAttributes"
],
"Resource": "*",
"Condition": {
Expand Down
21 changes: 21 additions & 0 deletions helm/aws-load-balancer-controller/crds/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ spec:
- value
type: object
type: array
listeners:
type: array
items:
type: object
properties:
protocol:
enum:
- HTTPS
- HTTP
type: string
port:
type: integer
listenerAttributes:
type: array
items:
type: object
properties:
key:
type: string
value:
type: string
type: object
type: object
served: true
Expand Down
2 changes: 2 additions & 0 deletions pkg/annotations/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
IngressSuffixManageSecurityGroupRules = "manage-backend-security-group-rules"
IngressSuffixMutualAuthentication = "mutual-authentication"
IngressSuffixSecurityGroupPrefixLists = "security-group-prefix-lists"
IngressSuffixlsAttsAnnotationPrefix = "listener-attributes"

// NLB annotation suffixes
// prefixes service.beta.kubernetes.io, service.kubernetes.io
Expand Down Expand Up @@ -88,4 +89,5 @@ const (
SvcLBSuffixManageSGRules = "aws-load-balancer-manage-backend-security-group-rules"
SvcLBSuffixEnforceSGInboundRulesOnPrivateLinkTraffic = "aws-load-balancer-inbound-sg-rules-on-private-link-traffic"
SvcLBSuffixSecurityGroupPrefixLists = "aws-load-balancer-security-group-prefix-lists"
SvcLBSuffixlsAttsAnnotationPrefix = "aws-load-balancer-listener-attributes"
)
13 changes: 12 additions & 1 deletion pkg/aws/services/elbv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package services

import (
"context"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/endpoints"
"time"
)

type ELBV2 interface {
Expand Down Expand Up @@ -57,6 +58,8 @@ type ELBV2 interface {
DescribeTrustStoresWithContext(ctx context.Context, input *elasticloadbalancingv2.DescribeTrustStoresInput) (*elasticloadbalancingv2.DescribeTrustStoresOutput, error)
RemoveListenerCertificatesWithContext(ctx context.Context, input *elasticloadbalancingv2.RemoveListenerCertificatesInput) (*elasticloadbalancingv2.RemoveListenerCertificatesOutput, error)
AddListenerCertificatesWithContext(ctx context.Context, input *elasticloadbalancingv2.AddListenerCertificatesInput) (*elasticloadbalancingv2.AddListenerCertificatesOutput, error)
DescribeListenerAttributesWithContext(ctx context.Context, input *elasticloadbalancingv2.DescribeListenerAttributesInput) (*elasticloadbalancingv2.DescribeListenerAttributesOutput, error)
ModifyListenerAttributesWithContext(ctx context.Context, input *elasticloadbalancingv2.ModifyListenerAttributesInput) (*elasticloadbalancingv2.ModifyListenerAttributesOutput, error)
}

func NewELBV2(cfg aws.Config, endpointsResolver *endpoints.Resolver) ELBV2 {
Expand Down Expand Up @@ -268,3 +271,11 @@ func (c *elbv2Client) DescribeRulesAsList(ctx context.Context, input *elasticloa
}
return result, nil
}

func (c *elbv2Client) DescribeListenerAttributesWithContext(ctx context.Context, input *elasticloadbalancingv2.DescribeListenerAttributesInput) (*elasticloadbalancingv2.DescribeListenerAttributesOutput, error) {
return c.elbv2Client.DescribeListenerAttributes(ctx, input)
}

func (c *elbv2Client) ModifyListenerAttributesWithContext(ctx context.Context, input *elasticloadbalancingv2.ModifyListenerAttributesInput) (*elasticloadbalancingv2.ModifyListenerAttributesOutput, error) {
return c.elbv2Client.ModifyListenerAttributes(ctx, input)
}
30 changes: 30 additions & 0 deletions pkg/aws/services/elbv2_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions pkg/deploy/elbv2/listener_attributes_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package elbv2

import (
"context"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
elbv2sdk "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
)

// Reconciler for Listener attributes
type ListenerAttributesReconciler interface {
Reconcile(ctx context.Context, resLS *elbv2model.Listener, sdkLS ListenerWithTags) error
}

// NewListenerAttributesReconciler constructs new ListenerAttributesReconciler.
func NewDefaultListenerAttributesReconciler(elbv2Client services.ELBV2, logger logr.Logger) *defaultListenerAttributesReconciler {
return &defaultListenerAttributesReconciler{
elbv2Client: elbv2Client,
logger: logger,
}
}

var _ ListenerAttributesReconciler = &defaultListenerAttributesReconciler{}

// default implementation for ListenerAttributeReconciler
type defaultListenerAttributesReconciler struct {
elbv2Client services.ELBV2
logger logr.Logger
}

func (r *defaultListenerAttributesReconciler) Reconcile(ctx context.Context, resLS *elbv2model.Listener, sdkLS ListenerWithTags) error {
desiredAttrs := r.getDesiredListenerAttributes(ctx, resLS)
currentAttrs, err := r.getCurrentListenerAttributes(ctx, sdkLS)
if err != nil {
return err
}
attributesToUpdate, _ := algorithm.DiffStringMap(desiredAttrs, currentAttrs)
if len(attributesToUpdate) > 0 {
req := &elbv2sdk.ModifyListenerAttributesInput{
ListenerArn: sdkLS.Listener.ListenerArn,
Attributes: nil,
}
for _, attrKey := range sets.StringKeySet(attributesToUpdate).List() {
req.Attributes = append(req.Attributes, elbv2types.ListenerAttribute{
Key: awssdk.String(attrKey),
Value: awssdk.String(attributesToUpdate[attrKey]),
})
}
r.logger.Info("modifying listener attributes",
"stackID", resLS.Stack().StackID(),
"resourceID", resLS.ID(),
"arn", awssdk.ToString(sdkLS.Listener.ListenerArn),
"change", attributesToUpdate)
if _, err := r.elbv2Client.ModifyListenerAttributesWithContext(ctx, req); err != nil {
return err
}
r.logger.Info("modified listener attribute",
"stackID", resLS.Stack().StackID(),
"resourceID", resLS.ID(),
"arn", awssdk.ToString(sdkLS.Listener.ListenerArn))

}
return nil

}

func (r *defaultListenerAttributesReconciler) getDesiredListenerAttributes(ctx context.Context, resLS *elbv2model.Listener) map[string]string {
lsAttributes := make(map[string]string, len(resLS.Spec.ListenerAttributes))
for _, attr := range resLS.Spec.ListenerAttributes {
lsAttributes[attr.Key] = attr.Value
}
return lsAttributes
}

func (r *defaultListenerAttributesReconciler) getCurrentListenerAttributes(ctx context.Context, sdkLS ListenerWithTags) (map[string]string, error) {
req := &elbv2sdk.DescribeListenerAttributesInput{
ListenerArn: sdkLS.Listener.ListenerArn,
}
resp, err := r.elbv2Client.DescribeListenerAttributesWithContext(ctx, req)
if err != nil {
return nil, err
}
lsAttributes := make(map[string]string, len(resp.Attributes))
for _, attr := range resp.Attributes {
lsAttributes[awssdk.ToString(attr.Key)] = awssdk.ToString(attr.Value)
}
return lsAttributes, nil
}
Loading

0 comments on commit a8a4a41

Please sign in to comment.