def list_services(self, namespace=None): """ List all available services. :param namespace: str, if not specified list services for all namespaces :return: collection of instances of :class:`conu.backend.k8s.service.Service` """ if namespace: return [ Service(name=s.metadata.name, ports=k8s_ports_to_metadata_ports(s.spec.ports), namespace=s.metadata.namespace, labels=s.metadata.labels, selector=s.spec.selector, spec=s.spec) for s in self.core_api.list_namespaced_service( namespace, watch=False).items ] return [ Service(name=s.metadata.name, ports=k8s_ports_to_metadata_ports(s.spec.ports), namespace=s.metadata.namespace, labels=s.metadata.labels, selector=s.spec.selector, spec=s.spec) for s in self.core_api.list_service_for_all_namespaces( watch=False).items ]
def test_database_deployment(): with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() with DockerBackend() as backend: postgres_image = backend.ImageClass("centos/postgresql-10-centos7") postgres_image_metadata = postgres_image.get_metadata() # set up env variables db_env_variables = {"POSTGRESQL_USER": "******", "POSTGRESQL_PASSWORD": "******", "POSTGRESQL_DATABASE": "db"} postgres_image_metadata.env_variables.update(db_env_variables) db_labels = {"app": "postgres"} db_service = Service(name="database", ports=["5432"], selector=db_labels, namespace=namespace, create_in_cluster=True) db_deployment = Deployment(name="database", selector=db_labels, labels=db_labels, image_metadata=postgres_image_metadata, namespace=namespace, create_in_cluster=True) try: db_deployment.wait(200) assert db_deployment.all_pods_ready() finally: db_deployment.delete() db_service.delete() k8s_backend.delete_namespace(namespace)
def test_list_services(): with K8sBackend() as k8s_backend: namespace = k8s_backend.create_namespace() labels = {"app": "postgres"} service = Service(name="database", ports=["5432"], selector=labels, namespace=namespace, create_in_cluster=True) try: assert any(service.name == s.name for s in k8s_backend.list_services()) finally: service.delete() k8s_backend.delete_namespace(namespace)
def list_services(self): """ List all available services. :return: collection of instances of :class:`conu.backend.k8s.service.Service` """ return [Service(name=s.metadata.name, ports=k8s_ports_to_metadata_ports(s.spec.ports), namespace=s.metadata.namespace, labels=s.metadata.labels, selector=s.spec.selector) for s in self.core_api.list_service_for_all_namespaces(watch=False).items]