forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress_controller.go
189 lines (159 loc) · 4.58 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
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"
)
const (
annotationIngressClass = "kubernetes.io/ingress.class"
ingressClassNginx = "nginx"
RdnsIPDomain = "lb.rancher.cloud"
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
}
ipDomain := settings.IngressIPDomain.Get()
if ipDomain != RdnsIPDomain {
return nil, nil
}
isNeedSync := false
for _, rule := range obj.Spec.Rules {
if strings.HasSuffix(rule.Host, RdnsIPDomain) {
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)
}
return c.refresh(fqdn, obj)
}
func (c *Controller) refresh(rootDomain string, obj *extensionsv1beta1.Ingress) (*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, RdnsIPDomain) {
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 string) error {
ingresses, err := c.ingressLister.List("", labels.NewSelector())
if err != nil {
return err
}
for _, obj := range ingresses {
if _, err = c.refresh(rootDomain, obj); 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) {
ipDomain := settings.IngressIPDomain.Get()
if ipDomain != RdnsIPDomain {
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)
}
}
}