def simple_http_server_resources(name, port=8080, create_service=False, create_route=False): """ Returns a list<dict> representing resources which, if instantiated, will run a simple pod, running a python-based http server on a specified port. If requested, a kube Service and Route can be created. :param name: The name of the pod to create :param port: The port the pod should expose :param create_service: If True, a Service will be created for the server pod :param create_route: If True, a Service & Route will be created for the server port :return: """ objs = [ oc.build_pod_simple(name, 'python:3', port=port, command=['python', '-m', 'http.server', str(port)], labels={'simple-server-run': name}), ] if create_service or create_route: objs.append( oc.build_service_simple( name, {'simple-server-run': name}, port, ), ) if create_route: objs.append({ 'apiVersion': 'v1', 'kind': 'Route', 'metadata': { 'name': name, }, 'spec': { 'host': '', 'port': { 'targetPort': port, }, 'to': { 'kind': 'Service', 'name': name, 'weight': None, } } }) return objs
def create_test_project(suffix, port): project_name = 'imperative-verify-test-project-network-{}'.format(suffix) # Delete any existing resources oc.delete_project(project_name, ignore_not_found=True, grace_period=1) server_name = 'server-{}'.format(suffix) client_name = 'client-{}'.format(suffix) with oc.new_project(project_name): # Create a simple http server running in project-A # It will be exposed by a service and route of the same name report_progress("Creating server in: " + project_name) server_sel = oc.create( simple_http_server_resources(server_name, port, create_service=True, create_route=True) ) report_progress("Created: {}".format(server_sel.qnames())) report_progress("Waiting for resources readiness...") server_sel.narrow('pod').until_all(1, success_func=oc.status.is_pod_running) server_sel.narrow('route').until_all(1, success_func=oc.status.is_route_admitted) # Create a passive pod that blocks forever so we exec commands within it client_sel = oc.create( oc.build_pod_simple(client_name, image='python:3', command=['tail', '-f', '/dev/null'])) client_sel.until_all(1, success_func=oc.status.is_pod_running) server_pod = server_sel.narrow('pod').object() service = server_sel.narrow('service').object() route = server_sel.narrow('route').object() client_pod = client_sel.narrow('pod').object() report_progress('Ensure client pod can communicate to server pod IP in same namespace') client_pod.execute(cmd_to_exec=['curl', 'http://{}:{}'.format(server_pod.model.status.podIP, port)], auto_raise=True) report_progress('Ensure client pod can communicate to server service IP in same namespace') client_pod.execute(cmd_to_exec=['curl', 'http://{}:{}'.format(service.model.spec.clusterIP, port)], auto_raise=True) report_progress('Ensure client pod can communicate to server service DNS in same namespace') client_pod.execute(cmd_to_exec=['curl', 'http://{}:{}'.format(server_name, port)], auto_raise=True) report_progress('Ensure client pod can communicate to server route in same namespace') client_pod.execute(cmd_to_exec=['curl', 'http://{}'.format(route.model.spec.host)], auto_raise=True) # Return a selector for server resources and client resources return project_name, server_pod, service, route, client_pod
def run_pods(pod_count=5, *, project_name=None): logger.info('Running in namespace: {}'.format(project_name)) for i in range(pod_count): pod_name = 'pod-{}'.format(i) logger.info('Creating: {}'.format(pod_name)) pod_selector = oc.create( oc.build_pod_simple(pod_name, image='python:3', command=['tail', '-f', '/dev/null'])) pod_selector.until_all(1, success_func=oc.status.is_pod_running) pods = oc.selector('pods').objects() logger.info('Found {} pods'.format(len(pods))) assert len(pods) == pod_count