@@ -60,15 +60,17 @@ def _cleanup_temp_files():
60
60
_temp_files = {}
61
61
62
62
63
- def _create_temp_file_with_content (content ):
63
+ def _create_temp_file_with_content (content , temp_file_path = None ):
64
64
if len (_temp_files ) == 0 :
65
65
atexit .register (_cleanup_temp_files )
66
66
# Because we may change context several times, try to remember files we
67
67
# created and reuse them at a small memory cost.
68
68
content_key = str (content )
69
69
if content_key in _temp_files :
70
70
return _temp_files [content_key ]
71
- _ , name = tempfile .mkstemp ()
71
+ if not os .path .isdir (temp_file_path ):
72
+ os .makedirs (name = temp_file_path , exist_ok = True )
73
+ _ , name = tempfile .mkstemp (dir = temp_file_path )
72
74
_temp_files [content_key ] = name
73
75
with open (name , 'wb' ) as fd :
74
76
fd .write (content .encode () if isinstance (content , str ) else content )
@@ -91,12 +93,14 @@ class FileOrData(object):
91
93
result in base64 encode of the file content after read."""
92
94
93
95
def __init__ (self , obj , file_key_name , data_key_name = None ,
94
- file_base_path = "" , base64_file_content = True ):
96
+ file_base_path = "" , base64_file_content = True ,
97
+ temp_file_path = None ):
95
98
if not data_key_name :
96
99
data_key_name = file_key_name + "-data"
97
100
self ._file = None
98
101
self ._data = None
99
102
self ._base64_file_content = base64_file_content
103
+ self ._temp_file_path = temp_file_path
100
104
if not obj :
101
105
return
102
106
if data_key_name in obj :
@@ -116,9 +120,9 @@ def as_file(self):
116
120
else :
117
121
content = self ._data
118
122
self ._file = _create_temp_file_with_content (
119
- base64 .standard_b64decode (content ))
123
+ base64 .standard_b64decode (content ), self . _temp_file_path )
120
124
else :
121
- self ._file = _create_temp_file_with_content (self ._data )
125
+ self ._file = _create_temp_file_with_content (self ._data , self . _temp_file_path )
122
126
if self ._file and not os .path .isfile (self ._file ):
123
127
raise ConfigException ("File does not exist: %s" % self ._file )
124
128
return self ._file
@@ -182,7 +186,8 @@ class KubeConfigLoader(object):
182
186
def __init__ (self , config_dict , active_context = None ,
183
187
get_google_credentials = None ,
184
188
config_base_path = "" ,
185
- config_persister = None ):
189
+ config_persister = None ,
190
+ temp_file_path = None ):
186
191
187
192
if config_dict is None :
188
193
raise ConfigException (
@@ -199,6 +204,7 @@ def __init__(self, config_dict, active_context=None,
199
204
self .set_active_context (active_context )
200
205
self ._config_base_path = config_base_path
201
206
self ._config_persister = config_persister
207
+ self ._temp_file_path = temp_file_path
202
208
203
209
def _refresh_credentials_with_cmd_path ():
204
210
config = self ._user ['auth-provider' ]['config' ]
@@ -489,12 +495,14 @@ def _load_from_exec_plugin(self):
489
495
status , None ,
490
496
data_key_name = 'clientCertificateData' ,
491
497
file_base_path = base_path ,
492
- base64_file_content = False ).as_file ()
498
+ base64_file_content = False ,
499
+ temp_file_path = self ._temp_file_path ).as_file ()
493
500
self .key_file = FileOrData (
494
501
status , None ,
495
502
data_key_name = 'clientKeyData' ,
496
503
file_base_path = base_path ,
497
- base64_file_content = False ).as_file ()
504
+ base64_file_content = False ,
505
+ temp_file_path = self ._temp_file_path ).as_file ()
498
506
return True
499
507
logging .error ('exec: missing token or clientCertificateData field '
500
508
'in plugin output' )
@@ -507,7 +515,8 @@ def _load_user_token(self):
507
515
token = FileOrData (
508
516
self ._user , 'tokenFile' , 'token' ,
509
517
file_base_path = base_path ,
510
- base64_file_content = False ).as_data ()
518
+ base64_file_content = False ,
519
+ temp_file_path = self ._temp_file_path ).as_data ()
511
520
if token :
512
521
self .token = "Bearer %s" % token
513
522
return True
@@ -533,17 +542,20 @@ def _load_cluster_info(self):
533
542
base_path = self ._get_base_path (self ._cluster .path )
534
543
self .ssl_ca_cert = FileOrData (
535
544
self ._cluster , 'certificate-authority' ,
536
- file_base_path = base_path ).as_file ()
545
+ file_base_path = base_path ,
546
+ temp_file_path = self ._temp_file_path ).as_file ()
537
547
if 'cert_file' not in self .__dict__ :
538
548
# cert_file could have been provided by
539
549
# _load_from_exec_plugin; only load from the _user
540
550
# section if we need it.
541
551
self .cert_file = FileOrData (
542
552
self ._user , 'client-certificate' ,
543
- file_base_path = base_path ).as_file ()
553
+ file_base_path = base_path ,
554
+ temp_file_path = self ._temp_file_path ).as_file ()
544
555
self .key_file = FileOrData (
545
556
self ._user , 'client-key' ,
546
- file_base_path = base_path ).as_file ()
557
+ file_base_path = base_path ,
558
+ temp_file_path = self ._temp_file_path ).as_file ()
547
559
if 'insecure-skip-tls-verify' in self ._cluster :
548
560
self .verify_ssl = not self ._cluster ['insecure-skip-tls-verify' ]
549
561
@@ -811,7 +823,8 @@ def load_kube_config(config_file=None, context=None,
811
823
812
824
def load_kube_config_from_dict (config_dict , context = None ,
813
825
client_configuration = None ,
814
- persist_config = True ):
826
+ persist_config = True ,
827
+ temp_file_path = None ):
815
828
"""Loads authentication and cluster information from config_dict file
816
829
and stores them in kubernetes.client.configuration.
817
830
@@ -822,16 +835,17 @@ def load_kube_config_from_dict(config_dict, context=None,
822
835
set configs to.
823
836
:param persist_config: If True, config file will be updated when changed
824
837
(e.g GCP token refresh).
838
+ :param temp_file_path: store temp files path.
825
839
"""
826
-
827
840
if config_dict is None :
828
841
raise ConfigException (
829
842
'Invalid kube-config dict. '
830
843
'No configuration found.' )
831
844
832
845
loader = _get_kube_config_loader (
833
846
config_dict = config_dict , active_context = context ,
834
- persist_config = persist_config )
847
+ persist_config = persist_config ,
848
+ temp_file_path = temp_file_path )
835
849
836
850
if client_configuration is None :
837
851
config = type .__call__ (Configuration )
0 commit comments