Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit 2ad94b3

Browse files
authored
Handle relative paths in kubeconfig (#55)
* Handle relative paths in kubeconfig * Fix linting
1 parent ac24ce3 commit 2ad94b3

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

pykube/config.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ def set_current_context(self, value):
129129
"""
130130
self._current_context = value
131131

132+
@property
133+
def kubeconfig_file(self):
134+
"""
135+
Returns the path to kubeconfig file, if it exists
136+
"""
137+
if not hasattr(self, "filename"):
138+
return None
139+
return self.filename
140+
132141
@property
133142
def current_context(self):
134143
if self._current_context is None:
@@ -148,7 +157,7 @@ def clusters(self):
148157
cs[cr["name"]] = c = copy.deepcopy(cr["cluster"])
149158
if "server" not in c:
150159
c["server"] = "http://localhost"
151-
BytesOrFile.maybe_set(c, "certificate-authority")
160+
BytesOrFile.maybe_set(c, "certificate-authority", self.kubeconfig_file)
152161
self._clusters = cs
153162
return self._clusters
154163

@@ -162,8 +171,8 @@ def users(self):
162171
if "users" in self.doc:
163172
for ur in self.doc["users"]:
164173
us[ur["name"]] = u = copy.deepcopy(ur["user"])
165-
BytesOrFile.maybe_set(u, "client-certificate")
166-
BytesOrFile.maybe_set(u, "client-key")
174+
BytesOrFile.maybe_set(u, "client-certificate", self.kubeconfig_file)
175+
BytesOrFile.maybe_set(u, "client-key", self.kubeconfig_file)
167176
self._users = us
168177
return self._users
169178

@@ -202,10 +211,10 @@ def namespace(self) -> str:
202211
return self.contexts[self.current_context].get("namespace", "default")
203212

204213
def persist_doc(self):
205-
if not hasattr(self, "filename") or not self.filename:
214+
if not self.kubeconfig_file:
206215
# Config was provided as string, not way to persit it
207216
return
208-
with open(self.filename, "w") as f:
217+
with open(self.kubeconfig_file, "w") as f:
209218
yaml.safe_dump(
210219
self.doc,
211220
f,
@@ -229,16 +238,16 @@ class BytesOrFile:
229238
"""
230239

231240
@classmethod
232-
def maybe_set(cls, d, key):
241+
def maybe_set(cls, d, key, kubeconfig_file):
233242
file_key = key
234243
data_key = "{}-data".format(key)
235244
if data_key in d:
236-
d[file_key] = cls(data=d[data_key])
245+
d[file_key] = cls(data=d[data_key], kubeconfig_file=kubeconfig_file)
237246
del d[data_key]
238247
elif file_key in d:
239-
d[file_key] = cls(filename=d[file_key])
248+
d[file_key] = cls(filename=d[file_key], kubeconfig_file=kubeconfig_file)
240249

241-
def __init__(self, filename=None, data=None):
250+
def __init__(self, filename=None, data=None, kubeconfig_file=None):
242251
"""
243252
Creates a new instance of BytesOrFile.
244253
@@ -251,6 +260,19 @@ def __init__(self, filename=None, data=None):
251260
if filename is not None and data is not None:
252261
raise TypeError("filename or data kwarg must be specified, not both")
253262
elif filename is not None:
263+
264+
# If relative path is given, should be made absolute with respect to the directory of the kube config
265+
# https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#file-references
266+
if not os.path.isabs(filename):
267+
if kubeconfig_file:
268+
filename = os.path.join(os.path.dirname(kubeconfig_file), filename)
269+
else:
270+
raise exceptions.PyKubeError(
271+
"{} passed as relative path, but cannot determine location of kube config".format(
272+
filename
273+
)
274+
)
275+
254276
if not os.path.isfile(filename):
255277
raise exceptions.PyKubeError(
256278
"'{}' file does not exist".format(filename)

0 commit comments

Comments
 (0)