Skip to content

Commit

Permalink
Support HTTPS_PROXY (or HTTP_PROXY) env vars for specifying a pro…
Browse files Browse the repository at this point in the history
…xy to route traffic through.

Note that this will only work with a TLS passthrough proxy anyway; since the TLS connections will fail cert validation otherwise.
  • Loading branch information
chadlwilson committed Oct 27, 2020
1 parent 5fe271d commit 25eb000
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions eksrollup/lib/k8s.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import os
import subprocess
import time
import sys
Expand All @@ -16,6 +17,11 @@ def ensure_config_loaded():
except config.ConfigException:
raise Exception("Could not configure kubernetes python client")

proxy_url = os.getenv('HTTPS_PROXY', os.getenv('HTTP_PROXY', None))
if proxy_url:
logger.info(f"Setting proxy: {proxy_url}")
client.Configuration._default.proxy = proxy_url


def get_k8s_nodes(exclude_node_label_keys=app_config["EXCLUDE_NODE_LABEL_KEYS"]):
"""
Expand Down
2 changes: 0 additions & 2 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ moto==1.3.13
python-box~=3.4.0
flake8~=3.7.7
nose2~=0.9.1
kubernetes~=10.0.1
python-dotenv~=0.10.2
twine
16 changes: 16 additions & 0 deletions tests/fixtures/example-kube-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Config
current-context: example-context
clusters:
- cluster:
server: https://localhost:8080
name: local-cluster
contexts:
- context:
cluster: local-cluster
namespace: default
user: user
name: example-context
users:
- name: user
user: {}
21 changes: 19 additions & 2 deletions tests/test_k8s.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import unittest
import json
from eksrollup.lib.k8s import k8s_nodes_count, k8s_nodes_ready, get_node_by_instance_id
from eksrollup.lib.k8s import k8s_nodes_count, k8s_nodes_ready, get_node_by_instance_id, ensure_config_loaded
from unittest.mock import patch
from box import Box

from kubernetes.client import ApiClient
from kubernetes.config import kube_config

class TestK8S(unittest.TestCase):

Expand All @@ -17,6 +18,22 @@ def setUp(self):
with open(f"{current_dir}/fixtures/k8s_response_unhealthy.json", "r") as file:
self.k8s_response_mock_unhealthy = json.load(file)

kube_config.KUBE_CONFIG_DEFAULT_LOCATION = f"{current_dir}/fixtures/example-kube-config.yaml"

def test_ensure_config_loaded_proxy_default(self):
ensure_config_loaded()
self.assertEqual(None, ApiClient().configuration.proxy)

def test_ensure_config_loaded_proxy_prefers_https(self):
with patch.dict(os.environ, {'HTTPS_PROXY': 'http://localhost:12345', 'HTTP_PROXY': 'http://localhost:6789'}):
ensure_config_loaded()
self.assertEqual('http://localhost:12345', ApiClient().configuration.proxy)

def test_ensure_config_loaded_proxy_fallback_to_http(self):
with patch.dict(os.environ, {'HTTP_PROXY': 'http://localhost:12345'}):
ensure_config_loaded()
self.assertEqual('http://localhost:12345', ApiClient().configuration.proxy)

def test_k8s_node_count(self):
with patch('eksrollup.lib.k8s.get_k8s_nodes') as get_k8s_nodes_mock:
get_k8s_nodes_mock.return_value = self.k8s_response_mock['items']
Expand Down

0 comments on commit 25eb000

Please sign in to comment.