Skip to content

add ELBV2 tagManager and LoadBalancerAttributeReconciler #1387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/algorithm/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,25 @@ func MergeStringMap(maps ...map[string]string) map[string]string {
}
return ret
}

// DiffStringMap will diff desired with current.
// Returns the k/v that need to be add/updated and k/v that need to be deleted to make current match desired.
func DiffStringMap(desired map[string]string, current map[string]string) (map[string]string, map[string]string) {
modify := make(map[string]string)
remove := make(map[string]string)

for key, desiredVal := range desired {
currentVal, ok := current[key]
if !ok || currentVal != desiredVal {
modify[key] = desiredVal
}
}

for key, currentVal := range current {
if _, ok := desired[key]; !ok {
remove[key] = currentVal
}
}

return modify, remove
}
96 changes: 96 additions & 0 deletions pkg/algorithm/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,99 @@ func TestMergeStringMap(t *testing.T) {
})
}
}

func TestDiffStringMap(t *testing.T) {
type args struct {
desired map[string]string
current map[string]string
}
tests := []struct {
name string
args args
wantUpdate map[string]string
wantRemove map[string]string
}{
{
name: "standard case",
args: args{
desired: map[string]string{
"a": "a1",
"b": "b1",
"c": "c1",
},
current: map[string]string{
"a": "a1",
"b": "b2",
"d": "d1",
},
},
wantUpdate: map[string]string{
"b": "b1",
"c": "c1",
},
wantRemove: map[string]string{
"d": "d1",
},
},
{
name: "only need to update",
args: args{
desired: map[string]string{
"a": "a1",
"b": "b1",
"c": "c1",
},
current: map[string]string{
"a": "a1",
"b": "b1",
},
},
wantUpdate: map[string]string{
"c": "c1",
},
wantRemove: map[string]string{},
},
{
name: "only need to remove",
args: args{
desired: map[string]string{
"a": "a1",
"b": "b1",
},
current: map[string]string{
"a": "a1",
"b": "b1",
"c": "c1",
},
},
wantUpdate: map[string]string{},
wantRemove: map[string]string{
"c": "c1",
},
},
{
name: "both map are equal",
args: args{
desired: map[string]string{
"a": "a1",
"b": "b1",
"c": "c1",
},
current: map[string]string{
"a": "a1",
"b": "b1",
"c": "c1",
},
},
wantUpdate: map[string]string{},
wantRemove: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUpdate, gotRemove := DiffStringMap(tt.args.desired, tt.args.current)
assert.Equal(t, tt.wantUpdate, gotUpdate)
assert.Equal(t, tt.wantRemove, gotRemove)
})
}
}
84 changes: 84 additions & 0 deletions pkg/deploy/elbv2/load_balancer_attributes_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package elbv2

import (
"context"
awssdk "github.com/aws/aws-sdk-go/aws"
elbv2sdk "github.com/aws/aws-sdk-go/service/elbv2"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/aws-alb-ingress-controller/pkg/algorithm"
"sigs.k8s.io/aws-alb-ingress-controller/pkg/aws/services"
elbv2model "sigs.k8s.io/aws-alb-ingress-controller/pkg/model/elbv2"
)

// reconciler for LoadBalancer attributes
type LoadBalancerAttributeReconciler interface {
// Reconcile loadBalancer attributes
Reconcile(ctx context.Context, sdkLB LoadBalancerWithTags, resLB *elbv2model.LoadBalancer) error
}

var _ LoadBalancerAttributeReconciler = &defaultLoadBalancerAttributeReconciler{}

// default implementation for LoadBalancerAttributeReconciler
type defaultLoadBalancerAttributeReconciler struct {
elbv2Client services.ELBV2
logger logr.Logger
}

func (r *defaultLoadBalancerAttributeReconciler) Reconcile(ctx context.Context, sdkLB LoadBalancerWithTags, resLB *elbv2model.LoadBalancer) error {
desiredAttrs := r.getDesiredLoadBalancerAttributes(ctx, resLB)
currentAttrs, err := r.getCurrentLoadBalancerAttributes(ctx, sdkLB)
if err != nil {
return err
}

attributesToUpdate, _ := algorithm.DiffStringMap(desiredAttrs, currentAttrs)
if len(attributesToUpdate) > 0 {
req := &elbv2sdk.ModifyLoadBalancerAttributesInput{
LoadBalancerArn: sdkLB.LoadBalancer.LoadBalancerArn,
Attributes: nil,
}
for _, attrKey := range sets.StringKeySet(attributesToUpdate).List() {
req.Attributes = append(req.Attributes, &elbv2sdk.LoadBalancerAttribute{
Key: awssdk.String(attrKey),
Value: awssdk.String(attributesToUpdate[attrKey]),
})
}

r.logger.Info("modifying loadBalancer attributes",
"resourceID", resLB.ID(),
"arn", awssdk.StringValue(sdkLB.LoadBalancer.LoadBalancerArn),
"change", attributesToUpdate)
if _, err := r.elbv2Client.ModifyLoadBalancerAttributesWithContext(ctx, req); err != nil {
return err
}
r.logger.Info("modified loadBalancer attributes",
"resourceID", resLB.ID(),
"arn", awssdk.StringValue(sdkLB.LoadBalancer.LoadBalancerArn))
}
return nil
}

func (r *defaultLoadBalancerAttributeReconciler) getDesiredLoadBalancerAttributes(ctx context.Context, resLB *elbv2model.LoadBalancer) map[string]string {
lbAttributes := make(map[string]string, len(resLB.Spec.LoadBalancerAttributes))
for _, attr := range resLB.Spec.LoadBalancerAttributes {
lbAttributes[attr.Key] = attr.Value
}
return lbAttributes
}

func (r *defaultLoadBalancerAttributeReconciler) getCurrentLoadBalancerAttributes(ctx context.Context, sdkLB LoadBalancerWithTags) (map[string]string, error) {
req := &elbv2sdk.DescribeLoadBalancerAttributesInput{
LoadBalancerArn: sdkLB.LoadBalancer.LoadBalancerArn,
}
resp, err := r.elbv2Client.DescribeLoadBalancerAttributesWithContext(ctx, req)
if err != nil {
return nil, err
}

lbAttributes := make(map[string]string, len(resp.Attributes))
for _, attr := range resp.Attributes {
lbAttributes[awssdk.StringValue(attr.Key)] = awssdk.StringValue(attr.Value)
}
return lbAttributes, nil
}
Loading