This repository was archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgostint-coupler.py
67 lines (53 loc) · 1.77 KB
/
gostint-coupler.py
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
"""
kubeless function deploy gostint-coupler \
--from-file gostint-coupler.py \
--handler gostint-coupler.handler \
--runtime python3.7 \
--dependencies requirements.txt
kubeless function update gostint-coupler \
--from-file gostint-coupler.py
kubeless trigger kafka create gostint-coupler \
--function-selector created-by=kubeless,function=gostint-coupler \
--trigger-topic automation_v1_request
kubeless function delete gostint-coupler
curl -sS http://127.0.0.1:3303/automation/v1 \
-X POST \
-H "Content-type: application/json" \
--data '{"foo":"bar"}'
{"data":{"data":{"value":"my response"},"event_uuid":"2a8bdf29-8568-11e9-86b3-0242ac110002"}
"""
import sys
import traceback
import json
import base64
import tempfile
from kubernetes import client, config
from kafka import KafkaProducer
config.load_incluster_config()
v1 = client.CoreV1Api()
def handler(event, context):
print("event:", event)
# establish the producer for each function call, cannot be global...
producer = KafkaProducer(
bootstrap_servers=['kafka.kubeless.svc.cluster.local:9092'])
#############################
# Call backend api goes here
#############################
# Return response event
try:
# mock response for test
response = {
"event_uuid": event["data"]["event_uuid"],
"data": {
"value": "my response"
}
}
new_event = bytearray(json.dumps(response), encoding='utf-8')
producer.send('automation_v1_response', key=b'event',
value=new_event).get(timeout=30)
# m = future.get(timeout=30)
producer.flush(timeout=5)
except Exception as err:
print(str(err))
print(traceback.format_exc())
return