Skip to content

Commit

Permalink
weaveworks#20 - Support comparing Kubernetes List type objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed May 15, 2018
1 parent a12d293 commit ba0cd57
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
33 changes: 18 additions & 15 deletions kubedifflib/_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,25 @@ def check_file(printer, path, config):

differences = 0
for data in expected:
kube_obj = KubeObject.from_dict(data, config["namespace"])

printer.add(path, kube_obj)

try:
running = kube_obj.get_from_cluster(config["kubeconfig"])
except subprocess.CalledProcessError, e:
printer.diff(path, Difference(e.output, None))
differences += 1
continue


for difference in diff("", data, running):
differences += 1
printer.diff(path, difference)
return differences
for kube_obj in KubeObject.from_dict(data, config["namespace"]):
printer.add(path, kube_obj)

try:
running = kube_obj.get_from_cluster(config["kubeconfig"])
except subprocess.CalledProcessError, e:
printer.diff(path, Difference(e.output, None))
differences += 1
continue

for difference in diff("", kube_obj.data, running):
differences += 1
printer.diff(path, difference)
except Exception:
print "Failed parsing %s." % (path)
raise

return differences


class StdoutPrinter(object):
Expand Down
12 changes: 9 additions & 3 deletions kubedifflib/_kube.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class KubeObject(object):
namespace = attr.ib()
kind = attr.ib()
name = attr.ib()
data = attr.ib()

@classmethod
def from_dict(cls, data, namespace=""):
Expand All @@ -39,9 +40,14 @@ def from_dict(cls, data, namespace=""):
:param str namespace: the namespace to use if it's not defined in the object definition
"""
kind = data["kind"]
name = data["metadata"]["name"]
namespace = data["metadata"].get("namespace", namespace)
return cls(namespace, kind, name)
if kind.lower() == "list":
for obj in data["items"]:
for kube_obj in KubeObject.from_dict(obj, namespace=namespace):
yield kube_obj
else:
name = data["metadata"]["name"]
namespace = data["metadata"].get("namespace", namespace)
yield cls(namespace, kind, name, data)

@property
def namespaced_name(self):
Expand Down

0 comments on commit ba0cd57

Please sign in to comment.