@@ -181,6 +181,94 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
181181},
182182wantErr : errors .New ("invalid IP address type ipv6 for TargetGroup tg-2" ),
183183},
184+ {
185+ name : "VpcID in spec matches with TG vpc" ,
186+ fields : fields {
187+ describeTargetGroupsAsListCalls : []describeTargetGroupsAsListCall {
188+ {
189+ req : & elbv2sdk.DescribeTargetGroupsInput {
190+ TargetGroupArns : awssdk .StringSlice ([]string {"tg-2" }),
191+ },
192+ resp : []* elbv2sdk.TargetGroup {
193+ {
194+ TargetGroupArn : awssdk .String ("tg-2" ),
195+ TargetType : awssdk .String ("instance" ),
196+ IpAddressType : awssdk .String ("ipv6" ),
197+ VpcId : awssdk .String ("vpcid-02" ),
198+ },
199+ },
200+ },
201+ {
202+ req : & elbv2sdk.DescribeTargetGroupsInput {
203+ TargetGroupArns : awssdk .StringSlice ([]string {"tg-2" }),
204+ },
205+ resp : []* elbv2sdk.TargetGroup {
206+ {
207+ TargetGroupArn : awssdk .String ("tg-2" ),
208+ TargetType : awssdk .String ("instance" ),
209+ IpAddressType : awssdk .String ("ipv6" ),
210+ VpcId : awssdk .String ("vpcid-02" ),
211+ },
212+ },
213+ },
214+ },
215+ },
216+ args : args {
217+ obj : & elbv2api.TargetGroupBinding {
218+ Spec : elbv2api.TargetGroupBindingSpec {
219+ TargetGroupARN : "tg-2" ,
220+ TargetType : & instanceTargetType ,
221+ IPAddressType : & targetGroupIPAddressTypeIPv6 ,
222+ VpcID : "vpcid-02" ,
223+ },
224+ },
225+ },
226+ wantErr : nil ,
227+ },
228+ {
229+ name : "VpcID provided doesnt match TG VpcID mismatch" ,
230+ fields : fields {
231+ describeTargetGroupsAsListCalls : []describeTargetGroupsAsListCall {
232+ {
233+ req : & elbv2sdk.DescribeTargetGroupsInput {
234+ TargetGroupArns : awssdk .StringSlice ([]string {"tg-2" }),
235+ },
236+ resp : []* elbv2sdk.TargetGroup {
237+ {
238+ TargetGroupArn : awssdk .String ("tg-2" ),
239+ TargetType : awssdk .String ("instance" ),
240+ IpAddressType : awssdk .String ("ipv6" ),
241+ VpcId : awssdk .String ("vpcid-02" ),
242+ },
243+ },
244+ },
245+ {
246+ req : & elbv2sdk.DescribeTargetGroupsInput {
247+ TargetGroupArns : awssdk .StringSlice ([]string {"tg-2" }),
248+ },
249+ resp : []* elbv2sdk.TargetGroup {
250+ {
251+ TargetGroupArn : awssdk .String ("tg-2" ),
252+ TargetType : awssdk .String ("instance" ),
253+ IpAddressType : awssdk .String ("ipv6" ),
254+ VpcId : awssdk .String ("vpcid-02" ),
255+ },
256+ },
257+ },
258+ },
259+ },
260+ args : args {
261+ obj : & elbv2api.TargetGroupBinding {
262+ Spec : elbv2api.TargetGroupBindingSpec {
263+ TargetGroupARN : "tg-2" ,
264+ TargetType : & instanceTargetType ,
265+ IPAddressType : & targetGroupIPAddressTypeIPv6 ,
266+ VpcID : "vpcid-01" ,
267+ },
268+ },
269+ },
270+ wantErr : errors .New ("invalid VpcID vpcid-01 doesnt match VpcID from TargetGroup tg-2" ),
271+ },
184272}
185273for _ , tt := range tests {
186274t .Run (tt .name , func (t * testing.T ) {
@@ -600,6 +688,64 @@ func Test_targetGroupBindingValidator_checkImmutableFields(t *testing.T) {
600688},
601689wantErr : errors .New ("TargetGroupBinding update may not change these fields: spec.ipAddressType" ),
602690},
691+ {
692+ name : "VpcID modified from vpc-01 to vpc-02" ,
693+ args : args {
694+ tgb : & elbv2api.TargetGroupBinding {
695+ Spec : elbv2api.TargetGroupBindingSpec {
696+ TargetGroupARN : "tg-2" ,
697+ TargetType : & ipTargetType ,
698+ VpcID : "vpc-02" ,
699+ },
700+ },
701+ oldTGB : & elbv2api.TargetGroupBinding {
702+ Spec : elbv2api.TargetGroupBindingSpec {
703+ TargetGroupARN : "tg-2" ,
704+ TargetType : & ipTargetType ,
705+ VpcID : "vpc-01" ,
706+ },
707+ },
708+ },
709+ wantErr : errors .New ("TargetGroupBinding update may not change these fields: spec.vpcID" ),
710+ },
711+ {
712+ name : "VpcID modified from vpc-01 to nil" ,
713+ args : args {
714+ tgb : & elbv2api.TargetGroupBinding {
715+ Spec : elbv2api.TargetGroupBindingSpec {
716+ TargetGroupARN : "tg-2" ,
717+ TargetType : & ipTargetType ,
718+ },
719+ },
720+ oldTGB : & elbv2api.TargetGroupBinding {
721+ Spec : elbv2api.TargetGroupBindingSpec {
722+ TargetGroupARN : "tg-2" ,
723+ TargetType : & ipTargetType ,
724+ VpcID : "vpc-01" ,
725+ },
726+ },
727+ },
728+ wantErr : errors .New ("TargetGroupBinding update may not change these fields: spec.vpcID" ),
729+ },
730+ {
731+ name : "VpcID modified from nil to vpc-01" ,
732+ args : args {
733+ tgb : & elbv2api.TargetGroupBinding {
734+ Spec : elbv2api.TargetGroupBindingSpec {
735+ TargetGroupARN : "tg-2" ,
736+ TargetType : & ipTargetType ,
737+ VpcID : "vpc-01" ,
738+ },
739+ },
740+ oldTGB : & elbv2api.TargetGroupBinding {
741+ Spec : elbv2api.TargetGroupBindingSpec {
742+ TargetGroupARN : "tg-2" ,
743+ TargetType : & ipTargetType ,
744+ },
745+ },
746+ },
747+ wantErr : errors .New ("TargetGroupBinding update may not change these fields: spec.vpcID" ),
748+ },
603749}
604750for _ , tt := range tests {
605751t .Run (tt .name , func (t * testing.T ) {
@@ -906,3 +1052,80 @@ func Test_targetGroupBindingValidator_checkExistingTargetGroups(t *testing.T) {
9061052})
9071053}
9081054}
1055+
1056+ func Test_targetGroupBindingValidator_checkTargetGroupVpcID (t * testing.T ) {
1057+ type args struct {
1058+ obj * elbv2api.TargetGroupBinding
1059+ }
1060+ type describeTargetGroupsAsListCall struct {
1061+ req * elbv2sdk.DescribeTargetGroupsInput
1062+ resp []* elbv2sdk.TargetGroup
1063+ err error
1064+ }
1065+ type fields struct {
1066+ describeTargetGroupsAsListCalls []describeTargetGroupsAsListCall
1067+ }
1068+ tests := []struct {
1069+ name string
1070+ fields fields
1071+ args args
1072+ wantErr error
1073+ }{
1074+ {
1075+ name : "[ok] VpcID is not set" ,
1076+ args : args {
1077+ obj : & elbv2api.TargetGroupBinding {
1078+ Spec : elbv2api.TargetGroupBindingSpec {},
1079+ },
1080+ },
1081+ wantErr : nil ,
1082+ },
1083+ {
1084+ name : "[err] vpcID is not found" ,
1085+ fields : fields {
1086+ describeTargetGroupsAsListCalls : []describeTargetGroupsAsListCall {
1087+ {
1088+ req : & elbv2sdk.DescribeTargetGroupsInput {
1089+ TargetGroupArns : awssdk .StringSlice ([]string {"tg-2" }),
1090+ },
1091+ err : errors .New ("vpcid not found" ),
1092+ },
1093+ },
1094+ },
1095+ args : args {
1096+ obj : & elbv2api.TargetGroupBinding {
1097+ Spec : elbv2api.TargetGroupBindingSpec {
1098+ TargetGroupARN : "tg-2" ,
1099+ VpcID : "vpcid-01" ,
1100+ },
1101+ },
1102+ },
1103+ wantErr : errors .New ("unable to get target group VpcID: vpcid not found" ),
1104+ },
1105+ }
1106+ for _ , tt := range tests {
1107+ t .Run (tt .name , func (t * testing.T ) {
1108+ ctrl := gomock .NewController (t )
1109+ defer ctrl .Finish ()
1110+ k8sSchema := runtime .NewScheme ()
1111+ clientgoscheme .AddToScheme (k8sSchema )
1112+ elbv2api .AddToScheme (k8sSchema )
1113+ k8sClient := testclient .NewFakeClientWithScheme (k8sSchema )
1114+ elbv2Client := services .NewMockELBV2 (ctrl )
1115+ for _ , call := range tt .fields .describeTargetGroupsAsListCalls {
1116+ elbv2Client .EXPECT ().DescribeTargetGroupsAsList (gomock .Any (), call .req ).Return (call .resp , call .err )
1117+ }
1118+ v := & targetGroupBindingValidator {
1119+ k8sClient : k8sClient ,
1120+ elbv2Client : elbv2Client ,
1121+ logger : logr .New (& log.NullLogSink {}),
1122+ }
1123+ err := v .checkTargetGroupVpcID (context .Background (), tt .args .obj )
1124+ if tt .wantErr != nil {
1125+ assert .EqualError (t , err , tt .wantErr .Error ())
1126+ } else {
1127+ assert .NoError (t , err )
1128+ }
1129+ })
1130+ }
1131+ }
0 commit comments