generate_listeners.py
Go to the documentation of this file.
1 # Map from listeners proto, with holes where filter config fragments should go, and
2 # a list of filter config fragment protos, to a final listeners.pb with the
3 # config fragments converted to the opaque Struct representation.
4 
5 import sys
6 
7 # Some evil hack to deal with the fact that Bazel puts both google/api and
8 # google/protobuf roots in the sys.path, and Python gets confused, e.g. it
9 # thinks that there is no api package if it encounters the google/protobuf root
10 # in sys.path first.
11 from pkgutil import extend_path
12 import google
13 
14 google.__path__ = extend_path(google.__path__, google.__name__)
15 
16 from google.protobuf import json_format
17 from google.protobuf import struct_pb2
18 from google.protobuf import text_format
19 
20 from envoy.api.v2 import lds_pb2
21 from envoy.config.filter.network.http_connection_manager.v2 import http_connection_manager_pb2
22 
23 
24 # Convert an arbitrary proto object to its Struct proto representation.
25 def proto_to_struct(proto):
26  json_rep = json_format.MessageToJson(proto)
27  parsed_msg = struct_pb2.Struct()
28  json_format.Parse(json_rep, parsed_msg)
29  return parsed_msg
30 
31 
32 # Parse a proto from the filesystem.
33 def parse_proto(path, filter_name):
34  # We only know about some filter config protos ahead of time.
35  KNOWN_FILTERS = {
36  'http_connection_manager': lambda: http_connection_manager_pb2.HttpConnectionManager()
37  }
38  filter_config = KNOWN_FILTERS[filter_name]()
39  with open(path, 'r') as f:
40  text_format.Merge(f.read(), filter_config)
41  return filter_config
42 
43 
44 def generate_listeners(listeners_pb_path, output_pb_path, output_json_path, fragments):
45  listener = lds_pb2.Listener()
46  with open(listeners_pb_path, 'r') as f:
47  text_format.Merge(f.read(), listener)
48 
49  for filter_chain in listener.filter_chains:
50  for f in filter_chain.filters:
51  f.config.CopyFrom(proto_to_struct(parse_proto(next(fragments), f.name)))
52 
53  with open(output_pb_path, 'w') as f:
54  f.write(str(listener))
55 
56  with open(output_json_path, 'w') as f:
57  f.write(json_format.MessageToJson(listener))
58 
59 
60 if __name__ == '__main__':
61  if len(sys.argv) < 4:
62  print(
63  'Usage: %s <path to listeners.pb> <output listeners.pb> <output '
64  'listeners.json> <filter config fragment paths>') % sys.argv[0]
65  sys.exit(1)
66 
67  generate_listeners(sys.argv[1], sys.argv[2], sys.argv[3], iter(sys.argv[4:]))
xds_interop_client.str
str
Definition: xds_interop_client.py:487
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
generate_listeners.proto_to_struct
def proto_to_struct(proto)
Definition: generate_listeners.py:25
generate_listeners
Definition: generate_listeners.py:1
generate_listeners.parse_proto
def parse_proto(path, filter_name)
Definition: generate_listeners.py:33
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
open
#define open
Definition: test-fs.c:46
iter
Definition: test_winkernel.cpp:47
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
generate_listeners.generate_listeners
def generate_listeners(listeners_pb_path, output_pb_path, output_json_path, fragments)
Definition: generate_listeners.py:44


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:25