@@ -69,28 +69,21 @@ func reconcileVPC(ctx context.Context, vpcScope *scope.VPCScope, logger logr.Log
6969 return err
7070 }
7171
72- vpcScope .LinodeVPC .Spec .VPCID = & vpc .ID
73- for _ , ipv6 := range vpc .IPv6 {
74- vpcScope .LinodeVPC .Spec .IPv6 = append (vpcScope .LinodeVPC .Spec .IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
75- }
72+ setVPCFields (& vpcScope .LinodeVPC .Spec , vpc )
7673 updateVPCSpecSubnets (vpcScope , vpc )
7774
7875 return nil
7976}
8077
8178func reconcileExistingVPC (ctx context.Context , vpcScope * scope.VPCScope , vpc * linodego.VPC ) error {
82- // Labels are unique
83- vpcScope .LinodeVPC .Spec .VPCID = & vpc .ID
84- for _ , ipv6 := range vpc .IPv6 {
85- vpcScope .LinodeVPC .Spec .IPv6 = append (vpcScope .LinodeVPC .Spec .IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
86- }
79+ setVPCFields (& vpcScope .LinodeVPC .Spec , vpc )
8780
8881 // build a map of existing subnets to easily check for existence
8982 existingSubnets := make (map [string ]int , len (vpc .Subnets ))
90- existingSubnetsIpv6 := make (map [string ][]linodego.VPCIPv6Range , len (vpc .Subnets ))
83+ existingSubnetsIPv6 := make (map [string ][]linodego.VPCIPv6Range , len (vpc .Subnets ))
9184 for _ , subnet := range vpc .Subnets {
9285 existingSubnets [subnet .Label ] = subnet .ID
93- existingSubnetsIpv6 [subnet .Label ] = subnet .IPv6
86+ existingSubnetsIPv6 [subnet .Label ] = subnet .IPv6
9487 }
9588
9689 // adopt or create subnets
@@ -100,7 +93,7 @@ func reconcileExistingVPC(ctx context.Context, vpcScope *scope.VPCScope, vpc *li
10093 }
10194 if id , ok := existingSubnets [subnet .Label ]; ok {
10295 vpcScope .LinodeVPC .Spec .Subnets [idx ].SubnetID = id
103- vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = existingSubnetsIpv6 [subnet .Label ]
96+ vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = existingSubnetsIPv6 [subnet .Label ]
10497 } else {
10598 ipv6 := []linodego.VPCSubnetCreateOptionsIPv6 {}
10699 for _ , ipv6Range := range subnet .IPv6Range {
@@ -118,10 +111,7 @@ func reconcileExistingVPC(ctx context.Context, vpcScope *scope.VPCScope, vpc *li
118111 if err != nil {
119112 return err
120113 }
121- vpcScope .LinodeVPC .Spec .Subnets [idx ].SubnetID = newSubnet .ID
122- for _ , ipv6 := range newSubnet .IPv6 {
123- vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = append (vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
124- }
114+ setSubnetFields (& vpcScope .LinodeVPC .Spec .Subnets [idx ], newSubnet )
125115 }
126116 }
127117
@@ -133,16 +123,33 @@ func updateVPCSpecSubnets(vpcScope *scope.VPCScope, vpc *linodego.VPC) {
133123 for idx , specSubnet := range vpcScope .LinodeVPC .Spec .Subnets {
134124 for _ , vpcSubnet := range vpc .Subnets {
135125 if specSubnet .Label == vpcSubnet .Label {
136- vpcScope .LinodeVPC .Spec .Subnets [idx ].SubnetID = vpcSubnet .ID
137- for _ , ipv6 := range vpcSubnet .IPv6 {
138- vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = append (vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
139- }
126+ setSubnetFields (& vpcScope .LinodeVPC .Spec .Subnets [idx ], & vpcSubnet )
140127 break
141128 }
142129 }
143130 }
144131}
145132
133+ // setVPCFields sets the VPCID and IPv6 in the LinodeVPCSpec from the Linode VPC.
134+ func setVPCFields (vpc * infrav1alpha2.LinodeVPCSpec , linodeVPC * linodego.VPC ) {
135+ vpc .VPCID = & linodeVPC .ID
136+ // Clear existing IPv6 ranges and set new ones
137+ vpc .IPv6 = nil
138+ for _ , ipv6 := range linodeVPC .IPv6 {
139+ vpc .IPv6 = append (vpc .IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
140+ }
141+ }
142+
143+ // setSubnetFields sets the SubnetID and IPv6 in the VPCSubnetCreateOptions from the Linode VPCSubnet.
144+ func setSubnetFields (subnet * infrav1alpha2.VPCSubnetCreateOptions , vpcSubnet * linodego.VPCSubnet ) {
145+ subnet .SubnetID = vpcSubnet .ID
146+ // Clear existing IPv6 ranges and set new ones
147+ subnet .IPv6 = nil
148+ for _ , ipv6 := range vpcSubnet .IPv6 {
149+ subnet .IPv6 = append (subnet .IPv6 , linodego.VPCIPv6Range {Range : ipv6 .Range })
150+ }
151+ }
152+
146153func linodeVPCSpecToVPCCreateConfig (vpcSpec infrav1alpha2.LinodeVPCSpec ) * linodego.VPCCreateOptions {
147154 vpcIPv6 := make ([]linodego.VPCCreateOptionsIPv6 , len (vpcSpec .IPv6Range ))
148155 for idx , ipv6 := range vpcSpec .IPv6Range {
0 commit comments