run_test_client.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. from absl import app
  16. from absl import flags
  17. from framework import xds_flags
  18. from framework import xds_k8s_flags
  19. from framework.infrastructure import k8s
  20. from framework.test_app import client_app
  21. logger = logging.getLogger(__name__)
  22. # Flags
  23. _CMD = flags.DEFINE_enum('cmd',
  24. default='run',
  25. enum_values=['run', 'cleanup'],
  26. help='Command')
  27. _SECURE = flags.DEFINE_bool("secure",
  28. default=False,
  29. help="Run client in the secure mode")
  30. _QPS = flags.DEFINE_integer('qps', default=25, help='Queries per second')
  31. _PRINT_RESPONSE = flags.DEFINE_bool("print_response",
  32. default=False,
  33. help="Client prints responses")
  34. _REUSE_NAMESPACE = flags.DEFINE_bool("reuse_namespace",
  35. default=True,
  36. help="Use existing namespace if exists")
  37. _CLEANUP_NAMESPACE = flags.DEFINE_bool(
  38. "cleanup_namespace",
  39. default=False,
  40. help="Delete namespace during resource cleanup")
  41. flags.adopt_module_key_flags(xds_flags)
  42. flags.adopt_module_key_flags(xds_k8s_flags)
  43. def main(argv):
  44. if len(argv) > 1:
  45. raise app.UsageError('Too many command-line arguments.')
  46. # Base namespace
  47. namespace = xds_flags.NAMESPACE.value
  48. client_namespace = namespace
  49. runner_kwargs = dict(
  50. deployment_name=xds_flags.CLIENT_NAME.value,
  51. image_name=xds_k8s_flags.CLIENT_IMAGE.value,
  52. gcp_service_account=xds_k8s_flags.GCP_SERVICE_ACCOUNT.value,
  53. network=xds_flags.NETWORK.value,
  54. td_bootstrap_image=xds_k8s_flags.TD_BOOTSTRAP_IMAGE.value,
  55. stats_port=xds_flags.CLIENT_PORT.value,
  56. reuse_namespace=_REUSE_NAMESPACE.value)
  57. if _SECURE.value:
  58. runner_kwargs.update(
  59. deployment_template='client-secure.deployment.yaml')
  60. k8s_api_manager = k8s.KubernetesApiManager(xds_k8s_flags.KUBE_CONTEXT.value)
  61. client_runner = client_app.KubernetesClientRunner(
  62. k8s.KubernetesNamespace(k8s_api_manager, client_namespace),
  63. **runner_kwargs)
  64. # Server target
  65. server_xds_host = xds_flags.SERVER_XDS_HOST.value
  66. server_xds_port = xds_flags.SERVER_XDS_PORT.value
  67. if _CMD.value == 'run':
  68. logger.info('Run client, secure_mode=%s', _SECURE.value)
  69. client_runner.run(
  70. server_target=f'xds:///{server_xds_host}:{server_xds_port}',
  71. qps=_QPS.value,
  72. print_response=_PRINT_RESPONSE.value,
  73. secure_mode=_SECURE.value)
  74. elif _CMD.value == 'cleanup':
  75. logger.info('Cleanup client')
  76. client_runner.cleanup(force=True,
  77. force_namespace=_CLEANUP_NAMESPACE.value)
  78. if __name__ == '__main__':
  79. app.run(main)