forked from dbsystel/kube-controller-dbsystel-go-common
/
ingress.go
55 lines (45 loc) · 1.39 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
package ingress
import (
"sync"
"time"
"github.com/dbsystel/kube-controller-dbsystel-go-common/controller"
"k8s.io/api/networking/v1beta1"
metav1 "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 IngressController struct {
Controller controller.Controller
informer cache.SharedIndexInformer
kclient *kubernetes.Clientset
}
func (ic *IngressController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
wg.Add(1)
go ic.informer.Run(stopCh)
<-stopCh
}
func (ic *IngressController) Initialize(kclient *kubernetes.Clientset) {
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return kclient.NetworkingV1beta1().Ingresses(metav1.NamespaceAll).List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return kclient.NetworkingV1beta1().Ingresses(metav1.NamespaceAll).Watch(options)
},
},
&v1beta1.Ingress{},
3*time.Minute,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ic.Controller.Create,
UpdateFunc: ic.Controller.Update,
DeleteFunc: ic.Controller.Delete,
})
ic.informer = informer
ic.kclient = kclient
}