@@ -62,8 +62,7 @@ def has_cluster(self, name):
62
62
Return true if this kubeconfig contains an entry
63
63
For the passed cluster name.
64
64
"""
65
- if 'clusters' not in self .content or \
66
- self .content ['clusters' ] is None :
65
+ if self .content .get ('clusters' ) is None :
67
66
return False
68
67
return name in [cluster ['name' ]
69
68
for cluster in self .content ['clusters' ] if 'name' in cluster ]
@@ -189,7 +188,7 @@ def write_kubeconfig(self, config):
189
188
except OSError as e :
190
189
if e .errno != errno .EEXIST :
191
190
raise KubeconfigInaccessableError (
192
- "Can't create directory for writing: {0}" .format (e ))
191
+ "Can't create directory for writing: {0}" .format (e ))
193
192
try :
194
193
with os .fdopen (
195
194
os .open (
@@ -206,34 +205,28 @@ def write_kubeconfig(self, config):
206
205
class KubeconfigAppender (object ):
207
206
def insert_entry (self , config , key , entry ):
208
207
"""
209
- Insert entry into the array at content[key]
208
+ Insert entry into the entries list at content[key]
210
209
Overwrite an existing entry if they share the same name
211
210
212
211
:param config: The kubeconfig to insert an entry into
213
212
:type config: Kubeconfig
214
213
"""
215
- if key not in config .content or \
216
- config .content [key ] is None :
217
- config .content [key ] = []
218
- array = config .content [key ]
219
- if not isinstance (array , list ):
220
- raise KubeconfigError ("Tried to insert into {0},"
221
- "which is a {1} "
222
- "not a {2}" .format (key ,
223
- type (array ),
224
- list ))
214
+ config .content [key ] = config .content .get (key ) or []
215
+ entries = config .content [key ]
216
+ if not isinstance (entries , list ):
217
+ raise KubeconfigError (f"Tried to insert into { key } , "
218
+ f"which is a { type (entries )} "
219
+ f"not a { list } " )
225
220
found = False
226
- for counter , existing_entry in enumerate (array ):
227
- if "name" in existing_entry and \
228
- "name" in entry and \
229
- existing_entry ["name" ] == entry ["name" ]:
230
- array [counter ] = entry
221
+ for i , existing_entry in enumerate (entries ):
222
+ if "name" in existing_entry and "name" in entry \
223
+ and existing_entry ["name" ] == entry ["name" ]:
224
+ entries [i ] = entry
231
225
found = True
232
226
233
227
if not found :
234
- array .append (entry )
228
+ entries .append (entry )
235
229
236
- config .content [key ] = array
237
230
return config
238
231
239
232
def _make_context (self , cluster , user , alias = None ):
0 commit comments