def wait_for_public_ip(v1: CoreV1Api, namespace: str) -> str: """ Wait for LoadBalancer to get the public ip. :param v1: CoreV1Api :param namespace: namespace :return: str """ resp = v1.list_namespaced_service(namespace) counter = 0 svc_item = first(x for x in resp.items if x.metadata.name == "nginx-ingress") while str( svc_item.status.load_balancer.ingress) == "None" and counter < 20: time.sleep(5) resp = v1.list_namespaced_service(namespace) svc_item = first(x for x in resp.items if x.metadata.name == "nginx-ingress") counter = counter + 1 if counter == 20: pytest.fail( "After 100 seconds the LB still doesn't have a Public IP. Exiting..." ) print(f"Public IP ='{svc_item.status.load_balancer.ingress[0].ip}'") return str(svc_item.status.load_balancer.ingress[0].ip)
def destroy_service(api: client.CoreV1Api, namespace, name): if len( api.list_namespaced_service( namespace=namespace, field_selector=f'metadata.name={name}').items) == 1: logger.info(f'destroying Service: {namespace}/{name}') api.delete_namespaced_service(name=name, namespace=namespace) else: logger.info(f'cannot find Service to destroy: {namespace}/{name}')
def ensure_service(api: client.CoreV1Api, service, namespace, name): if len( api.list_namespaced_service( namespace=namespace, field_selector=f'metadata.name={name}').items) == 0: logger.info(f'creating Service: {namespace}/{name}') api.create_namespaced_service(body=service, namespace=namespace) else: logger.info(f'Service exists: {namespace}/{name}')
def wait_for_public_ip(v1: CoreV1Api, namespace: str) -> str: """ Wait for LoadBalancer to get the public ip. :param v1: CoreV1Api :param namespace: namespace :return: str """ resp = v1.list_namespaced_service(namespace) counter = 0 while str(resp.items[0].status.load_balancer.ingress) == "None" and counter < 20: time.sleep(5) resp = v1.list_namespaced_service(namespace) counter = counter + 1 if counter == 20: pytest.fail("After 100 seconds the LB still doesn't have a Public IP. Exiting...") print(f"Public IP ='{resp.items[0].status.load_balancer.ingress[0].ip}'") return str(resp.items[0].status.load_balancer.ingress[0].ip)
def wait_for_public_ip(v1: CoreV1Api, namespace: str) -> str: """ Wait for LoadBalancer to get the public ip. :param v1: CoreV1Api :param namespace: namespace :return: str """ resp = v1.list_namespaced_service(namespace) counter = 0 while str(resp.items[0].status.load_balancer.ingress ) == "None" and counter < 20: time.sleep(10) resp = v1.list_namespaced_service(namespace) counter = counter + 1 if counter == 20: pytest.fail( "After 200 seconds the LB still doesn't have a Public IP. Exiting..." ) print(f"Public IP ='{resp.items[0].status.load_balancer.ingress[0].ip}'") return str(resp.items[0].status.load_balancer.ingress[0].ip)
def find_ips(v1: CoreV1Api): ips = [] # try: services = v1.list_namespaced_service(system_namespace).items for s in services: try: if s.status.load_balancer.ingress: for i in s.status.load_balancer.ingress: ips.append(i.ip) except: log.info("Cannot process service %s" % s.metadata.self_link) # except: # print("Cannot get all services in %s" % system_namespace) if len(ips) > 0: return ips else: return ["127.0.0.1"]
def get(client: CoreV1Api, log: BoundLogger, namespace: V1Namespace, svc: V1Service) -> Optional[V1Service]: return common_k8s.get_resource( lambda: client.list_namespaced_service(namespace=namespace.metadata. name), log, 'service', svc.metadata.name)