Skip to content

Support providing cluster info to the exec provider #2303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions kubernetes/base/config/exec_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +53,10 @@ def __init__(self, exec_config, cwd):
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):
Expand All @@ -67,6 +70,9 @@ def run(self, previous_response=None):
}
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,
Expand Down
21 changes: 21 additions & 0 deletions kubernetes/base/config/exec_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import unittest

Expand All @@ -31,6 +32,13 @@ def setUp(self):
'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",
Expand Down Expand Up @@ -162,6 +170,19 @@ def test_ok_no_console_attached(self, mock):
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()
2 changes: 1 addition & 1 deletion kubernetes/base/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def _load_from_exec_plugin(self):
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:
Expand Down
Loading