Debian Codegraph @

tools/run_tests/xds_k8s_test_driver/bin/run_test_client.py

raw
1 # Copyright 2020 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15 import signal
16
17 from absl import app
18 from absl import flags
19
20 from framework import xds_flags
21 from framework import xds_k8s_flags
22 from framework.infrastructure import gcp
23 from framework.infrastructure import k8s
24 from framework.test_app.runners.k8s import k8s_xds_client_runner
25
26 logger = logging.getLogger(__name__)
27 # Flags
28 _CMD = flags.DEFINE_enum('cmd',
29 default='run',
30 enum_values=['run', 'cleanup'],
31 help='Command')
32 _SECURE = flags.DEFINE_bool("secure",
33 default=False,
34 help="Run client in the secure mode")
35 _QPS = flags.DEFINE_integer('qps', default=25, help='Queries per second')
36 _PRINT_RESPONSE = flags.DEFINE_bool("print_response",
37 default=False,
38 help="Client prints responses")
39 _FOLLOW = flags.DEFINE_bool("follow",
40 default=False,
41 help="Follow pod logs. "
42 "Requires --collect_app_logs")
43 _CONFIG_MESH = flags.DEFINE_bool(
44 "config_mesh",
45 default=None,
46 help="Optional. Supplied to bootstrap generator to indicate AppNet mesh.")
47 _REUSE_NAMESPACE = flags.DEFINE_bool("reuse_namespace",
48 default=True,
49 help="Use existing namespace if exists")
50 _CLEANUP_NAMESPACE = flags.DEFINE_bool(
51 "cleanup_namespace",
52 default=False,
53 help="Delete namespace during resource cleanup")
54 flags.adopt_module_key_flags(xds_flags)
55 flags.adopt_module_key_flags(xds_k8s_flags)
56 # Running outside of a test suite, so require explicit resource_suffix.
57 flags.mark_flag_as_required("resource_suffix")
58
59 # Type aliases
60 _KubernetesClientRunner = k8s_xds_client_runner.KubernetesClientRunner
61
62
63 def make_sigint_handler(client_runner: _KubernetesClientRunner):
64
65 def sigint_handler(sig, frame):
66 del sig, frame
67 print('Caught Ctrl+C. Shutting down the logs')
68 client_runner.stop_pod_dependencies(log_drain_sec=3)
69
70 return sigint_handler
71
72
73 def main(argv):
74 if len(argv) > 1:
75 raise app.UsageError('Too many command-line arguments.')
76
77 # Must be called before KubernetesApiManager or GcpApiManager init.
78 xds_flags.set_socket_default_timeout_from_flag()
79
80 project: str = xds_flags.PROJECT.value
81 # GCP Service Account email
82 gcp_service_account: str = xds_k8s_flags.GCP_SERVICE_ACCOUNT.value
83
84 # KubernetesClientRunner arguments.
85 runner_kwargs = dict(
86 deployment_name=xds_flags.CLIENT_NAME.value,
87 image_name=xds_k8s_flags.CLIENT_IMAGE.value,
88 td_bootstrap_image=xds_k8s_flags.TD_BOOTSTRAP_IMAGE.value,
89 gcp_project=project,
90 gcp_api_manager=gcp.api.GcpApiManager(),
91 gcp_service_account=gcp_service_account,
92 xds_server_uri=xds_flags.XDS_SERVER_URI.value,
93 network=xds_flags.NETWORK.value,
94 stats_port=xds_flags.CLIENT_PORT.value,
95 reuse_namespace=_REUSE_NAMESPACE.value)
96
97 if _SECURE.value:
98 runner_kwargs.update(
99 deployment_template='client-secure.deployment.yaml')
100
101 k8s_api_manager = k8s.KubernetesApiManager(xds_k8s_flags.KUBE_CONTEXT.value)
102 client_namespace = _KubernetesClientRunner.make_namespace_name(
103 xds_flags.RESOURCE_PREFIX.value, xds_flags.RESOURCE_SUFFIX.value)
104 client_runner = _KubernetesClientRunner(
105 k8s.KubernetesNamespace(k8s_api_manager, client_namespace),
106 **runner_kwargs)
107
108 # Server target
109 server_xds_host = xds_flags.SERVER_XDS_HOST.value
110 server_xds_port = xds_flags.SERVER_XDS_PORT.value
111
112 if _CMD.value == 'run':
113 logger.info('Run client, secure_mode=%s', _SECURE.value)
114 client_runner.run(
115 server_target=f'xds:///{server_xds_host}:{server_xds_port}',
116 qps=_QPS.value,
117 print_response=_PRINT_RESPONSE.value,
118 secure_mode=_SECURE.value,
119 config_mesh=_CONFIG_MESH.value,
120 log_to_stdout=_FOLLOW.value)
121 if client_runner.should_collect_logs and _FOLLOW.value:
122 print('Following pod logs. Press Ctrl+C top stop')
123 signal.signal(signal.SIGINT, make_sigint_handler(client_runner))
124 signal.pause()
125
126 elif _CMD.value == 'cleanup':
127 logger.info('Cleanup client')
128 client_runner.cleanup(force=True,
129 force_namespace=_CLEANUP_NAMESPACE.value)
130
131
132 if __name__ == '__main__':
133 app.run(main)