Skip to content

Commit 14f5ce8

Browse files
Merge pull request #9169 from patrickdillon/gcp-zones-client-filter
OCPBUGS-44193: move GCP zone filtering client-side
2 parents f65365b + 45557db commit 14f5ce8

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

pkg/asset/installconfig/gcp/client.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (c *Client) GetMachineTypeWithZones(ctx context.Context, project, region, m
119119
return nil, nil, err
120120
}
121121

122-
pz, err := GetZones(ctx, svc, project, fmt.Sprintf("region eq .*%s", region))
122+
pz, err := GetZones(ctx, svc, project, region)
123123
if err != nil {
124124
return nil, nil, err
125125
}
@@ -379,34 +379,33 @@ func (c *Client) GetRegions(ctx context.Context, project string) ([]string, erro
379379
return computeRegions, nil
380380
}
381381

382-
// GetZones uses the GCP Compute Service API to get a list of zones from a project.
383-
func GetZones(ctx context.Context, svc *compute.Service, project, filter string) ([]*compute.Zone, error) {
382+
// GetZones uses the GCP Compute Service API to get a list of zones with UP status in a region from a project.
383+
func GetZones(ctx context.Context, svc *compute.Service, project, region string) ([]*compute.Zone, error) {
384384
req := svc.Zones.List(project)
385-
if filter != "" {
386-
req = req.Filter(filter)
387-
}
388-
389385
zones := []*compute.Zone{}
390386
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
391387
defer cancel()
392388
if err := req.Pages(ctx, func(page *compute.ZoneList) error {
393-
zones = append(zones, page.Items...)
389+
for _, zone := range page.Items {
390+
if strings.HasSuffix(zone.Region, region) && strings.EqualFold(zone.Status, "UP") {
391+
zones = append(zones, zone)
392+
}
393+
}
394394
return nil
395395
}); err != nil {
396396
return nil, errors.Wrapf(err, "failed to get zones from project %s", project)
397397
}
398-
399398
return zones, nil
400399
}
401400

402401
// GetZones uses the GCP Compute Service API to get a list of zones from a project.
403-
func (c *Client) GetZones(ctx context.Context, project, filter string) ([]*compute.Zone, error) {
402+
func (c *Client) GetZones(ctx context.Context, project, region string) ([]*compute.Zone, error) {
404403
svc, err := c.getComputeService(ctx)
405404
if err != nil {
406405
return nil, err
407406
}
408407

409-
return GetZones(ctx, svc, project, filter)
408+
return GetZones(ctx, svc, project, region)
410409
}
411410

412411
func (c *Client) getCloudResourceService(ctx context.Context) (*cloudresourcemanager.Service, error) {

pkg/asset/installconfig/gcp/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func ValidateCredentialMode(client API, ic *types.InstallConfig) field.ErrorList
590590
func validateZones(client API, ic *types.InstallConfig) field.ErrorList {
591591
allErrs := field.ErrorList{}
592592

593-
zones, err := client.GetZones(context.TODO(), ic.GCP.ProjectID, fmt.Sprintf("region eq .*%s", ic.GCP.Region))
593+
zones, err := client.GetZones(context.TODO(), ic.GCP.ProjectID, ic.GCP.Region)
594594
if err != nil {
595595
return append(allErrs, field.InternalError(nil, err))
596596
} else if len(zones) == 0 {

pkg/asset/machines/gcp/zones.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ func AvailabilityZones(project, region string) ([]string, error) {
2929
return nil, errors.Wrap(err, "failed to create compute service")
3030
}
3131

32-
regionURL := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s",
33-
project, region)
34-
filter := fmt.Sprintf("(region eq %s) (status eq UP)", regionURL)
35-
zones, err := gcpconfig.GetZones(ctx, svc, project, filter)
32+
zones, err := gcpconfig.GetZones(ctx, svc, project, region)
3633
if err != nil {
3734
return nil, errors.New("no zone was found")
3835
}
@@ -64,7 +61,7 @@ func ZonesForInstanceType(project, region, instanceType string) ([]string, error
6461
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
6562
defer cancel()
6663

67-
pZones, err := gcpconfig.GetZones(ctx, svc, project, fmt.Sprintf("(region eq .*%s) (status eq UP)", region))
64+
pZones, err := gcpconfig.GetZones(ctx, svc, project, region)
6865
if err != nil {
6966
return nil, fmt.Errorf("failed to get zones for project: %w", err)
7067
}

0 commit comments

Comments
 (0)