@@ -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 temp_file_path and not os .path .isdir (temp_file_path ):
72
+ os .makedirs (name = temp_file_path )
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,10 @@ 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 (
126
+ self ._data , self ._temp_file_path )
122
127
if self ._file and not os .path .isfile (self ._file ):
123
128
raise ConfigException ("File does not exist: %s" % self ._file )
124
129
return self ._file
@@ -182,7 +187,8 @@ class KubeConfigLoader(object):
182
187
def __init__ (self , config_dict , active_context = None ,
183
188
get_google_credentials = None ,
184
189
config_base_path = "" ,
185
- config_persister = None ):
190
+ config_persister = None ,
191
+ temp_file_path = None ):
186
192
187
193
if config_dict is None :
188
194
raise ConfigException (
@@ -199,6 +205,7 @@ def __init__(self, config_dict, active_context=None,
199
205
self .set_active_context (active_context )
200
206
self ._config_base_path = config_base_path
201
207
self ._config_persister = config_persister
208
+ self ._temp_file_path = temp_file_path
202
209
203
210
def _refresh_credentials_with_cmd_path ():
204
211
config = self ._user ['auth-provider' ]['config' ]
@@ -489,12 +496,14 @@ def _load_from_exec_plugin(self):
489
496
status , None ,
490
497
data_key_name = 'clientCertificateData' ,
491
498
file_base_path = base_path ,
492
- base64_file_content = False ).as_file ()
499
+ base64_file_content = False ,
500
+ temp_file_path = self ._temp_file_path ).as_file ()
493
501
self .key_file = FileOrData (
494
502
status , None ,
495
503
data_key_name = 'clientKeyData' ,
496
504
file_base_path = base_path ,
497
- base64_file_content = False ).as_file ()
505
+ base64_file_content = False ,
506
+ temp_file_path = self ._temp_file_path ).as_file ()
498
507
return True
499
508
logging .error ('exec: missing token or clientCertificateData field '
500
509
'in plugin output' )
@@ -507,7 +516,8 @@ def _load_user_token(self):
507
516
token = FileOrData (
508
517
self ._user , 'tokenFile' , 'token' ,
509
518
file_base_path = base_path ,
510
- base64_file_content = False ).as_data ()
519
+ base64_file_content = False ,
520
+ temp_file_path = self ._temp_file_path ).as_data ()
511
521
if token :
512
522
self .token = "Bearer %s" % token
513
523
return True
@@ -533,17 +543,20 @@ def _load_cluster_info(self):
533
543
base_path = self ._get_base_path (self ._cluster .path )
534
544
self .ssl_ca_cert = FileOrData (
535
545
self ._cluster , 'certificate-authority' ,
536
- file_base_path = base_path ).as_file ()
546
+ file_base_path = base_path ,
547
+ temp_file_path = self ._temp_file_path ).as_file ()
537
548
if 'cert_file' not in self .__dict__ :
538
549
# cert_file could have been provided by
539
550
# _load_from_exec_plugin; only load from the _user
540
551
# section if we need it.
541
552
self .cert_file = FileOrData (
542
553
self ._user , 'client-certificate' ,
543
- file_base_path = base_path ).as_file ()
554
+ file_base_path = base_path ,
555
+ temp_file_path = self ._temp_file_path ).as_file ()
544
556
self .key_file = FileOrData (
545
557
self ._user , 'client-key' ,
546
- file_base_path = base_path ).as_file ()
558
+ file_base_path = base_path ,
559
+ temp_file_path = self ._temp_file_path ).as_file ()
547
560
if 'insecure-skip-tls-verify' in self ._cluster :
548
561
self .verify_ssl = not self ._cluster ['insecure-skip-tls-verify' ]
549
562
@@ -811,7 +824,8 @@ def load_kube_config(config_file=None, context=None,
811
824
812
825
def load_kube_config_from_dict (config_dict , context = None ,
813
826
client_configuration = None ,
814
- persist_config = True ):
827
+ persist_config = True ,
828
+ temp_file_path = None ):
815
829
"""Loads authentication and cluster information from config_dict file
816
830
and stores them in kubernetes.client.configuration.
817
831
@@ -822,16 +836,17 @@ def load_kube_config_from_dict(config_dict, context=None,
822
836
set configs to.
823
837
:param persist_config: If True, config file will be updated when changed
824
838
(e.g GCP token refresh).
839
+ :param temp_file_path: store temp files path.
825
840
"""
826
-
827
841
if config_dict is None :
828
842
raise ConfigException (
829
843
'Invalid kube-config dict. '
830
844
'No configuration found.' )
831
845
832
846
loader = _get_kube_config_loader (
833
847
config_dict = config_dict , active_context = context ,
834
- persist_config = persist_config )
848
+ persist_config = persist_config ,
849
+ temp_file_path = temp_file_path )
835
850
836
851
if client_configuration is None :
837
852
config = type .__call__ (Configuration )
0 commit comments