forked from kubernetes-sigs/aws-load-balancer-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_group_attributes_reconciler.go
95 lines (83 loc) · 3.44 KB
/
target_group_attributes_reconciler.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
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"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/algorithm"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/aws/services"
elbv2model "github.com/sonal-chauhan/aws-load-balancer-controller/pkg/model/elbv2"
)
// reconciler for TargetGroup attributes
type TargetGroupAttributesReconciler interface {
// Reconcile TargetGroup attributes
Reconcile(ctx context.Context, resTG *elbv2model.TargetGroup, sdkTG TargetGroupWithTags) error
}
// NewDefaultTargetGroupAttributesReconciler constructs new TargetGroupAttributesReconciler.
func NewDefaultTargetGroupAttributesReconciler(elbv2Client services.ELBV2, logger logr.Logger) *defaultTargetGroupAttributeReconciler {
return &defaultTargetGroupAttributeReconciler{
elbv2Client: elbv2Client,
logger: logger,
}
}
var _ TargetGroupAttributesReconciler = &defaultTargetGroupAttributeReconciler{}
// default implementation for TargetGroupAttributesReconciler
type defaultTargetGroupAttributeReconciler struct {
elbv2Client services.ELBV2
logger logr.Logger
}
func (r *defaultTargetGroupAttributeReconciler) Reconcile(ctx context.Context, resTG *elbv2model.TargetGroup, sdkTG TargetGroupWithTags) error {
desiredAttrs := r.getDesiredTargetGroupAttributes(ctx, resTG)
currentAttrs, err := r.getCurrentTargetGroupAttributes(ctx, sdkTG)
if err != nil {
return err
}
attributesToUpdate, _ := algorithm.DiffStringMap(desiredAttrs, currentAttrs)
if len(attributesToUpdate) > 0 {
req := &elbv2sdk.ModifyTargetGroupAttributesInput{
TargetGroupArn: sdkTG.TargetGroup.TargetGroupArn,
Attributes: nil,
}
for _, attrKey := range sets.StringKeySet(attributesToUpdate).List() {
req.Attributes = append(req.Attributes, &elbv2sdk.TargetGroupAttribute{
Key: awssdk.String(attrKey),
Value: awssdk.String(attributesToUpdate[attrKey]),
})
}
r.logger.Info("modifying targetGroup attributes",
"stackID", resTG.Stack().StackID(),
"resourceID", resTG.ID(),
"arn", awssdk.StringValue(sdkTG.TargetGroup.TargetGroupArn),
"change", attributesToUpdate)
if _, err := r.elbv2Client.ModifyTargetGroupAttributesWithContext(ctx, req); err != nil {
return err
}
r.logger.Info("modified targetGroup attributes",
"stackID", resTG.Stack().StackID(),
"resourceID", resTG.ID(),
"arn", awssdk.StringValue(sdkTG.TargetGroup.TargetGroupArn))
}
return nil
}
func (r *defaultTargetGroupAttributeReconciler) getDesiredTargetGroupAttributes(ctx context.Context, resTG *elbv2model.TargetGroup) map[string]string {
tgAttributes := make(map[string]string, len(resTG.Spec.TargetGroupAttributes))
for _, attr := range resTG.Spec.TargetGroupAttributes {
tgAttributes[attr.Key] = attr.Value
}
return tgAttributes
}
func (r *defaultTargetGroupAttributeReconciler) getCurrentTargetGroupAttributes(ctx context.Context, sdkTG TargetGroupWithTags) (map[string]string, error) {
req := &elbv2sdk.DescribeTargetGroupAttributesInput{
TargetGroupArn: sdkTG.TargetGroup.TargetGroupArn,
}
resp, err := r.elbv2Client.DescribeTargetGroupAttributesWithContext(ctx, req)
if err != nil {
return nil, err
}
tgAttributes := make(map[string]string, len(resp.Attributes))
for _, attr := range resp.Attributes {
tgAttributes[awssdk.StringValue(attr.Key)] = awssdk.StringValue(attr.Value)
}
return tgAttributes, nil
}