forked from l7mp/stunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile.go
233 lines (199 loc) · 6.69 KB
/
reconcile.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package stunner
import (
"fmt"
"github.com/l7mp/stunner/internal/object"
"github.com/l7mp/stunner/pkg/apis/v1alpha1"
)
// Reconcile handles updates to the STUNner configuration. Some updates are destructive: in this
// case the returned error contains the names of the objects (usually, listeners) that were
// restarted during reconciliation (see the documentation of the corresponding STUNner objects for
// when STUNner may restart after a reconciliation). Reconcile returns nil no objects were
// restarted, v1alpha1.ErrRestarted to indicate that a shutdown-restart cycle was performed for at
// least one internal object (usually, a listener) for the new config (unless DryRun is enabled),
// and an error if an error has occurred during reconciliation, in which case it will rollback the
// last working configuration (unless SuppressRollback is on).
func (s *Stunner) Reconcile(req v1alpha1.StunnerConfig) error {
return s.reconcileWithRollback(req, false)
}
func (s *Stunner) reconcileWithRollback(req v1alpha1.StunnerConfig, inRollback bool) error {
var errFinal error
new, deleted, changed := 0, 0, 0
if err := req.Validate(); err != nil {
return err
}
s.log.Debugf("reconciling STUNner for config: %s ", req.String())
rollback := s.GetConfig()
toBeStarted, toBeRestarted := []object.Object{}, []object.Object{}
// admin
adminState, err := s.adminManager.PrepareReconciliation([]v1alpha1.Config{&req.Admin}, &req)
if err != nil {
return fmt.Errorf("error preparing reconciliation for admin config: %s",
err.Error())
}
toBeRestarted = append(toBeRestarted, adminState.ToBeRestarted...)
new += len(adminState.NewJobQueue)
changed += len(adminState.ChangedJobQueue)
deleted += len(adminState.DeletedJobQueue)
// auth
authState, err := s.authManager.PrepareReconciliation([]v1alpha1.Config{&req.Auth}, &req)
if err != nil {
return fmt.Errorf("error preparing reconciliation for auth config: %s",
err.Error())
}
toBeRestarted = append(toBeRestarted, authState.ToBeRestarted...)
new += len(authState.NewJobQueue)
changed += len(authState.ChangedJobQueue)
deleted += len(authState.DeletedJobQueue)
// listener
lconf := make([]v1alpha1.Config, len(req.Listeners))
for i := range req.Listeners {
lconf[i] = &(req.Listeners[i])
}
listenerState, err := s.listenerManager.PrepareReconciliation(lconf, &req)
if err != nil {
return fmt.Errorf("error preparing reconciliation for listener config: %s", err.Error())
}
toBeRestarted = append(toBeRestarted, listenerState.ToBeRestarted...)
new += len(listenerState.NewJobQueue)
changed += len(listenerState.ChangedJobQueue)
deleted += len(listenerState.DeletedJobQueue)
// cluster
cconf := make([]v1alpha1.Config, len(req.Clusters))
for i := range req.Clusters {
cconf[i] = &(req.Clusters[i])
}
clusterState, err := s.clusterManager.PrepareReconciliation(cconf, &req)
if err != nil {
return fmt.Errorf("error preparing reconciliation for cluster config: %s", err.Error())
}
toBeRestarted = append(toBeRestarted, clusterState.ToBeRestarted...)
new += len(clusterState.NewJobQueue)
changed += len(clusterState.ChangedJobQueue)
deleted += len(clusterState.DeletedJobQueue)
// find all objects (listeners) to be restarted and stop each
if !s.dryRun {
if err := s.stop(toBeRestarted); err != nil {
s.log.Errorf("could not stop object: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
// failing to stop the server is not critical: suppress error and go on
}
}
s.log.Tracef("reconciliation preparation ready")
// finish reconciliation
// admin
err = s.adminManager.FinishReconciliation(adminState)
if err != nil {
s.log.Errorf("could not reconcile admin config: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
return errFinal
}
toBeStarted = append(toBeStarted, adminState.ToBeStarted...)
s.log.Infof("setting loglevel to %q", s.GetAdmin().LogLevel)
s.logger.SetLevel(s.GetAdmin().LogLevel)
// auth
err = s.authManager.FinishReconciliation(authState)
if err != nil {
s.log.Errorf("could not reconcile auth config: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
return errFinal
}
toBeStarted = append(toBeStarted, authState.ToBeStarted...)
// listener
err = s.listenerManager.FinishReconciliation(listenerState)
if err != nil {
s.log.Errorf("could not reconcile listener config: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
return errFinal
}
toBeStarted = append(toBeStarted, listenerState.ToBeStarted...)
if len(s.listenerManager.Keys()) == 0 {
s.log.Warn("running with no listeners")
}
// cluster
err = s.clusterManager.FinishReconciliation(clusterState)
if err != nil {
s.log.Errorf("could not reconcile cluster config: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
return errFinal
}
toBeStarted = append(toBeStarted, clusterState.ToBeStarted...)
if len(s.clusterManager.Keys()) == 0 {
s.log.Warn("running with no clusters: all traffic will be dropped")
}
// find all objects (listeners) to be started or restarted and start each
if !s.dryRun {
if err := s.start(toBeStarted, toBeRestarted); err != nil {
s.log.Errorf("could not start object: %s", err.Error())
errFinal = err
if !inRollback {
goto rollback
}
return errFinal
}
}
// we are "ready" unless we are being shut down
if !s.shutdown && !s.ready {
s.ready = true
}
s.log.Infof("reconciliation ready: new objects: %d, changed objects: %d, "+
"deleted objects: %d, started objects: %d, restarted objects: %d",
new, changed, deleted, len(toBeStarted), len(toBeRestarted))
s.log.Info(s.Status())
if len(toBeRestarted) > 0 {
names := make([]string, len(toBeRestarted))
for i, n := range toBeRestarted {
names[i] = fmt.Sprintf("%s: %s", n.ObjectType(), n.ObjectName())
}
return v1alpha1.ErrRestarted{Objects: names}
}
return nil
rollback:
if !s.suppressRollback {
s.log.Infof("rolling back to previous configuration: %s", rollback.String())
return s.reconcileWithRollback(*rollback, true)
}
return errFinal
}
func (s *Stunner) stop(restarted []object.Object) error {
for _, o := range restarted {
switch l := o.(type) {
case *object.Listener:
if err := l.Close(); err != nil {
return err
}
default:
s.log.Errorf("internal error: stop() is not implemented for object %q",
o.ObjectName())
}
}
return nil
}
func (s *Stunner) start(started, restarted []object.Object) error {
for _, o := range append(started, restarted...) {
switch l := o.(type) {
case *object.Listener:
if err := s.StartServer(l); err != nil {
return err
}
default:
s.log.Errorf("internal error: start() is not implemented for object %q",
o.ObjectName())
}
}
return nil
}