Serge Bazanski | a959cbd | 2021-06-17 15:56:51 +0200 | [diff] [blame^] | 1 | package cluster |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "source.monogon.dev/metropolis/pkg/event" |
| 8 | cpb "source.monogon.dev/metropolis/proto/common" |
| 9 | ) |
| 10 | |
| 11 | type Watcher struct { |
| 12 | event.Watcher |
| 13 | } |
| 14 | |
| 15 | func (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. |
| 27 | func (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 | |
| 42 | func (m *Manager) Watch() Watcher { |
| 43 | return Watcher{ |
| 44 | Watcher: m.status.Watch(), |
| 45 | } |
| 46 | } |