-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
subscriptions.go
483 lines (425 loc) · 13.1 KB
/
subscriptions.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package pubsub
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
subscriptionsapiV1alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v1alpha1"
subscriptionsapiV2alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v2alpha1"
"github.com/dapr/dapr/pkg/channel"
"github.com/dapr/dapr/pkg/expr"
invokev1 "github.com/dapr/dapr/pkg/messaging/v1"
operatorv1pb "github.com/dapr/dapr/pkg/proto/operator/v1"
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
"github.com/dapr/dapr/pkg/resiliency"
"github.com/dapr/kit/logger"
"github.com/dapr/kit/utils"
)
const (
getTopicsError = "error getting topic list from app: %s"
deserializeTopicsError = "error getting topics from app: %s"
noSubscriptionsError = "user app did not subscribe to any topic"
subscriptionKind = "Subscription"
APIVersionV1alpha1 = "dapr.io/v1alpha1"
APIVersionV2alpha1 = "dapr.io/v2alpha1"
)
type (
SubscriptionJSON struct {
PubsubName string `json:"pubsubname"`
Topic string `json:"topic"`
DeadLetterTopic string `json:"deadLetterTopic"`
Metadata map[string]string `json:"metadata,omitempty"`
Route string `json:"route"` // Single route from v1alpha1
Routes RoutesJSON `json:"routes"` // Multiple routes from v2alpha1
BulkSubscribe BulkSubscribeJSON `json:"bulkSubscribe,omitempty"`
}
RoutesJSON struct {
Rules []*RuleJSON `json:"rules,omitempty"`
Default string `json:"default,omitempty"`
}
BulkSubscribeJSON struct {
Enabled bool `json:"enabled"`
MaxMessagesCount int32 `json:"maxMessagesCount,omitempty"`
MaxAwaitDurationMs int32 `json:"maxAwaitDurationMs,omitempty"`
}
RuleJSON struct {
Match string `json:"match"`
Path string `json:"path"`
}
)
func GetSubscriptionsHTTP(ctx context.Context, channel channel.AppChannel, log logger.Logger, r resiliency.Provider) ([]Subscription, error) {
req := invokev1.NewInvokeMethodRequest("dapr/subscribe").
WithHTTPExtension(http.MethodGet, "").
WithContentType(invokev1.JSONContentType)
defer req.Close()
policyDef := r.BuiltInPolicy(resiliency.BuiltInInitializationRetries)
if policyDef != nil && policyDef.HasRetries() {
req.WithReplay(true)
}
policyRunner := resiliency.NewRunnerWithOptions(ctx, policyDef,
resiliency.RunnerOpts[*invokev1.InvokeMethodResponse]{
Disposer: resiliency.DisposerCloser[*invokev1.InvokeMethodResponse],
},
)
resp, err := policyRunner(func(ctx context.Context) (*invokev1.InvokeMethodResponse, error) {
return channel.InvokeMethod(ctx, req, "")
})
if err != nil {
return nil, err
}
defer resp.Close()
var (
subscriptions []Subscription
subscriptionItems []SubscriptionJSON
)
switch resp.Status().GetCode() {
case http.StatusOK:
err = json.NewDecoder(resp.RawData()).Decode(&subscriptionItems)
if err != nil {
err = fmt.Errorf(deserializeTopicsError, err)
log.Error(err)
return nil, err
}
subscriptions = make([]Subscription, len(subscriptionItems))
for i, si := range subscriptionItems {
// Look for single route field and append it as a route struct.
// This preserves backward compatibility.
rules := make([]*Rule, len(si.Routes.Rules)+1)
n := 0
for _, r := range si.Routes.Rules {
rule, err := createRoutingRule(r.Match, r.Path)
if err != nil {
return nil, err
}
rules[n] = rule
n++
}
// If a default path is set, add a rule with a nil `Match`,
// which is treated as `true` and always selected if
// no previous rules match.
if si.Routes.Default != "" {
rules[n] = &Rule{
Path: si.Routes.Default,
}
n++
} else if si.Route != "" {
rules[n] = &Rule{
Path: si.Route,
}
n++
}
bulkSubscribe := &BulkSubscribe{
Enabled: si.BulkSubscribe.Enabled,
MaxMessagesCount: si.BulkSubscribe.MaxMessagesCount,
MaxAwaitDurationMs: si.BulkSubscribe.MaxAwaitDurationMs,
}
subscriptions[i] = Subscription{
PubsubName: si.PubsubName,
Topic: si.Topic,
Metadata: si.Metadata,
DeadLetterTopic: si.DeadLetterTopic,
Rules: rules[:n],
BulkSubscribe: bulkSubscribe,
}
}
case http.StatusNotFound:
log.Debug(noSubscriptionsError)
default:
// Unexpected response: both GRPC and HTTP have to log the same level.
log.Errorf("app returned http status code %v from subscription endpoint", resp.Status().GetCode())
}
log.Debugf("app responded with subscriptions %v", subscriptions)
return filterSubscriptions(subscriptions, log), nil
}
func filterSubscriptions(subscriptions []Subscription, log logger.Logger) []Subscription {
i := 0
for _, s := range subscriptions {
if len(s.Rules) == 0 {
log.Warnf("topic %s has an empty routes. removing from subscriptions list", s.Topic)
continue
}
subscriptions[i] = s
i++
}
return subscriptions[:i]
}
func GetSubscriptionsGRPC(ctx context.Context, channel runtimev1pb.AppCallbackClient, log logger.Logger, r resiliency.Provider) ([]Subscription, error) {
policyRunner := resiliency.NewRunner[*runtimev1pb.ListTopicSubscriptionsResponse](ctx,
r.BuiltInPolicy(resiliency.BuiltInInitializationRetries),
)
resp, err := policyRunner(func(ctx context.Context) (*runtimev1pb.ListTopicSubscriptionsResponse, error) {
rResp, rErr := channel.ListTopicSubscriptions(ctx, &emptypb.Empty{})
if rErr != nil {
s, ok := status.FromError(rErr)
if ok && s != nil {
if s.Code() == codes.Unimplemented {
log.Infof("pubsub subscriptions: gRPC app does not implement ListTopicSubscriptions")
return new(runtimev1pb.ListTopicSubscriptionsResponse), nil
}
}
}
return rResp, rErr
})
if err != nil {
// Unexpected response: both GRPC and HTTP have to log the same level.
log.Errorf(getTopicsError, err)
return nil, err
}
var subscriptions []Subscription
if len(resp.GetSubscriptions()) == 0 {
log.Debug(noSubscriptionsError)
} else {
subscriptions = make([]Subscription, len(resp.GetSubscriptions()))
for i, s := range resp.GetSubscriptions() {
rules, err := parseRoutingRulesGRPC(s.GetRoutes())
if err != nil {
return nil, err
}
var bulkSubscribe *BulkSubscribe
if s.GetBulkSubscribe() != nil {
bulkSubscribe = &BulkSubscribe{
Enabled: s.GetBulkSubscribe().GetEnabled(),
MaxMessagesCount: s.GetBulkSubscribe().GetMaxMessagesCount(),
MaxAwaitDurationMs: s.GetBulkSubscribe().GetMaxAwaitDurationMs(),
}
}
subscriptions[i] = Subscription{
PubsubName: s.GetPubsubName(),
Topic: s.GetTopic(),
Metadata: s.GetMetadata(),
DeadLetterTopic: s.GetDeadLetterTopic(),
Rules: rules,
BulkSubscribe: bulkSubscribe,
}
}
}
return subscriptions, nil
}
// DeclarativeLocal loads subscriptions from the given local resources path.
func DeclarativeLocal(resourcesPaths []string, namespace string, log logger.Logger) (subs []Subscription) {
for _, path := range resourcesPaths {
res := declarativeFile(path, namespace, log)
if len(res) > 0 {
subs = append(subs, res...)
}
}
return subs
}
// Used by DeclarativeLocal to load a single path.
func declarativeFile(resourcesPath string, namespace string, log logger.Logger) (subs []Subscription) {
if _, err := os.Stat(resourcesPath); os.IsNotExist(err) {
return subs
}
files, err := os.ReadDir(resourcesPath)
if err != nil {
log.Errorf("Failed to read subscriptions from path %s: %s", resourcesPath, err)
return subs
}
for _, f := range files {
if f.IsDir() {
continue
}
if !utils.IsYaml(f.Name()) {
log.Warnf("A non-YAML pubsub file %s was detected, it will not be loaded", f.Name())
continue
}
filePath := filepath.Join(resourcesPath, f.Name())
b, err := os.ReadFile(filePath)
if err != nil {
log.Warnf("Failed to read file %s: %v", f.Name(), err)
continue
}
bytesArray := bytes.Split(b, []byte("\n---"))
for _, item := range bytesArray {
// Skip empty items
item = bytes.TrimSpace(item)
if len(item) == 0 {
continue
}
subs, err = appendSubscription(subs, item, namespace)
if err != nil {
log.Warnf("Failed to add subscription from file %s: %v", f.Name(), err)
continue
}
}
}
return subs
}
func unmarshalSubscription(b []byte, namespace string) (*Subscription, error) {
// Parse only the type metadata first in order
// to filter out non-Subscriptions without other errors.
type typeInfo struct {
metav1.TypeMeta `json:",inline"`
}
var ti typeInfo
if err := yaml.Unmarshal(b, &ti); err != nil {
return nil, err
}
if ti.Kind != subscriptionKind {
return nil, nil
}
switch ti.APIVersion {
case APIVersionV2alpha1:
// "v2alpha1" is the CRD that introduces pubsub routing.
var sub subscriptionsapiV2alpha1.Subscription
if err := yaml.Unmarshal(b, &sub); err != nil {
return nil, err
}
if namespace != "" && sub.Namespace != "" && sub.Namespace != namespace {
return nil, nil
}
rules, err := parseRoutingRulesYAML(sub.Spec.Routes)
if err != nil {
return nil, err
}
return &Subscription{
Topic: sub.Spec.Topic,
PubsubName: sub.Spec.Pubsubname,
Rules: rules,
Metadata: sub.Spec.Metadata,
Scopes: sub.Scopes,
DeadLetterTopic: sub.Spec.DeadLetterTopic,
BulkSubscribe: &BulkSubscribe{
Enabled: sub.Spec.BulkSubscribe.Enabled,
MaxMessagesCount: sub.Spec.BulkSubscribe.MaxMessagesCount,
MaxAwaitDurationMs: sub.Spec.BulkSubscribe.MaxAwaitDurationMs,
},
}, nil
default:
// assume "v1alpha1" for backward compatibility as this was
// not checked before the introduction of "v2alpha".
var sub subscriptionsapiV1alpha1.Subscription
if err := yaml.Unmarshal(b, &sub); err != nil {
return nil, err
}
if namespace != "" && sub.Namespace != "" && sub.Namespace != namespace {
return nil, nil
}
return &Subscription{
Topic: sub.Spec.Topic,
PubsubName: sub.Spec.Pubsubname,
Rules: []*Rule{
{
Path: sub.Spec.Route,
},
},
Metadata: sub.Spec.Metadata,
Scopes: sub.Scopes,
DeadLetterTopic: sub.Spec.DeadLetterTopic,
BulkSubscribe: &BulkSubscribe{
Enabled: sub.Spec.BulkSubscribe.Enabled,
MaxMessagesCount: sub.Spec.BulkSubscribe.MaxMessagesCount,
MaxAwaitDurationMs: sub.Spec.BulkSubscribe.MaxAwaitDurationMs,
},
}, nil
}
}
func parseRoutingRulesYAML(routes subscriptionsapiV2alpha1.Routes) ([]*Rule, error) {
r := make([]*Rule, len(routes.Rules)+1)
var (
n int
err error
)
for _, rule := range routes.Rules {
r[n], err = createRoutingRule(rule.Match, rule.Path)
if err != nil {
return nil, err
}
n++
}
// If a default path is set, add a rule with a nil `Match`,
// which is treated as `true` and always selected if
// no previous rules match.
if routes.Default != "" {
r[n] = &Rule{
Path: routes.Default,
}
n++
}
return r[:n], nil
}
func parseRoutingRulesGRPC(routes *runtimev1pb.TopicRoutes) ([]*Rule, error) {
if routes == nil {
return []*Rule{{
Path: "",
}}, nil
}
r := make([]*Rule, 0, len(routes.GetRules())+1)
for _, rule := range routes.GetRules() {
rr, err := createRoutingRule(rule.GetMatch(), rule.GetPath())
if err != nil {
return nil, err
}
r = append(r, rr)
}
// If a default path is set, add a rule with a nil `Match`,
// which is treated as `true` and always selected if
// no previous rules match.
if routes.GetDefault() != "" {
r = append(r, &Rule{
Path: routes.GetDefault(),
})
}
// gRPC automatically specifies a default route
// if none are returned.
if len(r) == 0 {
r = append(r, &Rule{
Path: "",
})
}
return r, nil
}
func createRoutingRule(match, path string) (*Rule, error) {
var e *expr.Expr
matchTrimmed := strings.TrimSpace(match)
if matchTrimmed != "" {
e = &expr.Expr{}
if err := e.DecodeString(matchTrimmed); err != nil {
return nil, err
}
}
return &Rule{
Match: e,
Path: path,
}, nil
}
// DeclarativeKubernetes loads subscriptions from the operator when running in Kubernetes.
func DeclarativeKubernetes(ctx context.Context, client operatorv1pb.OperatorClient, podName string, namespace string, log logger.Logger) []Subscription {
var subs []Subscription
resp, err := client.ListSubscriptionsV2(ctx, &operatorv1pb.ListSubscriptionsRequest{
PodName: podName,
Namespace: namespace,
})
if err != nil {
log.Errorf("Failed to list subscriptions from operator: %s", err)
return subs
}
for _, s := range resp.GetSubscriptions() {
// No namespace filtering here as it's been already filtered by the operator
subs, err = appendSubscription(subs, s, "")
if err != nil {
log.Warnf("Failed to add subscription from operator: %s", err)
continue
}
}
return subs
}
func appendSubscription(list []Subscription, subBytes []byte, namespace string) ([]Subscription, error) {
sub, err := unmarshalSubscription(subBytes, namespace)
if err != nil {
return nil, err
}
if sub != nil {
list = append(list, *sub)
}
return list, nil
}