forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globaldns_handler.go
213 lines (184 loc) · 6.1 KB
/
globaldns_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
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
package globaldns
import (
"context"
"fmt"
"net"
"strings"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
clientv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
)
const (
GlobaldnsController = "mgmt-global-dns-controller"
annotationIngressClass = "kubernetes.io/ingress.class"
)
type GDController struct {
globalDNSs v3.GlobalDNSInterface
globalDNSLister v3.GlobalDNSLister
ingresses clientv1beta1.IngressInterface //need to use client-go IngressInterface to update Ingress.Status field
}
func newGlobalDNSController(ctx context.Context, mgmt *config.ManagementContext) *GDController {
n := &GDController{
globalDNSs: mgmt.Management.GlobalDNSs(namespace.GlobalNamespace),
globalDNSLister: mgmt.Management.GlobalDNSs(namespace.GlobalNamespace).Controller().Lister(),
ingresses: mgmt.K8sClient.Extensions().Ingresses(namespace.GlobalNamespace),
}
return n
}
//sync is called periodically and on real updates
func (n *GDController) sync(key string, obj *v3.GlobalDNS) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return nil, nil
}
//check if status.endpoints is set, if yes create a dummy ingress if not already present
//if ingress exists, update endpoints if different
var isUpdate bool
//check if ingress for this globaldns is already present
ingress, err := n.getIngressForGlobalDNS(obj)
if err != nil && !k8serrors.IsNotFound(err) {
return nil, fmt.Errorf("GlobalDNSController: Error listing ingress for the GlobalDNS %v", err)
}
if ingress != nil && err == nil {
isUpdate = true
}
if len(obj.Status.Endpoints) == 0 && !isUpdate {
return nil, nil
}
if !isUpdate {
ingress, err = n.createIngressForGlobalDNS(obj)
if err != nil {
return nil, fmt.Errorf("GlobalDNSController: Error creating an ingress for the GlobalDNS %v", err)
}
}
err = n.updateIngressEndpoints(ingress, obj.Status.Endpoints)
if err != nil {
return nil, fmt.Errorf("GlobalDNSController: Error updating ingress for the GlobalDNS %v", err)
}
return nil, nil
}
func (n *GDController) getIngressForGlobalDNS(globaldns *v3.GlobalDNS) (*v1beta1.Ingress, error) {
ingress, err := n.ingresses.Get(strings.Join([]string{"globaldns-ingress", globaldns.Name}, "-"), metav1.GetOptions{}) //n.Get("", strings.Join([]string{"globaldns-ingress", globaldns.Name}, "-"))
if err != nil {
return nil, err
}
//make sure the ingress is owned by this globalDNS
if n.isIngressOwnedByGlobalDNS(ingress, globaldns) {
return ingress, nil
}
return nil, nil
}
func (n *GDController) isIngressOwnedByGlobalDNS(ingress *v1beta1.Ingress, globaldns *v3.GlobalDNS) bool {
for i, owners := 0, ingress.GetOwnerReferences(); owners != nil && i < len(owners); i++ {
if owners[i].UID == globaldns.UID && owners[i].Kind == globaldns.Kind {
return true
}
}
return false
}
func (n *GDController) createIngressForGlobalDNS(globaldns *v3.GlobalDNS) (*v1beta1.Ingress, error) {
ingressSpec := n.generateNewIngressSpec(globaldns)
ingressObj, err := n.ingresses.Create(ingressSpec)
if err != nil {
return nil, err
}
logrus.Debugf("Created ingress %v for globalDNS %s", ingressObj.Name, globaldns.Name)
return ingressObj, nil
}
func (n *GDController) generateNewIngressSpec(globaldns *v3.GlobalDNS) *v1beta1.Ingress {
controller := true
return &v1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Join([]string{"globaldns-ingress", globaldns.Name}, "-"),
OwnerReferences: []metav1.OwnerReference{
{
Name: globaldns.Name,
APIVersion: "v3",
UID: globaldns.UID,
Kind: globaldns.Kind,
Controller: &controller,
},
},
Annotations: map[string]string{
annotationIngressClass: "rancher-external-dns",
},
Namespace: globaldns.Namespace,
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{
Host: globaldns.Spec.FQDN,
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
{
Backend: v1beta1.IngressBackend{
ServiceName: "http-svc-dummy",
ServicePort: intstr.IntOrString{
Type: intstr.Int,
IntVal: 42,
},
},
},
},
},
},
},
},
},
}
}
func (n *GDController) updateIngressEndpoints(ingress *v1beta1.Ingress, endpoints []string) error {
if !n.ifEndpointsDiffer(ingress.Status.LoadBalancer.Ingress, endpoints) {
return nil
}
ingress.Status.LoadBalancer.Ingress = n.sliceToStatus(endpoints)
updatedObj, err := n.ingresses.UpdateStatus(ingress)
if err != nil {
return fmt.Errorf("GlobalDNSController: Error updating Ingress %v", err)
}
logrus.Debugf("GlobalDNSController: Updated Ingress: %v", updatedObj)
return nil
}
func (n *GDController) ifEndpointsDiffer(ingressEps []apiv1.LoadBalancerIngress, endpoints []string) bool {
if len(ingressEps) != len(endpoints) {
return true
}
mapIngEndpoints := n.gatherIngressEndpoints(ingressEps)
for _, ep := range endpoints {
if !mapIngEndpoints[ep] {
return true
}
}
return false
}
func (n *GDController) gatherIngressEndpoints(ingressEps []apiv1.LoadBalancerIngress) map[string]bool {
mapIngEndpoints := make(map[string]bool)
for _, ep := range ingressEps {
if ep.IP != "" {
mapIngEndpoints[ep.IP] = true
} else if ep.Hostname != "" {
mapIngEndpoints[ep.Hostname] = true
}
}
return mapIngEndpoints
}
// sliceToStatus converts a slice of IP and/or hostnames to LoadBalancerIngress
func (n *GDController) sliceToStatus(endpoints []string) []apiv1.LoadBalancerIngress {
lbi := []apiv1.LoadBalancerIngress{}
for _, ep := range endpoints {
if net.ParseIP(ep) == nil {
lbi = append(lbi, apiv1.LoadBalancerIngress{Hostname: ep})
} else {
lbi = append(lbi, apiv1.LoadBalancerIngress{IP: ep})
}
}
return lbi
}