-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
81 lines (63 loc) · 2.02 KB
/
adapter.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
package cloudeventssource
import (
"context"
"fmt"
"go.uber.org/zap"
cloudevents "github.com/cloudevents/sdk-go/v2"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
pkgadapter "knative.dev/eventing/pkg/adapter/v2"
"knative.dev/pkg/logging"
"github.com/zeiss/typhoon/pkg/adapter/fs"
"github.com/zeiss/typhoon/pkg/apis/sources"
"github.com/zeiss/typhoon/pkg/sources/adapter/cloudeventssource/ratelimiter"
)
// NewAdapter satisfies pkgadapter.AdapterConstructor.
func NewAdapter(ctx context.Context, envAcc pkgadapter.EnvConfigAccessor, ceClient cloudevents.Client) pkgadapter.Adapter {
logger := logging.FromContext(ctx)
mt := &pkgadapter.MetricTag{
ResourceGroup: sources.CloudEventsSourceResource.String(),
Namespace: envAcc.GetNamespace(),
Name: envAcc.GetName(),
}
env := envAcc.(*envAccessor)
cfw, err := fs.NewCachedFileWatcher(logger)
if err != nil {
logger.Panicw("Could not create a file watcher", zap.Error(err))
}
for _, as := range env.BasicAuths {
if err := cfw.Add(as.MountedValueFile); err != nil {
logger.Panicw(
fmt.Sprintf("Authentication secret at %q could not be watched", as.MountedValueFile),
zap.Error(err))
}
}
ceh := &cloudEventsHandler{
basicAuths: env.BasicAuths,
cfw: cfw,
ceClient: ceClient,
logger: logger,
mt: mt,
}
// prepare CE server options
options := []cehttp.Option{}
if env.Path != "" {
options = append(options, cehttp.WithPath(env.Path))
}
if len(env.BasicAuths) != 0 {
options = append(options, cehttp.WithMiddleware(ceh.handleAuthentication))
}
if env.RequestsPerSecond != 0 {
rl, err := ratelimiter.New(env.RequestsPerSecond)
if err != nil {
logger.Panicw("Could not create rate limiter", zap.Error(err))
}
options = append(options, cehttp.WithRateLimiter(rl))
}
ceServer, err := cloudevents.NewClientHTTP(options...)
if err != nil {
logger.Panicw("Error creating CloudEvents client", zap.Error(err))
}
ceh.ceServer = ceServer
return ceh
}
var _ pkgadapter.Adapter = (*cloudEventsHandler)(nil)