-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingress_controller.go
222 lines (187 loc) · 5.56 KB
/
ingress_controller.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
package approuter
import (
"context"
"fmt"
"strings"
"time"
"k8s.io/apimachinery/pkg/runtime"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rancher/pkg/ticker"
v1 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/extensions/v1beta1"
"github.com/sirupsen/logrus"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/labels"
// pandaria
"github.com/rancher/rancher/pkg/controllers/user/ingress"
)
const (
annotationIngressClass = "kubernetes.io/ingress.class"
annotationGlobalDNS = "rancher.io/globalDNS.hostname"
ingressClassNginx = "nginx"
ingressClassExternalDNS = "rancher-external-dns"
maxHost = 10
)
var (
renewInterval = 24 * time.Hour
)
type Controller struct {
ctx context.Context
ingressInterface v1beta1.IngressInterface
ingressLister v1beta1.IngressLister
managementSecretLister v1.SecretLister
clusterName string
dnsClient *Client
}
func isGeneratedDomain(obj *extensionsv1beta1.Ingress, host, domain string) bool {
parts := strings.Split(host, ".")
return strings.HasSuffix(host, "."+domain) && len(parts) >= 6 && parts[1] == obj.Namespace
}
func (c *Controller) sync(key string, obj *extensionsv1beta1.Ingress) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return nil, nil
}
// PANDARIA: skip rancher-server ingress
if _, ok := obj.Annotations[ingress.IngressRServerAnnotation]; ok {
return nil, nil
}
isRDNS := settings.RDNSServerBaseURL.Get()
if isRDNS == "" {
return nil, nil
}
if _, ok := obj.Annotations[annotationGlobalDNS]; ok {
logrus.Debugf("ingress %s has not valid annotations", obj.Name)
return nil, nil
}
if v, ok := obj.Annotations[annotationIngressClass]; ok {
if v == ingressClassExternalDNS {
logrus.Debugf("ingress %s has not valid annotations", obj.Name)
return nil, nil
}
}
ipDomain := settings.IngressIPDomain.Get()
isNeedSync := false
for _, rule := range obj.Spec.Rules {
if strings.HasSuffix(rule.Host, ipDomain) {
isNeedSync = true
break
}
}
if !isNeedSync {
return nil, nil
}
serverURL := settings.RDNSServerBaseURL.Get()
if serverURL == "" {
return nil, errors.New("settings.baseRDNSServerURL is not set, dns name might not be reachable")
}
var ips []string
for _, status := range obj.Status.LoadBalancer.Ingress {
if status.IP != "" {
ips = append(ips, status.IP)
}
}
if len(ips) > maxHost {
logrus.Debugf("hosts number is %d, over %d", len(ips), maxHost)
ips = ips[:maxHost]
}
c.dnsClient.SetBaseURL(serverURL)
created, fqdn, err := c.dnsClient.ApplyDomain(ips)
if err != nil {
logrus.WithError(err).Errorf("update fqdn [%s] to server [%s] error", fqdn, serverURL)
return nil, err
}
//As a new secret is created, all the ingress obj will be updated
if created {
return nil, c.refreshAll(fqdn, ipDomain)
}
return c.refresh(fqdn, obj, ipDomain)
}
func (c *Controller) refresh(rootDomain string, obj *extensionsv1beta1.Ingress, ipDomain string) (*extensionsv1beta1.Ingress, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return nil, errors.New("Got a nil ingress object")
}
annotations := obj.Annotations
if annotations == nil {
annotations = make(map[string]string)
}
targetHostname := ""
switch annotations[annotationIngressClass] {
case "": // nginx as default
fallthrough
case ingressClassNginx:
targetHostname = c.getRdnsHostname(obj, rootDomain)
default:
return obj, nil
}
if targetHostname == "" {
return obj, nil
}
changed := false
for _, rule := range obj.Spec.Rules {
if !isGeneratedDomain(obj, rule.Host, rootDomain) {
changed = true
break
}
}
if !changed {
return obj, nil
}
newObj := obj.DeepCopy()
// Also need to update rules for hostname when using nginx
for i, rule := range newObj.Spec.Rules {
logrus.Debugf("Got ingress resource hostname: %s", rule.Host)
if strings.HasSuffix(rule.Host, ipDomain) {
newObj.Spec.Rules[i].Host = targetHostname
}
}
if _, err := c.ingressInterface.Update(newObj); err != nil {
return obj, err
}
return newObj, nil
}
func (c *Controller) refreshAll(rootDomain, ipDomain string) error {
ingresses, err := c.ingressLister.List("", labels.NewSelector())
if err != nil {
return err
}
for _, obj := range ingresses {
if _, ok := obj.Annotations[annotationGlobalDNS]; ok {
logrus.Debugf("ingress %s has not valid annotations", obj.Name)
continue
}
if v, ok := obj.Annotations[annotationIngressClass]; ok {
if v == ingressClassExternalDNS {
logrus.Debugf("ingress %s has not valid annotations", obj.Name)
continue
}
}
if _, err = c.refresh(rootDomain, obj, ipDomain); err != nil {
logrus.WithError(err).Errorf("refresh ingress %s:%s hostname annotation error", obj.Namespace, obj.Name)
}
}
return nil
}
func (c *Controller) getRdnsHostname(obj *extensionsv1beta1.Ingress, rootDomain string) string {
if rootDomain != "" {
return fmt.Sprintf("%s.%s.%s", obj.Name, obj.Namespace, rootDomain)
}
return ""
}
func (c *Controller) renew(ctx context.Context) {
for range ticker.Context(ctx, renewInterval) {
isRDNS := settings.RDNSServerBaseURL.Get()
if isRDNS == "" {
continue
}
serverURL := settings.RDNSServerBaseURL.Get()
if serverURL == "" {
logrus.Warn("RDNSServerBaseURL need to be set when enable approuter controller")
continue
}
c.dnsClient.SetBaseURL(serverURL)
if fqdn, err := c.dnsClient.RenewDomain(); err != nil {
logrus.WithError(err).Errorf("renew fqdn [%s] to server [%s] error", fqdn, serverURL)
}
}
}