Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Addint the option to specify a namespace to kubediff #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kubediff
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ appropriate environment. This tools runs kubectl, so unless your
~/.kube/config is configured for the correct environment, you will need to
supply the kubeconfig for the appropriate environment.""")
parser.add_option("--kubeconfig", help="path to kubeconfig")
parser.add_option("--namespace", help="The namespace in which to target")
parser.add_option("-j", "--json", help="output in json format", action="store_true", dest="json")
parser.add_option("--no-error-on-diff", help="don't exit with 2 if diff exists",
action="store_false", dest="exit_on_diff", default="true")
Expand All @@ -31,7 +32,7 @@ supply the kubeconfig for the appropriate environment.""")
if options.json:
printer = JSONPrinter()

failed = check_files(args, printer, options.kubeconfig)
failed = check_files(args, printer, options.namespace, options.kubeconfig)
if failed and options.exit_on_diff:
sys.exit(2)

Expand Down
11 changes: 6 additions & 5 deletions kubedifflib/_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ def diff(path, want, have):
yield not_equal(path, want, have)


def check_file(printer, path, kubeconfig=None):
def check_file(printer, path, namespace="default", kubeconfig=None):
"""Check YAML file 'path' for differences.

:param namespace: Specify the namespace to check.
:param printer: Where we report differences to.
:param str path: The YAML file to test.
:param str kubeconfig: Path to a Kubernetes configuration file.
Expand All @@ -151,7 +152,7 @@ def check_file(printer, path, kubeconfig=None):
printer.add(path, kube_obj)

try:
running = kube_obj.get_from_cluster(kubeconfig=kubeconfig)
running = kube_obj.get_from_cluster(namespace=namespace, kubeconfig=kubeconfig)
except subprocess.CalledProcessError, e:
printer.diff(path, Difference(e.output, None))
differences += 1
Expand Down Expand Up @@ -218,9 +219,9 @@ def finish(self):
print json.dumps(self.data, sort_keys=True, indent=2, separators=(',', ': '))


def check_files(paths, printer, kubeconfig=None):
def check_files(paths, printer, namespace="default", kubeconfig=None):
"""Check all files in 'paths' for differences to a Kubernetes cluster.

:param namespace: Specify the namespace to check from
:param printer: Where differences are reported to as they are found.
:param str kubeconfig: Path to a kubeconfig file for the cluster to diff
against.
Expand All @@ -231,7 +232,7 @@ def check_files(paths, printer, kubeconfig=None):
_, extension = os.path.splitext(path)
if extension != ".yaml":
continue
differences += check_file(printer, path, kubeconfig=kubeconfig)
differences += check_file(printer, path, namespace=namespace, kubeconfig=kubeconfig)

printer.finish()
return bool(differences)
5 changes: 3 additions & 2 deletions kubedifflib/_kube.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ def from_dict(cls, data):
def namespaced_name(self):
return "%s/%s" % (self.namespace, self.name)

def get_from_cluster(self, kubeconfig=None):
def get_from_cluster(self, namespace="default", kubeconfig=None):
"""Fetch data for this object from a Kubernetes cluster.

:param str namespace: Specify the namespace to target
:param str kubeconfig: Path to a Kubernetes configuration file. If None,
fetches data from the default cluster.
:return: A dict of data for this Kubernetes object.
"""
args = ["--namespace=%s" % self.namespace, "-o=yaml"]
args = ["--namespace=%s" % namespace, "-o=yaml"]
if kubeconfig is not None:
args.append("--kubeconfig=%s" % kubeconfig)

Expand Down