本文整理汇总了Python中kubernetes.client方法的典型用法代码示例。如果您正苦于以下问题:Python kubernetes.client方法的具体用法?Python kubernetes.client怎么用?Python kubernetes.client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kubernetes
的用法示例。
在下文中一共展示了kubernetes.client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def __init__(self,
kube_config: Any,
task_queue: 'Queue[KubernetesJobType]',
result_queue: 'Queue[KubernetesResultsType]',
kube_client: client.CoreV1Api,
worker_uuid: str):
super().__init__()
self.log.debug("Creating Kubernetes executor")
self.kube_config = kube_config
self.task_queue = task_queue
self.result_queue = result_queue
self.namespace = self.kube_config.kube_namespace
self.log.debug("Kubernetes using namespace %s", self.namespace)
self.kube_client = kube_client
self.launcher = PodLauncher(kube_client=self.kube_client)
self.worker_configuration_pod = WorkerConfiguration(kube_config=self.kube_config).as_pod()
self._manager = multiprocessing.Manager()
self.watcher_queue = self._manager.Queue()
self.worker_uuid = worker_uuid
self.kube_watcher = self._make_kube_watcher()
示例2: shared_client
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def shared_client(ClientType, *args, **kwargs):
"""Return a single shared kubernetes client instance
A weak reference to the instance is cached,
so that concurrent calls to shared_client
will all return the same instance until
all references to the client are cleared.
"""
kwarg_key = tuple((key, kwargs[key]) for key in sorted(kwargs))
cache_key = (ClientType, args, kwarg_key)
client = None
if cache_key in _client_cache:
# resolve cached weakref
# client can still be None after this!
client = _client_cache[cache_key]()
if client is None:
Client = getattr(kubernetes.client, ClientType)
client = Client(*args, **kwargs)
# cache weakref so that clients can be garbage collected
_client_cache[cache_key] = weakref.ref(client)
return client
示例3: run
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def run(self) -> None:
"""Performs watching"""
kube_client: client.CoreV1Api = get_kube_client()
if not self.worker_uuid:
raise AirflowException(NOT_STARTED_MESSAGE)
while True:
try:
self.resource_version = self._run(kube_client, self.resource_version,
self.worker_uuid, self.kube_config)
except ReadTimeoutError:
self.log.warning("There was a timeout error accessing the Kube API. "
"Retrying request.", exc_info=True)
time.sleep(1)
except Exception:
self.log.exception('Unknown error in KubernetesJobWatcher. Failing')
raise
else:
self.log.warning('Watch died gracefully, starting back up with: '
'last resource_version: %s', self.resource_version)
示例4: start
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def start(self) -> None:
"""Starts the executor"""
self.log.info('Start Kubernetes executor')
self.worker_uuid = KubeWorkerIdentifier.get_or_create_current_kube_worker_uuid()
if not self.worker_uuid:
raise AirflowException("Could not get worker uuid")
self.log.debug('Start with worker_uuid: %s', self.worker_uuid)
# always need to reset resource version since we don't know
# when we last started, note for behavior below
# https://github.com/kubernetes-client/python/blob/master/kubernetes/docs
# /CoreV1Api.md#list_namespaced_pod
KubeResourceVersion.reset_resource_version()
self.kube_client = get_kube_client()
self.kube_scheduler = AirflowKubernetesScheduler(
self.kube_config, self.task_queue, self.result_queue,
self.kube_client, self.worker_uuid
)
self._inject_secrets()
self.clear_not_launched_queued_tasks()
示例5: delete_pods
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def delete_pods(self, pods):
"""
https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/docs/
CoreV1Api.md#delete_namespaced_pod
Delete all pods synchronously
"""
for pod in pods:
try:
self.client_corev1api.delete_namespaced_pod(
name=pod.name,
namespace=pod.namespace,
grace_period_seconds=0
)
except ApiException as e:
self.logger.exception(e)
return False
return True
示例6: _authenticate_with_kubernetes
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def _authenticate_with_kubernetes(self):
"""Authenticate with the defined AKS cluster and write the configuration to a file"""
resource_group = get_resource_group_name(self.config, self.env)
cluster_name = get_kubernetes_name(self.config, self.env)
# get azure container service client
credentials = ActiveDirectoryUserCredentials(
vault_name=self.vault_name, vault_client=self.vault_client
).credentials(self.config)
client = ContainerServiceClient(
credentials=credentials,
subscription_id=SubscriptionId(self.vault_name, self.vault_client).subscription_id(self.config),
)
# authenticate with Kubernetes
credential_results = client.managed_clusters.list_cluster_user_credentials(
resource_group_name=resource_group, resource_name=cluster_name
)
self._write_kube_config(credential_results)
示例7: test_create_v1pod_timeout
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def test_create_v1pod_timeout(self, mock__get_k8s_api_client,
mock_random_choice, mock_time):
k8s_api = mock__get_k8s_api_client.return_value
manifest = (
{"apiVersion": "v1", "kind": "Pod",
"metadata": {"name": "nginx"}})
k8s_api.create_namespaced_pod.return_value = self.pod
mock_time.side_effect = [1, 2, 3, 4, 5, 1800, 1801]
not_ready_pod = kubernetes_client.models.V1Pod()
not_ready_status = kubernetes_client.models.V1PodStatus()
not_ready_status.phase = "not_ready"
not_ready_pod_metadata = kubernetes_client.models.V1ObjectMeta()
not_ready_pod_metadata.uid = "123456789"
not_ready_pod.status = not_ready_status
not_ready_pod.metadata = not_ready_pod_metadata
k8s_api.read_namespaced_pod = mock.MagicMock(
side_effect=[not_ready_pod
for i in range(4)])
self.assertRaises(
exceptions.TimeoutException,
self.scenario._create_v1pod, manifest)
示例8: delete_deployment
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def delete_deployment(params):
from kubernetes.client import V1DeleteOptions
spec = get_seldon_spec(params)
name = get_deployment_name(params) # spec["metadata"]["name"]
namespace = "default" # TODO: the namespace should be configured or be figured out dynamically
plural = spec["kind"].lower()+"s" # TODO: verify the "rule" for constructing plural
group, version = spec["apiVersion"].split("/")
del_opts = V1DeleteOptions()
api_client = get_custom_objects_api_client()
api_response = api_client.list_namespaced_custom_object(group, version, namespace, plural)
if name in [deployment["metadata"]["name"] for deployment in api_response["items"]]:
api_response = api_client.delete_namespaced_custom_object(group, version, namespace, plural, name, del_opts)
else:
LOG.error("Could not find the Seldon deployment '%s'" % name)
return {
"status": "Error",
"details": "Could not find a Seldon deployment with name '%s'" % name
}
# api_response_filtered = {key: api_response[key] for key in ["apiVersion", "kind"]}
LOG.info("%s ..." % str(api_response)[:160])
return api_response
示例9: replace_or_create_service
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def replace_or_create_service(self, namespace: str, service: V1Service):
'''Create a service in a given namespace. If the service already exists,
the previous version will be deleted beforehand
'''
not_empty(namespace)
not_none(service)
service_name = service.metadata.name
existing_service = self.get_service(namespace=namespace, name=service_name)
if existing_service:
delete_options = kubernetes.client.V1DeleteOptions()
delete_options.grace_period_seconds = 0
self.core_api.delete_namespaced_service(
namespace=namespace,
name=service_name,
body=delete_options,
)
self.create_service(namespace=namespace, service=service)
示例10: replace_or_create_deployment
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def replace_or_create_deployment(self, namespace: str, deployment: V1Deployment):
'''Create a deployment in a given namespace. If the deployment already exists,
the previous version will be deleted beforehand.
'''
not_empty(namespace)
not_none(deployment)
deployment_name = deployment.metadata.name
existing_deployment = self.get_deployment(namespace=namespace, name=deployment_name)
if existing_deployment:
self.apps_api.delete_namespaced_deployment(
namespace=namespace,
name=deployment_name,
body=kubernetes.client.V1DeleteOptions()
)
self.create_deployment(namespace=namespace, deployment=deployment)
示例11: replace_or_create_ingress
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def replace_or_create_ingress(self, namespace: str, ingress: V1beta1Ingress):
'''Create an ingress in a given namespace. If the ingress already exists,
the previous version will be deleted beforehand.
'''
not_empty(namespace)
not_none(ingress)
ingress_name = ingress.metadata.name
existing_ingress = self.get_ingress(namespace=namespace, name=ingress_name)
if existing_ingress:
self.extensions_v1beta1_api.delete_namespaced_ingress(
namespace=namespace,
name=ingress_name,
body=kubernetes.client.V1DeleteOptions()
)
self.create_ingress(namespace=namespace, ingress=ingress)
示例12: delete_pod
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def delete_pod(self, name: str, namespace: str, grace_period_seconds: int=0):
'''Delete a pod in the given namespace.
grace_period_seconds: the duration in seconds before the object should be deleted.
Value must be non-negative integer. The value zero indicates delete immediately.
'''
not_empty(namespace)
not_empty(name)
body = kubernetes.client.V1DeleteOptions()
try:
self.core_api.delete_namespaced_pod(
name, namespace, body=body, grace_period_seconds=grace_period_seconds
)
except ApiException as ae:
if ae.status == 404:
return None
raise ae
示例13: get_api_instance
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def get_api_instance(self):
self.cluster.change_to()
master = self.cluster.group_set.get(name='master').hosts.first()
if master is not None and master.ip is not None:
configuration = kubernetes.client.Configuration()
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.api_key['authorization'] = self.token
configuration.debug = True
configuration.host = 'https://' + master.ip + ":6443"
configuration.verify_ssl = False
self.api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(configuration))
self.app_v1_api = kubernetes.client.AppsV1Api(kubernetes.client.ApiClient(configuration))
self.storage_v1_Api = kubernetes.client.StorageV1Api(kubernetes.client.ApiClient(configuration))
示例14: check_authorization
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def check_authorization(self, retry_count):
if retry_count == 0:
raise Exception('init k8s client failed! retry_count=' + str(retry_count))
self.retry_count = retry_count - 1
try:
self.api_instance.list_node()
except ApiException as e:
if e.status == 401:
self.push_token_to_redis()
else:
logger.error(msg='init k8s client failed ' + e.reason, exc_info=True)
示例15: _run
# 需要导入模块: import kubernetes [as 别名]
# 或者: from kubernetes import client [as 别名]
def _run(self,
kube_client: client.CoreV1Api,
resource_version: Optional[str],
worker_uuid: str,
kube_config: Any) -> Optional[str]:
self.log.info(
'Event: and now my watch begins starting at resource_version: %s',
resource_version
)
watcher = watch.Watch()
kwargs = {'label_selector': 'airflow-worker={}'.format(worker_uuid)}
if resource_version:
kwargs['resource_version'] = resource_version
if kube_config.kube_client_request_args:
for key, value in kube_config.kube_client_request_args.items():
kwargs[key] = value
last_resource_version: Optional[str] = None
for event in watcher.stream(kube_client.list_pod_for_all_namespaces, **kwargs):
task = event['object']
self.log.info(
'Event: %s had an event of type %s',
task.metadata.name, event['type']
)
if event['type'] == 'ERROR':
return self.process_error(event)
self.process_status(
pod_id=task.metadata.name,
namespace=task.metadata.namespace,
status=task.status.phase,
labels=task.metadata.labels,
resource_version=task.metadata.resource_version,
event=event,
)
last_resource_version = task.metadata.resource_version
return last_resource_version