internal/pool: create a common buildlet naming function

The logic for naming buildlets appears in each particular buildlet
pool implementation. This change creates a function which contains
the buildlet naming logic. This is being added before the addition of
a new buildlet pool.

Updates golang/go#36841
Updates golang/go#38337

Change-Id: I8c03f9b513efde14414bcc6d823f3cf1a59c8f70
Reviewed-on: https://go-review.googlesource.com/c/build/+/243337
Run-TryBot: Carlos Amedee <carlos@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
diff --git a/internal/coordinator/pool/gce.go b/internal/coordinator/pool/gce.go
index 39079e6..5efac05 100644
--- a/internal/coordinator/pool/gce.go
+++ b/internal/coordinator/pool/gce.go
@@ -422,7 +422,7 @@
 		deleteIn = deleteTimeout
 	}
 
-	instName := "buildlet-" + strings.TrimPrefix(hostType, "host-") + "-rn" + randHex(7)
+	instName := instanceName(hostType, 7)
 	instName = strings.Replace(instName, "_", "-", -1) // Issue 22905; can't use underscores in GCE VMs
 	p.setInstanceUsed(instName, true)
 
@@ -724,7 +724,6 @@
 			log.Printf("deleting VM %q in zone %q; %s ...", inst.Name, zone, deleteReason)
 			deleteVM(zone, inst.Name)
 		}
-
 	}
 	return nil
 }
diff --git a/internal/coordinator/pool/kube.go b/internal/coordinator/pool/kube.go
index bec2955..7900dbd 100644
--- a/internal/coordinator/pool/kube.go
+++ b/internal/coordinator/pool/kube.go
@@ -261,7 +261,7 @@
 		deleteIn = podDeleteTimeout
 	}
 
-	podName := "buildlet-" + strings.TrimPrefix(hostType, "host-") + "-rn" + randHex(7)
+	podName := instanceName(hostType, 7)
 
 	// Get an estimate for when the pod will be started/running and set
 	// the context timeout based on that
diff --git a/internal/coordinator/pool/pool.go b/internal/coordinator/pool/pool.go
index 03c3a7a..1330a6c 100644
--- a/internal/coordinator/pool/pool.go
+++ b/internal/coordinator/pool/pool.go
@@ -9,6 +9,7 @@
 	"fmt"
 	"log"
 	"math/rand"
+	"strings"
 	"time"
 
 	"golang.org/x/build/buildlet"
@@ -56,3 +57,7 @@
 	d2 := ((d + 50*time.Microsecond) / (100 * time.Microsecond)) * (100 * time.Microsecond)
 	return d2.String()
 }
+
+func instanceName(hostType string, length int) string {
+	return fmt.Sprintf("buildlet-%s-rn%s", strings.TrimPrefix(hostType, "host-"), randHex(length))
+}