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
214213func (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-
253230func (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
278240func (v * targetGroupBindingValidator ) SetupWithManager (mgr ctrl.Manager ) {
0 commit comments