5
5
// +build go1.13
6
6
// +build linux darwin
7
7
8
- package main
8
+ package pool
9
9
10
10
import (
11
11
"context"
@@ -22,7 +22,6 @@ import (
22
22
23
23
"golang.org/x/build/buildlet"
24
24
"golang.org/x/build/dashboard"
25
- "golang.org/x/build/internal/coordinator/pool"
26
25
"golang.org/x/build/internal/sourcecache"
27
26
"golang.org/x/build/kubernetes"
28
27
"golang.org/x/build/kubernetes/api"
@@ -34,6 +33,15 @@ import (
34
33
This file implements the Kubernetes-based buildlet pool.
35
34
*/
36
35
36
+ const (
37
+ // podDeleteTimeout is how long before we delete a VM.
38
+ // In practice this need only be as long as the slowest
39
+ // builder (plan9 currently), because on startup this program
40
+ // already deletes all buildlets it doesn't know about
41
+ // (i.e. ones from a previous instance of the coordinator).
42
+ podDeleteTimeout = 45 * time .Minute
43
+ )
44
+
37
45
// Initialized by initKube:
38
46
var (
39
47
buildletsKubeClient * kubernetes.Client // for "buildlets" cluster
@@ -43,14 +51,17 @@ var (
43
51
kubeCluster * container.Cluster
44
52
)
45
53
46
- // initGCE must be called before initKube
47
- func initKube () error {
48
- if pool .GCEBuildEnv ().KubeBuild .MaxNodes == 0 {
54
+ // MonitorGitMirrorFunc defines a function used to monitor gitmirror.
55
+ type MonitorGitMirrorFunc func ()
56
+
57
+ // InitGCE must be called before initKube
58
+ func InitKube (monitorGitMirror MonitorGitMirrorFunc ) error {
59
+ if GCEBuildEnv ().KubeBuild .MaxNodes == 0 {
49
60
return errors .New ("Kubernetes builders disabled due to KubeBuild.MaxNodes == 0" )
50
61
}
51
62
52
63
// projectID was set by initGCE
53
- registryPrefix += "/" + pool . GCEBuildEnv ().ProjectName
64
+ registryPrefix += "/" + GCEBuildEnv ().ProjectName
54
65
if ! hasCloudPlatformScope () {
55
66
return errors .New ("coordinator not running with access to the Cloud Platform scope." )
56
67
}
@@ -59,19 +70,19 @@ func initKube() error {
59
70
defer cancel () // ctx is only used for discovery and connect; not retained.
60
71
var err error
61
72
buildletsKubeClient , err = gke .NewClient (ctx ,
62
- pool . GCEBuildEnv ().KubeBuild .Name ,
63
- gke .OptZone (pool . GCEBuildEnv ().ControlZone ),
64
- gke .OptProject (pool . GCEBuildEnv ().ProjectName ),
65
- gke .OptTokenSource (pool . GCPCredentials ().TokenSource ))
73
+ GCEBuildEnv ().KubeBuild .Name ,
74
+ gke .OptZone (GCEBuildEnv ().ControlZone ),
75
+ gke .OptProject (GCEBuildEnv ().ProjectName ),
76
+ gke .OptTokenSource (GCPCredentials ().TokenSource ))
66
77
if err != nil {
67
78
return err
68
79
}
69
80
70
81
goKubeClient , err = gke .NewClient (ctx ,
71
- pool . GCEBuildEnv ().KubeTools .Name ,
72
- gke .OptZone (pool . GCEBuildEnv ().ControlZone ),
73
- gke .OptProject (pool . GCEBuildEnv ().ProjectName ),
74
- gke .OptTokenSource (pool . GCPCredentials ().TokenSource ))
82
+ GCEBuildEnv ().KubeTools .Name ,
83
+ gke .OptZone (GCEBuildEnv ().ControlZone ),
84
+ gke .OptProject (GCEBuildEnv ().ProjectName ),
85
+ gke .OptTokenSource (GCPCredentials ().TokenSource ))
75
86
if err != nil {
76
87
return err
77
88
}
@@ -85,6 +96,26 @@ func initKube() error {
85
96
return nil
86
97
}
87
98
99
+ // KubeSetErr sets the kube error to passed in value.
100
+ func KubeSetErr (err error ) {
101
+ kubeErr = err
102
+ }
103
+
104
+ // KubeErr retrieves the kube error value.
105
+ func KubeErr () error {
106
+ return kubeErr
107
+ }
108
+
109
+ // KubePool returns the kube buildlet pool.
110
+ func KubePool () * kubeBuildletPool {
111
+ return kubePool
112
+ }
113
+
114
+ // KubeGoClient retrieves a kube client for the go cluster.
115
+ func KubeGoClient () * kubernetes.Client {
116
+ return goKubeClient
117
+ }
118
+
88
119
// kubeBuildletPool is the Kubernetes buildlet pool.
89
120
type kubeBuildletPool struct {
90
121
mu sync.Mutex // guards all following
@@ -136,12 +167,12 @@ func (p *kubeBuildletPool) pollCapacityLoop() {
136
167
func (p * kubeBuildletPool ) pollCapacity (ctx context.Context ) {
137
168
nodes , err := buildletsKubeClient .GetNodes (ctx )
138
169
if err != nil {
139
- log .Printf ("failed to retrieve nodes to calculate cluster capacity for %s/%s: %v" , pool . GCEBuildEnv ().ProjectName , pool . GCEBuildEnv ().Region (), err )
170
+ log .Printf ("failed to retrieve nodes to calculate cluster capacity for %s/%s: %v" , GCEBuildEnv ().ProjectName , GCEBuildEnv ().Region (), err )
140
171
return
141
172
}
142
173
pods , err := buildletsKubeClient .GetPods (ctx )
143
174
if err != nil {
144
- log .Printf ("failed to retrieve pods to calculate cluster capacity for %s/%s: %v" , pool . GCEBuildEnv ().ProjectName , pool . GCEBuildEnv ().Region (), err )
175
+ log .Printf ("failed to retrieve pods to calculate cluster capacity for %s/%s: %v" , GCEBuildEnv ().ProjectName , GCEBuildEnv ().Region (), err )
145
176
return
146
177
}
147
178
@@ -210,7 +241,7 @@ func (p *kubeBuildletPool) pollCapacity(ctx context.Context) {
210
241
211
242
}
212
243
213
- func (p * kubeBuildletPool ) GetBuildlet (ctx context.Context , hostType string , lg pool. Logger ) (* buildlet.Client , error ) {
244
+ func (p * kubeBuildletPool ) GetBuildlet (ctx context.Context , hostType string , lg Logger ) (* buildlet.Client , error ) {
214
245
hconf , ok := dashboard .Hosts [hostType ]
215
246
if ! ok || ! hconf .IsContainer () {
216
247
return nil , fmt .Errorf ("kubepool: invalid host type %q" , hostType )
@@ -222,7 +253,7 @@ func (p *kubeBuildletPool) GetBuildlet(ctx context.Context, hostType string, lg
222
253
panic ("expect non-nil buildletsKubeClient" )
223
254
}
224
255
225
- deleteIn , ok := ctx .Value (pool. BuildletTimeoutOpt {}).(time.Duration )
256
+ deleteIn , ok := ctx .Value (BuildletTimeoutOpt {}).(time.Duration )
226
257
if ! ok {
227
258
deleteIn = podDeleteTimeout
228
259
}
@@ -237,7 +268,7 @@ func (p *kubeBuildletPool) GetBuildlet(ctx context.Context, hostType string, lg
237
268
log .Printf ("Creating Kubernetes pod %q for %s" , podName , hostType )
238
269
239
270
bc , err := buildlet .StartPod (ctx , buildletsKubeClient , podName , hostType , buildlet.PodOpts {
240
- ProjectID : pool . GCEBuildEnv ().ProjectName ,
271
+ ProjectID : GCEBuildEnv ().ProjectName ,
241
272
ImageRegistry : registryPrefix ,
242
273
Description : fmt .Sprintf ("Go Builder for %s" , hostType ),
243
274
DeleteIn : deleteIn ,
@@ -354,16 +385,16 @@ func (p *kubeBuildletPool) podUsed(podName string) bool {
354
385
return ok
355
386
}
356
387
357
- func (p * kubeBuildletPool ) podsActive () (ret []pool. ResourceTime ) {
388
+ func (p * kubeBuildletPool ) podsActive () (ret []ResourceTime ) {
358
389
p .mu .Lock ()
359
390
defer p .mu .Unlock ()
360
391
for name , ph := range p .pods {
361
- ret = append (ret , pool. ResourceTime {
392
+ ret = append (ret , ResourceTime {
362
393
Name : name ,
363
394
Creation : ph .requestedAt ,
364
395
})
365
396
}
366
- sort .Sort (pool . ByCreationTime (ret ))
397
+ sort .Sort (ByCreationTime (ret ))
367
398
return ret
368
399
}
369
400
@@ -376,7 +407,7 @@ func (p *kubeBuildletPool) String() string {
376
407
return fmt .Sprintf ("Kubernetes pool capacity: %d/%d" , inUse , total )
377
408
}
378
409
379
- // cleanUpOldPods loops forever and periodically enumerates pods
410
+ // CleanUpOldPods loops forever and periodically enumerates pods
380
411
// and deletes those which have expired.
381
412
//
382
413
// A Pod is considered expired if it has a "delete-at" metadata
@@ -389,7 +420,7 @@ func (p *kubeBuildletPool) String() string {
389
420
// stranded and wasting resources forever, we instead set the
390
421
// "delete-at" metadata attribute on them when created to some time
391
422
// that's well beyond their expected lifetime.
392
- func (p * kubeBuildletPool ) cleanUpOldPodsLoop (ctx context.Context ) {
423
+ func (p * kubeBuildletPool ) CleanUpOldPodsLoop (ctx context.Context ) {
393
424
if buildletsKubeClient == nil {
394
425
log .Printf ("cleanUpOldPods: no buildletsKubeClient configured; aborting." )
395
426
return
@@ -438,7 +469,7 @@ func (p *kubeBuildletPool) cleanUpOldPods(ctx context.Context) {
438
469
}
439
470
if err == nil && time .Now ().Unix () > unixDeadline {
440
471
stats .DeletedOld ++
441
- log .Printf ("cleanUpOldPods: Deleting expired pod %q in zone %q ..." , pod .Name , pool . GCEBuildEnv ().ControlZone )
472
+ log .Printf ("cleanUpOldPods: Deleting expired pod %q in zone %q ..." , pod .Name , GCEBuildEnv ().ControlZone )
442
473
err = buildletsKubeClient .DeletePod (ctx , pod .Name )
443
474
if err != nil {
444
475
log .Printf ("cleanUpOldPods: problem deleting old pod %q: %v" , pod .Name , err )
@@ -468,5 +499,5 @@ func (p *kubeBuildletPool) cleanUpOldPods(ctx context.Context) {
468
499
}
469
500
470
501
func hasCloudPlatformScope () bool {
471
- return pool . HasScope (container .CloudPlatformScope )
502
+ return HasScope (container .CloudPlatformScope )
472
503
}
0 commit comments