Skip to content

Commit

Permalink
Added KubeConfig(current_context) kwarg
Browse files Browse the repository at this point in the history
This simplifies code that sets up KubeConfig to directly set
the current context when initializing versus on another line or
through the doc (many cases not possible or easy).
brosner committed Jun 30, 2017
1 parent 1438228 commit fd66483
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pykube/config.py
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ class KubeConfig(object):
"""

@classmethod
def from_service_account(cls, path="/var/run/secrets/kubernetes.io/serviceaccount"):
def from_service_account(cls, path="/var/run/secrets/kubernetes.io/serviceaccount", **kwargs):
with open(os.path.join(path, "token")) as fp:
token = fp.read()
host = os.environ.get("PYKUBE_KUBERNETES_SERVICE_HOST")
@@ -56,11 +56,11 @@ def from_service_account(cls, path="/var/run/secrets/kubernetes.io/serviceaccoun
],
"current-context": "self",
}
self = cls(doc)
self = cls(doc, **kwargs)
return self

@classmethod
def from_file(cls, filename):
def from_file(cls, filename, **kwargs):
"""
Creates an instance of the KubeConfig class from a kubeconfig file.
@@ -72,12 +72,12 @@ def from_file(cls, filename):
raise exceptions.PyKubeError("Configuration file {} not found".format(filename))
with open(filename) as f:
doc = yaml.safe_load(f.read())
self = cls(doc)
self = cls(doc, **kwargs)
self.filename = filename
return self

@classmethod
def from_url(cls, url):
def from_url(cls, url, **kwargs):
"""
Creates an instance of the KubeConfig class from a single URL (useful
for interacting with kubectl proxy).
@@ -101,16 +101,18 @@ def from_url(cls, url):
],
"current-context": "self",
}
self = cls(doc)
self = cls(doc, **kwargs)
return self

def __init__(self, doc):
def __init__(self, doc, current_context=None):
"""
Creates an instance of the KubeConfig class.
"""
self.doc = doc
self._current_context = None
if "current-context" in doc and doc["current-context"]:
if current_context is not None:
self.set_current_context(current_context)
elif "current-context" in doc and doc["current-context"]:
self.set_current_context(doc["current-context"])

def set_current_context(self, value):

0 comments on commit fd66483

Please sign in to comment.