Skip to content

Commit e9c4548

Browse files
Merge pull request openshift#8592 from shiftstack/OCPBUGS-35420
OCPBUGS-35420: OSASINFRA-1962: openstack: Validate additionalNetworkIDs and additionalSecurityGroupIDs
2 parents 27d9113 + 7fd615b commit e9c4548

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
1818
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
1919
networkquotasets "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/quotas"
20+
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/groups"
2021
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
2122
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
2223
azutils "github.com/gophercloud/utils/v2/openstack/compute/v2/availabilityzones"
@@ -46,6 +47,8 @@ type CloudInfo struct {
4647
VolumeTypes []string
4748
NetworkExtensions []extensions.Extension
4849
Quotas []quota.Quota
50+
Networks []string
51+
SecurityGroups []string
4952

5053
clients *clients
5154
}
@@ -238,6 +241,16 @@ func (ci *CloudInfo) collectInfo(ctx context.Context, ic *types.InstallConfig) e
238241
return fmt.Errorf("failed to fetch network extensions: %w", err)
239242
}
240243

244+
ci.Networks, err = ci.getNetworks(ctx)
245+
if err != nil {
246+
return err
247+
}
248+
249+
ci.SecurityGroups, err = ci.getSecurityGroups(ctx)
250+
if err != nil {
251+
return err
252+
}
253+
241254
return nil
242255
}
243256

@@ -295,6 +308,46 @@ func (ci *CloudInfo) getFlavor(ctx context.Context, flavorName string) (Flavor,
295308
}, nil
296309
}
297310

311+
// getNetworks returns all the network IDs available on the cloud.
312+
func (ci *CloudInfo) getNetworks(ctx context.Context) ([]string, error) {
313+
pages, err := networks.List(ci.clients.networkClient, nil).AllPages(ctx)
314+
if err != nil {
315+
return nil, err
316+
}
317+
318+
networks, err := networks.ExtractNetworks(pages)
319+
if err != nil {
320+
return nil, err
321+
}
322+
323+
networkIDs := make([]string, len(networks))
324+
for i := range networks {
325+
networkIDs[i] = networks[i].ID
326+
}
327+
328+
return networkIDs, nil
329+
}
330+
331+
// getSecurityGroups returns all the security group IDs available on the cloud.
332+
func (ci *CloudInfo) getSecurityGroups(ctx context.Context) ([]string, error) {
333+
pages, err := groups.List(ci.clients.networkClient, groups.ListOpts{}).AllPages(ctx)
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
groups, err := groups.ExtractGroups(pages)
339+
if err != nil {
340+
return nil, err
341+
}
342+
343+
sgIDs := make([]string, len(groups))
344+
for i := range groups {
345+
sgIDs[i] = groups[i].ID
346+
}
347+
348+
return sgIDs, nil
349+
}
350+
298351
func (ci *CloudInfo) getNetworkByName(ctx context.Context, networkName string) (*networks.Network, error) {
299352
if networkName == "" {
300353
return nil, nil

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,40 @@ 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"))...)
72+
allErrs = append(allErrs, validateAdditionalSecurityGroups(p.AdditionalSecurityGroupIDs, ci.SecurityGroups, fldPath.Child("additionalSecurityGroupIDs"))...)
7173

7274
return allErrs
7375
}
7476

77+
func validateAdditionalNetworks(additionalNetworkIDs, availableNetworks []string, fldPath *field.Path) field.ErrorList {
78+
allErrs := field.ErrorList{}
79+
networkSet := make(map[string]struct{}, len(availableNetworks))
80+
for i := range availableNetworks {
81+
networkSet[availableNetworks[i]] = struct{}{}
82+
}
83+
for i, n := range additionalNetworkIDs {
84+
if _, ok := networkSet[n]; !ok {
85+
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), n, "Network either does not exist in this cloud, or is not available"))
86+
}
87+
}
88+
return allErrs
89+
}
90+
91+
func validateAdditionalSecurityGroups(additionalSecurityGroupIDs, availableSecurityGroups []string, fldPath *field.Path) field.ErrorList {
92+
allErrs := field.ErrorList{}
93+
sgSet := make(map[string]struct{}, len(availableSecurityGroups))
94+
for i := range availableSecurityGroups {
95+
sgSet[availableSecurityGroups[i]] = struct{}{}
96+
}
97+
for i, n := range additionalSecurityGroupIDs {
98+
if _, ok := sgSet[n]; !ok {
99+
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), n, "Security group either does not exist in this cloud, or is not available"))
100+
}
101+
}
102+
return allErrs
103+
}
104+
75105
func validateZones(input []string, available []string, fldPath *field.Path) field.ErrorList {
76106
// check if machinepool default
77107
if len(input) == 1 && input[0] == "" {

0 commit comments

Comments
 (0)