11import re
22import os
33import sys
4+ import ssl
45import yaml
56import time
67import logging
78import requests
89import urllib3
10+ from urllib .parse import urlparse
11+ from urllib3 .contrib .socks import SOCKSProxyManager
912from collections import defaultdict
1013from kubernetes import client , config
1114import cerberus .invoke .command as runcommand
@@ -26,27 +29,26 @@ def initialize_clients(kubeconfig_path, chunk_size, timeout):
2629 global kubeconfig_path_global
2730
2831 """Initialize object and create clients from specified kubeconfig"""
29- client_config = client .Configuration ()
32+ config .load_kube_config (kubeconfig_path )
33+
34+ client_config = client .Configuration ().get_default_copy ()
3035 http_proxy = os .getenv ("http_proxy" , None )
31- """Proxy has auth header"""
32- proxy_auth = None
33- if http_proxy :
34- if "@" in http_proxy :
35- proxy_auth = http_proxy .split ("@" )[0 ].split ("//" )[1 ]
36- user_pass = proxy_auth .split (":" )[0 ]
37- client_config .username = user_pass [0 ]
38- client_config .password = user_pass [1 ]
39- client_config .ssl_ca_cert = False
40- client_config .verify_ssl = False
41- config .load_kube_config (config_file = kubeconfig_path , persist_config = True , client_configuration = client_config )
42- proxy_url = http_proxy
43- if proxy_url :
44- client_config .proxy = proxy_url
45- if proxy_auth :
46- client_config .proxy_headers = urllib3 .util .make_headers (proxy_basic_auth = proxy_auth )
36+ api_client = client .ApiClient (client_config )
37+ if http_proxy is not None :
38+ os .environ ["HTTP_PROXY" ] = http_proxy
39+ client_config .proxy = http_proxy
40+ proxy_auth = urlparse (http_proxy )
41+ if proxy_auth .username and proxy_auth .password :
42+ auth_string = proxy_auth .username + ":" + proxy_auth .password
43+ client_config .proxy_headers = urllib3 .util .make_headers (
44+ proxy_basic_auth = auth_string
45+ )
46+ else :
47+ api_client .rest_client .pool_manager = SOCKSProxyManager (proxy_url = http_proxy , cert_reqs = ssl .CERT_NONE , assert_hostname = False )
4748
4849 client .Configuration .set_default (client_config )
49- cli = client .CoreV1Api ()
50+
51+ cli = client .CoreV1Api (api_client )
5052 cmd_timeout = timeout
5153 request_chunk_size = str (chunk_size )
5254 kubeconfig_path_global = kubeconfig_path
@@ -61,7 +63,7 @@ def list_continue_helper(func, *args, **keyword_args):
6163 continue_string = ret .metadata ._continue
6264
6365 while continue_string :
64- ret = func (* args , ** keyword_args , _continue = continue_string )
66+ ret = func (* args , ** keyword_args , _continue = continue_string , _request_timeout = 10 )
6567 ret_overall .append (ret )
6668 continue_string = ret .metadata ._continue
6769
@@ -77,7 +79,7 @@ def list_nodes(label_selector=None):
7779 try :
7880 if label_selector :
7981 ret = list_continue_helper (
80- cli .list_node , pretty = True , label_selector = label_selector , limit = request_chunk_size
82+ cli .list_node , pretty = True , label_selector = label_selector , limit = request_chunk_size , _request_timeout = 10
8183 )
8284 else :
8385 ret = list_continue_helper (cli .list_node , pretty = True , limit = request_chunk_size )
@@ -94,7 +96,7 @@ def list_nodes(label_selector=None):
9496# List all namespaces
9597def list_namespaces ():
9698 namespaces = []
97- ret_overall = list_continue_helper (cli .list_namespace , pretty = True , limit = request_chunk_size )
99+ ret_overall = list_continue_helper (cli .list_namespace , pretty = True , limit = request_chunk_size , _request_timeout = 10 )
98100 for ret_items in ret_overall :
99101 for namespace in ret_items .items :
100102 namespaces .append (namespace .metadata .name )
0 commit comments