-
Notifications
You must be signed in to change notification settings - Fork 14
/
integrator.go
56 lines (47 loc) · 1.42 KB
/
integrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package server
import (
"context"
"time"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
)
// Integrator defines interface to integrate external addon into CKE server.
type Integrator interface {
// StartWatch starts watching etcd until the context is canceled.
//
// It should send an empty struct to the channel when some event occurs.
// To avoid blocking, use select when sending.
//
// If the integrator does not implement StartWatch, simply return nil.
StartWatch(context.Context, chan<- struct{}) error
// Init is called just once when the server becomes a new leader.
Init(ctx context.Context, leaderKey string) error
// Do does something for CKE. leaderKey is an etcd object key that
// exists as long as the current process is the leader.
Do(ctx context.Context, leaderKey string, clusterStatus *cke.ClusterStatus) error
}
// RunIntegrator simply executes Integrator until ctx is canceled.
// This is for debugging.
func RunIntegrator(ctx context.Context, it Integrator) error {
ch := make(chan struct{}, 1)
env := well.NewEnvironment(ctx)
env.Go(func(ctx context.Context) error {
return it.StartWatch(ctx, ch)
})
env.Go(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-ch:
case <-time.After(5 * time.Second):
}
err := it.Do(ctx, cke.KeySabakanTemplate, nil)
if err != nil {
return err
}
}
})
env.Stop()
return env.Wait()
}