-
Notifications
You must be signed in to change notification settings - Fork 12
/
engine.go
282 lines (243 loc) · 6.63 KB
/
engine.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"errors"
"fmt"
"sync"
"time"
"k8s.io/kubernetes/pkg/api"
"github.com/albertrdixon/gearbox/logger"
"github.com/cenkalti/backoff"
"golang.org/x/net/context"
"github.com/timelinelabs/romulus/kubernetes"
"github.com/timelinelabs/romulus/loadbalancer"
)
func NewEngine(kubeapi, user, pass string, insecure bool, lb loadbalancer.LoadBalancer, timeout time.Duration, ctx context.Context) (*Engine, error) {
kc, er := kubernetes.NewClient(kubeapi, user, pass, insecure)
if er != nil {
return nil, er
}
back := backoff.NewExponentialBackOff()
back.MaxElapsedTime = timeout
return &Engine{
Context: ctx,
BackOff: back,
LoadBalancer: lb,
Cache: kubernetes.NewCache(),
Client: kc,
}, nil
}
func (e *Engine) Start(selector kubernetes.Selector, resync time.Duration) error {
var (
er error
)
if er = kubernetes.Status(e.Client); er != nil {
return fmt.Errorf("Failed to connect to kubernetes: %v", er)
}
if er = e.LoadBalancer.Status(); er != nil {
return fmt.Errorf("Failed to connect to loadbalancer: %v", er)
}
e.Lock()
defer e.Unlock()
createObjectCache(e, selector, resync)
time.Sleep(200 * time.Millisecond)
return createKubernetesCallbacks(e, selector, resync)
}
func (e *Engine) Add(obj interface{}) {
e.Lock()
defer e.Unlock()
resources, er := kubernetes.GenResources(e.Cache, e.Client, obj)
if er != nil {
logger.Errorf(er.Error())
}
if er := addResources(e, resources); er != nil {
logger.Errorf(er.Error())
}
}
func (e *Engine) Delete(obj interface{}) {
e.Lock()
defer e.Unlock()
resources, er := kubernetes.GenResources(e.Cache, e.Client, obj)
if er != nil {
logger.Errorf(er.Error())
}
if o, ok := obj.(*api.Service); ok {
e.Cache.ServiceDeleted(o.GetNamespace(), o.GetName())
}
if er := deleteResources(e, resources); er != nil {
logger.Errorf(er.Error())
}
}
func (e *Engine) Update(old, next interface{}) {
e.Lock()
defer e.Unlock()
logger.Debugf("Gather resources from previous object")
oldResources, er := kubernetes.GenResources(e.Cache, e.Client, old)
if er != nil {
logger.Errorf(er.Error())
}
logger.Debugf("Gather resources from new object")
newResources, er := kubernetes.GenResources(e.Cache, e.Client, next)
if er != nil {
logger.Errorf(er.Error())
}
if er := updateResources(e, newResources, oldResources); er != nil {
logger.Errorf(er.Error())
}
}
func (e *Engine) Commit(fn UpsertFunc) error {
e.Reset()
for {
select {
case <-e.Done():
return nil
default:
duration := e.NextBackOff()
if duration == backoff.Stop {
return errors.New("Timed out trying to Commit changes to loadbalancer")
}
er := fn()
if er == nil {
return er
}
logger.Warnf("Commit failed, retry in %v: %v", duration, er)
time.Sleep(duration)
}
}
}
func updateResources(e *Engine, resources, previous kubernetes.ResourceList) error {
removals := kubernetes.ResourceList{}
m := resources.Map()
for _, rsc := range previous {
if _, ok := m[rsc.ID()]; !ok {
removals = append(removals, rsc)
}
}
if er := addResources(e, resources); er != nil {
return er
}
return deleteResources(e, removals)
}
func deleteResources(e *Engine, resources kubernetes.ResourceList) error {
for _, rsc := range resources {
backend, er := e.NewBackend(rsc)
if er != nil {
return er
}
frontend, er := e.NewFrontend(rsc)
if er != nil {
return er
}
fn := func() error {
logger.Infof("Removing %v", frontend)
if er := e.DeleteFrontend(frontend); er != nil {
return er
}
logger.Infof("Removing %v", backend)
return e.DeleteBackend(backend)
}
if er := e.Commit(fn); er != nil {
return er
}
}
return nil
}
func addResources(e *Engine, resources kubernetes.ResourceList) error {
backends := make([]loadbalancer.Backend, 0, len(resources))
frontends := make([]loadbalancer.Frontend, 0, len(resources))
for _, rsc := range resources {
logger.Debugf("[%v] Build Frontends and Backends", rsc.ID())
backend, er := e.NewBackend(rsc)
if er != nil {
return er
}
srvs, er := e.NewServers(rsc)
if er != nil {
return er
}
for i := range srvs {
backend.AddServer(srvs[i])
}
logger.Debugf("[%v] Created new object: %v", rsc.ID(), backend)
backends = append(backends, backend)
frontend, er := e.NewFrontend(rsc)
if er != nil {
return er
}
mids, er := e.NewMiddlewares(rsc)
if er != nil {
return er
}
for i := range mids {
frontend.AddMiddleware(mids[i])
}
frontends = append(frontends, frontend)
logger.Debugf("[%v] Created new object: %v", rsc.ID(), frontend)
}
return e.Commit(func() error {
for _, backend := range backends {
logger.Infof("Upserting %v", backend)
if er := e.UpsertBackend(backend); er != nil {
return er
}
}
for _, frontend := range frontends {
logger.Infof("Upserting %v", frontend)
if er := e.UpsertFrontend(frontend); er != nil {
return er
}
}
return nil
})
}
func createObjectCache(e *Engine, selector kubernetes.Selector, resync time.Duration) {
var (
uc = e.GetUnversionedClient()
ec = e.GetExtensionsClient()
)
logger.Infof("Creating kubernetes object cache")
service, er := kubernetes.CreateStore(kubernetes.ServicesKind, uc, selector, resync, e.Context)
if er != nil {
logger.Warnf("Failed to create Service cache")
}
endpoints, er := kubernetes.CreateStore(kubernetes.EndpointsKind, uc, selector, resync, e.Context)
if er != nil {
logger.Warnf("Failed to create Endpoints cache")
}
ingress, er := kubernetes.CreateStore(kubernetes.IngressesKind, ec, selector, resync, e.Context)
if er != nil {
logger.Warnf("Failed to create Ingress cache")
}
e.SetIngressStore(ingress)
e.SetServiceStore(service)
e.SetEndpointsStore(endpoints)
}
func createKubernetesCallbacks(e *Engine, selector kubernetes.Selector, resync time.Duration) error {
var (
uc = e.GetUnversionedClient()
ec = e.GetExtensionsClient()
)
logger.Infof("Starting kubernetes watchers")
_, endpoint := kubernetes.CreateFullController(kubernetes.EndpointsKind, e, uc, selector, resync)
_, service := kubernetes.CreateFullController(kubernetes.ServicesKind, e, uc, selector, resync)
_, ingress := kubernetes.CreateFullController(kubernetes.IngressesKind, e, ec, selector, resync)
go endpoint.Run(e.Done())
go service.Run(e.Done())
go ingress.Run(e.Done())
return nil
}
type Engine struct {
sync.Mutex
backoff.BackOff
context.Context
loadbalancer.LoadBalancer
*kubernetes.Cache
*kubernetes.Client
}
type UpsertFunc func() error
type ResourceFunc func(*Engine, kubernetes.ResourceList) error
const (
interval = 50 * time.Millisecond
serviceResource = "services"
endpointsResource = "endpoints"
ingressResource = "ingresses"
)