Merge pull request #2303 from kubernetes-client/exec_cluster
Support providing cluster info to the exec provider
This commit is contained in:
commit
542d948515
@ -31,7 +31,7 @@ class ExecProvider(object):
|
||||
* caching
|
||||
"""
|
||||
|
||||
def __init__(self, exec_config, cwd):
|
||||
def __init__(self, exec_config, cwd, cluster=None):
|
||||
"""
|
||||
exec_config must be of type ConfigNode because we depend on
|
||||
safe_get(self, key) to correctly handle optional exec provider
|
||||
@ -53,7 +53,10 @@ class ExecProvider(object):
|
||||
value = item['value']
|
||||
additional_vars[name] = value
|
||||
self.env.update(additional_vars)
|
||||
|
||||
if exec_config.safe_get('provideClusterInfo'):
|
||||
self.cluster = cluster
|
||||
else:
|
||||
self.cluster = None
|
||||
self.cwd = cwd or None
|
||||
|
||||
def run(self, previous_response=None):
|
||||
@ -67,6 +70,9 @@ class ExecProvider(object):
|
||||
}
|
||||
if previous_response:
|
||||
kubernetes_exec_info['spec']['response'] = previous_response
|
||||
if self.cluster:
|
||||
kubernetes_exec_info['spec']['cluster'] = self.cluster
|
||||
|
||||
self.env['KUBERNETES_EXEC_INFO'] = json.dumps(kubernetes_exec_info)
|
||||
process = subprocess.Popen(
|
||||
self.args,
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
|
||||
@ -31,6 +32,13 @@ class ExecProviderTest(unittest.TestCase):
|
||||
'apiVersion': 'client.authentication.k8s.io/v1beta1',
|
||||
'env': None
|
||||
})
|
||||
self.input_with_cluster = ConfigNode('test', {
|
||||
'command': 'aws-iam-authenticator',
|
||||
'args': ['token', '-i', 'dummy'],
|
||||
'apiVersion': 'client.authentication.k8s.io/v1beta1',
|
||||
'provideClusterInfo': True,
|
||||
'env': None
|
||||
})
|
||||
self.output_ok = """
|
||||
{
|
||||
"apiVersion": "client.authentication.k8s.io/v1beta1",
|
||||
@ -162,6 +170,19 @@ class ExecProviderTest(unittest.TestCase):
|
||||
self.assertTrue(isinstance(result, dict))
|
||||
self.assertTrue('token' in result)
|
||||
|
||||
@mock.patch('subprocess.Popen')
|
||||
def test_with_cluster_info(self, mock):
|
||||
instance = mock.return_value
|
||||
instance.wait.return_value = 0
|
||||
instance.communicate.return_value = (self.output_ok, '')
|
||||
ep = ExecProvider(self.input_with_cluster, None, {'server': 'name.company.com'})
|
||||
result = ep.run()
|
||||
self.assertTrue(isinstance(result, dict))
|
||||
self.assertTrue('token' in result)
|
||||
|
||||
obj = json.loads(mock.call_args.kwargs['env']['KUBERNETES_EXEC_INFO'])
|
||||
self.assertEqual(obj['spec']['cluster']['server'], 'name.company.com')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@ -487,7 +487,7 @@ class KubeConfigLoader(object):
|
||||
return
|
||||
try:
|
||||
base_path = self._get_base_path(self._cluster.path)
|
||||
status = ExecProvider(self._user['exec'], base_path).run()
|
||||
status = ExecProvider(self._user['exec'], base_path, self._cluster).run()
|
||||
if 'token' in status:
|
||||
self.token = "Bearer %s" % status['token']
|
||||
elif 'clientCertificateData' in status:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user