This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgetKnativeEventConfig.js
More file actions
181 lines (168 loc) · 4.42 KB
/
Copy pathgetKnativeEventConfig.js
File metadata and controls
181 lines (168 loc) · 4.42 KB
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
'use strict'
const validEvents = ['custom', 'cron', 'gcpPubSub', 'awsSqs', 'kafka', 'sinkBinding']
const knativeVersion = 'v1'
// TODO: update this when we're dealing with services other
// than the ones deployed by the Serverless Framework (e.g. K8S services)
function getRef(sinkName) {
return {
apiVersion: `serving.knative.dev/${knativeVersion}`,
kind: 'Service',
name: sinkName
}
}
function getBrokerRef(brokerName) {
return {
apiVersion: `eventing.knative.dev/${knativeVersion}`,
kind: 'Broker',
name: brokerName
}
}
function getCronConfig(sinkName, eventConfig) {
const { schedule, data } = eventConfig
if (!schedule) {
throw new Error('"schedule" configuration missing for cron event.')
}
if (!data) {
throw new Error('"data" configuration missing for cron event.')
}
return {
kind: 'CronJobSource',
knativeGroup: 'sources.eventing.knative.dev',
knativeVersion,
spec: {
schedule,
data,
sink: getRef(sinkName)
}
}
}
function getGcpPubSubConfig(sinkName, eventConfig) {
const { project, topic } = eventConfig
if (!project) {
throw new Error('"project" configuration missing for gcpPubSub event.')
}
if (!topic) {
throw new Error('"topic" configuration missing for gcpPubSub event.')
}
return {
kind: 'PullSubscription',
knativeGroup: 'pubsub.cloud.run',
knativeVersion,
spec: {
project,
topic,
sink: getRef(sinkName)
}
}
}
function getAwsSqsConfig(sinkName, eventConfig) {
const { secretName, secretKey, queue } = eventConfig
if (!secretName) {
throw new Error('"secretName" configuration missing for awsSqs event.')
}
if (!secretKey) {
throw new Error('"secretKey" configuration missing for awsSqs event.')
}
if (!queue) {
throw new Error('"queue" configuration missing for awsSqs event.')
}
return {
kind: 'AwsSqsSource',
knativeGroup: 'sources.eventing.knative.dev',
knativeVersion,
spec: {
awsCredsSecret: {
name: secretName,
key: secretKey
},
queueUrl: queue,
sink: getRef(sinkName)
}
}
}
function getKafkaConfig(sinkName, eventConfig) {
let resources = eventConfig.resources // eslint-disable-line prefer-destructuring
const { consumerGroup, bootstrapServers, topics } = eventConfig
if (!consumerGroup) {
throw new Error('"consumerGroup" configuration missing for kafka event.')
}
if (!bootstrapServers.length) {
throw new Error('"bootstrapServers" configuration can\'t be empty for kafka event.')
}
if (!topics.length) {
throw new Error('"topics" configuration can\'t be empty for kafka event.')
}
if (!resources) {
resources = {
limits: {
cpu: '250m',
memory: '512Mi'
},
requests: {
cpu: '250m',
memory: '512Mi'
}
}
}
return {
kind: 'KafkaSource',
knativeGroup: 'sources.eventing.knative.dev',
knativeVersion,
spec: {
consumerGroup,
bootstrapServers: bootstrapServers.join(','),
topics: topics.join(','),
resources,
sink: getRef(sinkName)
}
}
}
function getCustomConfig(sinkName, eventConfig) {
const { filter } = eventConfig
return {
kind: 'Trigger',
knativeGroup: 'eventing.knative.dev',
knativeVersion,
configName: eventConfig.name,
spec: {
broker: "default",
filter,
subscriber: {
ref: getRef(sinkName)
}
}
}
}
function getSinkBindingConfig(sinkName, eventConfig) {
const { filter } = eventConfig
return {
kind: 'SinkBinding',
knativeGroup: 'sources.knative.dev',
knativeVersion,
spec: {
sink: {
ref: getBrokerRef("default")
},
subject: getRef(sinkName)
}
}
}
function getKnativeEventConfig(sinkName, eventName, eventConfig) {
if (!validEvents.includes(eventName)) {
this.serverless.cli.log(`Unknown event "${eventName}"`)
return false
}
if (eventName === 'cron') {
return getCronConfig(sinkName, eventConfig)
} else if (eventName === 'gcpPubSub') {
return getGcpPubSubConfig(sinkName, eventConfig)
} else if (eventName === 'awsSqs') {
return getAwsSqsConfig(sinkName, eventConfig)
} else if (eventName === 'kafka') {
return getKafkaConfig(sinkName, eventConfig)
} else if (eventName === 'sinkBinding') {
return getSinkBindingConfig(sinkName, eventConfig)
}
return getCustomConfig(sinkName, eventConfig)
}
module.exports = getKnativeEventConfig