diff --git a/kubeconfig/kubeconfig.py b/kubeconfig/kubeconfig.py index ec5eb43..0d44ea1 100644 --- a/kubeconfig/kubeconfig.py +++ b/kubeconfig/kubeconfig.py @@ -15,6 +15,7 @@ # Check to see our changes. print(conf.view()) """ +import json import yaml @@ -85,7 +86,7 @@ def delete_context(self, name): Deletes a context entry from your config. :param str name: The name of the context to delete from your config. - :raise: :py:exc:`KubectlCommandError ` + :raise: :py:exc:`KubectlCommandError ` when an invalid context name is specified. """ self._run_kubectl_config('delete-context', name) @@ -229,11 +230,24 @@ def use_context(self, name): """ self._run_kubectl_config('use-context', name) - def view(self): + def view(self, raw=False): """ + :param bool raw: Set to true to return raw certificate data. Defaults + to false, where sensitive tokens are returned as REDACTED. Valid + only for kubectl versions >= 1.19, where the --raw flag was + introduced; output for previous kubectl versions is already raw. :rtype: dict :return: A dict representing your full kubeconfig file, after all merging has been done. """ - conf_doc_str = self._run_kubectl_config('view') + + args = ['view'] + version = json.loads( + self._run(subcmd_args="version -o json --client".split()) + )["clientVersion"] + should_be_raw = raw and version["major"] >= 1 and version["minor"] >= 19 + + if should_be_raw: + args.append("--raw") + conf_doc_str = self._run_kubectl_config(*args) return yaml.safe_load(conf_doc_str) diff --git a/setup.py b/setup.py index a2e1fa9..dddcbda 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ author_email='greg@gctaylor.com', license='BSD', url='http://kubeconfig-python.readthedocs.io', - version='1.1.1', + version='1.2.0', packages=find_packages(), install_requires=[ 'PyYAML>=5.2',