Skip to content

Commit

Permalink
Improve kubernetes discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 23, 2020
1 parent 921b780 commit 22104b4
Showing 1 changed file with 41 additions and 44 deletions.
85 changes: 41 additions & 44 deletions cloudselect/discovery/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# This file may not be copied, modified, or distributed
# except according to those terms.
"""Module collecting instances from Kubernetes namespaces."""
import copy
import logging
import re

Expand All @@ -24,7 +23,7 @@ class Kubernetes(DiscoveryService):

def __init__(self):
"""Class constructor."""
self.log = logging.getLogger("cloudselect.discovery.kubernetes")
self.log = logging.getLogger("cloudselect.discovery.Kubernetes")

def run(self):
"""Collect Kubernetes pods."""
Expand All @@ -34,15 +33,11 @@ def run(self):
def instances(self):
"""Collect Kubernetes pods."""
for i in self.find():
instance_id = i["metadata"]["uid"]
metadata = self.simplify_metadata(i)
configuration = i["cluster"]["configuration"]
metadata = self.simplify_metadata(i["metadata"])
container = i["container"]
context = i["cluster"]["context"]
ip = i["status"]["pod_ip"]
name = i["metadata"]["name"]
namespace = i["metadata"]["namespace"]
node_ip = i["status"]["host_ip"]
instance_id = metadata["metadata"]["uid"]
name = metadata["metadata"]["name"]
namespace = metadata["metadata"]["namespace"]

representation = [instance_id, name, container]
self.enrich_representation(representation, metadata)
Expand All @@ -53,12 +48,12 @@ def instances(self):
None,
metadata,
representation,
configuration,
i["configuration"],
container,
context,
i["context"],
namespace,
ip,
node_ip,
metadata["status"]["pod_ip"],
metadata["status"]["host_ip"],
)
yield instance

Expand All @@ -67,36 +62,38 @@ def find(self):
cluster = Container.config.discovery.cluster()
for cluster_id in cluster:
patterns = [re.compile(i) for i in cluster[cluster_id].get("namespace", [])]
config_file = cluster[cluster_id].get("configuration")
configuration = cluster[cluster_id].get("configuration")
context = cluster[cluster_id].get("context")
api_client = config.new_client_from_config(
config_file=config_file, context=context,
)
version_api = client.VersionApi(api_client=api_client)
version = version_api.get_code()
self.log.debug(
"Connected to '%s', control plane version %s",
cluster_id,
version.git_version,
)
core_v1 = client.CoreV1Api(api_client=api_client)
ret = core_v1.list_pod_for_all_namespaces(watch=False)
for pod in ret.items:
for container in pod.spec.containers:
instance = copy.deepcopy(pod.to_dict())
if instance["status"]["phase"] == "Running":
instance["cluster"] = {
"configuration": config_file,
pods = self.get_pods(cluster_id, configuration, context)
for pod in pods.items:
namespace = pod.metadata.namespace
matched = True
if patterns:
matched = False
for i in patterns:
if i.match(namespace) is not None:
matched = True
break
if pod.status.phase == "Running" and matched:
for container in pod.spec.containers:
yield {
"configuration": configuration,
"container": container.name,
"context": context,
"metadata": pod.to_dict(),
}
instance["container"] = container.name
if patterns:
namespace = instance["metadata"]["namespace"]
matched = next(
(m for m in patterns if m.match(namespace) is not None),
None,
)
if matched is not None:
yield instance
else:
yield instance

def get_pods(self, cluster_id, configuration, context):
"""Get list of pods from cluster."""
api_client = config.new_client_from_config(
config_file=configuration, context=context,
)
version_api = client.VersionApi(api_client=api_client)
version = version_api.get_code()
self.log.debug(
"Connected to '%s', control plane version %s",
cluster_id,
version.git_version,
)
core_v1 = client.CoreV1Api(api_client=api_client)
return core_v1.list_pod_for_all_namespaces(watch=False)

0 comments on commit 22104b4

Please sign in to comment.