-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingresshostgen.go
73 lines (60 loc) · 1.63 KB
/
ingresshostgen.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
package ingresshostgen
import (
"fmt"
"strings"
"github.com/rancher/rancher/pkg/controllers/user/approuter"
"github.com/rancher/rancher/pkg/settings"
v1beta12 "github.com/rancher/types/apis/extensions/v1beta1"
"github.com/rancher/types/config"
"k8s.io/api/extensions/v1beta1"
)
type IngressHostGen struct {
ingress v1beta12.IngressInterface
}
func Register(userOnlyContext *config.UserOnlyContext) {
c := &IngressHostGen{
ingress: userOnlyContext.Extensions.Ingresses(""),
}
userOnlyContext.Extensions.Ingresses("").AddHandler("ingress-host-gen", c.sync)
}
func isGeneratedDomain(obj *v1beta1.Ingress, host, domain string) bool {
parts := strings.Split(host, ".")
return strings.HasSuffix(host, "."+domain) && len(parts) == 8 && parts[1] == obj.Namespace
}
func (i *IngressHostGen) sync(key string, obj *v1beta1.Ingress) error {
if obj == nil {
return nil
}
ipDomain := settings.IngressIPDomain.Get()
if ipDomain == "" {
return nil
}
var xipHost string
for _, status := range obj.Status.LoadBalancer.Ingress {
if status.IP != "" {
xipHost = fmt.Sprintf("%s.%s.%s.%s", obj.Name, obj.Namespace, status.IP, ipDomain)
break
}
}
if xipHost == "" {
return nil
}
changed := false
for _, rule := range obj.Spec.Rules {
if (isGeneratedDomain(obj, rule.Host, ipDomain) || rule.Host == ipDomain) && rule.Host != xipHost && ipDomain != approuter.RdnsIPDomain {
changed = true
break
}
}
if !changed {
return nil
}
obj = obj.DeepCopy()
for i, rule := range obj.Spec.Rules {
if strings.HasSuffix(rule.Host, ipDomain) {
obj.Spec.Rules[i].Host = xipHost
}
}
_, err := i.ingress.Update(obj)
return err
}