@@ -44,9 +44,8 @@ func reconcileVPC(ctx context.Context, vpcScope *scope.VPCScope, logger logr.Log
4444
4545 createConfig .Label = vpcScope .LinodeVPC .Name
4646 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 ,
5049 }
5150 filter , err := listFilter .String ()
5251 if err != nil {
@@ -78,23 +77,35 @@ func reconcileVPC(ctx context.Context, vpcScope *scope.VPCScope, logger logr.Log
7877func reconcileExistingVPC (ctx context.Context , vpcScope * scope.VPCScope , vpc * linodego.VPC ) error {
7978 setVPCFields (& vpcScope .LinodeVPC .Spec , vpc )
8079
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 ))
8490 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
8793 }
8894
8995 // adopt or create subnets
9096 for idx , subnet := range vpcScope .LinodeVPC .Spec .Subnets {
9197 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
97107 } else {
108+ // Handle subnets that we need to create in the Linode API.
98109 ipv6 := []linodego.VPCSubnetCreateOptionsIPv6 {}
99110 for _ , ipv6Range := range subnet .IPv6Range {
100111 ipv6 = append (ipv6 , linodego.VPCSubnetCreateOptionsIPv6 {
0 commit comments