blob: 1251f2da33c94b79cf44c18663196c3224825c51 [file] [log] [blame]
Serge Bazanskia959cbd2021-06-17 15:56:51 +02001package cluster
2
3import (
4 "context"
5 "fmt"
6
7 "source.monogon.dev/metropolis/pkg/event"
8 cpb "source.monogon.dev/metropolis/proto/common"
9)
10
11type Watcher struct {
12 event.Watcher
13}
14
15func (w *Watcher) Get(ctx context.Context) (*Status, error) {
16 val, err := w.Watcher.Get(ctx)
17 if err != nil {
18 return nil, err
19 }
20 status := val.(Status)
21 return &status, err
22}
23
24// GetHome waits until the cluster, from the point of view of this node, is in
25// the ClusterHome state. This can be used to wait for the cluster manager to
26// 'settle', before clients start more node services.
27func (w *Watcher) GetHome(ctx context.Context) (*Status, error) {
28 for {
29 status, err := w.Get(ctx)
30 if err != nil {
31 return nil, err
32 }
33 switch status.State {
34 case cpb.ClusterState_CLUSTER_STATE_HOME:
35 return status, nil
36 case cpb.ClusterState_CLUSTER_STATE_DISOWNING:
37 return nil, fmt.Errorf("the cluster has disowned this node")
38 }
39 }
40}
41
42func (m *Manager) Watch() Watcher {
43 return Watcher{
44 Watcher: m.status.Watch(),
45 }
46}