forked from zalando-incubator/kube-ingress-aws-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
418 lines (360 loc) · 11.7 KB
/
worker.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"log"
"os"
"os/signal"
"reflect"
"sort"
"syscall"
"time"
"fmt"
"runtime/debug"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/pkg/errors"
"github.com/zalando-incubator/kube-ingress-aws-controller/aws"
"github.com/zalando-incubator/kube-ingress-aws-controller/certs"
"github.com/zalando-incubator/kube-ingress-aws-controller/kubernetes"
)
type loadBalancer struct {
ingresses map[string][]*kubernetes.Ingress
scheme string
stack *aws.Stack
shared bool
certTTL time.Duration
}
const (
ready int = iota
update
missing
delete
)
const (
maxTargetGroupSupported = 1000
)
func (l *loadBalancer) Status() int {
if l.stack.ShouldDelete() {
return delete
}
if len(l.ingresses) != 0 && l.stack == nil {
return missing
}
if !l.inSync() && l.stack.IsComplete() {
return update
}
return ready
}
// inSync checks if the loadBalancer is in sync with the backing CF stack.
// It's considered in sync when certs found for the ingresses match those
// already defined on the stack.
func (l *loadBalancer) inSync() bool {
return reflect.DeepEqual(l.CertificateARNs(), l.stack.CertificateARNs)
}
// AddIngress adds an ingress object to the load balancer.
// The function returns true when the ingress was successfully added. The
// adding can fail in case the load balancer reached its limit of ingress
// certificates or if the scheme doesn't match.
func (l *loadBalancer) AddIngress(certificateARNs []string, ingress *kubernetes.Ingress, maxCerts int) bool {
if l.scheme != ingress.Scheme {
return false
}
resourceName := fmt.Sprintf("%s/%s", ingress.Namespace, ingress.Name)
owner := ""
if l.stack != nil {
owner = l.stack.OwnerIngress
}
if !ingress.Shared && resourceName != owner {
return false
}
// check if we can fit the ingress on the load balancer based on
// maxCerts
newCerts := 0
for _, certificateARN := range certificateARNs {
if _, ok := l.ingresses[certificateARN]; ok {
continue
}
newCerts++
}
// if adding this ingress would result in more than maxCerts then we
// don't add the ingress
if len(l.ingresses)+newCerts > maxCerts {
return false
}
for _, certificateARN := range certificateARNs {
if ingresses, ok := l.ingresses[certificateARN]; ok {
l.ingresses[certificateARN] = append(ingresses, ingress)
} else {
l.ingresses[certificateARN] = []*kubernetes.Ingress{ingress}
}
}
l.shared = ingress.Shared
return true
}
// CertificateARNs returns a map of certificates and their expiry times.
func (l *loadBalancer) CertificateARNs() map[string]time.Time {
certificates := make(map[string]time.Time, len(l.ingresses))
for arn, ingresses := range l.ingresses {
// only include certificates required by at least one ingress.
if len(ingresses) > 0 {
certificates[arn] = time.Time{}
}
}
for arn, ttl := range l.stack.CertificateARNs {
if _, ok := certificates[arn]; !ok {
if ttl.IsZero() {
certificates[arn] = time.Now().UTC().Add(l.certTTL)
} else if ttl.After(time.Now().UTC()) {
certificates[arn] = ttl
}
}
}
return certificates
}
// Owner returns the ingress resource owning the load balancer. If there are no
// owners it will return an empty string meaning the load balancer is shared
// between multiple ingresses.
func (l *loadBalancer) Owner() string {
if l.shared {
return ""
}
for _, ingresses := range l.ingresses {
for _, ingress := range ingresses {
return fmt.Sprintf("%s/%s", ingress.Namespace, ingress.Name)
}
}
return ""
}
func waitForTerminationSignals(signals ...os.Signal) chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, signals...)
return c
}
func startPolling(quitCH chan struct{}, certsProvider certs.CertificatesProvider, certsPerALB int, certTTL time.Duration, awsAdapter *aws.Adapter, kubeAdapter *kubernetes.Adapter, pollingInterval time.Duration) {
for {
log.Printf("Start polling sleep %s", pollingInterval)
select {
case <-waitForTerminationSignals(syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT):
quitCH <- struct{}{}
return
case <-time.After(pollingInterval):
if err := doWork(certsProvider, certsPerALB, certTTL, awsAdapter, kubeAdapter); err != nil {
log.Println(err)
}
}
}
}
func doWork(certsProvider certs.CertificatesProvider, certsPerALB int, certTTL time.Duration, awsAdapter *aws.Adapter, kubeAdapter *kubernetes.Adapter) error {
defer func() error {
if r := recover(); r != nil {
log.Println("shit has hit the fan:", errors.Wrap(r.(error), "panic caused by"))
debug.PrintStack()
return r.(error)
}
return nil
}()
ingresses, err := kubeAdapter.ListIngress()
if err != nil {
return fmt.Errorf("doWork failed to list ingress resources: %v", err)
}
log.Printf("Found %d ingress(es)", len(ingresses))
stacks, err := awsAdapter.FindManagedStacks()
if err != nil {
return fmt.Errorf("doWork failed to list managed stacks: %v", err)
}
log.Printf("Found %d stack(s)", len(stacks))
err = awsAdapter.UpdateAutoScalingGroupsAndInstances()
if err != nil {
return fmt.Errorf("doWork failed to get instances from EC2: %v", err)
}
awsAdapter.UpdateTargetGroupsAndAutoScalingGroups(stacks)
log.Printf("Found %d auto scaling group(s)", len(awsAdapter.AutoScalingGroupNames()))
log.Printf("Found %d single instance(s)", len(awsAdapter.SingleInstances()))
log.Printf("Found %d EC2 instance(s)", awsAdapter.CachedInstances())
model := buildManagedModel(certsProvider, certsPerALB, certTTL, ingresses, stacks)
log.Printf("Have %d model(s)", len(model))
for _, loadBalancer := range model {
switch loadBalancer.Status() {
case delete:
deleteStack(awsAdapter, loadBalancer)
case missing:
createStack(awsAdapter, loadBalancer)
updateIngress(kubeAdapter, loadBalancer)
case ready:
updateIngress(kubeAdapter, loadBalancer)
case update:
updateStack(awsAdapter, loadBalancer)
updateIngress(kubeAdapter, loadBalancer)
}
}
return nil
}
func buildManagedModel(certsProvider certs.CertificatesProvider, certsPerALB int, certTTL time.Duration, ingresses []*kubernetes.Ingress, stacks []*aws.Stack) []*loadBalancer {
sort.Slice(stacks, func(i, j int) bool {
if len(stacks[i].CertificateARNs) == len(stacks[j].CertificateARNs) {
return stacks[i].Name < stacks[j].Name
}
return len(stacks[i].CertificateARNs) > len(stacks[j].CertificateARNs)
})
model := make([]*loadBalancer, 0, len(stacks))
for _, stack := range stacks {
lb := &loadBalancer{
stack: stack,
ingresses: make(map[string][]*kubernetes.Ingress),
scheme: stack.Scheme,
shared: stack.OwnerIngress == "",
certTTL: certTTL,
}
// initialize ingresses map with existing certificates from the
// stack.
for cert := range stack.CertificateARNs {
lb.ingresses[cert] = make([]*kubernetes.Ingress, 0)
}
model = append(model, lb)
}
var (
certificateARNs []string
err error
)
for _, ingress := range ingresses {
certificateARN := ingress.CertificateARN
if certificateARN != "" {
certificateARNs = append(certificateARNs, certificateARN)
}
if len(certificateARN) == 0 { // do discovery
certificateARNs, err = discoverCertificates(certsProvider, ingress)
if err != nil {
log.Printf("failed to find certificates for %v: %v", ingress.Hostnames, err)
continue
}
} else { // validate that certificateARN exists
err := checkCertificate(certsProvider, certificateARN)
if err != nil {
log.Printf("Failed to find certificates with ARN %s: %v", certificateARNs, err)
continue
}
}
// try to add ingress to existing ALB stacks until certificate
// limit is exeeded.
added := false
for _, lb := range model {
if lb.AddIngress(certificateARNs, ingress, certsPerALB) {
added = true
break
}
}
// if the ingress was not added to the ALB stack because of
// non-matching scheme or too many certificates, add a new
// stack.
if !added {
i := make(map[string][]*kubernetes.Ingress, len(certificateARNs))
for _, certificateARN := range certificateARNs {
i[certificateARN] = []*kubernetes.Ingress{ingress}
}
model = append(model, &loadBalancer{ingresses: i, scheme: ingress.Scheme, shared: ingress.Shared})
}
}
return model
}
func discoverCertificates(certsProvider certs.CertificatesProvider, ingress *kubernetes.Ingress) ([]string, error) {
knownCertificates, err := certsProvider.GetCertificates()
if err != nil {
return nil, fmt.Errorf("discoverCertificateAndUpdateIngress failed to obtain certificates: %v", err)
}
certificateSummaries := certs.FindBestMatchingCertificates(knownCertificates, ingress.Hostnames)
if len(certificateSummaries) < 1 {
return nil, fmt.Errorf("Failed to find any certificates for hostnames: %s", ingress.Hostnames)
}
certs := make([]string, 0, len(certificateSummaries))
for _, cert := range certificateSummaries {
certs = append(certs, cert.ID())
}
return certs, nil
}
// checkCertificate checks that a certificate with the specified ARN exists in
// the account. Returns error if no certificate is found.
func checkCertificate(certsProvider certs.CertificatesProvider, arn string) error {
certs, err := certsProvider.GetCertificates()
if err != nil {
return err
}
for _, cert := range certs {
if arn == cert.ID() {
return nil
}
}
return fmt.Errorf("certificate not found")
}
func createStack(awsAdapter *aws.Adapter, lb *loadBalancer) {
certificates := make([]string, 0, len(lb.ingresses))
for cert, _ := range lb.ingresses {
certificates = append(certificates, cert)
}
log.Printf("creating stack for certificates %q / ingress %q", certificates, lb.ingresses)
stackId, err := awsAdapter.CreateStack(certificates, lb.scheme, lb.Owner())
if err != nil {
if isAlreadyExistsError(err) {
lb.stack, err = awsAdapter.GetStack(stackId)
if err == nil {
return
}
}
log.Printf("createStack(%q) failed: %v", certificates, err)
} else {
log.Printf("stack %q for certificates %q created", stackId, certificates)
}
}
func updateStack(awsAdapter *aws.Adapter, lb *loadBalancer) {
certificates := lb.CertificateARNs()
log.Printf("updating %q stack for %d certificates / %d ingresses", lb.scheme, len(certificates), len(lb.ingresses))
stackId, err := awsAdapter.UpdateStack(lb.stack.Name, certificates, lb.scheme)
if isNoUpdatesToBePerformedError(err) {
log.Printf("stack(%q) is already up to date", certificates)
} else if err != nil {
log.Printf("updateStack(%q) failed: %v", certificates, err)
} else {
log.Printf("stack %q for certificate %q updated", stackId, certificates)
}
}
func isAlreadyExistsError(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
return awsErr.Code() == cloudformation.ErrCodeAlreadyExistsException
}
return false
}
func isNoUpdatesToBePerformedError(err error) bool {
if err == nil {
return false
}
if _, ok := err.(awserr.Error); ok {
return strings.Contains(err.Error(), "No updates are to be performed")
}
return false
}
func updateIngress(kubeAdapter *kubernetes.Adapter, lb *loadBalancer) {
if lb.stack == nil {
return
}
dnsName := strings.ToLower(lb.stack.DNSName) // lower case to satisfy Kubernetes reqs
for _, ingresses := range lb.ingresses {
for _, ing := range ingresses {
if err := kubeAdapter.UpdateIngressLoadBalancer(ing, dnsName); err != nil {
if err == kubernetes.ErrUpdateNotNeeded {
log.Printf("Ingress update not needed %v with DNS name %q", ing, dnsName)
} else {
log.Printf("Failed to update ingress: %v", err)
}
} else {
log.Printf("updated ingress %v with DNS name %q", ing, dnsName)
}
}
}
}
func deleteStack(awsAdapter *aws.Adapter, lb *loadBalancer) {
stackName := lb.stack.Name
if err := awsAdapter.DeleteStack(lb.stack); err != nil {
log.Printf("deleteStack failed to delete stack %q: %v", stackName, err)
} else {
log.Printf("deleted orphaned stack %q", stackName)
}
}