本文整理汇总了Python中kubernetes.watch.Watch方法的典型用法代码示例。如果您正苦于以下问题:Python watch.Watch方法的具体用法?Python watch.Watch怎么用?Python watch.Watch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kubernetes.watch
的用法示例。
在下文中一共展示了watch.Watch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def __init__(self,
kube_client: client.CoreV1Api = None,
in_cluster: bool = True,
cluster_context: Optional[str] = None,
extract_xcom: bool = False):
"""
Creates the launcher.
:param kube_client: kubernetes client
:param in_cluster: whether we are in cluster
:param cluster_context: context of the cluster
:param extract_xcom: whether we should extract xcom
"""
super().__init__()
self._client = kube_client or get_kube_client(in_cluster=in_cluster,
cluster_context=cluster_context)
self._watch = watch.Watch()
self.extract_xcom = extract_xcom
示例2: run
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [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)
示例3: wait_get_completed_podphase
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def wait_get_completed_podphase(
self, release, timeout=DEFAULT_K8S_TIMEOUT):
'''
:param release: part of namespace
:param timeout: time before disconnecting stream
'''
timeout = self._check_timeout(timeout)
w = watch.Watch()
found_events = False
for event in w.stream(self.client.list_pod_for_all_namespaces,
timeout_seconds=timeout):
resource_name = event['object'].metadata.name
if release in resource_name:
found_events = True
pod_state = event['object'].status.phase
if pod_state == 'Succeeded':
w.stop()
break
if not found_events:
LOG.warn('Saw no test events for release %s', release)
示例4: wait_get_completed_podphase
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def wait_get_completed_podphase(self, release,
timeout=DEFAULT_K8S_TIMEOUT):
'''
:param release - part of namespace
:param timeout - time before disconnecting stream
'''
w = watch.Watch()
for event in w.stream(self.client.list_pod_for_all_namespaces,
timeout_seconds=timeout):
pod_name = event['object'].metadata.name
if release in pod_name:
pod_state = event['object'].status.phase
if pod_state == 'Succeeded':
w.stop()
break
示例5: watch
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def watch(self):
from urllib3.exceptions import ReadTimeoutError
from kubernetes import watch
cur_pods = set(self.get(True))
w = watch.Watch()
while True:
# when some schedulers are not ready, we refresh faster
linger = 10 if self.is_all_ready() else 1
streamer = w.stream(self._client.list_namespaced_pod,
namespace=self._k8s_namespace,
label_selector=self._label_selector,
timeout_seconds=linger)
while True:
try:
event = self._pool.spawn(next, streamer, StopIteration).result()
if event is StopIteration:
raise StopIteration
except (ReadTimeoutError, StopIteration):
new_pods = set(self.get(True))
if new_pods != cur_pods:
cur_pods = new_pods
yield self.get(False)
break
except: # noqa: E722
logger.exception('Unexpected error when watching on kubernetes')
break
obj_dict = event['object'].to_dict()
pod_name, endpoint = self._extract_pod_name_ep(obj_dict)
self._pod_to_ep[pod_name] = endpoint \
if endpoint and self._extract_pod_ready(obj_dict) else None
yield self.get(False)
示例6: watch
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def watch(self, resource, namespace=None, name=None, label_selector=None, field_selector=None, resource_version=None, timeout=None):
"""
Stream events for a resource from the Kubernetes API
:param resource: The API resource object that will be used to query the API
:param namespace: The namespace to query
:param name: The name of the resource instance to query
:param label_selector: The label selector with which to filter results
:param field_selector: The field selector with which to filter results
:param resource_version: The version with which to filter results. Only events with
a resource_version greater than this value will be returned
:param timeout: The amount of time in seconds to wait before terminating the stream
:return: Event object with these keys:
'type': The type of event such as "ADDED", "DELETED", etc.
'raw_object': a dict representing the watched object.
'object': A ResourceInstance wrapping raw_object.
Example:
client = DynamicClient(k8s_client)
v1_pods = client.resources.get(api_version='v1', kind='Pod')
for e in v1_pods.watch(resource_version=0, namespace=default, timeout=5):
print(e['type'])
print(e['object'].metadata)
"""
watcher = watch.Watch()
for event in watcher.stream(
resource.get,
namespace=namespace,
name=name,
field_selector=field_selector,
label_selector=label_selector,
resource_version=resource_version,
serialize=False,
timeout_seconds=timeout
):
event['object'] = ResourceInstance(resource, event['object'])
yield event
示例7: _run
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [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
示例8: wait_for_pod_redeployment
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def wait_for_pod_redeployment(self, old_pod_name, namespace):
'''
:param old_pod_name: name of pods
:param namespace: kubernetes namespace
'''
base_pod_pattern = re.compile('^(.+)-[a-zA-Z0-9]+$')
if not base_pod_pattern.match(old_pod_name):
LOG.error(
'Could not identify new pod after purging %s', old_pod_name)
return
pod_base_name = base_pod_pattern.match(old_pod_name).group(1)
new_pod_name = ''
w = watch.Watch()
for event in w.stream(self.client.list_namespaced_pod, namespace):
event_name = event['object'].metadata.name
event_match = base_pod_pattern.match(event_name)
if not event_match or not event_match.group(1) == pod_base_name:
continue
pod_conditions = event['object'].status.conditions
# wait for new pod deployment
if event['type'] == 'ADDED' and not pod_conditions:
new_pod_name = event_name
elif new_pod_name:
for condition in pod_conditions:
if (condition.type == 'Ready'
and condition.status == 'True'):
LOG.info('New pod %s deployed', new_pod_name)
w.stop()
示例9: main
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
v1 = client.CoreV1Api()
count = 10
w = watch.Watch()
for event in w.stream(v1.list_namespace, timeout_seconds=10):
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
count -= 1
if not count:
w.stop()
print("Finished namespace stream.")
for event in w.stream(v1.list_pod_for_all_namespaces, timeout_seconds=10):
print("Event: %s %s %s" % (
event['type'],
event['object'].kind,
event['object'].metadata.name)
)
count -= 1
if not count:
w.stop()
print("Finished pod stream.")
示例10: _delete_job_action
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def _delete_job_action(self, list_func, delete_func, job_type_description,
name, namespace="default",
propagation_policy='Foreground',
timeout=DEFAULT_K8S_TIMEOUT):
try:
LOG.debug('Deleting %s %s, Wait timeout=%s',
job_type_description, name, timeout)
body = client.V1DeleteOptions()
w = watch.Watch()
issue_delete = True
for event in w.stream(list_func,
namespace=namespace,
timeout_seconds=timeout):
if issue_delete:
delete_func(
name=name, namespace=namespace, body=body,
propagation_policy=propagation_policy)
issue_delete = False
event_type = event['type'].upper()
job_name = event['object'].metadata.name
if event_type == 'DELETED' and job_name == name:
LOG.debug('Successfully deleted %s %s',
job_type_description, job_name)
return
err_msg = ('Reached timeout while waiting to delete %s: '
'name=%s, namespace=%s' %
(job_type_description, name, namespace))
LOG.error(err_msg)
raise exceptions.KubernetesWatchTimeoutException(err_msg)
except ApiException as e:
LOG.exception(
"Exception when deleting %s: name=%s, namespace=%s",
job_type_description, name, namespace)
raise e
示例11: wait_for_pod_redeployment
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def wait_for_pod_redeployment(self, old_pod_name, namespace):
'''
:param old_pod_name - name of pods
:param namespace - kubernetes namespace
'''
base_pod_pattern = re.compile('^(.+)-[a-zA-Z0-9]+$')
if not base_pod_pattern.match(old_pod_name):
LOG.error('Could not identify new pod after purging %s',
old_pod_name)
return
pod_base_name = base_pod_pattern.match(old_pod_name).group(1)
new_pod_name = ''
w = watch.Watch()
for event in w.stream(self.client.list_namespaced_pod, namespace):
event_name = event['object'].metadata.name
event_match = base_pod_pattern.match(event_name)
if not event_match or not event_match.group(1) == pod_base_name:
continue
pod_conditions = event['object'].status.conditions
# wait for new pod deployment
if event['type'] == 'ADDED' and not pod_conditions:
new_pod_name = event_name
elif new_pod_name:
for condition in pod_conditions:
if (condition.type == 'Ready' and
condition.status == 'True'):
LOG.info('New pod %s deployed', new_pod_name)
w.stop()
示例12: _watch_resource
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def _watch_resource(self):
return watch.Watch().stream(self._list_fn)
示例13: wait_until_deployment_available
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def wait_until_deployment_available(self, namespace: str, name: str, timeout_seconds: int=60):
'''Block until the given deployment has at least one available replica (or timeout)
Return `True` if the deployment is available, `False` if a timeout occured.
'''
not_empty(namespace)
not_empty(name)
w = watch.Watch()
# Work around IncompleteRead errors resulting in ProtocolErrors - no fault of our own
start_time = int(time.time())
while (start_time + timeout_seconds) > time.time():
try:
for event in w.stream(
self.apps_api.list_namespaced_deployment,
namespace=namespace,
timeout_seconds=timeout_seconds
):
deployment_spec = event['object']
if deployment_spec is not None:
if deployment_spec.metadata.name == name:
if deployment_spec.status.available_replicas is not None \
and deployment_spec.status.available_replicas > 0:
return True
# Check explicitly if timeout occurred
if (start_time + timeout_seconds) < time.time():
return False
# Regular Watch.stream() timeout occurred, no need for further checks
return False
except ProtocolError:
info('http connection error - ignored')
示例14: run
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def run(self):
"""
Log all events generated for a namespace
Will only log events that were generated after the EventWatcher thread has started
"""
log.info("Starting event watcher on namespace '%s'", self.namespace)
# Get the events from the API and grab the last one listed (if there is one)
# We only want to show events that appear AFTER this event.
# This is similar in function to 'oc get events --watch-only=true'
old_event_time = pytz.utc.localize(datetime.datetime.min)
old_events = self.get_all_events()
if old_events:
last_old_event = sorted(old_events, key=lambda x: x.last_timestamp)[-1]
old_event_time = last_old_event.last_timestamp
self._watcher = watch.Watch()
last_event_info = None
for event in self._watcher.stream(self.v1_client.list_namespaced_event, self.namespace):
obj = event["object"]
event_info = (
obj.last_timestamp,
obj.involved_object.kind,
obj.involved_object.name,
obj.type,
obj.reason,
obj.message,
)
# Only print new events, and don't print repeat events
if obj.last_timestamp > old_event_time and event_info != last_event_info:
log.info(" --> [%s] [%s %s] [%s - %s] %s", *event_info)
last_event_info = event_info
示例15: _deployment_readiness_has_state
# 需要导入模块: from kubernetes import watch [as 别名]
# 或者: from kubernetes.watch import Watch [as 别名]
def _deployment_readiness_has_state(
name: str, ready: bool,
ns: str = "default",
label_selector: str = None,
timeout: int = 30,
secrets: Secrets = None) -> Union[bool, None]:
"""
Check wether if the given deployment state is ready or not
according to the ready paramter.
If the state is not reached after `timeout` seconds, a
:exc:`chaoslib.exceptions.ActivityFailed` exception is raised.
"""
field_selector = "metadata.name={name}".format(name=name)
api = create_k8s_api_client(secrets)
v1 = client.AppsV1beta1Api(api)
w = watch.Watch()
timeout = int(timeout)
if label_selector is None:
watch_events = partial(w.stream, v1.list_namespaced_deployment,
namespace=ns,
field_selector=field_selector,
_request_timeout=timeout)
else:
label_selector = label_selector.format(name=name)
watch_events = partial(w.stream, v1.list_namespaced_deployment,
namespace=ns,
field_selector=field_selector,
label_selector=label_selector,
_request_timeout=timeout)
try:
logger.debug("Watching events for {t}s".format(t=timeout))
for event in watch_events():
deployment = event['object']
status = deployment.status
spec = deployment.spec
logger.debug(
"Deployment '{p}' {t}: "
"Ready Replicas {r} - "
"Unavailable Replicas {u} - "
"Desired Replicas {a}".format(
p=deployment.metadata.name, t=event["type"],
r=status.ready_replicas,
a=spec.replicas,
u=status.unavailable_replicas))
readiness = status.ready_replicas == spec.replicas
if ready == readiness:
w.stop()
return True
except urllib3.exceptions.ReadTimeoutError:
logger.debug("Timed out!")
return False