@@ -129,6 +129,15 @@ def set_current_context(self, value):
129
129
"""
130
130
self ._current_context = value
131
131
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
+
132
141
@property
133
142
def current_context (self ):
134
143
if self ._current_context is None :
@@ -148,7 +157,7 @@ def clusters(self):
148
157
cs [cr ["name" ]] = c = copy .deepcopy (cr ["cluster" ])
149
158
if "server" not in c :
150
159
c ["server" ] = "http://localhost"
151
- BytesOrFile .maybe_set (c , "certificate-authority" )
160
+ BytesOrFile .maybe_set (c , "certificate-authority" , self . kubeconfig_file )
152
161
self ._clusters = cs
153
162
return self ._clusters
154
163
@@ -162,8 +171,8 @@ def users(self):
162
171
if "users" in self .doc :
163
172
for ur in self .doc ["users" ]:
164
173
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 )
167
176
self ._users = us
168
177
return self ._users
169
178
@@ -202,10 +211,10 @@ def namespace(self) -> str:
202
211
return self .contexts [self .current_context ].get ("namespace" , "default" )
203
212
204
213
def persist_doc (self ):
205
- if not hasattr ( self , "filename" ) or not self . filename :
214
+ if not self . kubeconfig_file :
206
215
# Config was provided as string, not way to persit it
207
216
return
208
- with open (self .filename , "w" ) as f :
217
+ with open (self .kubeconfig_file , "w" ) as f :
209
218
yaml .safe_dump (
210
219
self .doc ,
211
220
f ,
@@ -229,16 +238,16 @@ class BytesOrFile:
229
238
"""
230
239
231
240
@classmethod
232
- def maybe_set (cls , d , key ):
241
+ def maybe_set (cls , d , key , kubeconfig_file ):
233
242
file_key = key
234
243
data_key = "{}-data" .format (key )
235
244
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 )
237
246
del d [data_key ]
238
247
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 )
240
249
241
- def __init__ (self , filename = None , data = None ):
250
+ def __init__ (self , filename = None , data = None , kubeconfig_file = None ):
242
251
"""
243
252
Creates a new instance of BytesOrFile.
244
253
@@ -251,6 +260,19 @@ def __init__(self, filename=None, data=None):
251
260
if filename is not None and data is not None :
252
261
raise TypeError ("filename or data kwarg must be specified, not both" )
253
262
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
+
254
276
if not os .path .isfile (filename ):
255
277
raise exceptions .PyKubeError (
256
278
"'{}' file does not exist" .format (filename )
0 commit comments