@@ -44,9 +44,8 @@ func reconcileVPC(ctx context.Context, vpcScope *scope.VPCScope, logger logr.Log
44
44
45
45
createConfig .Label = vpcScope .LinodeVPC .Name
46
46
listFilter := util.Filter {
47
- ID : vpcScope .LinodeVPC .Spec .VPCID ,
48
- Label : createConfig .Label ,
49
- Tags : nil ,
47
+ ID : vpcScope .LinodeVPC .Spec .VPCID ,
48
+ Tags : nil ,
50
49
}
51
50
filter , err := listFilter .String ()
52
51
if err != nil {
@@ -78,23 +77,35 @@ func reconcileVPC(ctx context.Context, vpcScope *scope.VPCScope, logger logr.Log
78
77
func reconcileExistingVPC (ctx context.Context , vpcScope * scope.VPCScope , vpc * linodego.VPC ) error {
79
78
setVPCFields (& vpcScope .LinodeVPC .Spec , vpc )
80
79
81
- // build a map of existing subnets to easily check for existence
82
- existingSubnets := make (map [string ]int , len (vpc .Subnets ))
83
- existingSubnetsIPv6 := make (map [string ][]linodego.VPCIPv6Range , len (vpc .Subnets ))
80
+ // Build a map of VPC subnets by both label and ID. We check for
81
+ // the subnet ID but fallback to the label because the ID is not guaranteed
82
+ // to be set until we've processed the subnet at least once.
83
+ type SubnetConfig struct {
84
+ ID int
85
+ Label string
86
+ IPv6 []linodego.VPCIPv6Range
87
+ }
88
+ subnetsByLabel := make (map [string ]SubnetConfig , len (vpc .Subnets ))
89
+ subnetsById := make (map [int ]SubnetConfig , len (vpc .Subnets ))
84
90
for _ , subnet := range vpc .Subnets {
85
- existingSubnets [ subnet . Label ] = subnet .ID
86
- existingSubnetsIPv6 [subnet .Label ] = subnet .IPv6
91
+ config := SubnetConfig { subnet .ID , subnet . Label , subnet . IPv6 }
92
+ subnetsByLabel [subnet .Label ], subnetsById [ subnet .ID ] = config , config
87
93
}
88
94
89
95
// adopt or create subnets
90
96
for idx , subnet := range vpcScope .LinodeVPC .Spec .Subnets {
91
97
if subnet .SubnetID != 0 {
92
- continue
93
- }
94
- if id , ok := existingSubnets [subnet .Label ]; ok {
95
- vpcScope .LinodeVPC .Spec .Subnets [idx ].SubnetID = id
96
- vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = existingSubnetsIPv6 [subnet .Label ]
98
+ if config , ok := subnetsById [subnet .SubnetID ]; ok {
99
+ vpcScope .LinodeVPC .Spec .Subnets [idx ].Label = config .Label
100
+ vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = config .IPv6
101
+ }
102
+ } else if config , ok := subnetsByLabel [subnet .Label ]; ok {
103
+ // Handle subnets that exist in the Linode API but have not had their
104
+ // ID set on the LinodeVPC yet.
105
+ vpcScope .LinodeVPC .Spec .Subnets [idx ].SubnetID = config .ID
106
+ vpcScope .LinodeVPC .Spec .Subnets [idx ].IPv6 = config .IPv6
97
107
} else {
108
+ // Handle subnets that we need to create in the Linode API.
98
109
ipv6 := []linodego.VPCSubnetCreateOptionsIPv6 {}
99
110
for _ , ipv6Range := range subnet .IPv6Range {
100
111
ipv6 = append (ipv6 , linodego.VPCSubnetCreateOptionsIPv6 {
0 commit comments