def cleanup_client(project, network, k8s_api_manager, resource_prefix, resource_suffix, gcp_service_account): runner_kwargs = dict( deployment_name=xds_flags.CLIENT_NAME.value, image_name=xds_k8s_flags.CLIENT_IMAGE.value, td_bootstrap_image=xds_k8s_flags.TD_BOOTSTRAP_IMAGE.value, gcp_project=project, gcp_api_manager=gcp.api.GcpApiManager(), gcp_service_account=gcp_service_account, xds_server_uri=xds_flags.XDS_SERVER_URI.value, network=network, stats_port=xds_flags.CLIENT_PORT.value) client_namespace = _KubernetesClientRunner.make_namespace_name( resource_prefix, resource_suffix) client_runner = _KubernetesClientRunner( k8s.KubernetesNamespace(k8s_api_manager, client_namespace), **runner_kwargs) logger.info('Cleanup client') client_runner.cleanup(force=True, force_namespace=True)
def initKubernetesClientRunner( self, td_bootstrap_image: Optional[str] = None ) -> KubernetesClientRunner: if not td_bootstrap_image: td_bootstrap_image = self.td_bootstrap_image return KubernetesClientRunner( k8s.KubernetesNamespace(self.k8s_api_manager, self.client_namespace), deployment_name=self.client_name, image_name=self.client_image, td_bootstrap_image=td_bootstrap_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, xds_server_uri=self.xds_server_uri, network=self.network, debug_use_port_forwarding=self.debug_use_port_forwarding, enable_workload_identity=self.enable_workload_identity, stats_port=self.client_port, reuse_namespace=self.server_namespace == self.client_namespace)
def main(argv): if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') # Base namespace namespace = xds_flags.NAMESPACE.value server_namespace = namespace runner_kwargs = dict( deployment_name=xds_flags.SERVER_NAME.value, image_name=xds_k8s_flags.SERVER_IMAGE.value, gcp_service_account=xds_k8s_flags.GCP_SERVICE_ACCOUNT.value, network=xds_flags.NETWORK.value, reuse_namespace=_REUSE_NAMESPACE.value) if _SECURE.value: runner_kwargs.update( td_bootstrap_image=xds_k8s_flags.TD_BOOTSTRAP_IMAGE.value, xds_server_uri=xds_flags.XDS_SERVER_URI.value, deployment_template='server-secure.deployment.yaml') k8s_api_manager = k8s.KubernetesApiManager( xds_k8s_flags.KUBE_CONTEXT.value) server_runner = server_app.KubernetesServerRunner( k8s.KubernetesNamespace(k8s_api_manager, server_namespace), **runner_kwargs) if _CMD.value == 'run': logger.info('Run server, secure_mode=%s', _SECURE.value) server_runner.run( test_port=xds_flags.SERVER_PORT.value, maintenance_port=xds_flags.SERVER_MAINTENANCE_PORT.value, secure_mode=_SECURE.value) elif _CMD.value == 'cleanup': logger.info('Cleanup server') server_runner.cleanup(force=True, force_namespace=_CLEANUP_NAMESPACE.value)
def create_test_client_runner(self): if self.resource_suffix: client_namespace_suffix = self.resource_suffix else: client_namespace_suffix = framework.helpers.rand.random_resource_suffix( ) logging.info('GcpResourceManager: client_namespace_suffix=%s', client_namespace_suffix) # Kubernetes Test Client namespace_name = _KubernetesClientRunner.make_namespace_name( self.resource_prefix, client_namespace_suffix) return _KubernetesClientRunner( k8s.KubernetesNamespace(self.k8s_api_manager, namespace_name), deployment_name=self.client_name, image_name=self.client_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, xds_server_uri=self.xds_server_uri, network=self.network, debug_use_port_forwarding=self.debug_use_port_forwarding, enable_workload_identity=self.enable_workload_identity, stats_port=self.client_port)
def __init__(self, absl_flags: Mapping[str, Any] = None): if absl_flags is not None: for key in absl_flags: setattr(self, key, absl_flags[key]) # Pick a client_namespace_suffix if not set if self.resource_suffix is None: self.resource_suffix = "" else: raise NotImplementedError( 'Predefined resource_suffix is not supported for UrlMap tests') logging.info('GcpResourceManager: resource prefix=%s, suffix=%s', self.resource_prefix, self.resource_suffix) # API managers self.k8s_api_manager = k8s.KubernetesApiManager(self.kube_context) self.gcp_api_manager = gcp.api.GcpApiManager() self.td = traffic_director.TrafficDirectorManager( self.gcp_api_manager, self.project, resource_prefix=self.resource_prefix, resource_suffix=(self.resource_suffix or ""), network=self.network, compute_api_version=self.compute_api_version, ) # Kubernetes namespace self.k8s_namespace = k8s.KubernetesNamespace(self.k8s_api_manager, self.resource_prefix) # Kubernetes Test Servers self.test_server_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name, image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, xds_server_uri=self.xds_server_uri, network=self.network, enable_workload_identity=self.enable_workload_identity) self.test_server_alternative_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name + '-alternative', image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, xds_server_uri=self.xds_server_uri, network=self.network, enable_workload_identity=self.enable_workload_identity, reuse_namespace=True) self.test_server_affinity_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name + '-affinity', image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, xds_server_uri=self.xds_server_uri, network=self.network, enable_workload_identity=self.enable_workload_identity, reuse_namespace=True) logging.info('Strategy of GCP resources management: %s', self.strategy)
def main(argv): if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') command = _CMD.value security_mode = _SECURITY.value project: str = xds_flags.PROJECT.value network: str = xds_flags.NETWORK.value namespace = xds_flags.NAMESPACE.value # Test server server_name = xds_flags.SERVER_NAME.value server_port = xds_flags.SERVER_PORT.value server_maintenance_port = xds_flags.SERVER_MAINTENANCE_PORT.value server_xds_host = xds_flags.SERVER_XDS_HOST.value server_xds_port = xds_flags.SERVER_XDS_PORT.value gcp_api_manager = gcp.api.GcpApiManager() if security_mode is None: td = traffic_director.TrafficDirectorManager(gcp_api_manager, project=project, resource_prefix=namespace, network=network) else: td = traffic_director.TrafficDirectorSecureManager( gcp_api_manager, project=project, resource_prefix=namespace, network=network) if server_maintenance_port is None: server_maintenance_port = _DEFAULT_SECURE_MODE_MAINTENANCE_PORT try: if command in ('create', 'cycle'): logger.info('Create mode') if security_mode is None: logger.info('No security') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) elif security_mode == 'mtls': logger.info('Setting up mtls') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=True) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=True, mtls=True) elif security_mode == 'tls': logger.info('Setting up tls') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=False) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=True, mtls=False) elif security_mode == 'plaintext': logger.info('Setting up plaintext') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=False, mtls=False) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=False, mtls=False) elif security_mode == 'mtls_error': # Error case: server expects client mTLS cert, # but client configured only for TLS logger.info('Setting up mtls_error') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=True) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=True, mtls=False) elif security_mode == 'server_authz_error': # Error case: client does not authorize server # because of mismatched SAN name. logger.info('Setting up mtls_error') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) # Regular TLS setup, but with client policy configured using # intentionality incorrect server_namespace. td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=False) incorrect_namespace = f'incorrect-namespace-{uuid.uuid4().hex}' td.setup_client_security(server_namespace=incorrect_namespace, server_name=server_name, tls=True, mtls=False) logger.info('Works!') except Exception: # noqa pylint: disable=broad-except logger.exception('Got error during creation') if command in ('cleanup', 'cycle'): logger.info('Cleaning up') td.cleanup(force=True) if command == 'backends-add': logger.info('Adding backends') k8s_api_manager = k8s.KubernetesApiManager( xds_k8s_flags.KUBE_CONTEXT.value) k8s_namespace = k8s.KubernetesNamespace(k8s_api_manager, namespace) neg_name, neg_zones = k8s_namespace.get_service_neg( server_name, server_port) td.load_backend_service() td.backend_service_add_neg_backends(neg_name, neg_zones) td.wait_for_backends_healthy_status() # TODO(sergiitk): wait until client reports rpc health elif command == 'backends-cleanup': td.load_backend_service() td.backend_service_remove_all_backends()
def __init__(self, absl_flags: Mapping[str, Any] = None): if absl_flags is not None: for key in absl_flags: setattr(self, key, absl_flags[key]) # Pick a client_namespace_suffix if not set if self.resource_suffix is None: self.resource_suffix = "" self.client_namespace_suffix = framework.helpers.rand.random_resource_suffix( ) else: self.client_namespace_suffix = self.resource_suffix logging.info( 'GcpResourceManager: resource prefix=%s, suffix=%s, client_namespace_suffix=%s', self.resource_prefix, self.resource_suffix, self.client_namespace_suffix) # API managers self.k8s_api_manager = k8s.KubernetesApiManager(self.kube_context) self.gcp_api_manager = gcp.api.GcpApiManager() self.td = traffic_director.TrafficDirectorManager( self.gcp_api_manager, self.project, resource_prefix=self.resource_prefix, resource_suffix=(self.resource_suffix or ""), network=self.network, ) # Kubernetes namespace self.k8s_namespace = k8s.KubernetesNamespace(self.k8s_api_manager, self.resource_prefix) if self.client_namespace_suffix != self.resource_suffix: self.k8s_client_namespace = k8s.KubernetesNamespace( self.k8s_api_manager, client_app.KubernetesClientRunner.make_namespace_name( self.resource_prefix, self.client_namespace_suffix)) else: self.k8s_client_namespace = self.k8s_namespace # Kubernetes Test Client self.test_client_runner = client_app.KubernetesClientRunner( self.k8s_client_namespace, deployment_name=self.client_name, image_name=self.client_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, network=self.network, debug_use_port_forwarding=self.debug_use_port_forwarding, stats_port=self.client_port, reuse_namespace=True) # Kubernetes Test Servers self.test_server_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name, image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, network=self.network) self.test_server_alternative_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name + '-alternative', image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, network=self.network, reuse_namespace=True) self.test_server_affinity_runner = server_app.KubernetesServerRunner( self.k8s_namespace, deployment_name=self.server_name + '-affinity', image_name=self.server_image, gcp_project=self.project, gcp_api_manager=self.gcp_api_manager, gcp_service_account=self.gcp_service_account, td_bootstrap_image=self.td_bootstrap_image, network=self.network, reuse_namespace=True) logging.info('Strategy of GCP resources management: %s', self.strategy)
def main(argv): # pylint: disable=too-many-locals,too-many-branches,too-many-statements if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') # Must be called before KubernetesApiManager or GcpApiManager init. xds_flags.set_socket_default_timeout_from_flag() command = _CMD.value security_mode = _SECURITY.value project: str = xds_flags.PROJECT.value network: str = xds_flags.NETWORK.value # Resource names. resource_prefix: str = xds_flags.RESOURCE_PREFIX.value resource_suffix: str = xds_flags.RESOURCE_SUFFIX.value # Test server server_name = xds_flags.SERVER_NAME.value server_port = xds_flags.SERVER_PORT.value server_maintenance_port = xds_flags.SERVER_MAINTENANCE_PORT.value server_xds_host = xds_flags.SERVER_XDS_HOST.value server_xds_port = xds_flags.SERVER_XDS_PORT.value server_namespace = _KubernetesServerRunner.make_namespace_name( resource_prefix, resource_suffix) gcp_api_manager = gcp.api.GcpApiManager() if security_mode is None: td = traffic_director.TrafficDirectorManager( gcp_api_manager, project=project, network=network, resource_prefix=resource_prefix, resource_suffix=resource_suffix) else: td = traffic_director.TrafficDirectorSecureManager( gcp_api_manager, project=project, network=network, resource_prefix=resource_prefix, resource_suffix=resource_suffix) if server_maintenance_port is None: server_maintenance_port = \ _KubernetesServerRunner.DEFAULT_SECURE_MODE_MAINTENANCE_PORT try: if command in ('create', 'cycle'): logger.info('Create mode') if security_mode is None: logger.info('No security') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) elif security_mode == 'mtls': logger.info('Setting up mtls') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=server_namespace, server_name=server_name, server_port=server_port, tls=True, mtls=True) td.setup_client_security(server_namespace=server_namespace, server_name=server_name, tls=True, mtls=True) elif security_mode == 'tls': logger.info('Setting up tls') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=server_namespace, server_name=server_name, server_port=server_port, tls=True, mtls=False) td.setup_client_security(server_namespace=server_namespace, server_name=server_name, tls=True, mtls=False) elif security_mode == 'plaintext': logger.info('Setting up plaintext') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=server_namespace, server_name=server_name, server_port=server_port, tls=False, mtls=False) td.setup_client_security(server_namespace=server_namespace, server_name=server_name, tls=False, mtls=False) elif security_mode == 'mtls_error': # Error case: server expects client mTLS cert, # but client configured only for TLS logger.info('Setting up mtls_error') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) td.setup_server_security(server_namespace=server_namespace, server_name=server_name, server_port=server_port, tls=True, mtls=True) td.setup_client_security(server_namespace=server_namespace, server_name=server_name, tls=True, mtls=False) elif security_mode == 'server_authz_error': # Error case: client does not authorize server # because of mismatched SAN name. logger.info('Setting up mtls_error') td.setup_for_grpc(server_xds_host, server_xds_port, health_check_port=server_maintenance_port) # Regular TLS setup, but with client policy configured using # intentionality incorrect server_namespace. td.setup_server_security(server_namespace=server_namespace, server_name=server_name, server_port=server_port, tls=True, mtls=False) td.setup_client_security( server_namespace=f'incorrect-namespace-{rand.rand_string()}', server_name=server_name, tls=True, mtls=False) logger.info('Works!') except Exception: # noqa pylint: disable=broad-except logger.exception('Got error during creation') if command in ('cleanup', 'cycle'): logger.info('Cleaning up') td.cleanup(force=True) if command == 'backends-add': logger.info('Adding backends') k8s_api_manager = k8s.KubernetesApiManager( xds_k8s_flags.KUBE_CONTEXT.value) k8s_namespace = k8s.KubernetesNamespace(k8s_api_manager, server_namespace) neg_name, neg_zones = k8s_namespace.get_service_neg( server_name, server_port) td.load_backend_service() td.backend_service_add_neg_backends(neg_name, neg_zones) td.wait_for_backends_healthy_status() elif command == 'backends-cleanup': td.load_backend_service() td.backend_service_remove_all_backends() elif command == 'unused-xds-port': try: unused_xds_port = td.find_unused_forwarding_rule_port() logger.info('Found unused forwarding rule port: %s', unused_xds_port) except Exception: # noqa pylint: disable=broad-except logger.exception("Couldn't find unused forwarding rule port")
def main(argv): if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') command = _CMD.value security_mode = _SECURITY.value project: str = xds_flags.PROJECT.value network: str = xds_flags.NETWORK.value namespace = xds_flags.NAMESPACE.value # Test server server_name = xds_flags.SERVER_NAME.value server_port = xds_flags.SERVER_PORT.value server_xds_host = xds_flags.SERVER_XDS_HOST.value server_xds_port = xds_flags.SERVER_XDS_PORT.value gcp_api_manager = gcp.api.GcpApiManager() if security_mode is None: td = traffic_director.TrafficDirectorManager(gcp_api_manager, project=project, resource_prefix=namespace, network=network) else: td = traffic_director.TrafficDirectorSecureManager( gcp_api_manager, project=project, resource_prefix=namespace, network=network) # noinspection PyBroadException try: if command == 'create' or command == 'cycle': logger.info('Create-only mode') if security_mode is None: logger.info('No security') td.setup_for_grpc(server_xds_host, server_xds_port) elif security_mode == 'mtls': logger.info('Setting up mtls') td.setup_for_grpc(server_xds_host, server_xds_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=True) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=True, mtls=True) elif security_mode == 'tls': logger.info('Setting up tls') td.setup_for_grpc(server_xds_host, server_xds_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=True, mtls=False) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=True, mtls=False) elif security_mode == 'plaintext': logger.info('Setting up plaintext') td.setup_for_grpc(server_xds_host, server_xds_port) td.setup_server_security(server_namespace=namespace, server_name=server_name, server_port=server_port, tls=False, mtls=False) td.setup_client_security(server_namespace=namespace, server_name=server_name, tls=False, mtls=False) logger.info('Works!') except Exception: logger.exception('Got error during creation') if command == 'cleanup' or command == 'cycle': logger.info('Cleaning up') td.cleanup(force=True) if command == 'backends-add': logger.info('Adding backends') k8s_api_manager = k8s.KubernetesApiManager( xds_k8s_flags.KUBE_CONTEXT.value) k8s_namespace = k8s.KubernetesNamespace(k8s_api_manager, namespace) neg_name, neg_zones = k8s_namespace.get_service_neg( server_name, server_port) td.load_backend_service() td.backend_service_add_neg_backends(neg_name, neg_zones) # TODO(sergiitk): wait until client reports rpc health elif command == 'backends-cleanup': td.load_backend_service() td.backend_service_remove_all_backends()