Skip to content

Commit c2515fe

Browse files
authored
Merge pull request #3630 from ikosenn/fix-tgb-backwards-compatibility
fix: Adding VpcID to TGB broke upgradability.
2 parents 8393192 + bec483d commit c2515fe

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func main() {
165165
corewebhook.NewServiceMutator(controllerCFG.ServiceConfig.LoadBalancerClass, ctrl.Log).SetupWithManager(mgr)
166166
elbv2webhook.NewIngressClassParamsValidator().SetupWithManager(mgr)
167167
elbv2webhook.NewTargetGroupBindingMutator(cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
168-
elbv2webhook.NewTargetGroupBindingValidator(mgr.GetClient(), cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
168+
elbv2webhook.NewTargetGroupBindingValidator(mgr.GetClient(), cloud.ELBV2(), cloud.VpcID(), ctrl.Log).SetupWithManager(mgr)
169169
networkingwebhook.NewIngressValidator(mgr.GetClient(), controllerCFG.IngressConfig, ctrl.Log).SetupWithManager(mgr)
170170
//+kubebuilder:scaffold:builder
171171

webhooks/elbv2/targetgroupbinding_validator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
const apiPathValidateELBv2TargetGroupBinding = "/validate-elbv2-k8s-aws-v1beta1-targetgroupbinding"
2222

2323
// NewTargetGroupBindingValidator returns a validator for TargetGroupBinding CRD.
24-
func NewTargetGroupBindingValidator(k8sClient client.Client, elbv2Client services.ELBV2, logger logr.Logger) *targetGroupBindingValidator {
24+
func NewTargetGroupBindingValidator(k8sClient client.Client, elbv2Client services.ELBV2, vpcID string, logger logr.Logger) *targetGroupBindingValidator {
2525
return &targetGroupBindingValidator{
2626
k8sClient: k8sClient,
2727
elbv2Client: elbv2Client,
2828
logger: logger,
29+
vpcID: vpcID,
2930
}
3031
}
3132

@@ -35,6 +36,7 @@ type targetGroupBindingValidator struct {
3536
k8sClient client.Client
3637
elbv2Client services.ELBV2
3738
logger logr.Logger
39+
vpcID string
3840
}
3941

4042
func (v *targetGroupBindingValidator) Prototype(_ admission.Request) (runtime.Object, error) {
@@ -112,7 +114,8 @@ func (v *targetGroupBindingValidator) checkImmutableFields(tgb *elbv2api.TargetG
112114
changedImmutableFields = append(changedImmutableFields, "spec.ipAddressType")
113115
}
114116
if (tgb.Spec.VpcID != "" && oldTGB.Spec.VpcID != "" && (tgb.Spec.VpcID) != (oldTGB.Spec.VpcID)) ||
115-
(tgb.Spec.VpcID == "") != (oldTGB.Spec.VpcID == "") {
117+
(oldTGB.Spec.VpcID != "" && tgb.Spec.VpcID == "") ||
118+
(oldTGB.Spec.VpcID == "" && tgb.Spec.VpcID != "" && tgb.Spec.VpcID != v.vpcID) {
116119
changedImmutableFields = append(changedImmutableFields, "spec.vpcID")
117120
}
118121
if len(changedImmutableFields) != 0 {

webhooks/elbv2/targetgroupbinding_validator_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
3939
}
4040
instanceTargetType := elbv2api.TargetTypeInstance
4141
ipTargetType := elbv2api.TargetTypeIP
42+
clusterVpcID := "vpcid-02"
4243
tests := []struct {
4344
name string
4445
fields fields
@@ -194,7 +195,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
194195
TargetGroupArn: awssdk.String("tg-2"),
195196
TargetType: awssdk.String("instance"),
196197
IpAddressType: awssdk.String("ipv6"),
197-
VpcId: awssdk.String("vpcid-02"),
198+
VpcId: &clusterVpcID,
198199
},
199200
},
200201
},
@@ -207,7 +208,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
207208
TargetGroupArn: awssdk.String("tg-2"),
208209
TargetType: awssdk.String("instance"),
209210
IpAddressType: awssdk.String("ipv6"),
210-
VpcId: awssdk.String("vpcid-02"),
211+
VpcId: &clusterVpcID,
211212
},
212213
},
213214
},
@@ -219,7 +220,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
219220
TargetGroupARN: "tg-2",
220221
TargetType: &instanceTargetType,
221222
IPAddressType: &targetGroupIPAddressTypeIPv6,
222-
VpcID: "vpcid-02",
223+
VpcID: clusterVpcID,
223224
},
224225
},
225226
},
@@ -238,7 +239,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
238239
TargetGroupArn: awssdk.String("tg-2"),
239240
TargetType: awssdk.String("instance"),
240241
IpAddressType: awssdk.String("ipv6"),
241-
VpcId: awssdk.String("vpcid-02"),
242+
VpcId: &clusterVpcID,
242243
},
243244
},
244245
},
@@ -251,7 +252,7 @@ func Test_targetGroupBindingValidator_ValidateCreate(t *testing.T) {
251252
TargetGroupArn: awssdk.String("tg-2"),
252253
TargetType: awssdk.String("instance"),
253254
IpAddressType: awssdk.String("ipv6"),
254-
VpcId: awssdk.String("vpcid-02"),
255+
VpcId: &clusterVpcID,
255256
},
256257
},
257258
},
@@ -479,6 +480,7 @@ func Test_targetGroupBindingValidator_checkImmutableFields(t *testing.T) {
479480
}
480481
instanceTargetType := elbv2api.TargetTypeInstance
481482
ipTargetType := elbv2api.TargetTypeIP
483+
clusterVpcID := "cluster-vpc-id"
482484
tests := []struct {
483485
name string
484486
args args
@@ -746,11 +748,31 @@ func Test_targetGroupBindingValidator_checkImmutableFields(t *testing.T) {
746748
},
747749
wantErr: errors.New("TargetGroupBinding update may not change these fields: spec.vpcID"),
748750
},
751+
{
752+
name: "VpcID modified from nil to cluster vpc-id is allowed",
753+
args: args{
754+
tgb: &elbv2api.TargetGroupBinding{
755+
Spec: elbv2api.TargetGroupBindingSpec{
756+
TargetGroupARN: "tg-2",
757+
TargetType: &ipTargetType,
758+
VpcID: clusterVpcID,
759+
},
760+
},
761+
oldTGB: &elbv2api.TargetGroupBinding{
762+
Spec: elbv2api.TargetGroupBindingSpec{
763+
TargetGroupARN: "tg-2",
764+
TargetType: &ipTargetType,
765+
},
766+
},
767+
},
768+
wantErr: nil,
769+
},
749770
}
750771
for _, tt := range tests {
751772
t.Run(tt.name, func(t *testing.T) {
752773
v := &targetGroupBindingValidator{
753774
logger: logr.New(&log.NullLogSink{}),
775+
vpcID: clusterVpcID,
754776
}
755777
err := v.checkImmutableFields(tt.args.tgb, tt.args.oldTGB)
756778
if tt.wantErr != nil {

0 commit comments

Comments
 (0)