|
1 | 1 | package haproxy |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "sync/atomic" |
| 5 | + "time" |
| 6 | + |
4 | 7 | "github.com/criteo/haproxy-consul-connect/consul" |
5 | 8 | "github.com/criteo/haproxy-consul-connect/haproxy/state" |
| 9 | + "github.com/criteo/haproxy-consul-connect/lib" |
6 | 10 | log "github.com/sirupsen/logrus" |
| 11 | + "gopkg.in/d4l3k/messagediff.v1" |
7 | 12 | ) |
8 | 13 |
|
9 | | -func (h *HAProxy) handleChange(cfg consul.Config) error { |
10 | | - log.Info("handling new configuration") |
11 | | - |
12 | | - newState, err := state.Generate(state.Options{ |
13 | | - EnableIntentions: h.opts.EnableIntentions, |
14 | | - LogRequests: h.opts.LogRequests, |
15 | | - LogSocket: h.haConfig.LogsSock, |
16 | | - SPOEConfigPath: h.haConfig.SPOE, |
17 | | - SPOESocket: h.haConfig.SPOESock, |
18 | | - }, h.haConfig, h.oldState, cfg) |
19 | | - if err != nil { |
20 | | - return err |
21 | | - } |
| 14 | +func (h *HAProxy) watch(sd *lib.Shutdown) error { |
| 15 | + throttle := time.Tick(50 * time.Millisecond) |
| 16 | + currentState := state.State{} |
| 17 | + nextState := &atomic.Value{} |
| 18 | + next := make(chan struct{}, 1) |
| 19 | + dirty := false |
22 | 20 |
|
23 | | - tx := h.dataplaneClient.Tnx() |
| 21 | + go func() { |
| 22 | + for c := range h.cfgC { |
| 23 | + select { |
| 24 | + case <-sd.Stop: |
| 25 | + return |
| 26 | + default: |
| 27 | + } |
24 | 28 |
|
25 | | - log.Debugf("applying new state: %+v", newState) |
| 29 | + log.Info("received consul config update") |
| 30 | + nextState.Store(c) |
| 31 | + h.currentConsulConfig = &c |
| 32 | + select { |
| 33 | + case next <- struct{}{}: |
| 34 | + default: |
| 35 | + } |
| 36 | + } |
| 37 | + }() |
26 | 38 |
|
27 | | - err = state.Apply(tx, h.oldState, newState) |
28 | | - if err != nil { |
29 | | - return err |
30 | | - } |
| 39 | + go func() { |
| 40 | + for range time.Tick(5 * time.Minute) { |
| 41 | + select { |
| 42 | + case <-sd.Stop: |
| 43 | + return |
| 44 | + default: |
| 45 | + } |
| 46 | + |
| 47 | + dirty = true |
| 48 | + } |
| 49 | + }() |
31 | 50 |
|
32 | | - err = tx.Commit() |
33 | | - if err != nil { |
34 | | - return err |
| 51 | + retry := func() { |
| 52 | + time.Sleep(3 * time.Second) |
| 53 | + select { |
| 54 | + case next <- struct{}{}: |
| 55 | + default: |
| 56 | + } |
35 | 57 | } |
36 | 58 |
|
37 | | - h.oldState = newState |
38 | | - h.currentCfg = &cfg |
| 59 | + started := false |
| 60 | + for { |
| 61 | + for { |
| 62 | + select { |
| 63 | + case <-sd.Stop: |
| 64 | + return nil |
| 65 | + case <-next: |
| 66 | + } |
| 67 | + |
| 68 | + <-throttle |
| 69 | + |
| 70 | + log.Info("handling new configuration") |
| 71 | + if !started { |
| 72 | + err := h.start(sd) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + started = true |
| 77 | + close(h.Ready) |
| 78 | + } |
| 79 | + |
| 80 | + if dirty { |
| 81 | + log.Info("refreshing haproxy state") |
| 82 | + fromHa, err := state.FromHAProxy(h.dataplaneClient) |
| 83 | + if err != nil { |
| 84 | + log.Errorf("error retrieving haproxy conf: %s", err) |
| 85 | + retry() |
| 86 | + continue |
| 87 | + } |
| 88 | + diff, equal := messagediff.PrettyDiff(currentState, fromHa) |
| 89 | + if !equal { |
| 90 | + log.Errorf("diff found between expected state and haproxy state: %s", diff) |
| 91 | + } |
| 92 | + currentState = fromHa |
| 93 | + dirty = false |
| 94 | + } |
| 95 | + |
| 96 | + newConsulCfg := nextState.Load().(consul.Config) |
39 | 97 |
|
40 | | - log.Info("state successfully applied") |
| 98 | + newState, err := state.Generate(state.Options{ |
| 99 | + EnableIntentions: h.opts.EnableIntentions, |
| 100 | + LogRequests: h.opts.LogRequests, |
| 101 | + LogSocket: h.haConfig.LogsSock, |
| 102 | + SPOEConfigPath: h.haConfig.SPOE, |
| 103 | + SPOESocket: h.haConfig.SPOESock, |
| 104 | + }, h.haConfig, currentState, newConsulCfg) |
| 105 | + if err != nil { |
| 106 | + log.Error(err) |
| 107 | + retry() |
| 108 | + continue |
| 109 | + } |
41 | 110 |
|
42 | | - return nil |
| 111 | + if currentState.Equal(newState) { |
| 112 | + log.Info("no change to apply to haproxy") |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + tx := h.dataplaneClient.Tnx() |
| 117 | + |
| 118 | + log.Debugf("applying new state: %+v", newState) |
| 119 | + |
| 120 | + err = state.Apply(tx, currentState, newState) |
| 121 | + if err != nil { |
| 122 | + log.Error(err) |
| 123 | + retry() |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + err = tx.Commit() |
| 128 | + if err != nil { |
| 129 | + log.Error(err) |
| 130 | + retry() |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + currentState = newState |
| 135 | + |
| 136 | + log.Info("state applied") |
| 137 | + } |
| 138 | + } |
43 | 139 | } |
0 commit comments