forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.go
371 lines (306 loc) · 12.4 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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package source
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"text/template"
log "github.com/sirupsen/logrus"
networkv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
kubeinformers "k8s.io/client-go/informers"
netinformers "k8s.io/client-go/informers/networking/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/external-dns/endpoint"
)
const (
// ALBDualstackAnnotationKey is the annotation used for determining if an ALB ingress is dualstack
ALBDualstackAnnotationKey = "alb.ingress.kubernetes.io/ip-address-type"
// ALBDualstackAnnotationValue is the value of the ALB dualstack annotation that indicates it is dualstack
ALBDualstackAnnotationValue = "dualstack"
// Possible values for the ingress-hostname-source annotation
IngressHostnameSourceAnnotationOnlyValue = "annotation-only"
IngressHostnameSourceDefinedHostsOnlyValue = "defined-hosts-only"
IngressClassAnnotationKey = "kubernetes.io/ingress.class"
)
// ingressSource is an implementation of Source for Kubernetes ingress objects.
// Ingress implementation will use the spec.rules.host value for the hostname
// Use targetAnnotationKey to explicitly set Endpoint. (useful if the ingress
// controller does not update, or to override with alternative endpoint)
type ingressSource struct {
client kubernetes.Interface
namespace string
annotationFilter string
ingressClassNames []string
fqdnTemplate *template.Template
combineFQDNAnnotation bool
ignoreHostnameAnnotation bool
ingressInformer netinformers.IngressInformer
ignoreIngressTLSSpec bool
ignoreIngressRulesSpec bool
labelSelector labels.Selector
}
// NewIngressSource creates a new ingressSource with the given config.
func NewIngressSource(ctx context.Context, kubeClient kubernetes.Interface, namespace, annotationFilter string, fqdnTemplate string, combineFqdnAnnotation bool, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool, labelSelector labels.Selector, ingressClassNames []string) (Source, error) {
tmpl, err := parseTemplate(fqdnTemplate)
if err != nil {
return nil, err
}
// ensure that ingress class is only set in either the ingressClassNames or
// annotationFilter but not both
if ingressClassNames != nil && annotationFilter != "" {
selector, err := getLabelSelector(annotationFilter)
if err != nil {
return nil, err
}
requirements, _ := selector.Requirements()
for _, requirement := range requirements {
if requirement.Key() == "kubernetes.io/ingress.class" {
return nil, errors.New("--ingress-class is mutually exclusive with the kubernetes.io/ingress.class annotation filter")
}
}
}
// Use shared informer to listen for add/update/delete of ingresses in the specified namespace.
// Set resync period to 0, to prevent processing when nothing has changed.
informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 0, kubeinformers.WithNamespace(namespace))
ingressInformer := informerFactory.Networking().V1().Ingresses()
// Add default resource event handlers to properly initialize informer.
ingressInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
},
},
)
informerFactory.Start(ctx.Done())
// wait for the local cache to be populated.
if err := waitForCacheSync(context.Background(), informerFactory); err != nil {
return nil, err
}
sc := &ingressSource{
client: kubeClient,
namespace: namespace,
annotationFilter: annotationFilter,
ingressClassNames: ingressClassNames,
fqdnTemplate: tmpl,
combineFQDNAnnotation: combineFqdnAnnotation,
ignoreHostnameAnnotation: ignoreHostnameAnnotation,
ingressInformer: ingressInformer,
ignoreIngressTLSSpec: ignoreIngressTLSSpec,
ignoreIngressRulesSpec: ignoreIngressRulesSpec,
labelSelector: labelSelector,
}
return sc, nil
}
// Endpoints returns endpoint objects for each host-target combination that should be processed.
// Retrieves all ingress resources on all namespaces
func (sc *ingressSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
ingresses, err := sc.ingressInformer.Lister().Ingresses(sc.namespace).List(sc.labelSelector)
if err != nil {
return nil, err
}
ingresses, err = sc.filterByAnnotations(ingresses)
if err != nil {
return nil, err
}
ingresses, err = sc.filterByIngressClass(ingresses)
if err != nil {
return nil, err
}
endpoints := []*endpoint.Endpoint{}
for _, ing := range ingresses {
// Check controller annotation to see if we are responsible.
controller, ok := ing.Annotations[controllerAnnotationKey]
if ok && controller != controllerAnnotationValue {
log.Debugf("Skipping ingress %s/%s because controller value does not match, found: %s, required: %s",
ing.Namespace, ing.Name, controller, controllerAnnotationValue)
continue
}
ingEndpoints := endpointsFromIngress(ing, sc.ignoreHostnameAnnotation, sc.ignoreIngressTLSSpec, sc.ignoreIngressRulesSpec)
// apply template if host is missing on ingress
if (sc.combineFQDNAnnotation || len(ingEndpoints) == 0) && sc.fqdnTemplate != nil {
iEndpoints, err := sc.endpointsFromTemplate(ing)
if err != nil {
return nil, err
}
ingEndpoints = append(ingEndpoints, iEndpoints...)
}
if len(ingEndpoints) == 0 {
log.Debugf("No endpoints could be generated from ingress %s/%s", ing.Namespace, ing.Name)
continue
}
log.Debugf("Endpoints generated from ingress: %s/%s: %v", ing.Namespace, ing.Name, ingEndpoints)
sc.setDualstackLabel(ing, ingEndpoints)
endpoints = append(endpoints, ingEndpoints...)
}
for _, ep := range endpoints {
sort.Sort(ep.Targets)
}
return endpoints, nil
}
func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpoint.Endpoint, error) {
hostnames, err := execTemplate(sc.fqdnTemplate, ing)
if err != nil {
return nil, err
}
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
ttl := getTTLFromAnnotations(ing.Annotations, resource)
targets := getTargetsFromTargetAnnotation(ing.Annotations)
if len(targets) == 0 {
targets = targetsFromIngressStatus(ing.Status)
}
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
var endpoints []*endpoint.Endpoint
for _, hostname := range hostnames {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
return endpoints, nil
}
// filterByAnnotations filters a list of ingresses by a given annotation selector.
func (sc *ingressSource) filterByAnnotations(ingresses []*networkv1.Ingress) ([]*networkv1.Ingress, error) {
selector, err := getLabelSelector(sc.annotationFilter)
if err != nil {
return nil, err
}
// empty filter returns original list
if selector.Empty() {
return ingresses, nil
}
filteredList := []*networkv1.Ingress{}
for _, ingress := range ingresses {
// include ingress if its annotations match the selector
if matchLabelSelector(selector, ingress.Annotations) {
filteredList = append(filteredList, ingress)
}
}
return filteredList, nil
}
// filterByIngressClass filters a list of ingresses based on a required ingress
// class
func (sc *ingressSource) filterByIngressClass(ingresses []*networkv1.Ingress) ([]*networkv1.Ingress, error) {
// if no class filter is specified then there's nothing to do
if len(sc.ingressClassNames) == 0 {
return ingresses, nil
}
classNameReq, err := labels.NewRequirement(IngressClassAnnotationKey, selection.In, sc.ingressClassNames)
if err != nil {
return nil, err
}
selector := labels.NewSelector()
selector = selector.Add(*classNameReq)
filteredList := []*networkv1.Ingress{}
for _, ingress := range ingresses {
var matched = false
for _, nameFilter := range sc.ingressClassNames {
if ingress.Spec.IngressClassName != nil && len(*ingress.Spec.IngressClassName) > 0 {
if nameFilter == *ingress.Spec.IngressClassName {
matched = true
}
} else if matchLabelSelector(selector, ingress.Annotations) {
matched = true
}
if matched {
filteredList = append(filteredList, ingress)
break
}
}
if !matched {
log.Debugf("Discarding ingress %s/%s because it does not match required ingress classes %v", ingress.Namespace, ingress.Name, sc.ingressClassNames)
}
}
return filteredList, nil
}
func (sc *ingressSource) setDualstackLabel(ingress *networkv1.Ingress, endpoints []*endpoint.Endpoint) {
val, ok := ingress.Annotations[ALBDualstackAnnotationKey]
if ok && val == ALBDualstackAnnotationValue {
log.Debugf("Adding dualstack label to ingress %s/%s.", ingress.Namespace, ingress.Name)
for _, ep := range endpoints {
ep.Labels[endpoint.DualstackLabelKey] = "true"
}
}
}
// endpointsFromIngress extracts the endpoints from ingress object
func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool) []*endpoint.Endpoint {
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
ttl := getTTLFromAnnotations(ing.Annotations, resource)
targets := getTargetsFromTargetAnnotation(ing.Annotations)
if len(targets) == 0 {
targets = targetsFromIngressStatus(ing.Status)
}
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
// Gather endpoints defined on hosts sections of the ingress
var definedHostsEndpoints []*endpoint.Endpoint
// Skip endpoints if we do not want entries from Rules section
if !ignoreIngressRulesSpec {
for _, rule := range ing.Spec.Rules {
if rule.Host == "" {
continue
}
definedHostsEndpoints = append(definedHostsEndpoints, endpointsForHostname(rule.Host, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}
// Skip endpoints if we do not want entries from tls spec section
if !ignoreIngressTLSSpec {
for _, tls := range ing.Spec.TLS {
for _, host := range tls.Hosts {
if host == "" {
continue
}
definedHostsEndpoints = append(definedHostsEndpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}
}
// Gather endpoints defined on annotations in the ingress
var annotationEndpoints []*endpoint.Endpoint
if !ignoreHostnameAnnotation {
for _, hostname := range getHostnamesFromAnnotations(ing.Annotations) {
annotationEndpoints = append(annotationEndpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}
// Determine which hostnames to consider in our final list
hostnameSourceAnnotation, hostnameSourceAnnotationExists := ing.Annotations[ingressHostnameSourceKey]
if !hostnameSourceAnnotationExists {
return append(definedHostsEndpoints, annotationEndpoints...)
}
// Include endpoints according to the hostname source annotation in our final list
var endpoints []*endpoint.Endpoint
if strings.ToLower(hostnameSourceAnnotation) == IngressHostnameSourceDefinedHostsOnlyValue {
endpoints = append(endpoints, definedHostsEndpoints...)
}
if strings.ToLower(hostnameSourceAnnotation) == IngressHostnameSourceAnnotationOnlyValue {
endpoints = append(endpoints, annotationEndpoints...)
}
return endpoints
}
func targetsFromIngressStatus(status networkv1.IngressStatus) endpoint.Targets {
var targets endpoint.Targets
for _, lb := range status.LoadBalancer.Ingress {
if lb.IP != "" {
targets = append(targets, lb.IP)
}
if lb.Hostname != "" {
targets = append(targets, lb.Hostname)
}
}
return targets
}
func (sc *ingressSource) AddEventHandler(ctx context.Context, handler func()) {
log.Debug("Adding event handler for ingress")
// Right now there is no way to remove event handler from informer, see:
// https://github.com/kubernetes/kubernetes/issues/79610
sc.ingressInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
}