Skip to content

Commit abdb293

Browse files
committed
Add vpcid missing test case
- update docs
1 parent ba1e49c commit abdb293

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

docs/guide/targetgroupbinding/targetgroupbinding.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ TargetGroupBinding CR supports TargetGroups of either `instance` or `ip` TargetT
1717
If TargetType is not explicitly specified, a mutating webhook will automatically call AWS API to find the TargetType for your TargetGroup and set it to correct value.
1818

1919

20+
## Sample YAML
21+
```yaml
22+
apiVersion: elbv2.k8s.aws/v1beta1
23+
kind: TargetGroupBinding
24+
metadata:
25+
name: my-tgb
26+
spec:
27+
serviceRef:
28+
name: awesome-service # route traffic to the awesome-service
29+
port: 80
30+
targetGroupARN: <arn-to-targetGroup>
31+
```
32+
33+
2034
## VpcId
2135
TargetGroupBinding CR supports the explicit definition of the Virtual Private Cloud (VPC) of your TargetGroup.
2236
@@ -35,6 +49,7 @@ spec:
3549
name: awesome-service # route traffic to the awesome-service
3650
port: 80
3751
targetGroupARN: <arn-to-targetGroup>
52+
vpcId: <vpcId>
3853
```
3954
4055

pkg/targetgroupbinding/resource_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ func (m *defaultResourceManager) registerPodEndpoints(ctx context.Context, tgARN
388388
// Target group is in a different VPC from the cluster's VPC
389389
if tgVpcId != "" && tgVpcId != m.vpcID {
390390
vpcId = tgVpcId
391+
m.logger.Info("registering endpoints using vpcId", tgVpcId,
392+
"which is different from the cluster's vpcId", m.vpcID)
391393
}
392394
vpcInfo, err := m.vpcInfoProvider.FetchVPCInfo(ctx, vpcId)
393395
if err != nil {

webhooks/elbv2/targetgroupbinding_mutator_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,40 @@ func Test_targetGroupBindingMutator_MutateCreate(t *testing.T) {
181181
},
182182
},
183183
},
184+
{
185+
name: "targetGroupBinding with VpcId absent will be defaulted via AWS API",
186+
fields: fields{
187+
describeTargetGroupsAsListCalls: []describeTargetGroupsAsListCall{
188+
{
189+
req: &elbv2sdk.DescribeTargetGroupsInput{
190+
TargetGroupArns: awssdk.StringSlice([]string{"tg-1"}),
191+
},
192+
resp: []*elbv2sdk.TargetGroup{
193+
{
194+
VpcId: awssdk.String("vpcid-01"),
195+
},
196+
},
197+
},
198+
},
199+
},
200+
args: args{
201+
obj: &elbv2api.TargetGroupBinding{
202+
Spec: elbv2api.TargetGroupBindingSpec{
203+
TargetGroupARN: "tg-1",
204+
TargetType: &instanceTargetType,
205+
IPAddressType: &targetGroupIPAddressTypeIPv4,
206+
},
207+
},
208+
},
209+
want: &elbv2api.TargetGroupBinding{
210+
Spec: elbv2api.TargetGroupBindingSpec{
211+
TargetGroupARN: "tg-1",
212+
TargetType: &instanceTargetType,
213+
IPAddressType: &targetGroupIPAddressTypeIPv4,
214+
VpcId: "vpcid-01",
215+
},
216+
},
217+
},
184218
}
185219
for _, tt := range tests {
186220
t.Run(tt.name, func(t *testing.T) {
@@ -436,3 +470,86 @@ func Test_targetGroupBindingMutator_getIPAddressTypeFromAWS(t *testing.T) {
436470
})
437471
}
438472
}
473+
474+
func Test_targetGroupBindingMutator_obtainSDKVpcIDFromAWS(t *testing.T) {
475+
type describeTargetGroupsAsListCall struct {
476+
req *elbv2sdk.DescribeTargetGroupsInput
477+
resp []*elbv2sdk.TargetGroup
478+
err error
479+
}
480+
481+
type fields struct {
482+
describeTargetGroupsAsListCalls []describeTargetGroupsAsListCall
483+
}
484+
type args struct {
485+
tgARN string
486+
}
487+
tests := []struct {
488+
name string
489+
fields fields
490+
args args
491+
want string
492+
wantErr error
493+
}{
494+
{
495+
name: "fetch vpcid from aws",
496+
fields: fields{
497+
describeTargetGroupsAsListCalls: []describeTargetGroupsAsListCall{
498+
{
499+
req: &elbv2sdk.DescribeTargetGroupsInput{
500+
TargetGroupArns: awssdk.StringSlice([]string{"tg-1"}),
501+
},
502+
resp: []*elbv2sdk.TargetGroup{
503+
{
504+
VpcId: awssdk.String("vpcid-01"),
505+
},
506+
},
507+
},
508+
},
509+
},
510+
args: args{
511+
tgARN: "tg-1",
512+
},
513+
want: "vpcid-01",
514+
},
515+
{
516+
name: "some error while fetching vpcId",
517+
fields: fields{
518+
describeTargetGroupsAsListCalls: []describeTargetGroupsAsListCall{
519+
{
520+
req: &elbv2sdk.DescribeTargetGroupsInput{
521+
TargetGroupArns: awssdk.StringSlice([]string{"tg-1"}),
522+
},
523+
err: errors.New("vpcid not found"),
524+
},
525+
},
526+
},
527+
args: args{
528+
tgARN: "tg-1",
529+
},
530+
wantErr: errors.New("vpcid not found"),
531+
},
532+
}
533+
for _, tt := range tests {
534+
t.Run(tt.name, func(t *testing.T) {
535+
ctrl := gomock.NewController(t)
536+
defer ctrl.Finish()
537+
elbv2Client := services.NewMockELBV2(ctrl)
538+
for _, call := range tt.fields.describeTargetGroupsAsListCalls {
539+
elbv2Client.EXPECT().DescribeTargetGroupsAsList(gomock.Any(), call.req).Return(call.resp, call.err)
540+
}
541+
542+
m := &targetGroupBindingMutator{
543+
elbv2Client: elbv2Client,
544+
logger: logr.New(&log.NullLogSink{}),
545+
}
546+
got, err := m.getVpcIdFromAWS(context.Background(), tt.args.tgARN)
547+
if tt.wantErr != nil {
548+
assert.EqualError(t, err, tt.wantErr.Error())
549+
} else {
550+
assert.NoError(t, err)
551+
assert.Equal(t, tt.want, got)
552+
}
553+
})
554+
}
555+
}

0 commit comments

Comments
 (0)