forked from voyagermesh/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingresses.go
104 lines (93 loc) · 3.49 KB
/
ingresses.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
package operator
import (
"errors"
acrt "github.com/appscode/go/runtime"
"github.com/appscode/log"
"github.com/appscode/voyager/api"
"github.com/appscode/voyager/pkg/analytics"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
apiv1 "k8s.io/client-go/pkg/api/v1"
extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/tools/cache"
)
// Blocks caller. Intended to be called as a Go routine.
func (op *Operator) WatchIngresses() {
defer acrt.HandleCrash()
lw := &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
return op.KubeClient.ExtensionsV1beta1().Ingresses(apiv1.NamespaceAll).List(metav1.ListOptions{})
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return op.KubeClient.ExtensionsV1beta1().Ingresses(apiv1.NamespaceAll).Watch(metav1.ListOptions{})
},
}
_, ctrl := cache.NewInformer(lw,
&extensions.Ingress{},
op.SyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if ingress, ok := obj.(*extensions.Ingress); ok {
log.Infof("%s %s@%s added", ingress.GroupVersionKind(), ingress.Name, ingress.Namespace)
engress, err := api.NewEngressFromIngress(ingress)
if err != nil {
log.Errorf("Failed to convert Ingress %s@%s into Engress. Reason %v", ingress.Name, ingress.Namespace, err)
return
}
if !engress.ShouldHandleIngress(op.Opt.IngressClass) {
log.Infof("%s %s@%s does not match ingress class", ingress.GroupVersionKind(), ingress.Name, ingress.Namespace)
return
}
go analytics.Send(ingress.GroupVersionKind().String(), "ADD", "success")
op.AddEngress(engress)
}
},
UpdateFunc: func(old, new interface{}) {
oldIngress, ok := old.(*extensions.Ingress)
if !ok {
log.Errorln(errors.New("Invalid Ingress object"))
return
}
newIngress, ok := new.(*extensions.Ingress)
if !ok {
log.Errorln(errors.New("Invalid Ingress object"))
return
}
oldEngress, err := api.NewEngressFromIngress(oldIngress)
if err != nil {
log.Errorf("Failed to convert Ingress %s@%s into Engress. Reason %v", oldIngress.Name, oldIngress.Namespace, err)
return
}
newEngress, err := api.NewEngressFromIngress(newIngress)
if err != nil {
log.Errorf("Failed to convert Ingress %s@%s into Engress. Reason %v", newIngress.Name, newIngress.Namespace, err)
return
}
if changed, _ := oldEngress.HasChanged(*newEngress); !changed {
log.Infof("%s %s@%s has unchanged spec and annotations", newIngress.GroupVersionKind(), newIngress.Name, newIngress.Namespace)
return
}
op.UpdateEngress(oldEngress, newEngress)
},
DeleteFunc: func(obj interface{}) {
if ingress, ok := obj.(*extensions.Ingress); ok {
log.Infof("%s %s@%s deleted", ingress.GroupVersionKind(), ingress.Name, ingress.Namespace)
engress, err := api.NewEngressFromIngress(ingress)
if err != nil {
log.Errorf("Failed to convert Ingress %s@%s into Engress. Reason %v", ingress.Name, ingress.Namespace, err)
return
}
if !engress.ShouldHandleIngress(op.Opt.IngressClass) {
log.Infof("%s %s@%s does not match ingress class", ingress.GroupVersionKind(), ingress.Name, ingress.Namespace)
return
}
go analytics.Send(ingress.GroupVersionKind().String(), "DELETE", "success")
op.DeleteEngress(engress)
}
},
},
)
ctrl.Run(wait.NeverStop)
}