Skip to content

Commit ceba5ba

Browse files
openstack: Validate additionalNetworkIDs
Add a pre-flight check that verifies that the networks listed in the machine-pool property `additionalNetworkIDs` actually exist on the cloud.
1 parent 27d9113 commit ceba5ba

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pkg/asset/installconfig/openstack/validation/cloudinfo.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type CloudInfo struct {
4646
VolumeTypes []string
4747
NetworkExtensions []extensions.Extension
4848
Quotas []quota.Quota
49+
Networks []string
4950

5051
clients *clients
5152
}
@@ -238,6 +239,11 @@ func (ci *CloudInfo) collectInfo(ctx context.Context, ic *types.InstallConfig) e
238239
return fmt.Errorf("failed to fetch network extensions: %w", err)
239240
}
240241

242+
ci.Networks, err = ci.getNetworks(ctx)
243+
if err != nil {
244+
return err
245+
}
246+
241247
return nil
242248
}
243249

@@ -295,6 +301,26 @@ func (ci *CloudInfo) getFlavor(ctx context.Context, flavorName string) (Flavor,
295301
}, nil
296302
}
297303

304+
// getNetworks returns all the network IDs available on the cloud.
305+
func (ci *CloudInfo) getNetworks(ctx context.Context) ([]string, error) {
306+
pages, err := networks.List(ci.clients.networkClient, nil).AllPages(ctx)
307+
if err != nil {
308+
return nil, err
309+
}
310+
311+
networks, err := networks.ExtractNetworks(pages)
312+
if err != nil {
313+
return nil, err
314+
}
315+
316+
networkIDs := make([]string, len(networks))
317+
for i := range networks {
318+
networkIDs[i] = networks[i].ID
319+
}
320+
321+
return networkIDs, nil
322+
}
323+
298324
func (ci *CloudInfo) getNetworkByName(ctx context.Context, networkName string) (*networks.Network, error) {
299325
if networkName == "" {
300326
return nil, nil

pkg/asset/installconfig/openstack/validation/machinepool.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,25 @@ func ValidateMachinePool(p *openstack.MachinePool, ci *CloudInfo, controlPlane b
6868
allErrs = append(allErrs, validateZones(p.Zones, ci.ComputeZones, fldPath.Child("zones"))...)
6969
allErrs = append(allErrs, validateUUIDV4s(p.AdditionalNetworkIDs, fldPath.Child("additionalNetworkIDs"))...)
7070
allErrs = append(allErrs, validateUUIDV4s(p.AdditionalSecurityGroupIDs, fldPath.Child("additionalSecurityGroupIDs"))...)
71+
allErrs = append(allErrs, validateAdditionalNetworks(p.AdditionalNetworkIDs, ci.Networks, fldPath.Child("additionalNetworkIDs"))...)
7172

7273
return allErrs
7374
}
7475

76+
func validateAdditionalNetworks(additionalNetworkIDs, availableNetworks []string, fldPath *field.Path) field.ErrorList {
77+
allErrs := field.ErrorList{}
78+
networkSet := make(map[string]struct{}, len(availableNetworks))
79+
for i := range availableNetworks {
80+
networkSet[availableNetworks[i]] = struct{}{}
81+
}
82+
for i, n := range additionalNetworkIDs {
83+
if _, ok := networkSet[n]; !ok {
84+
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), n, "Network either does not exist in this cloud, or is not available"))
85+
}
86+
}
87+
return allErrs
88+
}
89+
7590
func validateZones(input []string, available []string, fldPath *field.Path) field.ErrorList {
7691
// check if machinepool default
7792
if len(input) == 1 && input[0] == "" {

0 commit comments

Comments
 (0)