-
Notifications
You must be signed in to change notification settings - Fork 99
Adds support for juju client proxies #492
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from juju.client.proxy.kubernetes.proxy import KubernetesProxy | ||
|
|
||
|
|
||
| def proxy_from_config(conf): | ||
| if conf is None: | ||
| return None | ||
|
|
||
| if 'type' not in conf: | ||
| return None | ||
|
|
||
| proxy_type = conf['type'] | ||
| if proxy_type != 'kubernetes-port-forward': | ||
| raise ValueError('unknown proxy type %s' % proxy_type) | ||
|
|
||
| return _construct_kube_proxy(conf['config']) | ||
|
|
||
|
|
||
| def _construct_kube_proxy(config): | ||
| return KubernetesProxy( | ||
| config.get('api-host', ''), | ||
| config.get('namespace', ''), | ||
| config.get('remote-port', ''), | ||
| config.get('service', ''), | ||
| config.get('service-account-token', ''), | ||
| config.get('ca-cert', None), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import tempfile | ||
|
|
||
| from juju.client.proxy.proxy import Proxy, ProxyNotConnectedError | ||
| from kubernetes import client | ||
| from kubernetes.stream import portforward | ||
|
|
||
|
|
||
| class KubernetesProxy(Proxy): | ||
| def __init__( | ||
| self, | ||
| api_host, | ||
| namespace, | ||
| remote_port, | ||
| service, | ||
| service_account_token, | ||
| ca_cert=None, | ||
| ): | ||
| config = client.Configuration() | ||
| config.host = api_host | ||
| config.ssl_ca_cert = ca_cert | ||
| config.api_key = {"authorization": "Bearer " + service_account_token} | ||
|
|
||
| self.namespace = namespace | ||
| self.remote_port = remote_port | ||
| self.service = service | ||
|
|
||
| try: | ||
| self.remote_port = int(remote_port) | ||
| except ValueError: | ||
| raise ValueError("Invalid port number: {}".format(remote_port)) | ||
|
|
||
| if ca_cert: | ||
| self.temp_ca_file = tempfile.NamedTemporaryFile() | ||
| self.temp_ca_file.write(bytes(ca_cert, 'utf-8')) | ||
| self.temp_ca_file.flush() | ||
| config.ssl_ca_cert = self.temp_ca_file.name | ||
|
tlm marked this conversation as resolved.
|
||
|
|
||
| self.api_client = client.ApiClient(config) | ||
|
|
||
| def connect(self): | ||
| corev1 = client.CoreV1Api(self.api_client) | ||
| service = corev1.read_namespaced_service(self.service, self.namespace) | ||
|
|
||
| label_selector = ','.join(k + '=' + v for k, v in service.spec.selector.items()) | ||
|
|
||
| pods = corev1.list_namespaced_pod( | ||
| namespace=self.namespace, | ||
| label_selector=label_selector, | ||
| ) | ||
|
|
||
| self.port_forwarder = portforward( | ||
| corev1.connect_get_namespaced_pod_portforward, | ||
| pods.items[0].metadata.name, | ||
| self.namespace, | ||
| ports=str(self.remote_port), | ||
| ) | ||
|
|
||
| def __del__(self): | ||
| self.close() | ||
|
|
||
| def close(self): | ||
| try: | ||
| self.port_forwarder.close() | ||
| self.temp_ca_file.close() | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| def socket(self): | ||
| if self.port_forwarder is not None: | ||
| return self.port_forwarder.socket(self.remote_port)._socket | ||
| raise ProxyNotConnectedError() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from abc import abstractmethod | ||
|
|
||
|
|
||
| class ProxyNotConnectedError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| class Proxy(): | ||
| """ | ||
| Abstract class to represent a generic controller connection proxy | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def connect(self): | ||
| raise NotImplementedError() | ||
|
|
||
| @abstractmethod | ||
| def close(self): | ||
| raise NotImplementedError() | ||
|
|
||
| @abstractmethod | ||
| def socket(self): | ||
| raise NotImplementedError() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import unittest | ||
|
|
||
| from juju.client.proxy.factory import proxy_from_config | ||
| from juju.client.proxy.kubernetes.proxy import KubernetesProxy | ||
|
|
||
|
|
||
| class TestJujuDataFactory(unittest.TestCase): | ||
|
|
||
| def test_proxy_from_config_unknown_type(self): | ||
| """ | ||
| Test that a unknown proxy type results in a UnknownProxyTypeError | ||
| exception | ||
| """ | ||
| self.assertRaises(ValueError, proxy_from_config, { | ||
| "config": {}, | ||
| "type": "does-not-exists", | ||
| }) | ||
|
|
||
| def test_proxy_from_config_missing_type(self): | ||
| """ | ||
| Test that a nil proxy type returns None | ||
| """ | ||
| self.assertIsNone(proxy_from_config({ | ||
| "config": {}, | ||
| })) | ||
|
|
||
| def test_proxy_from_config_non_arg(self): | ||
| """ | ||
| Tests that providing an empty proxy config results in a None proxy | ||
| """ | ||
| self.assertIsNone(proxy_from_config(None)) | ||
|
|
||
| def test_proxy_from_config_kubernetes(self): | ||
| """ | ||
| Tests that a Kubernetes proxy is correctly created from config | ||
| """ | ||
| proxy = proxy_from_config({ | ||
| "type": "kubernetes-port-forward", | ||
| "config": { | ||
| "api-host": "https://localhost:8456", | ||
| "namespace": "controller-python-test", | ||
| "remote-port": "1234", | ||
| "service": "controller", | ||
| "service-account-token": "==AA", | ||
| "ca-cert": "==AA", | ||
| }, | ||
| }) | ||
|
|
||
| self.assertIs(type(proxy), KubernetesProxy) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import unittest | ||
| from juju.client.proxy.kubernetes.proxy import KubernetesProxy | ||
|
|
||
|
|
||
| class TestKubernetesProxy(unittest.TestCase): | ||
| def test_remote_port_error(self): | ||
| self.assertRaises( | ||
| ValueError, | ||
| KubernetesProxy, | ||
| api_host="https://localhost:1234", | ||
| namespace="controller", | ||
| remote_port="not-a-integer-port", | ||
| service="service", | ||
| service_account_token="==AA", | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.