forked from voyagermesh/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress_tprs.go
241 lines (219 loc) · 7.69 KB
/
ingress_tprs.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package operator
import (
"reflect"
"github.com/appscode/errors"
acrt "github.com/appscode/go/runtime"
"github.com/appscode/log"
tapi "github.com/appscode/voyager/api"
"github.com/appscode/voyager/pkg/analytics"
"github.com/appscode/voyager/pkg/certificate"
"github.com/appscode/voyager/pkg/ingress"
"github.com/appscode/voyager/pkg/monitor"
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"
"k8s.io/client-go/tools/cache"
)
// Blocks caller. Intended to be called as a Go routine.
func (op *Operator) WatchIngressTPRs() {
defer acrt.HandleCrash()
lw := &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
return op.ExtClient.Ingresses(apiv1.NamespaceAll).List(metav1.ListOptions{})
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return op.ExtClient.Ingresses(apiv1.NamespaceAll).Watch(metav1.ListOptions{})
},
}
_, ctrl := cache.NewInformer(lw,
&tapi.Ingress{},
op.SyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if engress, ok := obj.(*tapi.Ingress); ok {
log.Infof("%s %s@%s added", engress.GroupVersionKind(), engress.Name, engress.Namespace)
if !engress.ShouldHandleIngress(op.Opt.IngressClass) {
log.Infof("%s %s@%s does not match ingress class", engress.GroupVersionKind(), engress.Name, engress.Namespace)
return
}
go analytics.Send(engress.GroupVersionKind().String(), "ADD", "success")
op.AddEngress(engress)
}
},
UpdateFunc: func(old, new interface{}) {
oldEngress, ok := old.(*tapi.Ingress)
if !ok {
log.Errorln(errors.New("Invalid Ingress object").Err())
return
}
newEngress, ok := new.(*tapi.Ingress)
if !ok {
log.Errorln(errors.New("Invalid Ingress object").Err())
return
}
if changed, _ := oldEngress.HasChanged(*newEngress); !changed {
log.Infof("%s %s@%s has unchanged spec and annotations", newEngress.GroupVersionKind(), newEngress.Name, newEngress.Namespace)
return
}
op.UpdateEngress(oldEngress, newEngress)
},
DeleteFunc: func(obj interface{}) {
if engress, ok := obj.(*tapi.Ingress); ok {
log.Infof("%s %s@%s deleted", engress.GroupVersionKind(), engress.Name, engress.Namespace)
if !engress.ShouldHandleIngress(op.Opt.IngressClass) {
log.Infof("%s %s@%s does not match ingress class", engress.GroupVersionKind(), engress.Name, engress.Namespace)
return
}
go analytics.Send(engress.GroupVersionKind().String(), "DELETE", "success")
op.DeleteEngress(engress)
}
},
},
)
ctrl.Run(wait.NeverStop)
}
func (op *Operator) AddEngress(engress *tapi.Ingress) {
ctrl := ingress.NewController(op.KubeClient, op.ExtClient, op.PromClient, op.Opt, engress)
if ctrl.IsExists() {
// Loadbalancer resource for this ingress is found in its place,
// so no need to create the resources. First trying to update
// the configMap only for the rules.
// In case of any failure in soft update we will make hard update
// to the resource. If hard update encounters errors then we will
// recreate the resource from scratch.
log.Infoln("Loadbalancer is exists, trying to update")
if svc, err := op.KubeClient.CoreV1().Services(engress.Namespace).Get(engress.OffshootName(), metav1.GetOptions{}); err == nil {
// check port
curPorts := make(map[int]apiv1.ServicePort)
for _, p := range svc.Spec.Ports {
curPorts[int(p.Port)] = p
}
var updateFW bool
for svcPort, targetPort := range ctrl.Ports {
if sp, ok := curPorts[svcPort]; !ok || sp.TargetPort.IntValue() != targetPort {
updateFW = true // new port has to be opened
break
} else {
delete(curPorts, svcPort)
}
}
if len(curPorts) > 0 {
updateFW = true // additional port was open previously
}
if updateFW {
ctrl.Update(ingress.UpdateFirewall | ingress.UpdateStats)
} else {
ctrl.Update(ingress.UpdateConfig | ingress.UpdateStats)
}
} else {
log.Warningln("Loadbalancer is exists but Soft Update failed. Retrying Hard Update")
restartErr := ctrl.Update(ingress.RestartHAProxy)
if restartErr != nil {
log.Warningln("Loadbalancer is exists, But Hard Update is also failed, recreating with a cleanup")
ctrl.Create()
}
}
return
} else {
ctrl.Create()
}
for _, meta := range engress.BackendServices() {
svc, err := op.KubeClient.CoreV1().Services(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
continue
}
op.ensureServiceAnnotations(engress, svc)
}
err := certificate.NewController(op.KubeClient, op.ExtClient, op.Opt, nil).HandleIngress(engress)
if err != nil {
log.Error(err)
}
}
func (op *Operator) UpdateEngress(oldEngress, newEngress *tapi.Ingress) {
oldHandled := oldEngress.ShouldHandleIngress(op.Opt.IngressClass)
newHandled := newEngress.ShouldHandleIngress(op.Opt.IngressClass)
if !oldHandled && !newHandled {
return
}
ctrl := ingress.NewController(op.KubeClient, op.ExtClient, op.PromClient, op.Opt, newEngress)
if oldHandled && !newHandled {
ctrl.Delete()
} else {
if ctrl.IsExists() {
var updateMode ingress.UpdateMode
// Ingress Annotations Changed, Apply Changes to Targets
// The following method do not update to HAProxy config or restart pod. It only sets the annotations
// to the required targets.
ctrl.UpdateTargetAnnotations(oldEngress, newEngress)
if oldEngress.IsKeepSourceChanged(*newEngress, op.Opt.CloudProvider) {
updateMode |= ingress.UpdateConfig
}
if oldEngress.IsStatsChanged(*newEngress) {
updateMode |= ingress.UpdateStats
if oldEngress.IsStatsSecretChanged(*newEngress) {
updateMode |= ingress.UpdateRBAC
}
}
// Check for changes in ingress.appscode.com/monitoring-agent
if newMonSpec, newErr := newEngress.MonitorSpec(); newErr == nil {
if oldMonSpec, oldErr := oldEngress.MonitorSpec(); oldErr == nil {
if !reflect.DeepEqual(oldMonSpec, newMonSpec) {
promCtrl := monitor.NewPrometheusController(ctrl.KubeClient, ctrl.PromClient)
err := promCtrl.UpdateMonitor(ctrl.Ingress, oldMonSpec, newMonSpec)
if err != nil {
return
}
}
if (oldMonSpec == nil && newMonSpec != nil) ||
(oldMonSpec != nil && newMonSpec == nil) {
updateMode |= ingress.UpdateStats
}
}
}
if oldEngress.IsPortChanged(*newEngress) || oldEngress.IsLoadBalancerSourceRangeChanged(*newEngress) {
updateMode |= ingress.UpdateFirewall
} else if oldEngress.IsSecretChanged(*newEngress) {
updateMode |= ingress.RestartHAProxy
} else {
updateMode |= ingress.UpdateConfig
}
if updateMode > 0 {
// For ingress update update HAProxy once
ctrl.Update(updateMode)
}
} else {
ctrl.Create()
}
}
backends := map[string]metav1.ObjectMeta{}
for k, v := range oldEngress.BackendServices() {
backends[k] = v
}
for k, v := range newEngress.BackendServices() {
backends[k] = v
}
for _, meta := range backends {
svc, err := op.KubeClient.CoreV1().Services(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
continue
}
op.ensureServiceAnnotations(newEngress, svc)
}
err := certificate.NewController(op.KubeClient, op.ExtClient, op.Opt, nil).HandleIngress(newEngress)
if err != nil {
log.Error(err)
}
}
func (op *Operator) DeleteEngress(engress *tapi.Ingress) {
ctrl := ingress.NewController(op.KubeClient, op.ExtClient, op.PromClient, op.Opt, engress)
ctrl.Delete()
for _, meta := range engress.BackendServices() {
svc, err := op.KubeClient.CoreV1().Services(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
continue
}
op.ensureServiceAnnotations(engress, svc)
}
}