Skip to content

Commit 542d948

Browse files
authored
Merge pull request #2303 from kubernetes-client/exec_cluster
Support providing cluster info to the exec provider
2 parents 4da83df + 8c60fe9 commit 542d948

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

kubernetes/base/config/exec_provider.py

Lines changed: 8 additions & 2 deletions
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, cwd):
34+
def __init__(self, exec_config, cwd, cluster=None):
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,7 +53,10 @@ def __init__(self, exec_config, cwd):
5353
value = item['value']
5454
additional_vars[name] = value
5555
self.env.update(additional_vars)
56-
56+
if exec_config.safe_get('provideClusterInfo'):
57+
self.cluster = cluster
58+
else:
59+
self.cluster = None
5760
self.cwd = cwd or None
5861

5962
def run(self, previous_response=None):
@@ -67,6 +70,9 @@ def run(self, previous_response=None):
6770
}
6871
if previous_response:
6972
kubernetes_exec_info['spec']['response'] = previous_response
73+
if self.cluster:
74+
kubernetes_exec_info['spec']['cluster'] = self.cluster
75+
7076
self.env['KUBERNETES_EXEC_INFO'] = json.dumps(kubernetes_exec_info)
7177
process = subprocess.Popen(
7278
self.args,

kubernetes/base/config/exec_provider_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import os
1617
import unittest
1718

@@ -31,6 +32,13 @@ def setUp(self):
3132
'apiVersion': 'client.authentication.k8s.io/v1beta1',
3233
'env': None
3334
})
35+
self.input_with_cluster = ConfigNode('test', {
36+
'command': 'aws-iam-authenticator',
37+
'args': ['token', '-i', 'dummy'],
38+
'apiVersion': 'client.authentication.k8s.io/v1beta1',
39+
'provideClusterInfo': True,
40+
'env': None
41+
})
3442
self.output_ok = """
3543
{
3644
"apiVersion": "client.authentication.k8s.io/v1beta1",
@@ -162,6 +170,19 @@ def test_ok_no_console_attached(self, mock):
162170
self.assertTrue(isinstance(result, dict))
163171
self.assertTrue('token' in result)
164172

173+
@mock.patch('subprocess.Popen')
174+
def test_with_cluster_info(self, mock):
175+
instance = mock.return_value
176+
instance.wait.return_value = 0
177+
instance.communicate.return_value = (self.output_ok, '')
178+
ep = ExecProvider(self.input_with_cluster, None, {'server': 'name.company.com'})
179+
result = ep.run()
180+
self.assertTrue(isinstance(result, dict))
181+
self.assertTrue('token' in result)
182+
183+
obj = json.loads(mock.call_args.kwargs['env']['KUBERNETES_EXEC_INFO'])
184+
self.assertEqual(obj['spec']['cluster']['server'], 'name.company.com')
185+
165186

166187
if __name__ == '__main__':
167188
unittest.main()

kubernetes/base/config/kube_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def _load_from_exec_plugin(self):
487487
return
488488
try:
489489
base_path = self._get_base_path(self._cluster.path)
490-
status = ExecProvider(self._user['exec'], base_path).run()
490+
status = ExecProvider(self._user['exec'], base_path, self._cluster).run()
491491
if 'token' in status:
492492
self.token = "Bearer %s" % status['token']
493493
elif 'clientCertificateData' in status:

0 commit comments

Comments
 (0)