@@ -2,6 +2,7 @@ package elbv2
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"strings"
6
7
7
8
awssdk "github.com/aws/aws-sdk-go/aws"
@@ -84,9 +85,20 @@ func (v *targetGroupBindingValidator) checkRequiredFields(ctx context.Context, t
84
85
if tgb .Spec .TargetGroupName == "" {
85
86
absentRequiredFields = append (absentRequiredFields , "either TargetGroupARN or TargetGroupName" )
86
87
} 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
+
87
99
tgObj , err := v .getTargetGroupsByNameFromAWS (ctx , tgb .Spec .TargetGroupName )
88
100
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 )
90
102
}
91
103
tgb .Spec .TargetGroupARN = * tgObj .TargetGroupArn
92
104
}
@@ -194,6 +206,7 @@ func (v *targetGroupBindingValidator) getTargetGroupFromAWS(ctx context.Context,
194
206
return tgList [0 ], nil
195
207
}
196
208
209
+ // getTargetGroupFromAWS returns the AWS target group corresponding to the tgName
197
210
func (v * targetGroupBindingValidator ) getTargetGroupsByNameFromAWS (ctx context.Context , tgName string ) (* elbv2sdk.TargetGroup , error ) {
198
211
req := & elbv2sdk.DescribeTargetGroupsInput {
199
212
Names : awssdk .StringSlice ([]string {tgName }),
0 commit comments