Skip to content

Commit 35a7ea7

Browse files
author
Christopher Burnett
committed
Adding Envoy gRPC bridge filter example
1 parent 0a4fb97 commit 35a7ea7

30 files changed

+1520
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/grpc-bridge/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/service
2+
.idea

examples/grpc-bridge/Dockerfile-grpc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM lyft/envoy:latest
2+
3+
RUN mkdir /var/log/envoy/
4+
COPY ./bin/service /usr/local/bin/srv
5+
COPY ./script/grpc_start /etc/grpc_start
6+
CMD /etc/grpc_start
7+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM lyft/envoy:latest
2+
3+
RUN apt-get install -y python-dev
4+
RUN pip install grpcio requests
5+
ADD ./client /client
6+
RUN chmod a+x /client/client.py
7+
RUN mkdir /var/log/envoy/
8+
CMD /usr/local/bin/envoy -c /etc/s2s-python-envoy.json

examples/grpc-bridge/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# envoy-grpc
2+
3+
This is an example usage of the Envoy [gRPC bridge filter](https://lyft.github.io/envoy/docs/configuration/http_filters/grpc_http1_bridge_filter.html#config-http-filters-grpc-bridge). Included is a gRPC in memory Key/Value store with a Python HTTP client. The Python client makes HTTP/1 requests through the Envoy sidecar process which are upgraded into HTTP/2 gRPC requests. Response trailers are then buffered and sent back to the client as a HTTP/1 header payload.
4+
5+
## Building the Go service
6+
7+
```bash
8+
script/bootstrap
9+
script/build
10+
```
11+
12+
## Docker compose
13+
14+
To run the docker compose file, and set up both the Python and the gRPC containers
15+
run:
16+
17+
```bash
18+
docker-compose up --build
19+
```
20+
21+
## Sending requests to the Key/Value store
22+
23+
```bash
24+
# set a key
25+
docker-compose exec python /client/client.py set foo bar
26+
=> setf foo to bar
27+
28+
# get a key
29+
docker-compose exec python /client/client.py get foo
30+
=> bar
31+
```

examples/grpc-bridge/bin/.gitkeep

Whitespace-only changes.

examples/grpc-bridge/client/client.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
3+
import requests, sys
4+
import kv_pb2 as kv
5+
from struct import pack
6+
7+
HOST = "http://localhost:9001"
8+
HEADERS = {'content-type': 'application/grpc','Host':'grpc'}
9+
USAGE = """
10+
11+
envoy-python-client usage:
12+
./client.py set <key> <value> - sets the <key> and <value>
13+
./client.py get <key> - gets the value for <key>
14+
"""
15+
16+
class KVClient():
17+
18+
def get(self, key):
19+
r = kv.GetRequest(key=key)
20+
21+
# Build the gRPC frame
22+
data = r.SerializeToString()
23+
data = pack('!cI', b'\0', len(data)) + data
24+
25+
resp = requests.post(HOST + "/kv.KV/Get", data=data, headers=HEADERS)
26+
27+
return kv.GetResponse().FromString(resp.content[5:])
28+
29+
30+
def set(self, key, value):
31+
r = kv.SetRequest(key=key, value=value)
32+
data = r.SerializeToString()
33+
data = pack('!cI', b'\0', len(data)) + data
34+
35+
return requests.post(HOST + "/kv.KV/Set", data=data, headers=HEADERS)
36+
37+
def run():
38+
if len(sys.argv) == 1:
39+
print(USAGE)
40+
41+
sys.exit(0)
42+
43+
cmd = sys.argv[1]
44+
45+
client = KVClient()
46+
47+
if cmd == "get":
48+
# ensure a key was provided
49+
if len(sys.argv) != 3:
50+
print(USAGE)
51+
sys.exit(1)
52+
53+
# get the key to fetch
54+
key = sys.argv[2]
55+
56+
# send the request to the server
57+
response = client.get(key)
58+
59+
print(response.value)
60+
sys.exit(0)
61+
62+
elif cmd == "set":
63+
# ensure a key and value were provided
64+
if len(sys.argv) < 4:
65+
print(USAGE)
66+
sys.exit(1)
67+
68+
# get the key and the full text of value
69+
key = sys.argv[2]
70+
value = " ".join(sys.argv[3:])
71+
72+
# send the request to the server
73+
response = client.set(key, value)
74+
75+
print("setf %s to %s" % (key, value))
76+
77+
if __name__ == '__main__':
78+
run()

0 commit comments

Comments
 (0)