-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ingress.go
403 lines (343 loc) · 14.1 KB
/
ingress.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
package controller
import (
"fmt"
"sort"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/coreos/alb-ingress-controller/awsutil"
"github.com/coreos/alb-ingress-controller/controller/alb"
"github.com/coreos/alb-ingress-controller/controller/config"
"github.com/coreos/alb-ingress-controller/controller/util"
"github.com/coreos/alb-ingress-controller/log"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/apis/extensions"
)
// ALBIngress contains all information above the cluster, ingress resource, and AWS resources
// needed to assemble an ALB, TargetGroup, Listener, Rules, and Route53 Resource Records.
type ALBIngress struct {
id *string
namespace *string
ingressName *string
clusterName *string
lock *sync.Mutex
annotations *config.Annotations
LoadBalancers alb.LoadBalancers
}
// ALBIngressesT is a list of ALBIngress. It is held by the ALBController instance and evaluated
// against to determine what should be created, deleted, and modified.
type ALBIngressesT []*ALBIngress
// NewALBIngress returns a minimal ALBIngress instance with a generated name that allows for lookup
// when new ALBIngress objects are created to determine if an instance of that ALBIngress already
// exists.
func NewALBIngress(namespace, name, clustername string) *ALBIngress {
return &ALBIngress{
id: aws.String(fmt.Sprintf("%s-%s", namespace, name)),
namespace: aws.String(namespace),
clusterName: aws.String(clustername),
ingressName: aws.String(name),
lock: new(sync.Mutex),
}
}
// NewALBIngressFromIngress builds ALBIngress's based off of an Ingress object
// https://godoc.org/k8s.io/kubernetes/pkg/apis/extensions#Ingress. Creates a new ingress object,
// and looks up to see if a previous ingress object with the same id is known to the ALBController.
// If there is an issue and the ingress is invalid, nil is returned.
func NewALBIngressFromIngress(ingress *extensions.Ingress, ac *ALBController) *ALBIngress {
var err error
// Create newIngress ALBIngress object holding the resource details and some cluster information.
newIngress := NewALBIngress(ingress.GetNamespace(), ingress.Name, *ac.clusterName)
// Find the previous version of this ingress (if it existed) and copy its Current state.
if i := ac.ALBIngresses.find(newIngress); i >= 0 {
// Acquire a lock to prevent race condition if existing ingress's state is currently being synced
// with Amazon..
*newIngress = *ac.ALBIngresses[i]
newIngress.lock.Lock()
defer newIngress.lock.Unlock()
// Ensure all desired state is removed from the copied ingress. The desired state of each
// component will be generated later in this function.
newIngress.StripDesiredState()
}
// Load up the ingress with our current annotations.
newIngress.annotations, err = config.ParseAnnotations(ingress.Annotations)
if err != nil {
log.Errorf("Error parsing annotations for ingress %v. Error: %s", "controller", newIngress.Name(), err.Error())
return nil
}
// If annotation set is nil, its because it was cached as an invalid set before. Stop processing
// and return nil.
if newIngress.annotations == nil {
log.Debugf("%s-%s: Skipping processing due to a history of bad annotations", newIngress.Name(), ingress.GetNamespace(), ingress.Name)
return nil
}
// Create a new LoadBalancer instance for every item in ingress.Spec.Rules. This means that for
// each host specified (1 per ingress.Spec.Rule) a new load balancer is expected.
for _, rule := range ingress.Spec.Rules {
// Start with a new LoadBalancer with a new DesiredState.
// TODO: RETURNING NIL SHOULD NOT BE AN OPTION HERE, otherwise memory access violations will
// occur.
lb := alb.NewLoadBalancer(*ac.clusterName, ingress.GetNamespace(), ingress.Name, rule.Host, newIngress.id, newIngress.annotations, newIngress.Tags())
// If this rule is for a previously defined loadbalancer, pull it out so we can work on it
if i := newIngress.LoadBalancers.Find(lb); i >= 0 {
// Save the Desired state to our old Loadbalancer.
newIngress.LoadBalancers[i].DesiredLoadBalancer = lb.DesiredLoadBalancer
newIngress.LoadBalancers[i].DesiredTags = lb.DesiredTags
newIngress.LoadBalancers[i].Hostname = lb.Hostname
// Set lb to our old but updated LoadBalancer.
lb = newIngress.LoadBalancers[i]
// Remove the old LoadBalancer from the list.
newIngress.LoadBalancers = append(newIngress.LoadBalancers[:i], newIngress.LoadBalancers[i+1:]...)
}
// Create a new TargetGroup and Listener, associated with a LoadBalancer for every item in
// rule.HTTP.Paths. TargetGroups are constructed based on namespace, ingress name, and port.
// Listeners are constructed based on path and port.
for _, path := range rule.HTTP.Paths {
serviceKey := fmt.Sprintf("%s/%s", *newIngress.namespace, path.Backend.ServiceName)
port, err := ac.GetServiceNodePort(serviceKey, path.Backend.ServicePort.IntVal)
if err != nil {
glog.Infof("%s: %s", newIngress.Name(), err)
continue
}
// Start with a new target group with a new Desired state.
targetGroup := alb.NewTargetGroup(newIngress.annotations, newIngress.Tags(), newIngress.clusterName, lb.ID, port, newIngress.id, path.Backend.ServiceName)
// If this rule/path matches an existing target group, pull it out so we can work on it.
if i := lb.TargetGroups.Find(targetGroup); i >= 0 {
// Save the Desired state to our old TargetGroup
lb.TargetGroups[i].DesiredTags = targetGroup.DesiredTags
lb.TargetGroups[i].DesiredTargetGroup = targetGroup.DesiredTargetGroup
// Set targetGroup to our old but updated TargetGroup.
targetGroup = lb.TargetGroups[i]
// Remove the old TG from our list.
lb.TargetGroups = append(lb.TargetGroups[:i], lb.TargetGroups[i+1:]...)
}
// Add desired targets set to the targetGroup.
targetGroup.DesiredTargets = GetNodes(ac)
lb.TargetGroups = append(lb.TargetGroups, targetGroup)
// Start with a new listener
listenerList := alb.NewListener(newIngress.annotations, newIngress.id)
for _, listener := range listenerList {
// If this listener matches an existing listener, pull it out so we can work on it.
// TODO: We should refine the lookup. Find is really not adequate as this could be a first
// statrt where no Listeners have CurrentListeners attached. In other words, find should be
// rewritten.
if i := lb.Listeners.Find(listener.DesiredListener); i >= 0 {
// Save the Desired state to our old Listener.
lb.Listeners[i].DesiredListener = listener.DesiredListener
// Set listener to our old but updated Listener.
listener = lb.Listeners[i]
// Remove the old Listener from our list.
lb.Listeners = append(lb.Listeners[:i], lb.Listeners[i+1:]...)
}
lb.Listeners = append(lb.Listeners, listener)
// Start with a new rule
rule := alb.NewRule(path, newIngress.id)
// If this rule matches an existing rule, pull it out so we can work on it
if i := listener.Rules.Find(rule.DesiredRule); i >= 0 {
// Save the Desired state to our old Rule
listener.Rules[i].DesiredRule = rule.DesiredRule
// Set rule to our old but updated Rule
rule = listener.Rules[i]
// Remove the old Rule from our list.
listener.Rules = append(listener.Rules[:i], listener.Rules[i+1:]...)
}
listener.Rules = append(listener.Rules, rule)
}
// Create a new ResourceRecordSet for the hostname.
if resourceRecordSet, err := alb.NewResourceRecordSet(lb.Hostname, lb.IngressID); err == nil {
// If the load balancer has a CurrentResourceRecordSet, set
// this value inside our new resourceRecordSet.
if lb.ResourceRecordSet != nil {
resourceRecordSet.CurrentResourceRecordSet = lb.ResourceRecordSet.CurrentResourceRecordSet
}
// Assign the resourceRecordSet to the load balancer
lb.ResourceRecordSet = resourceRecordSet
}
}
// Add the newly constructed LoadBalancer to the new ALBIngress's Loadbalancer list.
newIngress.LoadBalancers = append(newIngress.LoadBalancers, lb)
}
return newIngress
}
// assembleIngresses builds a list of existing ingresses from resources in AWS
func assembleIngresses(ac *ALBController) ALBIngressesT {
var ALBIngresses ALBIngressesT
log.Infof("Build up list of existing ingresses", "controller")
loadBalancers, err := awsutil.ALBsvc.DescribeLoadBalancers(ac.clusterName)
if err != nil {
glog.Fatal(err)
}
for _, loadBalancer := range loadBalancers {
log.Debugf("Fetching Tags for %s", "controller", *loadBalancer.LoadBalancerArn)
tags, err := awsutil.ALBsvc.DescribeTags(loadBalancer.LoadBalancerArn)
if err != nil {
glog.Fatal(err)
}
ingressName, ok := tags.Get("IngressName")
if !ok {
log.Infof("The LoadBalancer %s does not have an IngressName tag, can't import", "controller", *loadBalancer.LoadBalancerName)
continue
}
namespace, ok := tags.Get("Namespace")
if !ok {
log.Infof("The LoadBalancer %s does not have an Namespace tag, can't import", "controller", *loadBalancer.LoadBalancerName)
continue
}
hostname, ok := tags.Get("Hostname")
if !ok {
log.Infof("The LoadBalancer %s does not have a Hostname tag, can't import", "controller", *loadBalancer.LoadBalancerName)
continue
}
zone, err := awsutil.Route53svc.GetZoneID(&hostname)
if err != nil {
log.Infof("Failed to resolve %s zoneID. Returned error %s", "controller", hostname, err.Error())
continue
}
log.Infof("Fetching resource recordset for %s/%s %s", "controller", namespace, ingressName, hostname)
resourceRecordSet, err := awsutil.Route53svc.DescribeResourceRecordSets(zone.Id,
&hostname)
if err != nil {
log.Errorf("Failed to find %s in AWS Route53", "controller", hostname)
}
ingressID := namespace + "-" + ingressName
rs := &alb.ResourceRecordSet{
IngressID: &ingressID,
ZoneID: zone.Id,
CurrentResourceRecordSet: resourceRecordSet,
}
lb := &alb.LoadBalancer{
ID: loadBalancer.LoadBalancerName,
IngressID: &ingressID,
Hostname: aws.String(hostname),
CurrentLoadBalancer: loadBalancer,
ResourceRecordSet: rs,
CurrentTags: tags,
}
targetGroups, err := awsutil.ALBsvc.DescribeTargetGroups(loadBalancer.LoadBalancerArn)
if err != nil {
glog.Fatal(err)
}
for _, targetGroup := range targetGroups {
tags, err := awsutil.ALBsvc.DescribeTags(targetGroup.TargetGroupArn)
if err != nil {
glog.Fatal(err)
}
svcName, ok := tags.Get("ServiceName")
if !ok {
log.Infof("The LoadBalancer %s does not have an Namespace tag, can't import", "controller", *loadBalancer.LoadBalancerName)
continue
}
tg := &alb.TargetGroup{
ID: targetGroup.TargetGroupName,
IngressID: &ingressID,
SvcName: svcName,
CurrentTags: tags,
CurrentTargetGroup: targetGroup,
}
log.Infof("Fetching Targets for Target Group %s", "controller", *targetGroup.TargetGroupArn)
targets, err := awsutil.ALBsvc.DescribeTargetGroupTargets(targetGroup.TargetGroupArn)
if err != nil {
glog.Fatal(err)
}
tg.CurrentTargets = targets
lb.TargetGroups = append(lb.TargetGroups, tg)
}
listeners, err := awsutil.ALBsvc.DescribeListeners(loadBalancer.LoadBalancerArn)
if err != nil {
glog.Fatal(err)
}
for _, listener := range listeners {
log.Infof("Fetching Rules for Listener %s", "controller", *listener.ListenerArn)
rules, err := awsutil.ALBsvc.DescribeRules(listener.ListenerArn)
if err != nil {
glog.Fatal(err)
}
l := &alb.Listener{
CurrentListener: listener,
IngressID: &ingressID,
}
for _, rule := range rules {
var svcName string
for _, tg := range lb.TargetGroups {
if *rule.Actions[0].TargetGroupArn == *tg.CurrentTargetGroup.TargetGroupArn {
svcName = tg.SvcName
}
}
log.Debugf("Assembling rule with svc name: %s", "controller", svcName)
l.Rules = append(l.Rules, &alb.Rule{
IngressID: &ingressID,
SvcName: svcName,
CurrentRule: rule,
})
}
// Set the highest known priority to the amount of current rules plus 1
lb.LastRulePriority = int64(len(l.Rules)) + 1
lb.Listeners = append(lb.Listeners, l)
}
a := NewALBIngress(namespace, ingressName, *ac.clusterName)
a.LoadBalancers = []*alb.LoadBalancer{lb}
if i := ALBIngresses.find(a); i >= 0 {
a = ALBIngresses[i]
a.LoadBalancers = append(a.LoadBalancers, lb)
} else {
ALBIngresses = append(ALBIngresses, a)
}
}
log.Infof("Assembled %d ingresses from existing AWS resources", "controller", len(ALBIngresses))
return ALBIngresses
}
// Reconcile begins the state sync for all AWS resource satisfying this ALBIngress instance.
func (a *ALBIngress) Reconcile() {
a.lock.Lock()
defer a.lock.Unlock()
var err error
a.LoadBalancers, err = a.LoadBalancers.Reconcile()
if err != nil {
log.Errorf("Sync stopped due to error. Error: %s", *a.id, err.Error())
}
}
// Name returns the name of the ingress
func (a *ALBIngress) Name() string {
return fmt.Sprintf("%s-%s", *a.namespace, *a.ingressName)
}
// StripDesiredState strips all desired objects from an ALBIngress
func (a *ALBIngress) StripDesiredState() {
a.LoadBalancers.StripDesiredState()
for _, lb := range a.LoadBalancers {
lb.Listeners.StripDesiredState()
lb.TargetGroups.StripDesiredState()
for _, listener := range lb.Listeners {
listener.Rules.StripDesiredState()
}
}
}
// Tags returns an elbv2.Tag slice of standard tags for the ingress AWS resources
func (a *ALBIngress) Tags() []*elbv2.Tag {
tags := a.annotations.Tags
tags = append(tags, &elbv2.Tag{
Key: aws.String("Namespace"),
Value: a.namespace,
})
tags = append(tags, &elbv2.Tag{
Key: aws.String("IngressName"),
Value: a.ingressName,
})
return tags
}
func (a ALBIngressesT) find(b *ALBIngress) int {
for p, v := range a {
if *v.id == *b.id {
return p
}
}
return -1
}
// GetNodes returns a list of the cluster node external ids
func GetNodes(ac *ALBController) util.AWSStringSlice {
var result util.AWSStringSlice
nodes, _ := ac.storeLister.Node.List()
for _, node := range nodes.Items {
result = append(result, aws.String(node.Spec.ExternalID))
}
sort.Sort(result)
return result
}