-
Notifications
You must be signed in to change notification settings - Fork 190
gcloud: Fallback zone support (WIP) #4536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
angelcerveraroldan
wants to merge
2
commits into
coreos:main
Choose a base branch
from
angelcerveraroldan:gcp-fallback-zone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,13 @@ package gcloud | |
|
|
||
| import ( | ||
| "context" | ||
| "google.golang.org/api/option" | ||
| "fmt" | ||
| "net/http" | ||
| "regexp" | ||
| "time" | ||
|
|
||
| "google.golang.org/api/option" | ||
|
|
||
| "github.com/coreos/pkg/capnslog" | ||
| "google.golang.org/api/compute/v1" | ||
|
|
||
|
|
@@ -35,7 +38,7 @@ var ( | |
| type Options struct { | ||
| Image string | ||
| Project string | ||
| Zone string | ||
| PreferredZone string | ||
| MachineType string | ||
| DiskType string | ||
| Network string | ||
|
|
@@ -50,6 +53,68 @@ type API struct { | |
| client *http.Client | ||
| compute *compute.Service | ||
| options *Options | ||
| zones []string | ||
| } | ||
|
|
||
| // This regex should match all standard (non-AI) zones | ||
| // See: https://docs.cloud.google.com/compute/docs/regions-zones | ||
| var standardZoneRegexp = regexp.MustCompile(`^([a-z]+-[a-z]+\d+)-[a-z]$`) | ||
|
|
||
| // zones are in the form "us-central1-a" and the region would be "us-central1" | ||
| // See: https://docs.cloud.google.com/compute/docs/regions-zones | ||
| func extractRegionFromZone(zone string) (string, error) { | ||
| matches := standardZoneRegexp.FindStringSubmatch(zone) | ||
|
angelcerveraroldan marked this conversation as resolved.
|
||
| if matches == nil { | ||
| return "", fmt.Errorf("zone %q does not match expected format {region}-{letter}", zone) | ||
| } | ||
| return matches[1], nil | ||
| } | ||
|
|
||
| func getAvailableZones(computeService *compute.Service, opts *Options) ([]string, error) { | ||
| if opts.MachineType == "" { | ||
| return []string{opts.PreferredZone}, nil | ||
| } | ||
|
|
||
| list, err := computeService.MachineTypes.AggregatedList(opts.Project). | ||
| Filter("name=" + opts.MachineType).Do() | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| targetRegion, err := extractRegionFromZone(opts.PreferredZone) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not extract region from zone %q: %w", opts.PreferredZone, err) | ||
| } | ||
|
|
||
| zones := []string{} | ||
| for _, scopedList := range list.Items { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact, go maps do not specifically iterate in order. So in a way this is kinda a cool outcome due to load spreading across the list of zones. Though if we want order we could sort it after collection of zones. |
||
| // There should be either 1 or 0 MachineTypes | ||
| // 0 if this zone does not have the required machine type | ||
| // 1 if this zone does have the required machine type | ||
| if len(scopedList.MachineTypes) == 0 { | ||
| continue | ||
| } | ||
| if len(scopedList.MachineTypes) > 1 { | ||
| plog.Warningf("Unexpected: got %d machine types for filter name=%s", len(scopedList.MachineTypes), opts.MachineType) | ||
| continue | ||
| } | ||
| zone := scopedList.MachineTypes[0].Zone | ||
| if region, err := extractRegionFromZone(zone); err == nil && region == targetRegion { | ||
| // If the preferred zone can be used, it should be the first zone that we use, | ||
| // so we will make add it to the start of the list, rather than the end. | ||
| if zone == opts.PreferredZone { | ||
| zones = append([]string{zone}, zones...) | ||
| } else { | ||
| zones = append(zones, zone) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(zones) == 0 { | ||
| return zones, fmt.Errorf("no zones in region %s for machine type %s were found", targetRegion, opts.MachineType) | ||
| } | ||
| return zones, nil | ||
| } | ||
|
|
||
| func New(opts *Options) (*API, error) { | ||
|
|
@@ -83,6 +148,12 @@ func New(opts *Options) (*API, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| zones, err := getAvailableZones(computeService, opts) | ||
| if err != nil { | ||
| plog.Warningf("Failed to discover available zones: %v. Falling back to preferred zone (%s) only.", err, opts.PreferredZone) | ||
| zones = []string{opts.PreferredZone} | ||
| } | ||
|
|
||
| if opts.ServiceAcct == "" { | ||
| proj, err := computeService.Projects.Get(opts.Project).Do() | ||
| if err != nil { | ||
|
|
@@ -95,6 +166,7 @@ func New(opts *Options) (*API, error) { | |
| client: client, | ||
| compute: computeService, | ||
| options: opts, | ||
| zones: zones, | ||
| } | ||
|
|
||
| return api, nil | ||
|
|
@@ -105,5 +177,10 @@ func (a *API) Client() *http.Client { | |
| } | ||
|
|
||
| func (a *API) GC(gracePeriod time.Duration) error { | ||
| return a.gcInstances(gracePeriod) | ||
| for _, zone := range a.zones { | ||
| if err := a.gcInstances(gracePeriod, zone); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.