-
Notifications
You must be signed in to change notification settings - Fork 67
/
handler.go
105 lines (97 loc) · 3.14 KB
/
handler.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
/*
* Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* 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
*
*/
package ingress
import (
"fmt"
"github.com/gardener/controller-manager-library/pkg/controllermanager/controller"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/controller-manager-library/pkg/resources"
"github.com/gardener/controller-manager-library/pkg/utils"
"github.com/gardener/external-dns-management/pkg/dns/source"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
)
type IngressSource struct {
source.DefaultDNSSource
}
func NewIngressSource(controller.Interface) (source.DNSSource, error) {
return &IngressSource{DefaultDNSSource: source.NewDefaultDNSSource(nil)}, nil
}
func (this *IngressSource) GetDNSInfo(logger logger.LogContext, obj resources.Object, current *source.DNSCurrentState) (*source.DNSInfo, error) {
info := &source.DNSInfo{Targets: this.GetTargets(obj)}
hosts, err := this.extractRuleHosts(obj)
if err != nil {
return nil, err
}
info.Names = utils.StringSet{}
all := current.AnnotatedNames.Contains("all") || current.AnnotatedNames.Contains("*")
for _, host := range hosts {
if host != "" && (all || current.AnnotatedNames.Contains(host)) {
info.Names.Add(host)
}
}
_, del := current.AnnotatedNames.DiffFrom(info.Names)
del.Remove("all")
del.Remove("*")
if len(del) > 0 {
return info, fmt.Errorf("annotated dns names %s not declared by ingress", del)
}
return info, nil
}
func (this *IngressSource) extractRuleHosts(obj resources.Object) ([]string, error) {
hosts := []string{}
switch data := obj.Data().(type) {
case *networkingv1beta1.Ingress:
for _, i := range data.Spec.Rules {
hosts = append(hosts, i.Host)
}
return hosts, nil
case *networkingv1.Ingress:
for _, i := range data.Spec.Rules {
hosts = append(hosts, i.Host)
}
return hosts, nil
default:
return nil, fmt.Errorf("unexpected ingress type: %#v", obj.Data())
}
}
func (this *IngressSource) GetTargets(obj resources.Object) utils.StringSet {
set := utils.StringSet{}
switch data := obj.Data().(type) {
case *networkingv1beta1.Ingress:
for _, i := range data.Status.LoadBalancer.Ingress {
if i.Hostname != "" && i.IP == "" {
set.Add(i.Hostname)
} else {
if i.IP != "" {
set.Add(i.IP)
}
}
}
case *networkingv1.Ingress:
for _, i := range data.Status.LoadBalancer.Ingress {
if i.Hostname != "" && i.IP == "" {
set.Add(i.Hostname)
} else {
if i.IP != "" {
set.Add(i.IP)
}
}
}
default:
}
return set
}