def _create_stream(self, resource, namespace, wait_timeout): """ Create a stream of events for the object """ w = None stream = None try: w = watch.Watch() w._api_client = self.client.client stream = w.stream(resource.get, serialize=False, namespace=namespace, timeout_seconds=wait_timeout) except KubernetesException as exc: self.fail_json(msg='Failed to initialize watch: {0}'.format(exc.message)) return w, stream
def _create_stream(self, resource, namespace, wait_time): """ Create a stream of events for the object """ w = None stream = None try: w = watch.Watch() w._api_client = self.client.client if namespace: stream = w.stream(resource.get, serialize=False, namespace=namespace, timeout_seconds=wait_time) else: stream = w.stream(resource.get, serialize=False, namespace=namespace, timeout_seconds=wait_time) except KubernetesException: pass return w, stream
def _create_stream(self, namespace, wait_time): """ Create a stream of events for the object """ w = None stream = None try: list_method = self.helper.lookup_method('list', namespace) w = watch.Watch() w._api_client = self.helper.api_client if namespace: stream = w.stream(list_method, namespace, timeout_seconds=wait_time) else: stream = w.stream(list_method, timeout_seconds=wait_time) except KubernetesException: pass except Exception: raise return w, stream
def test_watch(monkeypatch): class MockHTTPResponse(object): def __init__(self, *args, **kwargs): pass def close(self): pass def release_conn(self): pass def mock_iter_resp_lines(resp): for name in ['test1', 'test2', 'test3']: yield json.dumps({ 'type': 'ADDED', 'object': models.V1DeploymentConfig( metadata=k8s_models.V1ObjectMeta(name=name, resource_version=1), spec=models.V1DeploymentConfigSpec()).to_dict() }) oapi = client.OapiApi() monkeypatch.setattr(oapi, 'list_deployment_config_for_all_namespaces', MockHTTPResponse) monkeypatch.setattr(k8s_watch.watch, 'iter_resp_lines', mock_iter_resp_lines) count = 2 names = ['test1', 'test2', 'test3'] w = watch.Watch(return_type="V1DeploymentConfig") for event in w.stream(oapi.list_deployment_config_for_all_namespaces, _request_timeout=2): assert event['object'].metadata.name in names names.remove(event['object'].metadata.name) count -= 1 if not count: w.stop()
def main(): v1 = client.CoreV1Api() count = 10000 w = watch.Watch() ret = v1.list_pod_for_all_namespaces(watch=False) for event in ret.items: dict = event.metadata.labels.get('backup') if dict is None: # Check if key is there, but None dict = [] else: dict print("Pod name is: %s and tag is %s" % (event.metadata.name, dict)) if dict == "postgresql": print "backup postgresql" backup_postgresql(event.metadata.name, event.metadata.namespace, dict) if dict == "mysql": print "backup mysql" backup_mysql(event.metadata.name, event.metadata.namespace, dict) count -= 1 if not count: w.stop()
'INJECT_POD'] if 'INJECT_POD' in os.environ else 'karmab/kdummy' annotation = os.environ[ 'ANNOTATION'] if 'ANNOTATION' in os.environ else None if annotation is not None: print "Injecting %s to new deployment configs with annotation %s set to true" % ( inject, annotation) else: print "Injecting %s to all new deployment configs" % inject if 'KUBERNETES_PORT' in os.environ: config.load_incluster_config() else: config.load_kube_config() oapi = client.OapiApi() resource_version = '' while True: stream = watch.Watch().stream( oapi.list_image_stream_for_all_namespaces, resource_version=resource_version) for event in stream: obj = event["object"] operation = event['type'] spec = obj.spec if not spec: continue metadata = obj.metadata resource_version = metadata._resource_version name = metadata.name if operation == 'ADDED': print("Handling %s on %s" % (operation, name)) process_image(obj)
print("This initializer operates on deployment configs") env = Environment(loader=FileSystemLoader('.')) templ = env.get_template(os.path.basename(TEMPLATE)) render = templ.render(initimage=initimage, proxyimage=proxyimage, imagepullpolicy=imagepullpolicy, verbosity=verbosity) base = yaml.load(render) containers = base['containers'] initcontainers = base['initContainers'] volumes = base['volumes'] api = oclient.OapiApi() resource_version = '' while True: stream = watch.Watch().stream( api.list_deployment_config_for_all_namespaces, include_uninitialized=True, resource_version=resource_version) for event in stream: obj = event["object"] operation = event['type'] spec = obj.spec if not spec: continue metadata = obj.metadata resource_version = metadata._resource_version name = metadata.name namespace = metadata.namespace if operation == 'ADDED': if policy != 'enabled': print("Skipping %s on %s as per current policy" % (operation, name))