@@ -168,24 +168,51 @@ func getNodeBalancerBackendIPv4SubnetID(client client.Client) (int, error) {
168168 return subnetID , nil
169169}
170170
171- func validateVPCSubnetFlags () error {
172- switch {
173- case len (Options .VPCIDs ) > 0 && len (Options .VPCNames ) > 0 :
171+ // validateBothNameAndIDNotSet checks if both VPC IDs and names are set.
172+ func validateBothNameAndIDNotSet () error {
173+ if len (Options .VPCIDs ) != 0 && len (Options .VPCNames ) != 0 {
174174 return fmt .Errorf ("cannot have both vpc-ids and vpc-names set" )
175- case len (Options .VPCIDs ) > 0 && len (Options .SubnetIDs ) == 0 :
175+ }
176+ return nil
177+ }
178+
179+ // validateVPCAndSubnetPairing checks if both VPC and subnet IDs or names are set correctly.
180+ func validateVPCAndSubnetPairing () error {
181+ if len (Options .VPCIDs ) != 0 && len (Options .SubnetIDs ) == 0 {
176182 return fmt .Errorf ("vpc-ids cannot be set without subnet-ids" )
177- case len (Options .SubnetIDs ) > 0 && len (Options .VPCIDs ) == 0 :
183+ }
184+ if len (Options .SubnetIDs ) != 0 && len (Options .VPCIDs ) == 0 {
178185 return fmt .Errorf ("subnet-ids cannot be set without vpc-ids" )
179- // since subnet-names defaults to "default", we also check if route-controller is enabled
180- // and if so, we require vpc-names to be set
181- case len (Options .SubnetNames ) > 0 && len (Options .VPCNames ) == 0 && Options .EnableRouteController :
186+ }
187+ return nil
188+ }
189+
190+ // validateSubnetNamesWithRouteController checks if subnet-names are set without vpc-names
191+ func validateSubnetNamesWithRouteController () error {
192+ if len (Options .SubnetNames ) != 0 && len (Options .VPCNames ) == 0 && Options .EnableRouteController {
182193 return fmt .Errorf ("subnet-names cannot be set without vpc-names" )
183- case len (Options .VPCIDs ) == 0 :
184- return nil
185194 }
186195 return nil
187196}
188197
198+ // validateVPCSubnetFlags validates the VPC and subnet flags for the route controller.
199+ // It checks that both VPC and subnet IDs or names are set correctly, and that they are paired correctly.
200+ // It also checks that if subnet names are set, VPC names must also be set,
201+ // and that if subnet IDs are set, VPC IDs must also be set.
202+ func validateVPCSubnetFlags () error {
203+ if err := validateBothNameAndIDNotSet (); err != nil {
204+ return err
205+ }
206+ if err := validateVPCAndSubnetPairing (); err != nil {
207+ return err
208+ }
209+ if err := validateSubnetNamesWithRouteController (); err != nil {
210+ return err
211+ }
212+ return nil
213+ }
214+
215+ // resolveSubnetNames resolves subnet ids to names for the given VPC ID.
189216func resolveSubnetNames (client client.Client , vpcID int ) ([]string , error ) {
190217 subnetNames := []string {}
191218 for _ , subnetID := range Options .SubnetIDs {
@@ -201,6 +228,9 @@ func resolveSubnetNames(client client.Client, vpcID int) ([]string, error) {
201228 return subnetNames , nil
202229}
203230
231+ // validateAndSetVPCSubnetFlags validates the VPC and subnet flags and sets the vpcNames and subnetNames options.
232+ // It retrieves the VPC names and subnet names from the Linode API based on the provided flags.
233+ // If subnet IDs are provided, it resolves the subnet names based on the first VPC ID.
204234func validateAndSetVPCSubnetFlags (client client.Client ) error {
205235 // ignore default subnet-names if subnet-ids are set
206236 if len (Options .SubnetIDs ) > 0 {
@@ -224,6 +254,9 @@ func validateAndSetVPCSubnetFlags(client client.Client) error {
224254 vpcIDs [vpc .Label ] = vpcID
225255 vpcNames = append (vpcNames , vpc .Label )
226256
257+ // we resolve subnet names only for the first VPC ID
258+ // as there is no vpc to subnet mapping in input flags
259+ // and we assume all subnets are in the same VPC
227260 if idx == 0 && len (Options .SubnetIDs ) > 0 {
228261 subnetNames , err := resolveSubnetNames (client , vpcID )
229262 if err != nil {
0 commit comments