-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
167 lines (125 loc) · 3.72 KB
/
app.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
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
from flask import Flask
from flask import request
from flask import jsonify
import threading
import os
import time
import argparse
app = Flask(__name__)
def getid(node):
ret = "unknown"
sl = node.split('~')
if len(sl) > 2:
ret = sl[2]
return ret
# example
# /listeners/istio-proxy/sidecar~10.32.1.20~httpbin-57db476f4d-svs9h.default~default.svc.cluster.local
@app.route('/listeners/<cluster>/<node>', methods=['POST'])
def lds(cluster, node):
op = insert_lua(request.get_json(), getid(node))
return jsonify(op)
# example
# /clusters/istio-proxy/sidecar~10.32.1.20~httpbin-57db476f4d-svs9h.default~default.svc.cluster.local
@app.route('/clusters/<cluster>/<node>', methods=['POST'])
def cds(cluster, node):
output = request.data
return output
# example
# /routes/15003/istio-proxy/sidecar~10.32.1.20~httpbin-57db476f4d-svs9h.default~default.svc.cluster.local
@app.route('/routes/<name>/<cluster>/<node>', methods=['POST'])
def rds(name, cluster, node):
output = request.data
return output
"""
# example listener configuration
listeners:
- address: tcp://0.0.0.0:80
bind_to_port: true
filters:
- name: http_connection_manager
config:
access_log:
- path: /dev/stdout
codec_type: auto
filters:
- name: mixer
config: {}
- name: lua
config:
inline_code: <code>
"""
#
# inserts lua as a filter in the http_connection_manager
#
def insert_lua(listeners, nodeid):
for l in listeners.get("listeners", []):
for f in l.get("filters", []):
if f["name"] != "http_connection_manager":
continue
ff = f["config"].get("filters", [])
ff.insert(0, lua_config(nodeid))
return listeners
def lua_config(nodeid):
s = FILE_STORE[SCRIPT]
return {"name": "lua",
"config":
{"inline_code": s.format(nodeid=nodeid)}}
DEFAULT_LUA_SCRIPT = """
-- Called on the request path.
function envoy_on_request(request_handle)
request_handle:headers():add("x-lua-header", "true")
end
-- Called on the response path.
function envoy_on_response(response_handle)
response_handle:headers():add("x-lua-resp-header", "{nodeid}")
end
"""
SCRIPT = "SCRIPT"
FILE_STORE = {
SCRIPT: DEFAULT_LUA_SCRIPT
}
# polls for file chage
class poller(object):
def __init__(self, filepath, cfg):
self.filepath = filepath
self.done = False
self.cfg = cfg
def cancel(self):
self.done = True
def __call__(self):
modtime = 0
while not self.done:
modtime = self.read_if_changed(modtime)
time.sleep(5)
def read_if_changed(self, modtime):
if not os.path.isfile(self.filepath):
print self.filepath, "not found"
return modtime
new_modtime = os.path.getmtime(self.filepath)
if new_modtime == modtime:
return modtime
with open(self.filepath, "rt") as fl:
ls = fl.read()
print "File updated"
print ls
self.cfg[SCRIPT] = ls
return new_modtime
def get_args_parser():
parser = argparse.ArgumentParser(
description="Run pilot webhook")
parser.add_argument("--script", help="path of the lua script to inject",
default="scripts/plugin.lua")
parser.add_argument("--port", help="port to listen on",
type=int, default=5000)
return parser
def main(args):
p = poller(args.script, FILE_STORE)
threading.Thread(target=p).start()
ret = app.run(host="0.0.0.0", port=args.port)
p.cancel()
return ret
if __name__ == "__main__":
import sys
parser = get_args_parser()
args = parser.parse_args()
sys.exit(main(args))