Skip to content

Commit 8fda219

Browse files
committed
implemented changes requested by reviewers
1 parent 47c55b9 commit 8fda219

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

webhooks/elbv2/targetgroupbinding_mutator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package elbv2
22

33
import (
44
"context"
5+
56
awssdk "github.com/aws/aws-sdk-go/aws"
67
elbv2sdk "github.com/aws/aws-sdk-go/service/elbv2"
78
"github.com/go-logr/logr"
@@ -38,7 +39,7 @@ func (m *targetGroupBindingMutator) Prototype(_ admission.Request) (runtime.Obje
3839
func (m *targetGroupBindingMutator) MutateCreate(ctx context.Context, obj runtime.Object) (runtime.Object, error) {
3940
tgb := obj.(*elbv2api.TargetGroupBinding)
4041
if tgb.Spec.TargetGroupARN == "" && tgb.Spec.TargetGroupName == "" {
41-
return nil, errors.Errorf("You must provide either TargetGroupARN or TargetGroupName")
42+
return nil, errors.Errorf("must provide either TargetGroupARN or TargetGroupName")
4243
}
4344
if err := m.getArnFromNameIfNeeded(ctx, tgb); err != nil {
4445
return nil, err

webhooks/elbv2/targetgroupbinding_validator.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package elbv2
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

78
awssdk "github.com/aws/aws-sdk-go/aws"
@@ -84,9 +85,20 @@ func (v *targetGroupBindingValidator) checkRequiredFields(ctx context.Context, t
8485
if tgb.Spec.TargetGroupName == "" {
8586
absentRequiredFields = append(absentRequiredFields, "either TargetGroupARN or TargetGroupName")
8687
} else if tgb.Spec.TargetGroupName != "" {
88+
/*
89+
The purpose of this code is to guarantee that the either the ARN of the TargetGroup exists
90+
or it's possible to infer the ARN by the name of the TargetGroup (since it's unique).
91+
92+
And even though the validator can't mutate, I added tgb.Spec.TargetGroupARN = *tgObj.TargetGroupArn
93+
to guarantee the object is in a consistent state though the rest of the process.
94+
95+
The whole code of aws-load-balancer-controller was written assuming there is an ARN.
96+
By changing the object here I guarantee as early as possible that that assumption is true.
97+
*/
98+
8799
tgObj, err := v.getTargetGroupsByNameFromAWS(ctx, tgb.Spec.TargetGroupName)
88100
if err != nil {
89-
return errors.Errorf("Can't locate TargetGroup with name %s", tgb.Spec.TargetGroupName)
101+
return fmt.Errorf("searching TargetGroup with name %s: %w", tgb.Spec.TargetGroupName, err)
90102
}
91103
tgb.Spec.TargetGroupARN = *tgObj.TargetGroupArn
92104
}
@@ -194,6 +206,7 @@ func (v *targetGroupBindingValidator) getTargetGroupFromAWS(ctx context.Context,
194206
return tgList[0], nil
195207
}
196208

209+
// getTargetGroupFromAWS returns the AWS target group corresponding to the tgName
197210
func (v *targetGroupBindingValidator) getTargetGroupsByNameFromAWS(ctx context.Context, tgName string) (*elbv2sdk.TargetGroup, error) {
198211
req := &elbv2sdk.DescribeTargetGroupsInput{
199212
Names: awssdk.StringSlice([]string{tgName}),

0 commit comments

Comments
 (0)