本文整理汇总了Python中kubernetes.client.V1Namespace方法的典型用法代码示例。如果您正苦于以下问题:Python client.V1Namespace方法的具体用法?Python client.V1Namespace怎么用?Python client.V1Namespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kubernetes.client
的用法示例。
在下文中一共展示了client.V1Namespace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kube_client
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def kube_client(request, kube_ns):
"""fixture for the Kubernetes client object.
skips test that require kubernetes if kubernetes cannot be contacted
"""
load_kube_config()
client = shared_client('CoreV1Api')
try:
namespaces = client.list_namespace(_request_timeout=3)
except Exception as e:
pytest.skip("Kubernetes not found: %s" % e)
if not any(ns.metadata.name == kube_ns for ns in namespaces.items):
print("Creating namespace %s" % kube_ns)
client.create_namespace(V1Namespace(metadata=dict(name=kube_ns)))
else:
print("Using existing namespace %s" % kube_ns)
# delete the test namespace when we finish
request.addfinalizer(lambda: client.delete_namespace(kube_ns, body={}))
return client
示例2: ns_create
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def ns_create(namespace):
"""Create K8S namespace.
Args:
namespace (str): Name of namespace.
"""
try:
ns_read(namespace)
except ApiException:
ns = client.V1Namespace()
ns.metadata = client.V1ObjectMeta(name=namespace)
api.create_namespace(ns)
logging.info(f'Created namespace "{namespace}"')
logging.debug(pretty_print(json.dumps(ns.metadata, default=str)))
# TODO: Can we be more precise with the return type annotation?
示例3: _configure_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def _configure_namespace(provider_config):
namespace_field = "namespace"
if namespace_field not in provider_config:
raise ValueError("Must specify namespace in Kubernetes config.")
namespace = provider_config[namespace_field]
field_selector = "metadata.name={}".format(namespace)
namespaces = core_api().list_namespace(field_selector=field_selector).items
if len(namespaces) > 0:
assert len(namespaces) == 1
logger.info(log_prefix +
using_existing_msg(namespace_field, namespace))
return namespace
logger.info(log_prefix + not_found_msg(namespace_field, namespace))
namespace_config = client.V1Namespace(
metadata=client.V1ObjectMeta(name=namespace))
core_api().create_namespace(namespace_config)
logger.info(log_prefix + created_msg(namespace_field, namespace))
return namespace
示例4: create_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create_namespace(self):
"""
Create namespace with random name
:return: name of new created namespace
"""
name = 'namespace-{random_string}'.format(random_string=random_str(5))
namespace = client.V1Namespace(metadata=client.V1ObjectMeta(name=name))
self.core_api.create_namespace(namespace)
logger.info("Creating namespace: %s", name)
# save all namespaces created with this backend
self.managed_namespaces.append(name)
# wait for namespace to be ready
Probe(timeout=30, pause=5, expected_retval=True,
fnc=self._namespace_ready, namespace=name).run()
return name
示例5: namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def namespace(self) -> str:
""" Namespace object we are managing
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1Namespace.md"""
return self._namespace
示例6: create
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create(self):
""" Create a namespace in the configured kubernetes cluster if it does not already exist
Arguments:
None
Returns Namespace
Raises error in case of failure
"""
return client.V1Namespace(
metadata=client.V1ObjectMeta(name=self.namespace_name)
)
示例7: create
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create(self):
""" Create a namespace in the configured kubernetes cluster if it does not already exist
Arguments:
None
Returns Namespace
Raises error in case of failure
"""
_namespaces = [namespace for namespace in self.cluster_namespaces if namespace.metadata.name == self.namespace_name]
if _namespaces == []:
logging.info('Namespace {} not found. Creating it now.'.format(self.namespace_name))
try:
return self.v1client.create_namespace(
client.V1Namespace(
metadata=client.V1ObjectMeta(name=self.namespace_name)
)
)
except Exception as e:
logging.error("Unable to create namespace in cluster! {}".format(e))
logging.debug(traceback.format_exc())
raise e
else:
return _namespaces[0]
示例8: setUpClass
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def setUpClass(cls):
cls.config = base.get_e2e_configuration()
cls.path_prefix = "kubernetes/e2e_test/test_yaml/"
cls.test_namespace = "e2e-test-utils"
k8s_client = client.api_client.ApiClient(configuration=cls.config)
core_v1 = client.CoreV1Api(api_client=k8s_client)
body = client.V1Namespace(metadata=client.V1ObjectMeta(name=cls.test_namespace))
core_v1.create_namespace(body=body)
示例9: ensure_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def ensure_namespace(kube_client: KubeClient, namespace: str) -> None:
paasta_namespace = V1Namespace(
metadata=V1ObjectMeta(name=namespace, labels={"name": namespace})
)
namespaces = kube_client.core.list_namespace()
namespace_names = [item.metadata.name for item in namespaces.items]
if namespace not in namespace_names:
log.warning(f"Creating namespace: {namespace} as it does not exist")
kube_client.core.create_namespace(body=paasta_namespace)
示例10: create_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create_namespace(name, quota, id_token):
annotations = None
if 'maxEndpoints' in quota:
annotations = {'maxEndpoints': str(quota.pop('maxEndpoints'))}
name_object = k8s_client.V1ObjectMeta(name=name, annotations=annotations,
labels={'created_by': PLATFORM_ADMIN_LABEL})
namespace = k8s_client.V1Namespace(metadata=name_object)
api_instance = get_k8s_api_client(id_token=id_token)
try:
response = api_instance.create_namespace(namespace)
except ApiException as apiException:
raise KubernetesCreateException('namespace', apiException)
logger.info("Namespace {} created".format(name))
return response
示例11: _setup_test
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def _setup_test(api_client, run_label):
"""Create the namespace for the test.
Returns:
test_dir: The local test directory.
"""
api = k8s_client.CoreV1Api(api_client)
namespace = k8s_client.V1Namespace()
namespace.api_version = "v1"
namespace.kind = "Namespace"
namespace.metadata = k8s_client.V1ObjectMeta(
name=run_label, labels={
"app": "kubeflow-e2e-test",
})
try:
logging.info("Creating namespace %s", namespace.metadata.name)
namespace = api.create_namespace(namespace)
logging.info("Namespace %s created.", namespace.metadata.name)
except rest.ApiException as e:
if e.status == 409:
logging.info("Namespace %s already exists.", namespace.metadata.name)
else:
raise
return namespace
示例12: create_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create_namespace(config, ns_name):
metadata = {'name': ns_name}
namespace = V1Namespace(metadata=metadata)
k8s_api = client_from_config(config)
k8s_api.create_namespace(namespace)
# Get available namespaces.
示例13: create_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create_namespace(self, ns_name):
self.cluster.create_namespace(client.V1Namespace(metadata=client.V1ObjectMeta(name=ns_name)))
示例14: new
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def new(cls, name: str) -> 'Namespace':
"""Create a new Namespace with object backing.
Args:
name: The name of the new Namespace.
Returns:
A new Namespace instance.
"""
return cls(client.V1Namespace(
metadata=client.V1ObjectMeta(
name=name
)
))
示例15: create_namespace
# 需要导入模块: from kubernetes import client [as 别名]
# 或者: from kubernetes.client import V1Namespace [as 别名]
def create_namespace(self):
namespace = client.V1Namespace(
api_version='v1',
kind="Namespace",
metadata=client.V1ObjectMeta(name=self.namespace)
)
try:
self.corev1_api.create_namespace(namespace)
except client.rest.ApiException as e:
raise RuntimeError("Failed to create namesapce. Got exception: {}".format(e))