Skip to content

Commit

Permalink
Log restarts of K8S watches
Browse files Browse the repository at this point in the history
Signed-off-by: Bartosz Zurkowski <zurkowski.bartosz@gmail.com>
  • Loading branch information
bzurkowski committed Apr 9, 2020
1 parent cb42a06 commit 323a6c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions orca/common/clients/istio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ def get(kind):
client = k8s.ClientFactory.get()
if kind == 'virtual_service':
return k8s.CustomResourceProxy(
client.CustomObjectsApi().list_cluster_custom_object,
'virtual_service', client.CustomObjectsApi().list_cluster_custom_object,
group="networking.istio.io",
version="v1alpha3",
plural="virtualservices")
elif kind == 'destination_rule':
return k8s.CustomResourceProxy(
client.CustomObjectsApi().list_cluster_custom_object,
'destination_rule', client.CustomObjectsApi().list_cluster_custom_object,
group="networking.istio.io",
version="v1alpha3",
plural="destinationrules")
elif kind == 'gateway':
return k8s.CustomResourceProxy(
client.CustomObjectsApi().list_cluster_custom_object,
'gateway', client.CustomObjectsApi().list_cluster_custom_object,
group="networking.istio.io",
version="v1alpha3",
plural="gateways")
Expand Down
35 changes: 20 additions & 15 deletions orca/common/clients/k8s/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,68 @@ def get(kind):
client = ClientFactory.get()
if kind == 'pod':
return ResourceProxy(
client.CoreV1Api().list_pod_for_all_namespaces)
'pod', client.CoreV1Api().list_pod_for_all_namespaces)
elif kind == 'service':
return ResourceProxy(
client.CoreV1Api().list_service_for_all_namespaces)
'service', client.CoreV1Api().list_service_for_all_namespaces)
elif kind == 'endpoints':
return ResourceProxy(
client.CoreV1Api().list_endpoints_for_all_namespaces)
'endpoints', client.CoreV1Api().list_endpoints_for_all_namespaces)
elif kind == 'config_map':
return ResourceProxy(
client.CoreV1Api().list_config_map_for_all_namespaces)
'config_map', client.CoreV1Api().list_config_map_for_all_namespaces)
elif kind == 'secret':
return ResourceProxy(
client.CoreV1Api().list_secret_for_all_namespaces)
'secret', client.CoreV1Api().list_secret_for_all_namespaces)
elif kind == 'node':
return ResourceProxy(
client.CoreV1Api().list_node)
'node', client.CoreV1Api().list_node)
elif kind == 'deployment':
return ResourceProxy(
client.AppsV1Api().list_deployment_for_all_namespaces)
'deployment', client.AppsV1Api().list_deployment_for_all_namespaces)
elif kind == 'stateful_set':
return ResourceProxy(
client.AppsV1Api().list_stateful_set_for_all_namespaces)
'stateful_set', client.AppsV1Api().list_stateful_set_for_all_namespaces)
elif kind == 'daemon_set':
return ResourceProxy(
client.AppsV1Api().list_daemon_set_for_all_namespaces)
'daemon_set', client.AppsV1Api().list_daemon_set_for_all_namespaces)
elif kind == 'replica_set':
return ResourceProxy(
'replica_set',
client.ExtensionsV1beta1Api().list_replica_set_for_all_namespaces)
elif kind == 'storage_class':
return ResourceProxy(
client.StorageV1Api().list_storage_class)
'storage_class', client.StorageV1Api().list_storage_class)
elif kind == 'persistent_volume':
return ResourceProxy(
client.CoreV1Api().list_persistent_volume)
'persistent_volume', client.CoreV1Api().list_persistent_volume)
elif kind == 'persistent_volume_claim':
return ResourceProxy(
'persistent_volume_claim',
client.CoreV1Api().list_persistent_volume_claim_for_all_namespaces)
elif kind == 'namespace':
return ResourceProxy(
client.CoreV1Api().list_namespace)
'namespace', client.CoreV1Api().list_namespace)
elif kind == 'horizontal_pod_autoscaler':
return ResourceProxy(
'horizontal_pod_autoscaler',
client.AutoscalingV1Api().list_horizontal_pod_autoscaler_for_all_namespaces)
else:
raise Exception("Unknown kind %s" % kind)


class ResourceProxy(object):

def __init__(self, list_fn):
def __init__(self, kind, list_fn):
self._kind = kind
self._list_fn = list_fn

def get_all(self):
return self._list_fn().items

def watch(self):
while True:
LOG.debug("Restarting watch for resource kind: %s", self._kind)
for event in self._watch_resource():
event_type, event_obj = event['type'], event['object']

Expand All @@ -118,8 +123,8 @@ def _watch_resource(self):

class CustomResourceProxy(ResourceProxy):

def __init__(self, list_fn, group, version, plural):
super().__init__(list_fn)
def __init__(self, kind, list_fn, group, version, plural):
super().__init__(kind, list_fn)
self._group = group
self._version = version
self._plural = plural
Expand Down

0 comments on commit 323a6c1

Please sign in to comment.