diff --git a/cli/internal/gcp/client/client.go b/cli/internal/gcp/client/client.go index 5ad43e0ec..d361dec8f 100644 --- a/cli/internal/gcp/client/client.go +++ b/cli/internal/gcp/client/client.go @@ -324,6 +324,23 @@ func (c *Client) generateUID() (string, error) { return string(uid), nil } +// buildInstanceName returns a formatted name string. +// The names are joined with a '-'. +// If names is empty, the returned value is c.name + "-" + c.uid. +func (c *Client) buildResourceName(names ...string) string { + builder := strings.Builder{} + + builder.WriteString(c.name) + builder.WriteRune('-') + for _, name := range names { + builder.WriteString(name) + builder.WriteRune('-') + } + builder.WriteString(c.uid) + + return builder.String() +} + func (c *Client) resourceURI(scope resourceScope, resourceType, resourceName string) string { const baseURI = "https://www.googleapis.com/compute/v1/projects/" @@ -338,17 +355,17 @@ func (c *Client) resourceURI(scope resourceScope, resourceType, resourceName str case scopeRegion: builder.WriteString("/regions/") builder.WriteString(c.region) - builder.WriteString("/") + builder.WriteRune('/') case scopeZone: builder.WriteString("/zones/") builder.WriteString(c.zone) - builder.WriteString("/") + builder.WriteRune('/') default: panic("unknown scope") } builder.WriteString(resourceType) - builder.WriteString("/") + builder.WriteRune('/') builder.WriteString(resourceName) return builder.String() diff --git a/cli/internal/gcp/client/client_test.go b/cli/internal/gcp/client/client_test.go index d0d2f479f..7517a1ec3 100644 --- a/cli/internal/gcp/client/client_test.go +++ b/cli/internal/gcp/client/client_test.go @@ -123,6 +123,48 @@ func TestInit(t *testing.T) { assert.Equal("name", client.name) } +func TestBuildResourceName(t *testing.T) { + testCases := map[string]struct { + clientUID string + clientName string + names []string + wantName string + }{ + "no names": { + clientUID: "uid", + clientName: "name", + wantName: "name-uid", + }, + "one name": { + clientUID: "uid", + clientName: "name", + names: []string{"foo"}, + wantName: "name-foo-uid", + }, + "two names": { + clientUID: "uid", + clientName: "name", + names: []string{"foo", "bar"}, + wantName: "name-foo-bar-uid", + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert := assert.New(t) + + client := Client{ + name: tc.clientName, + uid: tc.clientUID, + } + + name := client.buildResourceName(tc.names...) + + assert.Equal(tc.wantName, name) + }) + } +} + func TestResourceURI(t *testing.T) { testCases := map[string]struct { scope resourceScope diff --git a/cli/internal/gcp/client/instances.go b/cli/internal/gcp/client/instances.go index 0bc50d1bd..3646c1a17 100644 --- a/cli/internal/gcp/client/instances.go +++ b/cli/internal/gcp/client/instances.go @@ -27,7 +27,7 @@ func (c *Client) CreateInstances(ctx context.Context, input CreateInstancesInput ops := []Operation{} workerTemplateInput := insertInstanceTemplateInput{ - Name: c.name + "-worker-" + c.uid, + Name: c.buildResourceName("worker"), Network: c.network, SecondarySubnetworkRangeName: c.secondarySubnetworkRange, Subnetwork: c.subnetwork, @@ -50,7 +50,7 @@ func (c *Client) CreateInstances(ctx context.Context, input CreateInstancesInput c.workerTemplate = workerTemplateInput.Name controlPlaneTemplateInput := insertInstanceTemplateInput{ - Name: c.name + "-control-plane-" + c.uid, + Name: c.buildResourceName("control-plane"), Network: c.network, Subnetwork: c.subnetwork, SecondarySubnetworkRangeName: c.secondarySubnetworkRange, diff --git a/cli/internal/gcp/client/loadbalancer.go b/cli/internal/gcp/client/loadbalancer.go index d07fa04ae..7bf59d08c 100644 --- a/cli/internal/gcp/client/loadbalancer.go +++ b/cli/internal/gcp/client/loadbalancer.go @@ -39,7 +39,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error { // c.loadbalancers = append(c.loadbalancers, &loadBalancer{ - name: c.name + "-" + "kube" + "-" + c.uid, + name: c.buildResourceName("kube"), ip: c.loadbalancerIP, frontendPort: constants.KubernetesPort, backendPortName: "kubernetes", @@ -48,7 +48,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error { }) c.loadbalancers = append(c.loadbalancers, &loadBalancer{ - name: c.name + "-" + "boot" + "-" + c.uid, + name: c.buildResourceName("boot"), ip: c.loadbalancerIPname, frontendPort: constants.BootstrapperPort, backendPortName: "bootstrapper", @@ -56,7 +56,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error { }) c.loadbalancers = append(c.loadbalancers, &loadBalancer{ - name: c.name + "-" + "verify" + "-" + c.uid, + name: c.buildResourceName("verify"), ip: c.loadbalancerIPname, frontendPort: constants.VerifyServiceNodePortGRPC, backendPortName: "verify", @@ -64,7 +64,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error { }) c.loadbalancers = append(c.loadbalancers, &loadBalancer{ - name: c.name + "-" + "debugd" + "-" + c.uid, + name: c.buildResourceName("debugd"), ip: c.loadbalancerIPname, frontendPort: constants.DebugdPort, backendPortName: "debugd", @@ -350,7 +350,7 @@ func (c *Client) terminateHealthCheck(ctx context.Context, lb *loadBalancer) err } func (c *Client) createIPAddr(ctx context.Context) error { - ipName := c.name + "-" + c.uid + ipName := c.buildResourceName() insertReq := &computepb.InsertAddressRequest{ Project: c.project, Region: c.region, @@ -365,7 +365,7 @@ func (c *Client) createIPAddr(ctx context.Context) error { if err := c.waitForOperations(ctx, []Operation{op}); err != nil { return err } - c.loadbalancerIPname = c.name + "-" + c.uid + c.loadbalancerIPname = ipName getReq := &computepb.GetAddressRequest{ Project: c.project, diff --git a/cli/internal/gcp/client/network.go b/cli/internal/gcp/client/network.go index 2b6bc3d53..ddcf04147 100644 --- a/cli/internal/gcp/client/network.go +++ b/cli/internal/gcp/client/network.go @@ -88,7 +88,7 @@ type FirewallInput struct { // CreateVPCs creates all necessary VPC networks. func (c *Client) CreateVPCs(ctx context.Context) error { - c.network = c.name + "-" + c.uid + c.network = c.buildResourceName() op, err := c.createVPC(ctx, c.network) if err != nil {