generated from layer5io/layer5-repo-template
/
handlers.go
263 lines (233 loc) · 7.14 KB
/
handlers.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package meshsync
import (
"fmt"
"time"
"github.com/layer5io/meshkit/broker"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/kubernetes"
"github.com/layer5io/meshsync/internal/channels"
"github.com/layer5io/meshsync/internal/config"
"github.com/layer5io/meshsync/pkg/model"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
func debounce(d time.Duration, f func(ch chan struct{})) func(ch chan struct{}) {
timer := time.NewTimer(d)
return func(pipelineCh chan struct{}) {
timer.Stop()
timer = time.NewTimer(d)
<-timer.C
f(pipelineCh)
timer.Reset(d)
timer.Stop()
}
}
func (h *Handler) Run() {
pipelineCh := make(chan struct{})
go h.startDiscovery(pipelineCh)
debouncedStartDiscovery := debounce(time.Second*5, func(pipelinechannel chan struct{}) {
if !utils.IsClosed[struct{}](pipelinechannel) {
h.Log.Info("closing previous instance ")
close(pipelinechannel)
}
pipelineCh = make(chan struct{})
err := h.UpdateInformer()
if err != nil {
h.Log.Error(err)
}
h.Log.Info("starting over")
h.startDiscovery(pipelineCh)
})
for range h.channelPool[channels.ReSync].(channels.ReSyncChannel) {
go debouncedStartDiscovery(pipelineCh)
}
}
func (h *Handler) UpdateInformer() error {
dynamicClient, err := dynamic.NewForConfig(&h.restConfig)
if err != nil {
return ErrNewInformer(err)
}
listOptionsFunc, err := GetListOptionsFunc(h.Config)
if err != nil {
return err
}
h.informer = GetDynamicInformer(h.Config, dynamicClient, listOptionsFunc)
return nil
}
func (h *Handler) ListenToRequests() {
listenerConfigs := make(map[string]config.ListenerConfig, 10)
err := h.Config.GetObject(config.ListenersKey, &listenerConfigs)
if err != nil {
h.Log.Error(ErrGetObject(err))
}
h.Log.Info("Listening for requests in: ", listenerConfigs[config.RequestStream].SubscribeTo)
reqChan := make(chan *broker.Message)
err = h.Broker.SubscribeWithChannel(listenerConfigs[config.RequestStream].SubscribeTo, listenerConfigs[config.RequestStream].ConnectionName, reqChan)
if err != nil {
h.Log.Error(ErrSubscribeRequest(err))
}
for request := range reqChan {
if request.Request == nil {
h.Log.Error(ErrInvalidRequest)
continue
}
switch request.Request.Entity {
case broker.LogRequestEntity:
h.Log.Info("Starting log session")
err := h.processLogRequest(request.Request.Payload, listenerConfigs[config.LogStream])
if err != nil {
h.Log.Error(err)
continue
}
// TODO: Add this to the broker pkg
case "informer-store":
d, err := utils.Marshal(request.Request.Payload)
// TODO: Update broker pkg in Meshkit to include Reply types
var payload struct{ Reply string }
if err != nil {
h.Log.Error(err)
continue
}
err = utils.Unmarshal(d, &payload)
if err != nil {
h.Log.Error(err)
continue
}
replySubject := payload.Reply
storeObjects := h.listStoreObjects()
splitSlices := splitIntoMultipleSlices(storeObjects, 5) // performance of NATS is bound to degrade if huge messages are sent
h.Log.Info("Publishing the data from informer stores to the subject: ", replySubject)
for _, val := range splitSlices {
err = h.Broker.Publish(replySubject, &broker.Message{
Object: val,
})
if err != nil {
h.Log.Error(err)
continue
}
}
case broker.ReSyncDiscoveryEntity:
h.Log.Info("Resyncing")
h.channelPool[channels.ReSync].(channels.ReSyncChannel) <- struct{}{}
case broker.ExecRequestEntity:
h.Log.Info("Starting interactive session")
err := h.processExecRequest(request.Request.Payload, listenerConfigs[config.ExecShell])
if err != nil {
h.Log.Error(err)
continue
}
case broker.ActiveExecEntity:
h.Log.Info("Connecting to channel pool")
err := h.processActiveExecRequest()
if err != nil {
h.Log.Error(err)
continue
}
case "meshsync-meta":
h.Log.Info("Publishing MeshSync metadata to the subject")
err := h.Broker.Publish("meshsync-meta", &broker.Message{
Object: config.Server["version"],
})
if err != nil {
h.Log.Error(err)
continue
}
}
}
}
func (h *Handler) listStoreObjects() []model.KubernetesResource {
objects := make([]interface{}, 0)
for _, v := range h.stores {
objects = append(objects, v.List()...)
}
parsedObjects := make([]model.KubernetesResource, 0)
for _, obj := range objects {
parsedObjects = append(parsedObjects, model.ParseList(*obj.(*unstructured.Unstructured), broker.Add))
}
return parsedObjects
}
func (h *Handler) WatchCRDs() {
kubeclient, err := kubernetes.New(nil)
if err != nil {
h.Log.Error(err)
return
}
crdWatcher, err := kubeclient.DynamicKubeClient.Resource(schema.GroupVersionResource{
Group: "apiextensions.k8s.io",
Version: "v1",
Resource: "customresourcedefinitions",
}).Watch(context.Background(), metav1.ListOptions{})
if err != nil {
h.Log.Error(err)
return
}
for event := range crdWatcher.ResultChan() {
crd := &kubernetes.CRDItem{}
byt, err := utils.Marshal(event.Object)
if err != nil {
h.Log.Error(err)
continue
}
err = utils.Unmarshal(byt, crd)
if err != nil {
h.Log.Error(err)
continue
}
gvr := kubernetes.GetGVRForCustomResources(crd)
existingPipelines := config.Pipelines
err = h.Config.GetObject(config.ResourcesKey, existingPipelines)
if err != nil {
h.Log.Error(err)
continue
}
existingPipelineConfigs := existingPipelines[config.GlobalResourceKey]
configName := fmt.Sprintf("%s.%s.%s", gvr.Resource, gvr.Version, gvr.Group)
updatedPipelineConfigs := existingPipelineConfigs
switch event.Type {
case watch.Added:
// No need to verify if config is already added because If the config already exists then it indicates the informer has already synced that resource.
// Any subsequent updates will have event type as "modified"
updatedPipelineConfigs = existingPipelineConfigs.Add(config.PipelineConfig{
Name: configName,
PublishTo: config.DefaultPublishingSubject,
Events: []string{"ADDED", "MODIFIED", "DELETED"},
})
case watch.Deleted:
updatedPipelineConfigs = existingPipelineConfigs.Delete(config.PipelineConfig{
Name: configName,
})
}
existingPipelines[config.GlobalResourceKey] = updatedPipelineConfigs
err = h.Config.SetObject(config.ResourcesKey, existingPipelines)
if err != nil {
h.Log.Error(err)
h.Log.Info("skipping informer resync")
return
}
h.channelPool[channels.ReSync].(channels.ReSyncChannel).ReSyncInformer()
}
}
// TODO: move this to meshkit
// given [1,2,3,4,5,6,7,5,4,4] and 3 as its arguments, it would
// return [[1,2,3], [4,5,6], [7,5,4], [4]]
func splitIntoMultipleSlices(s []model.KubernetesResource, maxItmsPerSlice int) []([]model.KubernetesResource) {
result := make([]([]model.KubernetesResource), 0)
temp := make([]model.KubernetesResource, 0)
for idx, val := range s {
temp = append(temp, val)
if ((idx + 1) % maxItmsPerSlice) == 0 {
result = append(result, temp)
temp = nil
}
if idx+1 == len(s) {
if len(temp) != 0 {
result = append(result, temp)
}
}
}
return result
}