Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
richardchen331 committed Jan 26, 2023
1 parent 69b509f commit 529a6a2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"strings"

"sigs.k8s.io/cluster-api-provider-gcp/util/location"

"google.golang.org/api/option"

"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -209,8 +211,8 @@ func parseLocation(location string) string {

// Region returns the region of the GKE cluster.
func (s *ManagedControlPlaneScope) Region() string {
region := parseLocation(s.GCPManagedControlPlane.Spec.Location)
return region
loc, _ := location.Parse(s.GCPManagedControlPlane.Spec.Location)
return loc.Region
}

// ClusterLocation returns the location of the cluster.
Expand Down
6 changes: 4 additions & 2 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"

"sigs.k8s.io/cluster-api-provider-gcp/util/location"

"google.golang.org/api/option"
"sigs.k8s.io/cluster-api/util/conditions"

Expand Down Expand Up @@ -208,8 +210,8 @@ func (s *ManagedMachinePoolScope) NodePoolName() string {

// Region returns the region of the GKE node pool.
func (s *ManagedMachinePoolScope) Region() string {
region := parseLocation(s.GCPManagedControlPlane.Spec.Location)
return region
loc, _ := location.Parse(s.GCPManagedControlPlane.Spec.Location)
return loc.Region
}

// NodePoolLocation returns the location of the node pool.
Expand Down
50 changes: 50 additions & 0 deletions util/location/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package location implements location parsing utilities.
package location

import (
"strings"

"github.com/pkg/errors"
)

// Location captures the region and zone of a GCP location.
// Examples of GCP location:
// us-central1 (region).
// us-central1-c (region with zone).
type Location struct {
Region string
Zone *string
}

// Parse parses a location string.
func Parse(location string) (Location, error) {
parts := strings.Split(location, "-")
if len(parts) < 2 {
return Location{}, errors.New("invalid location")
}
region := strings.Join(parts[:2], "-")
var zone *string
if len(parts) == 3 {
zone = &parts[2]
}
return Location{
Region: region,
Zone: zone,
}, nil
}

0 comments on commit 529a6a2

Please sign in to comment.