diff --git a/pkg/asset/installconfig/gcp/client.go b/pkg/asset/installconfig/gcp/client.go index a62c60d327b..7ac9caef040 100644 --- a/pkg/asset/installconfig/gcp/client.go +++ b/pkg/asset/installconfig/gcp/client.go @@ -104,9 +104,6 @@ func GetMachineTypeList(ctx context.Context, svc *compute.Service, project, regi } return nil }) - if len(machines) == 0 { - return nil, errors.New("failed to fetch instance type, this error usually occurs if the region or the instance type is not found") - } return machines, err } @@ -118,25 +115,38 @@ func (c *Client) GetMachineTypeWithZones(ctx context.Context, project, region, m return nil, nil, err } + pz, err := GetZones(ctx, svc, project, fmt.Sprintf("region eq .*%s", region)) + if err != nil { + return nil, nil, err + } + projZones := sets.New[string]() + for _, zone := range pz { + projZones.Insert(zone.Name) + } + machines, err := GetMachineTypeList(ctx, svc, project, region, machineType, "") if err != nil { return nil, nil, err } + // Custom machine types are not included in aggregated lists, so let's try + // to get the machine type directly before returning an error. Also + // fallback to all the zones in the project + if len(machines) == 0 { + cctx, cancel := context.WithTimeout(ctx, defaultTimeout) + defer cancel() + machine, err := svc.MachineTypes.Get(project, pz[0].Name, machineType).Context(cctx).Do() + if err != nil { + return nil, nil, fmt.Errorf("failed to fetch instance type: %w", err) + } + return machine, projZones, nil + } + zones := sets.New[string]() for _, machine := range machines { zones.Insert(machine.Zone) } - // Restrict to zones avaialable in the project - pz, err := GetZones(ctx, svc, project, fmt.Sprintf("region eq .*%s", region)) - if err != nil { - return nil, nil, err - } - projZones := sets.New[string]() - for _, zone := range pz { - projZones.Insert(zone.Name) - } zones = zones.Intersection(projZones) return machines[0], zones, nil diff --git a/pkg/asset/machines/gcp/zones.go b/pkg/asset/machines/gcp/zones.go index 526e9f732f2..0c6e01b5d31 100644 --- a/pkg/asset/machines/gcp/zones.go +++ b/pkg/asset/machines/gcp/zones.go @@ -64,10 +64,23 @@ func ZonesForInstanceType(project, region, instanceType string) ([]string, error ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) defer cancel() + pZones, err := gcpconfig.GetZones(ctx, svc, project, fmt.Sprintf("(region eq .*%s) (status eq UP)", region)) + if err != nil { + return nil, fmt.Errorf("failed to get zones for project: %w", err) + } + pZoneNames := sets.New[string]() + for _, z := range pZones { + pZoneNames.Insert(z.Name) + } + machines, err := gcpconfig.GetMachineTypeList(ctx, svc, project, region, instanceType, "items/*/machineTypes(zone),nextPageToken") if err != nil { return nil, fmt.Errorf("failed to get zones for instance type: %w", err) } + // Custom machine types do not show up in the list. Let's fallback to the project zones + if len(machines) == 0 { + return sets.List(pZoneNames), nil + } zones := sets.New[string]() for _, machine := range machines { @@ -75,14 +88,5 @@ func ZonesForInstanceType(project, region, instanceType string) ([]string, error } // Not all instance zones might be available in the project - pZones, err := gcpconfig.GetZones(ctx, svc, project, fmt.Sprintf("(region eq .*%s) (status eq UP)", region)) - if err != nil { - return nil, fmt.Errorf("failed to get zones for project: %w", err) - } - pZoneNames := sets.New[string]() - for _, z := range pZones { - pZoneNames.Insert(z.Name) - } - return sets.List(zones.Intersection(pZoneNames)), nil }