/
connector_agent.go
413 lines (354 loc) · 14.6 KB
/
connector_agent.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package handlers
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/connector/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/connector/internal/api/dbapi"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/connector/internal/api/private"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/connector/internal/presenters"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/handlers"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/services/signalbus"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/db"
"github.com/getsentry/sentry-go"
"github.com/golang/glog"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/errors"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/services"
"github.com/gorilla/mux"
)
func (h *ConnectorClusterHandler) UpdateConnectorClusterStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
var resource private.ConnectorClusterStatus
cfg := &handlers.HandlerConfig{
MarshalInto: &resource,
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
handlers.Validation("phase", (*string)(&resource.Phase), handlers.IsOneOf(dbapi.AgentRequestConnectorClusterStatus...)),
},
Action: func() (interface{}, *errors.ServiceError) {
convResource := presenters.ConvertConnectorClusterStatus(resource)
err := h.Service.UpdateConnectorClusterStatus(ctx, connectorClusterId, convResource)
if err == nil {
// get empty deleting cluster namespaces
var namespaceList dbapi.ConnectorNamespaceList
namespaceList, err = h.ConnectorNamespace.GetEmptyDeletingNamespaces(connectorClusterId)
if err == nil {
// convert namespace errors to a single error
var errorList []*errors.ServiceError
deletingNamespaces := make(map[string]*dbapi.ConnectorNamespace)
for _, ns := range namespaceList {
deletingNamespaces[ns.ID] = ns
}
for _, namespaceStatus := range resource.Namespaces {
// physical namespace hasn't been deleted yet
delete(deletingNamespaces, namespaceStatus.Id)
err := h.ConnectorNamespace.UpdateConnectorNamespaceStatus(
ctx,
namespaceStatus.Id,
presenters.ConvertConnectorNamespaceDeploymentStatus(namespaceStatus))
if err != nil {
errorList = append(errorList, err)
}
}
// process missing namespaces as deleted
if len(deletingNamespaces) > 0 {
for _, namespace := range deletingNamespaces {
// set namespace phase to deleted
namespace.Status.Phase = dbapi.ConnectorNamespacePhaseDeleted
if err := h.ConnectorNamespace.Update(ctx, namespace); err != nil {
errorList = append(errorList, err)
}
}
h.Bus.Notify("reconcile:connector_namespace")
}
// consolidate namespace update errors
if len(errorList) > 0 {
var msg []string
for _, e := range errorList {
msg = append(msg, e.Error())
}
reason := strings.Join(msg, ";")
reason = strings.Replace(reason, errors.ERROR_CODE_PREFIX, errors.CONNECTOR_MGMT_ERROR_CODE_PREFIX, -1)
err = errors.GeneralError(reason)
}
}
}
return nil, err
},
}
handlers.Handle(w, r, cfg, http.StatusNoContent)
}
func (h *ConnectorClusterHandler) ListDeployments(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query()
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
cfg := &handlers.HandlerConfig{
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
},
Action: func() (interface{}, *errors.ServiceError) {
gtVersion := int64(0)
if v := query.Get("gt_version"); v != "" {
gtVersion, _ = strconv.ParseInt(v, 10, 0)
}
listArgs := services.NewListArguments(query)
getList := func() (list private.ConnectorDeploymentList, err *errors.ServiceError) {
resources, paging, err := h.Service.ListConnectorDeployments(ctx, connectorClusterId, false, false, false, listArgs, gtVersion)
if err != nil {
return
}
list = private.ConnectorDeploymentList{
Kind: "ConnectorDeploymentList",
Page: int32(paging.Page),
Size: int32(paging.Size),
Total: int32(paging.Total),
}
for _, resource := range resources {
converted, serviceError := h.resolveConnectorRefsAndPresentDeployment(resource)
if serviceError != nil {
sentry.CaptureException(serviceError)
glog.Errorf("Failed to present connector deployment %s: %v", resource.ID, serviceError)
// also reduce size and total count
list.Size--
list.Total--
} else {
list.Items = append(list.Items, converted)
}
}
return
}
if v := query.Get("watch"); v == "true" {
idx := 0
list, err := getList()
bookmarkSent := false
sub := h.Bus.Subscribe(fmt.Sprintf("/kafka_connector_clusters/%s/deployments", connectorClusterId))
return handlers.EventStream{
ContentType: "application/json;stream=watch",
Close: sub.Close,
GetNextEvent: func() (interface{}, *errors.ServiceError) {
for { // This function blocks until there is an event to return...
if err != nil {
return nil, err
}
if idx < len(list.Items) {
result := list.Items[idx]
gtVersion = result.Metadata.ResourceVersion
idx += 1
return private.ConnectorDeploymentWatchEvent{
Type: "CHANGE",
Object: result,
}, nil
} else {
// get the next list..
list, err = getList()
if err != nil {
return nil, err
}
idx = 0
// did we run out of items to send?
if len(list.Items) == 0 {
// bookmark idea taken from: https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks
if !bookmarkSent {
bookmarkSent = true
return private.ConnectorDeploymentWatchEvent{
Type: "BOOKMARK",
}, nil
}
// Lets wait for some items to come into the list
// release the DB connection so that we don't tie those up while we wait to poll again..
err := db.Resolve(ctx)
if err != nil {
return nil, errors.GeneralError("internal error")
}
if waitForCancelOrTimeoutOrNotification(ctx, 30*time.Second, sub) {
// ctx was canceled... likely due to the http connection being closed by
// the client. Signal the event stream is done.
return nil, nil
}
// get a new DB connection...
err = db.Begin(ctx)
if err != nil {
return nil, errors.GeneralError("internal error")
}
}
}
}
},
}, nil
} else {
return getList()
}
},
}
handlers.HandleList(w, r, cfg)
}
func (h *ConnectorClusterHandler) resolveConnectorRefsAndPresentDeployment(connectorDeployment dbapi.ConnectorDeployment) (private.ConnectorDeployment, *errors.ServiceError) {
// avoid ignoring this deployment altogether if there is an issue in getting secrets from the vault
invalidSecrets, err := h.Connectors.ResolveConnectorRefsWithBase64Secrets(&connectorDeployment.Connector)
if err != nil {
if invalidSecrets {
// log error in getting secrets and signal that connector spec doesn't have secrets
glog.Errorf("Error getting connector %s with base64 secrets: %s", connectorDeployment.Connector.ID, err)
connectorDeployment.Connector.ConnectorSpec = []byte("{}")
} else {
return private.ConnectorDeployment{}, err
}
}
converted, err := presenters.PresentConnectorDeployment(connectorDeployment, !invalidSecrets)
if err != nil {
return private.ConnectorDeployment{}, err
}
return converted, nil
}
// waitForCancelOrTimeoutOrNotification returns true if the context has been canceled or false after the timeout or sub signal
func waitForCancelOrTimeoutOrNotification(ctx context.Context, timeout time.Duration, sub *signalbus.Subscription) bool {
tc, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
select {
case <-tc.Done():
return false
case <-sub.Signal():
return false
case <-ctx.Done():
return true
}
}
func (h *ConnectorClusterHandler) GetDeployment(w http.ResponseWriter, r *http.Request) {
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
deploymentId := mux.Vars(r)["deployment_id"]
ctx := r.Context()
cfg := &handlers.HandlerConfig{
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
handlers.Validation("deployment_id", &deploymentId, handlers.MinLen(1), handlers.MaxLen(maxConnectorIdLength)),
},
Action: func() (i interface{}, serviceError *errors.ServiceError) {
var resource dbapi.ConnectorDeployment
resource, err := h.Service.GetDeployment(r.Context(), deploymentId)
if err != nil {
return nil, err
}
if resource.ClusterID != connectorClusterId {
return nil, errors.NotFound("Connector deployment not found")
}
return h.resolveConnectorRefsAndPresentDeployment(resource)
},
}
handlers.HandleGet(w, r, cfg)
}
func (h *ConnectorClusterHandler) GetAgentNamespaces(writer http.ResponseWriter, request *http.Request) {
connectorClusterId := mux.Vars(request)["connector_cluster_id"]
ctx := request.Context()
cfg := &handlers.HandlerConfig{
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
},
Action: func() (interface{}, *errors.ServiceError) {
ctx := request.Context()
query := request.URL.Query()
listArgs := services.NewListArguments(query)
gtVersion := int64(0)
if v := query.Get("gt_version"); v != "" {
gtVersion, _ = strconv.ParseInt(v, 10, 0)
}
resources, paging, err := h.ConnectorNamespace.List(ctx, []string{connectorClusterId}, listArgs, gtVersion)
if err != nil {
return nil, err
}
resourceList := private.ConnectorNamespaceDeploymentList{
Kind: "ConnectorNamespaceDeploymentList",
Page: int32(paging.Page),
Size: int32(paging.Size),
Total: int32(paging.Total),
}
for _, resource := range resources {
converted := presenters.PresentConnectorNamespaceDeployment(resource, h.QuotaConfig)
resourceList.Items = append(resourceList.Items, converted)
}
return resourceList, nil
},
}
handlers.HandleList(writer, request, cfg)
}
func (h *ConnectorClusterHandler) doGetNamespace(w http.ResponseWriter, r *http.Request, presenter func(*dbapi.ConnectorNamespace, *config.ConnectorsQuotaConfig) interface{}) {
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
namespaceId := mux.Vars(r)["namespace_id"]
ctx := r.Context()
cfg := &handlers.HandlerConfig{
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
handlers.Validation("namespace_id", &namespaceId, handlers.MinLen(1), handlers.MaxLen(maxConnectorNamespaceIdLength)),
},
Action: func() (i interface{}, serviceError *errors.ServiceError) {
var resource *dbapi.ConnectorNamespace
resource, err := h.ConnectorNamespace.Get(r.Context(), namespaceId)
if err != nil {
return nil, err
}
if resource.ClusterId != connectorClusterId {
return nil, errors.NotFound("Connector namespace %s not found", namespaceId)
}
return presenter(resource, h.QuotaConfig), nil
},
}
handlers.HandleGet(w, r, cfg)
}
func (h *ConnectorClusterHandler) GetNamespace(w http.ResponseWriter, r *http.Request) {
h.doGetNamespace(w, r, func(ns *dbapi.ConnectorNamespace, quotaConfig *config.ConnectorsQuotaConfig) interface{} {
return presenters.PresentConnectorNamespace(ns, quotaConfig)
})
}
func (h *ConnectorClusterHandler) GetAgentNamespace(w http.ResponseWriter, r *http.Request) {
h.doGetNamespace(w, r, func(ns *dbapi.ConnectorNamespace, quotaConfig *config.ConnectorsQuotaConfig) interface{} {
return presenters.PresentConnectorNamespaceDeployment(ns, quotaConfig)
})
}
func (h *ConnectorClusterHandler) UpdateDeploymentStatus(w http.ResponseWriter, r *http.Request) {
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
deploymentId := mux.Vars(r)["deployment_id"]
var resource private.ConnectorDeploymentStatus
ctx := r.Context()
cfg := &handlers.HandlerConfig{
MarshalInto: &resource,
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
handlers.Validation("deployment_id", &deploymentId, handlers.MinLen(1), handlers.MaxLen(maxConnectorIdLength)),
handlers.Validation("phase", (*string)(&resource.Phase), handlers.IsOneOf(dbapi.AgentConnectorStatusPhase...)),
},
Action: func() (interface{}, *errors.ServiceError) {
ctx := r.Context()
converted, serr := presenters.ConvertConnectorDeploymentStatus(resource)
if serr != nil {
return nil, serr
}
converted.ID = deploymentId
err := h.Service.UpdateConnectorDeploymentStatus(ctx, converted)
return nil, err
},
}
handlers.Handle(w, r, cfg, http.StatusNoContent)
}
func (h *ConnectorClusterHandler) UpdateNamespaceStatus(w http.ResponseWriter, r *http.Request) {
connectorClusterId := mux.Vars(r)["connector_cluster_id"]
namespaceId := mux.Vars(r)["namespace_id"]
var resource private.ConnectorNamespaceDeploymentStatus
ctx := r.Context()
cfg := &handlers.HandlerConfig{
MarshalInto: &resource,
Validate: []handlers.Validate{
handlers.Validation("connector_cluster_id", &connectorClusterId, handlers.MinLen(1), handlers.MaxLen(maxConnectorClusterIdLength), validateConnectorClusterId(ctx, h.Service)),
handlers.Validation("namespace_id", &namespaceId, handlers.MinLen(1), handlers.MaxLen(maxConnectorNamespaceIdLength)),
},
Action: func() (interface{}, *errors.ServiceError) {
ctx := r.Context()
converted := presenters.ConvertConnectorNamespaceDeploymentStatus(resource)
err := h.ConnectorNamespace.UpdateConnectorNamespaceStatus(ctx, namespaceId, converted)
return nil, err
},
}
handlers.Handle(w, r, cfg, http.StatusNoContent)
}