Skip to content
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

Adding proxy settings to client setting #349

Closed
wants to merge 3 commits into from
Closed
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
38 changes: 26 additions & 12 deletions kraken/kubernetes/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import re
import sys
import time
import urllib3
import os
from urllib.parse import urlparse

from kubernetes import client, config, utils, watch
from kubernetes.client.rest import ApiException
from kubernetes.dynamic.client import DynamicClient
from kubernetes.stream import stream

from ..kubernetes.resources import (PVC, ChaosEngine, ChaosResult, Container,
Expand All @@ -15,23 +17,39 @@
kraken_node_name = ""


# Load kubeconfig and initialize kubernetes python client
def initialize_clients(kubeconfig_path):
"""Load kubeconfig and initialize kubernetes python client"""
global cli
global batch_cli
global client_config
global watch_resource
global api_client
global dyn_client
global custom_object_client
try:
config.load_kube_config(kubeconfig_path)
# Initialize object and create clients from specified kubeconfig
client_config = client.Configuration()
http_proxy = os.getenv("http_proxy", None)
# Proxy has auth header
if http_proxy and "@" in http_proxy:
proxy_auth = urlparse(http_proxy)
auth_string = proxy_auth.username + ":" + proxy_auth.password
paigerube14 marked this conversation as resolved.
Show resolved Hide resolved
client_config.username = proxy_auth.username
client_config.password = proxy_auth.password

config.load_kube_config(kubeconfig_path, persist_config=True, client_configuration=client_config)

proxy_url = http_proxy
if proxy_url:
client_config.proxy = proxy_url
if proxy_auth:
client_config.proxy_headers = urllib3.util.make_headers(proxy_basic_auth=auth_string)

client.Configuration.set_default(client_config)
cli = client.CoreV1Api()
batch_cli = client.BatchV1Api()
watch_resource = watch.Watch()
api_client = client.ApiClient()
custom_object_client = client.CustomObjectsApi()
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)
except ApiException as e:
logging.error("Failed to initialize kubernetes client: %s\n" % e)
sys.exit(1)
Expand Down Expand Up @@ -647,12 +665,8 @@ def check_if_namespace_exists(name: str) -> bool:
Boolean value indicating whether the namespace exists or not
"""

v1_projects = dyn_client.resources.get(
api_version='project.openshift.io/v1',
kind='Project'
)
project_list = v1_projects.get()
return True if name in str(project_list) else False
namespace_list = list_namespaces()
return True if name in str(namespace_list) else False


def check_if_pod_exists(name: str, namespace: str) -> bool:
Expand Down