@@ -55,6 +55,9 @@ func (v *targetGroupBindingValidator) ValidateCreate(ctx context.Context, obj ru
55
55
if err := v .checkTargetGroupIPAddressType (ctx , tgb ); err != nil {
56
56
return err
57
57
}
58
+ if err := v .checkTargetGroupVpcId (ctx , tgb ); err != nil {
59
+ return err
60
+ }
58
61
return nil
59
62
}
60
63
@@ -83,6 +86,9 @@ func (v *targetGroupBindingValidator) checkRequiredFields(tgb *elbv2api.TargetGr
83
86
if tgb .Spec .TargetType == nil {
84
87
absentRequiredFields = append (absentRequiredFields , "spec.targetType" )
85
88
}
89
+ if tgb .Spec .VpcId == "" {
90
+ absentRequiredFields = append (absentRequiredFields , "spec.vpcId" )
91
+ }
86
92
if len (absentRequiredFields ) != 0 {
87
93
return errors .Errorf ("%s must specify these fields: %s" , "TargetGroupBinding" , strings .Join (absentRequiredFields , "," ))
88
94
}
@@ -108,6 +114,10 @@ func (v *targetGroupBindingValidator) checkImmutableFields(tgb *elbv2api.TargetG
108
114
if oldTGB .Spec .IPAddressType != nil && tgb .Spec .IPAddressType != nil && (* oldTGB .Spec .IPAddressType ) != (* tgb .Spec .IPAddressType ) {
109
115
changedImmutableFields = append (changedImmutableFields , "spec.ipAddressType" )
110
116
}
117
+ if (tgb .Spec .VpcId != "" && oldTGB .Spec .VpcId != "" && (tgb .Spec .VpcId ) != (oldTGB .Spec .VpcId )) ||
118
+ (tgb .Spec .VpcId == "" ) != (oldTGB .Spec .VpcId == "" ) {
119
+ changedImmutableFields = append (changedImmutableFields , "spec.vpcId" )
120
+ }
111
121
if len (changedImmutableFields ) != 0 {
112
122
return errors .Errorf ("%s update may not change these fields: %s" , "TargetGroupBinding" , strings .Join (changedImmutableFields , "," ))
113
123
}
@@ -150,6 +160,18 @@ func (v *targetGroupBindingValidator) checkTargetGroupIPAddressType(ctx context.
150
160
return nil
151
161
}
152
162
163
+ // checkTargetGroupVpcId ensures VpcId matches with that on the AWS target group
164
+ func (v * targetGroupBindingValidator ) checkTargetGroupVpcId (ctx context.Context , tgb * elbv2api.TargetGroupBinding ) error {
165
+ vpcId , err := v .getVpcIdFromAWS (ctx , tgb .Spec .TargetGroupARN )
166
+ if err != nil {
167
+ return errors .Wrap (err , "unable to get target group VpcId" )
168
+ }
169
+ if vpcId != tgb .Spec .VpcId {
170
+ return errors .Errorf ("invalid vpc Id %v doesnt match VpcId from TargetGroup %v" , tgb .Spec .VpcId , tgb .Spec .TargetGroupARN )
171
+ }
172
+ return nil
173
+ }
174
+
153
175
// getTargetGroupIPAddressTypeFromAWS returns the target group IP address type of AWS target group
154
176
func (v * targetGroupBindingValidator ) getTargetGroupIPAddressTypeFromAWS (ctx context.Context , tgARN string ) (elbv2api.TargetGroupIPAddressType , error ) {
155
177
targetGroup , err := v .getTargetGroupFromAWS (ctx , tgARN )
@@ -183,6 +205,14 @@ func (v *targetGroupBindingValidator) getTargetGroupFromAWS(ctx context.Context,
183
205
return tgList [0 ], nil
184
206
}
185
207
208
+ func (v * targetGroupBindingValidator ) getVpcIdFromAWS (ctx context.Context , tgARN string ) (string , error ) {
209
+ targetGroup , err := v .getTargetGroupFromAWS (ctx , tgARN )
210
+ if err != nil {
211
+ return "" , err
212
+ }
213
+ return awssdk .StringValue (targetGroup .VpcId ), nil
214
+ }
215
+
186
216
// +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
187
217
188
218
func (v * targetGroupBindingValidator ) SetupWithManager (mgr ctrl.Manager ) {
0 commit comments