Skip to content

Commit c88fdc0

Browse files
committed
all: move services to new Kubernetes cluster
Our makefiles install xb from the repository, so this has to be checked in before I do anything else, and nobody else should deploy anything until I'm done. Because the new cluster is an Autopilot cluster, it lives in a region (us-central1) and that's what you pass to the kubectl command, etc. Move Region/Zone into the individual KubeConfigs and use the correct ones as appropriate. For golang/go#48408. Change-Id: Iceacfe68305a3744aa87ce0fef777b977a252586 Reviewed-on: https://go-review.googlesource.com/c/build/+/350137 Trust: Heschi Kreinick <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent fc41926 commit c88fdc0

File tree

7 files changed

+54
-78
lines changed

7 files changed

+54
-78
lines changed

buildenv/envs.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"math/rand"
1515
"os"
1616
"path/filepath"
17-
"strings"
1817
"sync"
1918

2019
"golang.org/x/oauth2"
@@ -42,13 +41,32 @@ type KubeConfig struct {
4241
// MachineType is the GCE machine type to use for the Kubernetes cluster nodes.
4342
MachineType string
4443

44+
// The zone of the cluster. Autopilot clusters have no single zone.
45+
Zone string
46+
47+
// The region of the cluster.
48+
Region string
49+
4550
// Name is the name of the Kubernetes cluster that will be created.
4651
Name string
4752

4853
// Namespace is the Kubernetes namespace to use within the cluster.
4954
Namespace string
5055
}
5156

57+
// ZoneOrRegion returns the zone or if unset, the region of the cluster.
58+
// This is the string to use as the "zone" of the cluster when connecting to it
59+
// with the Kubernetes API.
60+
func (kc KubeConfig) ZoneOrRegion() string {
61+
if kc.Zone != "" {
62+
return kc.Zone
63+
}
64+
if kc.Region != "" {
65+
return kc.Region
66+
}
67+
panic(fmt.Sprintf("KubeConfig has neither zone nor region: %#v", kc))
68+
}
69+
5270
// Environment describes the configuration of the infrastructure for a
5371
// coordinator and its buildlet resources running on Google Cloud Platform.
5472
// Staging and Production are the two common build environments.
@@ -72,11 +90,6 @@ type Environment struct {
7290
// disabled and the coordinator serves on 8119.
7391
IsProd bool
7492

75-
// ControlZone is the GCE zone that the coordinator instance and Kubernetes cluster
76-
// will run in. This field may be overridden as necessary without impacting
77-
// other fields.
78-
ControlZone string
79-
8093
// VMZones are the GCE zones that the VMs will be deployed to. These
8194
// GCE zones will be periodically cleaned by deleting old VMs. The zones
8295
// should all exist within a single region.
@@ -90,10 +103,10 @@ type Environment struct {
90103
// MachineType is the GCE machine type to use for the coordinator.
91104
MachineType string
92105

93-
// KubeBuild is the Kubernetes config for the buildlet cluster.
106+
// KubeBuild is the cluster that runs buildlets.
94107
KubeBuild KubeConfig
95-
// KubeTools is the Kubernetes config for the tools cluster.
96-
KubeTools KubeConfig
108+
// KubeServices is the cluster that runs the coordinator and other services.
109+
KubeServices KubeConfig
97110

98111
// PreferContainersOnCOS controls whether we do most builds on
99112
// Google's Container-Optimized OS Linux image running on a VM
@@ -154,19 +167,10 @@ func (e Environment) ComputePrefix() string {
154167
}
155168

156169
// RandomVMZone returns a randomly selected zone from the zones in VMZones.
157-
// The Zone value will be returned if VMZones is not set.
158170
func (e Environment) RandomVMZone() string {
159-
if len(e.VMZones) == 0 {
160-
return e.ControlZone
161-
}
162171
return e.VMZones[rand.Intn(len(e.VMZones))]
163172
}
164173

165-
// Region returns the GCE region, derived from its zone.
166-
func (e Environment) Region() string {
167-
return e.ControlZone[:strings.LastIndex(e.ControlZone, "-")]
168-
}
169-
170174
// SnapshotURL returns the absolute URL of the .tar.gz containing a
171175
// built Go tree for the builderType and Go rev (40 character Git
172176
// commit hash). The tarball is suitable for passing to
@@ -248,20 +252,23 @@ var Staging = &Environment{
248252
ProjectNumber: 302018677728,
249253
GoProjectName: "go-dashboard-dev",
250254
IsProd: true,
251-
ControlZone: "us-central1-f",
252255
VMZones: []string{"us-central1-a", "us-central1-b", "us-central1-c", "us-central1-f"},
253256
StaticIP: "104.154.113.235",
254257
MachineType: "n1-standard-1",
255258
PreferContainersOnCOS: true,
256259
KubeBuild: KubeConfig{
257260
MinNodes: 1,
258261
MaxNodes: 1, // auto-scaling disabled
262+
Zone: "us-central1-f",
263+
Region: "us-central1",
259264
Name: "buildlets",
260265
MachineType: "n1-standard-4", // only used for make.bash due to PreferContainersOnCOS
261266
},
262-
KubeTools: KubeConfig{
267+
KubeServices: KubeConfig{
263268
MinNodes: 3,
264269
MaxNodes: 3,
270+
Zone: "us-central1-f",
271+
Region: "us-central1",
265272
Name: "go",
266273
MachineType: "n1-standard-4",
267274
Namespace: "default",
@@ -284,21 +291,23 @@ var Production = &Environment{
284291
ProjectNumber: 872405196845,
285292
GoProjectName: "golang-org",
286293
IsProd: true,
287-
ControlZone: "us-central1-f",
288294
VMZones: []string{"us-central1-a", "us-central1-b", "us-central1-c", "us-central1-f"},
289295
StaticIP: "107.178.219.46",
290296
MachineType: "n1-standard-4",
291297
PreferContainersOnCOS: true,
292298
KubeBuild: KubeConfig{
293299
MinNodes: 2,
294300
MaxNodes: 2, // auto-scaling disabled
301+
Zone: "us-central1-f",
302+
Region: "us-central1",
295303
Name: "buildlets",
296304
MachineType: "n1-standard-4", // only used for make.bash due to PreferContainersOnCOS
297305
},
298-
KubeTools: KubeConfig{
306+
KubeServices: KubeConfig{
299307
MinNodes: 4,
300308
MaxNodes: 4,
301-
Name: "go",
309+
Region: "us-central1",
310+
Name: "services",
302311
MachineType: "n1-standard-4",
303312
Namespace: "prod",
304313
},

buildenv/envs_test.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,13 @@ import (
99
)
1010

1111
func TestEnvironmentNextZone(t *testing.T) {
12-
testCases := []struct {
13-
name string
14-
env Environment
15-
wantOneOf []string // desired zone should appear in this slice
16-
}{
17-
{
18-
name: "zones-not-set",
19-
env: Environment{
20-
ControlZone: "kentucky",
21-
VMZones: []string{},
22-
},
23-
wantOneOf: []string{"kentucky"},
24-
},
25-
{
26-
name: "zone-and-zones-set",
27-
env: Environment{
28-
ControlZone: "kentucky",
29-
VMZones: []string{"texas", "california", "washington"},
30-
},
31-
32-
wantOneOf: []string{"texas", "california", "washington"},
33-
},
34-
{
35-
name: "zones-only-contains-one-entry",
36-
env: Environment{
37-
ControlZone: "kentucky",
38-
VMZones: []string{"texas"},
39-
},
40-
wantOneOf: []string{"texas"},
41-
},
12+
env := Environment{
13+
VMZones: []string{"texas", "california", "washington"},
4214
}
43-
for _, tc := range testCases {
44-
t.Run(tc.name, func(t *testing.T) {
45-
got := tc.env.RandomVMZone()
46-
if !containsString(got, tc.wantOneOf) {
47-
t.Errorf("got=%q; want %v", got, tc.wantOneOf)
48-
}
49-
})
15+
wantOneOf := []string{"texas", "california", "washington"}
16+
got := env.RandomVMZone()
17+
if !containsString(got, wantOneOf) {
18+
t.Errorf("got=%q; want %v", got, wantOneOf)
5019
}
5120
}
5221

cmd/gcpinit/gcpinit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ resources:
3232
- name: "{{ .Kube.Name }}"
3333
type: container.v1.cluster
3434
properties:
35-
zone: "{{ .Env.ControlZone }}"
35+
zone: "{{ .Env.KubeServices.Zone }}"
3636
cluster:
3737
initial_node_count: {{ .Kube.MinNodes }}
3838
network: "default"
@@ -77,7 +77,7 @@ func main() {
7777
log.Fatalf("could not create client: %v", err)
7878
}
7979

80-
for _, c := range []*buildenv.KubeConfig{&buildEnv.KubeBuild, &buildEnv.KubeTools} {
80+
for _, c := range []*buildenv.KubeConfig{&buildEnv.KubeBuild, &buildEnv.KubeServices} {
8181
err := createCluster(bgc, c)
8282
if err != nil {
8383
log.Fatalf("Error creating Kubernetes cluster %q: %v", c.Name, err)

cmd/xb/xb.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,14 @@ func main() {
5656
case "kubectl":
5757
env := getEnv()
5858
curCtx := kubeCurrentContext()
59-
wantCtx := fmt.Sprintf("gke_%s_%s_go", env.ProjectName, env.ControlZone)
59+
wantCtx := fmt.Sprintf("gke_%s_%s_%s", env.ProjectName, env.KubeServices.ZoneOrRegion(), env.KubeServices.Name)
6060
if curCtx != wantCtx {
6161
log.SetFlags(0)
62-
log.Fatalf("Wrong kubectl context; currently using %q; want %q\nRun:\n gcloud container clusters get-credentials --project=%s --zone=%s go",
62+
log.Fatalf("Wrong kubectl context; currently using %q; want %q\nRun:\n gcloud container clusters get-credentials --project=%s --zone=%s %s",
6363
curCtx, wantCtx,
64-
env.ProjectName, env.ControlZone,
64+
env.ProjectName, env.KubeServices.ZoneOrRegion(), env.KubeServices.Name,
6565
)
6666
}
67-
// gcloud container clusters get-credentials --zone=us-central1-f go
68-
// gcloud container clusters get-credentials --zone=us-central1-f buildlets
6967
runCmd()
7068
case "docker":
7169
runDocker()

internal/buildgo/basepin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (c *Client) MakeBasepinDisks(ctx context.Context) error {
8282
delete(need, d.SourceImage)
8383
continue
8484
}
85-
if zone != c.Env.ControlZone {
85+
if zone != c.Env.KubeBuild.Zone {
8686
log.Printf("basepin: deleting unnecessary disk %v in zone %v", d.Name, zone)
8787
op, err := svc.Disks.Delete(c.Env.ProjectName, zone, d.Name).Do()
8888
if err != nil {

internal/coordinator/pool/gce.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func InitGCE(sc *secret.Client, vmDeleteTimeout time.Duration, tFiles map[string
133133

134134
// Convert the zone from "projects/1234/zones/us-central1-a" to "us-central1-a".
135135
projectZone = path.Base(projectZone)
136-
buildEnv.ControlZone = projectZone
136+
buildEnv.KubeBuild.Zone = projectZone
137137

138138
if buildEnv.StaticIP == "" {
139139
buildEnv.StaticIP, err = metadata.ExternalIP()
@@ -349,9 +349,9 @@ func (p *GCEBuildlet) pollQuotaLoop() {
349349

350350
func (p *GCEBuildlet) pollQuota() {
351351
gceAPIGate()
352-
reg, err := computeService.Regions.Get(buildEnv.ProjectName, buildEnv.Region()).Do()
352+
reg, err := computeService.Regions.Get(buildEnv.ProjectName, buildEnv.KubeBuild.Region).Do()
353353
if err != nil {
354-
log.Printf("Failed to get quota for %s/%s: %v", buildEnv.ProjectName, buildEnv.Region(), err)
354+
log.Printf("Failed to get quota for %s/%s: %v", buildEnv.ProjectName, buildEnv.KubeBuild.Region, err)
355355
return
356356
}
357357
p.mu.Lock()

internal/coordinator/pool/kube.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ func InitKube(monitorGitMirror MonitorGitMirrorFunc) error {
7474
var err error
7575
buildletsKubeClient, err = gke.NewClient(ctx,
7676
gceBuildEnv.KubeBuild.Name,
77-
gke.OptZone(gceBuildEnv.ControlZone),
77+
gke.OptZone(gceBuildEnv.KubeBuild.ZoneOrRegion()),
7878
gke.OptProject(gceBuildEnv.ProjectName),
7979
gke.OptTokenSource(gce.GCPCredentials().TokenSource))
8080
if err != nil {
8181
return err
8282
}
8383

8484
goKubeClient, err = gke.NewClient(ctx,
85-
gceBuildEnv.KubeTools.Name,
86-
gke.OptNamespace(gceBuildEnv.KubeTools.Namespace),
87-
gke.OptZone(gceBuildEnv.ControlZone),
85+
gceBuildEnv.KubeServices.Name,
86+
gke.OptNamespace(gceBuildEnv.KubeServices.Namespace),
87+
gke.OptZone(gceBuildEnv.KubeServices.ZoneOrRegion()),
8888
gke.OptProject(gceBuildEnv.ProjectName),
8989
gke.OptTokenSource(gce.GCPCredentials().TokenSource))
9090
if err != nil {
@@ -172,12 +172,12 @@ func (p *kubeBuildletPool) pollCapacity(ctx context.Context) {
172172
gceBuildEnv := NewGCEConfiguration().BuildEnv()
173173
nodes, err := buildletsKubeClient.GetNodes(ctx)
174174
if err != nil {
175-
log.Printf("failed to retrieve nodes to calculate cluster capacity for %s/%s: %v", gceBuildEnv.ProjectName, gceBuildEnv.Region(), err)
175+
log.Printf("failed to retrieve nodes to calculate cluster capacity for %s/%s: %v", gceBuildEnv.ProjectName, gceBuildEnv.KubeBuild.Region, err)
176176
return
177177
}
178178
pods, err := buildletsKubeClient.GetPods(ctx)
179179
if err != nil {
180-
log.Printf("failed to retrieve pods to calculate cluster capacity for %s/%s: %v", gceBuildEnv.ProjectName, gceBuildEnv.Region(), err)
180+
log.Printf("failed to retrieve pods to calculate cluster capacity for %s/%s: %v", gceBuildEnv.ProjectName, gceBuildEnv.KubeBuild.Region, err)
181181
return
182182
}
183183

@@ -474,7 +474,7 @@ func (p *kubeBuildletPool) cleanUpOldPods(ctx context.Context) {
474474
}
475475
if err == nil && time.Now().Unix() > unixDeadline {
476476
stats.DeletedOld++
477-
log.Printf("cleanUpOldPods: Deleting expired pod %q in zone %q ...", pod.Name, NewGCEConfiguration().BuildEnv().ControlZone)
477+
log.Printf("cleanUpOldPods: Deleting expired pod %q...", pod.Name)
478478
err = buildletsKubeClient.DeletePod(ctx, pod.Name)
479479
if err != nil {
480480
log.Printf("cleanUpOldPods: problem deleting old pod %q: %v", pod.Name, err)

0 commit comments

Comments
 (0)