Skip to content

Commit

Permalink
fix kubernetes api connect issue (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqan213 authored and davideschiera committed Nov 26, 2019
1 parent b4d4993 commit 068e659
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
29 changes: 20 additions & 9 deletions kube_obj_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,33 @@ def parse(self, url, endpoint):
continue

def _kube_get(self, url, endpoint):
headers = {}
k8s_cert_existed = False

if os.path.exists(K8S_BEARER_TOKEN_FILE_NAME) and os.stat(K8S_BEARER_TOKEN_FILE_NAME).st_size > 0:
try:
with open(K8S_BEARER_TOKEN_FILE_NAME, 'r') as tokenfile:
headers = {'Authorization': 'Bearer ' + tokenfile.read() }
except:
Logger.log(sys.exc_info()[1], 'error')
traceback.print_exc()
sys.exit(1)
else:
Logger.log('Connect Kubernetes API server failed: Could not find bearer token at ' + K8S_BEARER_TOKEN_FILE_NAME + '. Exiting.')
sys.exit(1)
if os.path.exists(K8S_CA_CRT_FILE_NAME) and os.stat(K8S_CA_CRT_FILE_NAME).st_size > 0:
k8s_cert_existed = True

if url:
return requests.get(url + endpoint)
if k8s_cert_existed:
return requests.get(url + endpoint, verify = K8S_CA_CRT_FILE_NAME, headers=headers)
else:
kube_service_port = os.getenv('KUBERNETES_SERVICE_PORT_HTTPS')
if kube_service_port is None:
Logger.log('Autodiscover of Kubernetes API server failed:' +
'Could not find env variable KUBERNETES_SERVICE_PORT_HTTPS. Exiting.')
sys.exit(1)
if os.path.exists(K8S_BEARER_TOKEN_FILE_NAME) and os.stat(K8S_BEARER_TOKEN_FILE_NAME).st_size > 0:
with open(K8S_BEARER_TOKEN_FILE_NAME, 'r') as tokenfile:
headers = {'Authorization': 'Bearer ' + tokenfile.read() }
else:
Logger.log('Autodiscover of Kubernetes API server failed: Could not find bearer token at ' +
K8S_BEARER_TOKEN_FILE_NAME + '. Exiting.')
sys.exit(1)
if os.path.exists(K8S_CA_CRT_FILE_NAME) and os.stat(K8S_CA_CRT_FILE_NAME).st_size > 0:
if k8s_cert_existed:
return requests.get('https://' + K8S_DEFAULT_DNS_NAME + ':' + kube_service_port + endpoint,
verify = K8S_CA_CRT_FILE_NAME,
headers=headers)
3 changes: 3 additions & 0 deletions kubewatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import traceback
from sdcclient import SdcClient
from kube_obj_parser import KubeObjParser, KubeURLParser, Logger
# fix the 'InsecureRequestWarning' error
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def log(str, severity='info'):
Logger.log(str, severity)
Expand Down

0 comments on commit 068e659

Please sign in to comment.