/
hooks.go
167 lines (149 loc) · 5.16 KB
/
hooks.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package load_balancer
import (
"context"
svcapitypes "github.com/aws-controllers-k8s/elbv2-controller/apis/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
"github.com/aws/aws-sdk-go/aws"
svcsdk "github.com/aws/aws-sdk-go/service/elbv2"
)
// setResourceAdditionalFields will describe the fields that are not return by the
// describeLoadBalancer API calls.
func (rm *resourceManager) setResourceAdditionalFields(
ctx context.Context,
ko *svcapitypes.LoadBalancer,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.setResourceAdditionalFields")
defer exit(err)
ko.Spec.Attributes, err = rm.getLoadBalancerAttributes(ctx, ko)
if err != nil {
return err
}
return nil
}
// getLoadBalancerAttributes returns the attributes of the load balancer.
func (rm *resourceManager) getLoadBalancerAttributes(
ctx context.Context,
ko *svcapitypes.LoadBalancer,
) ([]*svcapitypes.LoadBalancerAttribute, error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.getLoadBalancerAttributes")
var err error
defer func() { exit(err) }()
attributes := []*svcapitypes.LoadBalancerAttribute{}
var resp *svcsdk.DescribeLoadBalancerAttributesOutput
resp, err = rm.sdkapi.DescribeLoadBalancerAttributesWithContext(ctx, &svcsdk.DescribeLoadBalancerAttributesInput{
LoadBalancerArn: (*string)(ko.Status.ACKResourceMetadata.ARN),
})
rm.metrics.RecordAPICall("READ_ONE", "DescribeLoadBalancerAttributes", err)
if err != nil {
return nil, err
}
// Convert the attributes SDK type to the k8s API type
for _, attr := range resp.Attributes {
attribute := &svcapitypes.LoadBalancerAttribute{
Key: attr.Key,
Value: attr.Value,
}
attributes = append(attributes, attribute)
}
return attributes, nil
}
// attributesHaveChanged returns true if one of desired attributes (a) have
// drifted from the latest attributes (b).
func attributesHaveChanged(a, b []*svcapitypes.LoadBalancerAttribute) bool {
for _, attrA := range a {
if !containsExactAttribute(b, attrA) {
return false
}
}
return true
}
// containsExactAttribute returns true if the key is in the attributes slice
// and has the same value.
func containsExactAttribute(attributes []*svcapitypes.LoadBalancerAttribute, targetAttribute *svcapitypes.LoadBalancerAttribute) bool {
for _, attribute := range attributes {
if *attribute.Key == *targetAttribute.Key && *attribute.Value == *targetAttribute.Value {
return true
}
}
return false
}
// customPreCompare is a custom pre compare function that compares the attributes
// of the load balancer.
func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if !attributesHaveChanged(a.ko.Spec.Attributes, b.ko.Spec.Attributes) {
delta.Add("Spec.Attributes", a.ko.Spec.Attributes, b.ko.Spec.Attributes)
}
}
// customUpdateLoadBalancer is a custom update function that updates the attributes/tags
// of the load balancer.
func (rm *resourceManager) customUpdateLoadBalancer(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (*resource, error) {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateLoadBalancer")
defer func() { exit(err) }()
if delta.DifferentAt("Spec.Attributes") && len(desired.ko.Spec.Attributes) > 0 {
if err := rm.updateLoadBalancerAttributes(ctx, desired, latest); err != nil {
return nil, err
}
}
// Leaving room for tag updates...
// if delta.DifferentAt("Spec.Tags") {...}
return desired, nil
}
// updateLoadBalancerAttributes updates the attributes of the load balancer.
func (rm *resourceManager) updateLoadBalancerAttributes(
ctx context.Context,
desired *resource,
latest *resource,
) error {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.updateLoadBalancerAttributes")
var err error
defer func() { exit(err) }()
sdkAttributes := []*svcsdk.LoadBalancerAttribute{}
for _, attr := range desired.ko.Spec.Attributes {
// Only set non-empty attributes
if attr.Key == nil || attr.Value == nil || *attr.Key == "" || *attr.Value == "" {
continue
}
sdkAttribute := &svcsdk.LoadBalancerAttribute{
Key: aws.String(*attr.Key),
Value: aws.String(*attr.Value),
}
sdkAttributes = append(sdkAttributes, sdkAttribute)
}
input := &svcsdk.ModifyLoadBalancerAttributesInput{
LoadBalancerArn: (*string)(desired.ko.Status.ACKResourceMetadata.ARN),
Attributes: sdkAttributes,
}
_, err = rm.sdkapi.ModifyLoadBalancerAttributesWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "ModifyLoadBalancerAttributes", err)
if err != nil {
return err
}
return nil
}