Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c5bf58

Browse files
committed
Run kubeconfig exec commands in the correct directory. This fixes configs that rely on relative paths.
1 parent f0fa950 commit 1c5bf58

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

config/exec_provider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ExecProvider(object):
3131
* caching
3232
"""
3333

34-
def __init__(self, exec_config):
34+
def __init__(self, exec_config, cwd):
3535
"""
3636
exec_config must be of type ConfigNode because we depend on
3737
safe_get(self, key) to correctly handle optional exec provider
@@ -53,6 +53,7 @@ def __init__(self, exec_config):
5353
value = item['value']
5454
additional_vars[name] = value
5555
self.env.update(additional_vars)
56+
self.cwd = cwd
5657

5758
def run(self, previous_response=None):
5859
kubernetes_exec_info = {
@@ -69,6 +70,7 @@ def run(self, previous_response=None):
6970
self.args,
7071
stdout=subprocess.PIPE,
7172
stderr=subprocess.PIPE,
73+
cwd=self.cwd,
7274
env=self.env,
7375
universal_newlines=True)
7476
(stdout, stderr) = process.communicate()

config/exec_provider_test.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_missing_input_keys(self):
4747
ConfigNode('test3', {'apiVersion': ''})]
4848
for exec_config in exec_configs:
4949
with self.assertRaises(ConfigException) as context:
50-
ExecProvider(exec_config)
50+
ExecProvider(exec_config, None)
5151
self.assertIn('exec: malformed request. missing key',
5252
context.exception.args[0])
5353

@@ -57,7 +57,7 @@ def test_error_code_returned(self, mock):
5757
instance.wait.return_value = 1
5858
instance.communicate.return_value = ('', '')
5959
with self.assertRaises(ConfigException) as context:
60-
ep = ExecProvider(self.input_ok)
60+
ep = ExecProvider(self.input_ok, None)
6161
ep.run()
6262
self.assertIn('exec: process returned %d' %
6363
instance.wait.return_value, context.exception.args[0])
@@ -68,7 +68,7 @@ def test_nonjson_output_returned(self, mock):
6868
instance.wait.return_value = 0
6969
instance.communicate.return_value = ('', '')
7070
with self.assertRaises(ConfigException) as context:
71-
ep = ExecProvider(self.input_ok)
71+
ep = ExecProvider(self.input_ok, None)
7272
ep.run()
7373
self.assertIn('exec: failed to decode process output',
7474
context.exception.args[0])
@@ -102,7 +102,7 @@ def test_missing_output_keys(self, mock):
102102
for output in outputs:
103103
instance.communicate.return_value = (output, '')
104104
with self.assertRaises(ConfigException) as context:
105-
ep = ExecProvider(self.input_ok)
105+
ep = ExecProvider(self.input_ok, None)
106106
ep.run()
107107
self.assertIn('exec: malformed response. missing key',
108108
context.exception.args[0])
@@ -123,7 +123,7 @@ def test_mismatched_api_version(self, mock):
123123
""" % wrong_api_version
124124
instance.communicate.return_value = (output, '')
125125
with self.assertRaises(ConfigException) as context:
126-
ep = ExecProvider(self.input_ok)
126+
ep = ExecProvider(self.input_ok, None)
127127
ep.run()
128128
self.assertIn(
129129
'exec: plugin api version %s does not match' %
@@ -135,11 +135,20 @@ def test_ok_01(self, mock):
135135
instance = mock.return_value
136136
instance.wait.return_value = 0
137137
instance.communicate.return_value = (self.output_ok, '')
138-
ep = ExecProvider(self.input_ok)
138+
ep = ExecProvider(self.input_ok, None)
139139
result = ep.run()
140140
self.assertTrue(isinstance(result, dict))
141141
self.assertTrue('token' in result)
142142

143+
@mock.patch('subprocess.Popen')
144+
def test_run_in_dir(self, mock):
145+
instance = mock.return_value
146+
instance.wait.return_value = 0
147+
instance.communicate.return_value = (self.output_ok, '')
148+
ep = ExecProvider(self.input_ok, '/some/directory')
149+
ep.run()
150+
self.assertEqual(mock.call_args.kwargs['cwd'], '/some/directory')
151+
143152

144153
if __name__ == '__main__':
145154
unittest.main()

config/kube_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ def _load_from_exec_plugin(self):
483483
if 'exec' not in self._user:
484484
return
485485
try:
486-
status = ExecProvider(self._user['exec']).run()
486+
base_path = self._get_base_path(self._cluster.path)
487+
status = ExecProvider(self._user['exec'], base_path).run()
487488
if 'token' in status:
488489
self.token = "Bearer %s" % status['token']
489490
elif 'clientCertificateData' in status:
@@ -493,7 +494,6 @@ def _load_from_exec_plugin(self):
493494
logging.error('exec: missing clientKeyData field in '
494495
'plugin output')
495496
return None
496-
base_path = self._get_base_path(self._cluster.path)
497497
self.cert_file = FileOrData(
498498
status, None,
499499
data_key_name='clientCertificateData',

0 commit comments

Comments
 (0)