Skip to content

Commit d515402

Browse files
author
Rahul Sharma
committed
resolve comments
1 parent 9aa8eb4 commit d515402

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

cloud/linode/vpc.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
189216
func 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.
204234
func 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 {

deploy/chart/templates/daemonset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ spec:
119119
{{- if and $vpcIDs (not $subnetIDs) }}
120120
{{- fail "subnetIDs must be set when vpcIDs are set for route-controller" }}
121121
{{- end }}
122+
{{- if and $subnetIDs (not $vpcIDs) }}
123+
{{- fail "vpcIDs must be set when subnetIDs are set for route-controller" }}
124+
{{- end }}
122125
{{- if not $clusterCIDR }}
123126
{{- fail "clusterCIDR is required if route-controller is enabled" }}
124127
{{- end }}

0 commit comments

Comments
 (0)