From 7a2ea0abff638dcd76adf852e4ece64e08d01234 Mon Sep 17 00:00:00 2001 From: Nishad Mathur Date: Tue, 6 Jun 2023 17:47:52 -0700 Subject: [PATCH] Add support for regional non-autopilot clusters --- gcp/container/client.go | 4 ++-- gcp/container/cluster.go | 16 ++++++++-------- gcp/container/clusters.go | 4 ++-- gcp/container/operation_waiter.go | 13 +++++++++---- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/gcp/container/client.go b/gcp/container/client.go index ef2ba676..51a589ad 100644 --- a/gcp/container/client.go +++ b/gcp/container/client.go @@ -30,8 +30,8 @@ func (c client) ListClusters() (*gcpcontainer.ListClustersResponse, error) { return c.containers.List(parent).Do() } -func (c client) DeleteCluster(zone string, cluster string) error { - name := fmt.Sprintf("projects/%v/locations/%v/clusters/%v", c.project, zone, cluster) +func (c client) DeleteCluster(location string, cluster string) error { + name := fmt.Sprintf("projects/%v/locations/%v/clusters/%v", c.project, location, cluster) return c.wait(c.containers.Delete(name)) } diff --git a/gcp/container/cluster.go b/gcp/container/cluster.go index 7b53e083..acfe7ef7 100644 --- a/gcp/container/cluster.go +++ b/gcp/container/cluster.go @@ -3,21 +3,21 @@ package container import "fmt" type Cluster struct { - name string - zone string - client clustersClient + name string + location string + client clustersClient } -func NewCluster(client clustersClient, zone string, name string) Cluster { +func NewCluster(client clustersClient, location string, name string) Cluster { return Cluster{ - name: name, - zone: zone, - client: client, + name: name, + location: location, + client: client, } } func (c Cluster) Delete() error { - err := c.client.DeleteCluster(c.zone, c.name) + err := c.client.DeleteCluster(c.location, c.name) if err != nil { return fmt.Errorf("Delete: %s", err) } diff --git a/gcp/container/clusters.go b/gcp/container/clusters.go index adc2f0f4..c1cbcccd 100644 --- a/gcp/container/clusters.go +++ b/gcp/container/clusters.go @@ -17,7 +17,7 @@ type Clusters struct { //go:generate faux --interface clustersClient --output fakes/clusters_client.go type clustersClient interface { ListClusters() (*gcpcontainer.ListClustersResponse, error) - DeleteCluster(zone, cluster string) error + DeleteCluster(location, cluster string) error } func NewClusters(client clustersClient, zones map[string]string, logger logger) Clusters { @@ -38,7 +38,7 @@ func (c Clusters) List(filter string) ([]common.Deletable, error) { deletables := []common.Deletable{} for _, cluster := range clusters { - resource := NewCluster(c.client, cluster.Zone, cluster.Name) + resource := NewCluster(c.client, cluster.Location, cluster.Name) if !strings.Contains(resource.Name(), filter) { continue diff --git a/gcp/container/operation_waiter.go b/gcp/container/operation_waiter.go index fc1713c4..be4033f3 100644 --- a/gcp/container/operation_waiter.go +++ b/gcp/container/operation_waiter.go @@ -2,7 +2,6 @@ package container import ( "fmt" - "github.com/genevieve/leftovers/gcp/common" gcpcontainer "google.golang.org/api/container/v1" @@ -10,7 +9,7 @@ import ( type operationWaiter struct { op *gcpcontainer.Operation - service *gcpcontainer.ProjectsZonesService + service *gcpcontainer.ProjectsLocationsService project string logger logger } @@ -18,7 +17,7 @@ type operationWaiter struct { func NewOperationWaiter(op *gcpcontainer.Operation, service *gcpcontainer.Service, project string, logger logger) operationWaiter { return operationWaiter{ op: op, - service: service.Projects.Zones, + service: service.Projects.Locations, project: project, logger: logger, } @@ -42,8 +41,14 @@ func (w *operationWaiter) Wait() error { func (c *operationWaiter) refreshFunc() common.StateRefreshFunc { return func() (interface{}, string, error) { - op, err := c.service.Operations.Get(c.project, c.op.Zone, c.op.Name).Do() + location := c.op.Location + if location == "" { + // For some reason `c.op.Location` can be empty here despite this being a regional cluster + location = c.op.Zone + } + name := fmt.Sprintf("projects/%v/locations/%v/operations/%v", c.project, location, c.op.Name) + op, err := c.service.Operations.Get(name).Do() if err != nil { return nil, "", fmt.Errorf("Refreshing operation request: %s", err) }