-
Notifications
You must be signed in to change notification settings - Fork 280
/
bootstrap.go
128 lines (105 loc) · 3.49 KB
/
bootstrap.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Package bootstrap fetches the very initial configuration for Pomerium Core to start.
package bootstrap
/*
* Initial configuration for Pomerium start-up is obtained from the cloud.
* Some parameters are derived from the cluster token.
*
* The expectation is that if the user wishes to survive a cloud outage,
* it should be sufficient to set up Pomerium to use a durable database (Postgres)
* and receive cloud configuration once.
*
*/
import (
"context"
"fmt"
"time"
"golang.org/x/sync/errgroup"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/retry"
connect_mux "github.com/pomerium/pomerium/internal/zero/connect-mux"
)
const (
// DefaultCheckForUpdateIntervalWhenDisconnected is the default interval to check for updates
// if there is no connection to the update service
DefaultCheckForUpdateIntervalWhenDisconnected = 5 * time.Minute
// DefaultCheckForUpdateIntervalWhenConnected is the default interval to check for updates
// if there is a connection to the update service
DefaultCheckForUpdateIntervalWhenConnected = time.Hour
)
// Run initializes the bootstrap config source
func (svc *Source) Run(ctx context.Context) error {
svc.tryLoadFromFile(ctx)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { return svc.watchUpdates(ctx) })
eg.Go(func() error { return svc.updateLoop(ctx) })
return eg.Wait()
}
func (svc *Source) watchUpdates(ctx context.Context) error {
return svc.api.Watch(ctx,
connect_mux.WithOnConnected(func(_ context.Context) {
svc.triggerUpdate(DefaultCheckForUpdateIntervalWhenConnected)
}),
connect_mux.WithOnDisconnected(func(_ context.Context) {
svc.updateInterval.Store(DefaultCheckForUpdateIntervalWhenDisconnected)
}),
connect_mux.WithOnBootstrapConfigUpdated(func(_ context.Context) {
svc.triggerUpdate(DefaultCheckForUpdateIntervalWhenConnected)
}),
)
}
func (svc *Source) updateLoop(ctx context.Context) error {
ticker := time.NewTicker(svc.updateInterval.Load())
defer ticker.Stop()
for {
err := retry.Retry(ctx,
"update bootstrap", svc.updateAndSave,
retry.WithWatch("bootstrap config updated", svc.checkForUpdate, nil),
)
if err != nil {
return fmt.Errorf("update bootstrap config: %w", err)
}
ticker.Reset(svc.updateInterval.Load())
select {
case <-ctx.Done():
return ctx.Err()
case <-svc.checkForUpdate:
case <-ticker.C:
}
}
}
// triggerUpdate triggers an update of the bootstrap config
// and sets the interval for the next update
func (svc *Source) triggerUpdate(newUpdateInterval time.Duration) {
svc.updateInterval.Store(newUpdateInterval)
select {
case svc.checkForUpdate <- struct{}{}:
default:
}
}
func (svc *Source) updateAndSave(ctx context.Context) error {
cfg, err := svc.api.GetClusterBootstrapConfig(ctx)
if err != nil {
return fmt.Errorf("load bootstrap config from API: %w", err)
}
svc.UpdateBootstrap(ctx, *cfg)
if svc.fileCachePath == nil {
return nil
}
err = SaveBootstrapConfigToFile(cfg, *svc.fileCachePath, svc.fileCipher)
if err != nil {
log.Ctx(ctx).Error().Err(err).
Msg("failed to save bootstrap config to file, note it may prevent Pomerium from starting up in case of connectivity issues")
}
return nil
}
func (svc *Source) tryLoadFromFile(ctx context.Context) {
if svc.fileCachePath == nil {
return
}
cfg, err := LoadBootstrapConfigFromFile(*svc.fileCachePath, svc.fileCipher)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to load bootstrap config from file")
return
}
svc.UpdateBootstrap(ctx, *cfg)
}