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
15import socket
16
17from absl import flags
18
19from framework.helpers import highlighter
20
21# GCP
22PROJECT = flags.DEFINE_string("project",
23                              default=None,
24                              help="(required) GCP Project ID.")
25RESOURCE_PREFIX = flags.DEFINE_string(
26    "resource_prefix",
27    default=None,
28    help=("(required) The prefix used to name GCP resources.\n"
29          "Together with `resource_suffix` used to create unique "
30          "resource names."))
31RESOURCE_SUFFIX = flags.DEFINE_string(
32    "resource_suffix",
33    default=None,
34    help=("The suffix used to name GCP resources.\n"
35          "Together with `resource_prefix` used to create unique "
36          "resource names.\n"
37          "(default: test suite will generate a random suffix, based on suite "
38          "resource management preferences)"))
39NETWORK = flags.DEFINE_string("network",
40                              default="default",
41                              help="GCP Network ID")
42COMPUTE_API_VERSION = flags.DEFINE_string(
43    "compute_api_version",
44    default='v1',
45    help="The version of the GCP Compute API, e.g., v1, v1alpha")
46# Mirrors --xds-server-uri argument of Traffic Director gRPC Bootstrap
47XDS_SERVER_URI = flags.DEFINE_string(
48    "xds_server_uri",
49    default=None,
50    help="Override Traffic Director server URI.")
51ENSURE_FIREWALL = flags.DEFINE_bool(
52    "ensure_firewall",
53    default=False,
54    help="Ensure the allow-health-check firewall exists before each test case")
55FIREWALL_SOURCE_RANGE = flags.DEFINE_list(
56    "firewall_source_range",
57    default=['35.191.0.0/16', '130.211.0.0/22'],
58    help="Update the source range of the firewall rule.")
59FIREWALL_ALLOWED_PORTS = flags.DEFINE_list(
60    "firewall_allowed_ports",
61    default=['8080-8100'],
62    help="Update the allowed ports of the firewall rule.")
63
64# Test server
65SERVER_NAME = flags.DEFINE_string(
66    "server_name",
67    default="psm-grpc-server",
68    help="The name to use for test server deployments.")
69SERVER_PORT = flags.DEFINE_integer(
70    "server_port",
71    default=8080,
72    lower_bound=1,
73    upper_bound=65535,
74    help="Server test port.\nMust be within --firewall_allowed_ports.")
75SERVER_MAINTENANCE_PORT = flags.DEFINE_integer(
76    "server_maintenance_port",
77    default=None,
78    lower_bound=1,
79    upper_bound=65535,
80    help=("Server port running maintenance services: Channelz, CSDS, Health, "
81          "XdsUpdateHealth, and ProtoReflection (optional).\n"
82          "Must be within --firewall_allowed_ports.\n"
83          "(default: the port is chosen automatically based on "
84          "the security configuration)"))
85SERVER_XDS_HOST = flags.DEFINE_string(
86    "server_xds_host",
87    default="xds-test-server",
88    help=("The xDS hostname of the test server.\n"
89          "Together with `server_xds_port` makes test server target URI, "
90          "xds:///hostname:port"))
91# Note: port 0 known to represent a request for dynamically-allocated port
92# https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
93SERVER_XDS_PORT = flags.DEFINE_integer(
94    "server_xds_port",
95    default=8080,
96    lower_bound=0,
97    upper_bound=65535,
98    help=("The xDS port of the test server.\n"
99          "Together with `server_xds_host` makes test server target URI, "
100          "xds:///hostname:port\n"
101          "Must be unique within a GCP project.\n"
102          "Set to 0 to select any unused port."))
103
104# Test client
105CLIENT_NAME = flags.DEFINE_string(
106    "client_name",
107    default="psm-grpc-client",
108    help="The name to use for test client deployments")
109CLIENT_PORT = flags.DEFINE_integer(
110    "client_port",
111    default=8079,
112    lower_bound=1,
113    upper_bound=65535,
114    help=(
115        "The port test client uses to run gRPC services: Channelz, CSDS, "
116        "XdsStats, XdsUpdateClientConfigure, and ProtoReflection (optional).\n"
117        "Doesn't have to be within --firewall_allowed_ports."))
118
119# Testing metadata
120TESTING_VERSION = flags.DEFINE_string(
121    "testing_version",
122    default=None,
123    help="The testing gRPC version branch name. Like master, dev, v1.55.x")
124
125FORCE_CLEANUP = flags.DEFINE_bool(
126    "force_cleanup",
127    default=False,
128    help="Force resource cleanup, even if not created by this test run")
129
130COLLECT_APP_LOGS = flags.DEFINE_bool(
131    'collect_app_logs',
132    default=False,
133    help=('Collect the logs of the xDS Test Client and Server\n'
134          f'into the test_app_logs/ directory under the log directory.\n'
135          'See --log_dir description for configuring the log directory.'))
136
137# Needed to configure urllib3 socket timeout, which is infinity by default.
138SOCKET_DEFAULT_TIMEOUT = flags.DEFINE_float(
139    "socket_default_timeout",
140    default=60,
141    lower_bound=0,
142    help=("Set the default timeout in seconds on blocking socket operations.\n"
143          "If zero is given, the new sockets have no timeout. "))
144
145
146def set_socket_default_timeout_from_flag() -> None:
147    """A helper to configure default socket timeout from a flag.
148
149    This is known to affect the following pip packages:
150      - google-api-python-client: has the default timeout set to 60:
151        https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.http-module.html#build_http
152      - kubernetes: falls back to urllib3 timeout, which is infinity by default:
153        https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html#urllib3.util.Timeout
154
155    NOTE: Must be called _after_ the flags were parsed by absl, but before
156          the before KubernetesApiManager or GcpApiManager initialized.
157    """
158    timeout: float = SOCKET_DEFAULT_TIMEOUT.value
159    # None is inf timeout, which is represented by 0 in the flag.
160    socket.setdefaulttimeout(None if timeout == 0 else timeout)
161
162
163flags.adopt_module_key_flags(highlighter)
164
165flags.mark_flags_as_required([
166    "project",
167    "resource_prefix",
168])
169