This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ingress.go
97 lines (88 loc) · 2.66 KB
/
ingress.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
package main
import (
"log"
"time"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
type eventHandlerFunc func(eventType watch.EventType, oldIngress *v1beta1.Ingress, newIngress *v1beta1.Ingress)
type ingressWatcher struct {
client kubernetes.Interface
eventHandler eventHandlerFunc
resyncPeriod time.Duration
labelSelector string
stopChannel chan struct{}
store cache.Store
}
func newIngressWatcher(client kubernetes.Interface, eventHandler eventHandlerFunc, labelSelector string, resyncPeriod time.Duration) *ingressWatcher {
return &ingressWatcher{
client: client,
eventHandler: eventHandler,
resyncPeriod: resyncPeriod,
labelSelector: labelSelector,
stopChannel: make(chan struct{}),
}
}
func (iw *ingressWatcher) Start() {
lw := &cache.ListWatch{
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
options.LabelSelector = iw.labelSelector
return iw.client.Extensions().Ingresses(v1.NamespaceAll).List(options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
options.LabelSelector = iw.labelSelector
return iw.client.Extensions().Ingresses(v1.NamespaceAll).Watch(options)
},
}
eh := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
iw.eventHandler(watch.Added, nil, obj.(*v1beta1.Ingress))
},
UpdateFunc: func(oldObj, newObj interface{}) {
iw.eventHandler(watch.Modified, oldObj.(*v1beta1.Ingress), newObj.(*v1beta1.Ingress))
},
DeleteFunc: func(obj interface{}) {
iw.eventHandler(watch.Deleted, obj.(*v1beta1.Ingress), nil)
},
}
store, controller := cache.NewInformer(lw, &v1beta1.Ingress{}, iw.resyncPeriod, eh)
iw.store = store
log.Println("[INFO] starting ingress watcher")
controller.Run(iw.stopChannel)
log.Println("[INFO] ingress watcher stopped")
}
func (iw *ingressWatcher) Stop() {
log.Println("[INFO] stopping ingress watcher ...")
close(iw.stopChannel)
}
func (iw *ingressWatcher) HostnameOwners(hostname string) []string {
owners := []string{}
for _, i := range iw.store.List() {
for _, h := range getHostnamesFromIngress(i.(*v1beta1.Ingress)) {
if hostname == h {
owners = append(owners, i.(*v1beta1.Ingress).Name)
}
}
}
return owners
}
func getHostnamesFromIngress(ingress *v1beta1.Ingress) []string {
hostnames := []string{}
for _, rule := range ingress.Spec.Rules {
found := false
for _, h := range hostnames {
if h == rule.Host {
found = true
break
}
}
if !found {
hostnames = append(hostnames, rule.Host)
}
}
return hostnames
}