Skip to content

Commit 577e1b6

Browse files
committed
refactor target group lookup in webhooks to reduce code duplication
1 parent ddc77cd commit 577e1b6

File tree

3 files changed

+52
-89
lines changed

3 files changed

+52
-89
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package elbv2
2+
3+
import (
4+
"context"
5+
elbv2sdk "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
6+
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
7+
"github.com/pkg/errors"
8+
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
9+
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
10+
)
11+
12+
// getTargetGroupFromAWS returns the AWS target group corresponding to the arn
13+
func getTargetGroupFromAWS(ctx context.Context, elbv2Client services.ELBV2, tgb *elbv2api.TargetGroupBinding) (*elbv2types.TargetGroup, error) {
14+
tgARN := tgb.Spec.TargetGroupARN
15+
req := &elbv2sdk.DescribeTargetGroupsInput{
16+
TargetGroupArns: []string{tgARN},
17+
}
18+
return getTargetGroupHelper(ctx, elbv2Client, tgb, tgARN, req)
19+
}
20+
21+
// getTargetGroupsByNameFromAWS returns the AWS target group corresponding to the name
22+
func getTargetGroupsByNameFromAWS(ctx context.Context, elbv2Client services.ELBV2, tgb *elbv2api.TargetGroupBinding) (*elbv2types.TargetGroup, error) {
23+
req := &elbv2sdk.DescribeTargetGroupsInput{
24+
Names: []string{tgb.Spec.TargetGroupName},
25+
}
26+
27+
return getTargetGroupHelper(ctx, elbv2Client, tgb, tgb.Spec.TargetGroupName, req)
28+
}
29+
30+
func getTargetGroupHelper(ctx context.Context, elbv2Client services.ELBV2, tgb *elbv2api.TargetGroupBinding, tgIdentifier string, req *elbv2sdk.DescribeTargetGroupsInput) (*elbv2types.TargetGroup, error) {
31+
clientToUse, err := elbv2Client.AssumeRole(ctx, tgb.Spec.IamRoleArnToAssume, tgb.Spec.AssumeRoleExternalId)
32+
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
tgList, err := clientToUse.DescribeTargetGroupsAsList(ctx, req)
38+
if err != nil {
39+
return nil, err
40+
}
41+
if len(tgList) != 1 {
42+
return nil, errors.Errorf("expecting a single targetGroup with query [%s] but got %v", tgIdentifier, len(tgList))
43+
}
44+
return &tgList[0], nil
45+
}

webhooks/elbv2/targetgroupbinding_mutator.go

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
77

88
awssdk "github.com/aws/aws-sdk-go-v2/aws"
9-
elbv2sdk "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
10-
119
"github.com/go-logr/logr"
1210
"github.com/pkg/errors"
1311
"k8s.io/apimachinery/pkg/runtime"
@@ -61,7 +59,7 @@ func (m *targetGroupBindingMutator) MutateCreate(ctx context.Context, obj runtim
6159

6260
func (m *targetGroupBindingMutator) getArnFromNameIfNeeded(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
6361
if tgb.Spec.TargetGroupARN == "" && tgb.Spec.TargetGroupName != "" {
64-
tgObj, err := m.getTargetGroupsByNameFromAWS(ctx, tgb)
62+
tgObj, err := getTargetGroupsByNameFromAWS(ctx, m.elbv2Client, tgb)
6563
if err != nil {
6664
return err
6765
}
@@ -121,7 +119,7 @@ func (m *targetGroupBindingMutator) defaultingVpcID(ctx context.Context, tgb *el
121119
}
122120

123121
func (m *targetGroupBindingMutator) obtainSDKTargetTypeFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (string, error) {
124-
targetGroup, err := m.getTargetGroupFromAWS(ctx, tgb)
122+
targetGroup, err := getTargetGroupFromAWS(ctx, m.elbv2Client, tgb)
125123
if err != nil {
126124
return "", err
127125
}
@@ -130,7 +128,7 @@ func (m *targetGroupBindingMutator) obtainSDKTargetTypeFromAWS(ctx context.Conte
130128

131129
// getTargetGroupIPAddressTypeFromAWS returns the target group IP address type of AWS target group
132130
func (m *targetGroupBindingMutator) getTargetGroupIPAddressTypeFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (elbv2api.TargetGroupIPAddressType, error) {
133-
targetGroup, err := m.getTargetGroupFromAWS(ctx, tgb)
131+
targetGroup, err := getTargetGroupFromAWS(ctx, m.elbv2Client, tgb)
134132
if err != nil {
135133
return "", err
136134
}
@@ -146,50 +144,8 @@ func (m *targetGroupBindingMutator) getTargetGroupIPAddressTypeFromAWS(ctx conte
146144
return ipAddressType, nil
147145
}
148146

149-
func (m *targetGroupBindingMutator) getTargetGroupFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (*elbv2types.TargetGroup, error) {
150-
tgARN := tgb.Spec.TargetGroupARN
151-
req := &elbv2sdk.DescribeTargetGroupsInput{
152-
TargetGroupArns: []string{tgARN},
153-
}
154-
155-
clientToUse, err := m.elbv2Client.AssumeRole(ctx, tgb.Spec.IamRoleArnToAssume, tgb.Spec.AssumeRoleExternalId)
156-
157-
if err != nil {
158-
return nil, err
159-
}
160-
161-
tgList, err := clientToUse.DescribeTargetGroupsAsList(ctx, req)
162-
if err != nil {
163-
return nil, err
164-
}
165-
if len(tgList) != 1 {
166-
return nil, errors.Errorf("expecting a single targetGroup but got %v", len(tgList))
167-
}
168-
return &tgList[0], nil
169-
}
170-
171-
func (m *targetGroupBindingMutator) getTargetGroupsByNameFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (*elbv2types.TargetGroup, error) {
172-
req := &elbv2sdk.DescribeTargetGroupsInput{
173-
Names: []string{tgb.Spec.TargetGroupName},
174-
}
175-
clientToUse, err := m.elbv2Client.AssumeRole(ctx, tgb.Spec.IamRoleArnToAssume, tgb.Spec.AssumeRoleExternalId)
176-
177-
if err != nil {
178-
return nil, err
179-
}
180-
181-
tgList, err := clientToUse.DescribeTargetGroupsAsList(ctx, req)
182-
if err != nil {
183-
return nil, err
184-
}
185-
if len(tgList) != 1 {
186-
return nil, errors.Errorf("expecting a single targetGroup with name [%s] but got %v", tgb.Spec.TargetGroupName, len(tgList))
187-
}
188-
return &tgList[0], nil
189-
}
190-
191147
func (m *targetGroupBindingMutator) getVpcIDFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (string, error) {
192-
targetGroup, err := m.getTargetGroupFromAWS(ctx, tgb)
148+
targetGroup, err := getTargetGroupFromAWS(ctx, m.elbv2Client, tgb)
193149
if err != nil {
194150
return "", err
195151
}

webhooks/elbv2/targetgroupbinding_validator.go

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
1010

1111
awssdk "github.com/aws/aws-sdk-go-v2/aws"
12-
elbv2sdk "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
1312
"github.com/go-logr/logr"
1413
"github.com/pkg/errors"
1514
"k8s.io/apimachinery/pkg/runtime"
@@ -110,7 +109,7 @@ func (v *targetGroupBindingValidator) checkRequiredFields(ctx context.Context, t
110109
By changing the object here I guarantee as early as possible that that assumption is true.
111110
*/
112111

113-
tgObj, err := v.getTargetGroupsByNameFromAWS(ctx, tgb.Spec.TargetGroupName)
112+
tgObj, err := getTargetGroupsByNameFromAWS(ctx, v.elbv2Client, tgb)
114113
if err != nil {
115114
return fmt.Errorf("searching TargetGroup with name %s: %w", tgb.Spec.TargetGroupName, err)
116115
}
@@ -212,7 +211,7 @@ func (v *targetGroupBindingValidator) checkTargetGroupVpcID(ctx context.Context,
212211

213212
// getTargetGroupIPAddressTypeFromAWS returns the target group IP address type of AWS target group
214213
func (v *targetGroupBindingValidator) getTargetGroupIPAddressTypeFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (elbv2api.TargetGroupIPAddressType, error) {
215-
targetGroup, err := v.getTargetGroupFromAWS(ctx, tgb)
214+
targetGroup, err := getTargetGroupFromAWS(ctx, v.elbv2Client, tgb)
216215
if err != nil {
217216
return "", err
218217
}
@@ -228,51 +227,14 @@ func (v *targetGroupBindingValidator) getTargetGroupIPAddressTypeFromAWS(ctx con
228227
return ipAddressType, nil
229228
}
230229

231-
// getTargetGroupFromAWS returns the AWS target group corresponding to the ARN
232-
func (v *targetGroupBindingValidator) getTargetGroupFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (*elbv2types.TargetGroup, error) {
233-
tgARN := tgb.Spec.TargetGroupARN
234-
req := &elbv2sdk.DescribeTargetGroupsInput{
235-
TargetGroupArns: []string{tgARN},
236-
}
237-
238-
clientToUse, err := v.elbv2Client.AssumeRole(ctx, tgb.Spec.IamRoleArnToAssume, tgb.Spec.AssumeRoleExternalId)
239-
if err != nil {
240-
return nil, err
241-
}
242-
243-
tgList, err := clientToUse.DescribeTargetGroupsAsList(ctx, req)
244-
if err != nil {
245-
return nil, err
246-
}
247-
if len(tgList) != 1 {
248-
return nil, errors.Errorf("expecting a single targetGroup but got %v", len(tgList))
249-
}
250-
return &tgList[0], nil
251-
}
252-
253230
func (v *targetGroupBindingValidator) getVpcIDFromAWS(ctx context.Context, tgb *elbv2api.TargetGroupBinding) (string, error) {
254-
targetGroup, err := v.getTargetGroupFromAWS(ctx, tgb)
231+
targetGroup, err := getTargetGroupFromAWS(ctx, v.elbv2Client, tgb)
255232
if err != nil {
256233
return "", err
257234
}
258235
return awssdk.ToString(targetGroup.VpcId), nil
259236
}
260237

261-
// getTargetGroupFromAWS returns the AWS target group corresponding to the tgName
262-
func (v *targetGroupBindingValidator) getTargetGroupsByNameFromAWS(ctx context.Context, tgName string) (*elbv2types.TargetGroup, error) {
263-
req := &elbv2sdk.DescribeTargetGroupsInput{
264-
Names: []string{tgName},
265-
}
266-
tgList, err := v.elbv2Client.DescribeTargetGroupsAsList(ctx, req)
267-
if err != nil {
268-
return nil, err
269-
}
270-
if len(tgList) != 1 {
271-
return nil, errors.Errorf("expecting a single targetGroup with name [%s] but got %v", tgName, len(tgList))
272-
}
273-
return &tgList[0], nil
274-
}
275-
276238
// +kubebuilder:webhook:path=/validate-elbv2-k8s-aws-v1beta1-targetgroupbinding,mutating=false,failurePolicy=fail,groups=elbv2.k8s.aws,resources=targetgroupbindings,verbs=create;update,versions=v1beta1,name=vtargetgroupbinding.elbv2.k8s.aws,sideEffects=None,webhookVersions=v1,admissionReviewVersions=v1beta1
277239

278240
func (v *targetGroupBindingValidator) SetupWithManager(mgr ctrl.Manager) {

0 commit comments

Comments
 (0)