def build_k8s_config(config_path): cluster_path = os.path.join(config_path, "cluster.yaml") if not os.path.isfile(cluster_path): cluster_path = os.path.join(config_path, STATUS_YAML) with open(cluster_path) as f: cluster_config = yaml.full_load(f) config = Configuration() infra_host = find_infra_node_name(cluster_config["machines"]) if os.path.isfile(cluster_path): config.host = "https://%s.%s:1443" % ( infra_host, cluster_config["network"]["domain"]) basic_auth = cluster_config["basic_auth"] else: config.host = cluster_config["machines"][infra_host]["fqdns"] with open(os.path.join(config_path, "clusterID", "k8s_basic_auth.yml")) as auf: basic_auth = yaml.safe_load(auf)["basic_auth"] config.username = basic_auth.split(",")[1] config.password = basic_auth.split(",")[0] bearer = "%s:%s" % (config.username, config.password) encoded = base64.b64encode(bearer.encode("utf-8")).decode("utf-8") config.api_key["authorization"] = "Basic " + encoded config.ssl_ca_cert = os.path.join(config_path, "ssl/apiserver/ca.pem") return config
def init_k8s_client(kube_context: str) -> client.CoreV1Api: env = os.environ if kube_context: # config.load_kube_config() config.load_kube_config(kube_context) configuration = Configuration() configuration.verify_ssl = bool( strtobool((env.get("KUBERNETES_VERIFY_SSL", "false")))) api_client = client.ApiClient(configuration) return client.CoreV1Api(api_client) elif env.get("CHAOSTOOLKIT_IN_POD") == "true": config.load_incluster_config() proxy_url = os.getenv('HTTP_PROXY', None) if proxy_url: configuration = Configuration() configuration.proxy = proxy_url api_client = client.ApiClient(configuration) return client.CoreV1Api(api_client) else: api = client.ApiClient() return client.CoreV1Api(api) else: configuration = client.Configuration() configuration.debug = True configuration.host = os.environ.get("KUBERNETES_HOST", "http://localhost") configuration.verify_ssl = bool( strtobool((env.get("KUBERNETES_VERIFY_SSL", "false")))) configuration.cert_file = os.environ.get("KUBERNETES_CA_CERT_FILE") if "KUBERNETES_CERT_FILE" in env: configuration.cert_file = os.environ.get( "KUBERNETES_CERT_FILE") configuration.key_file = os.environ.get("KUBERNETES_KEY_FILE") elif "KUBERNETES_USERNAME" in env: configuration.username = os.environ.get("KUBERNETES_USERNAME") configuration.password = os.environ.get( "KUBERNETES_PASSWORD", "") proxy_url = os.getenv('HTTP_PROXY', None) if proxy_url: configuration.proxy = proxy_url api = client.ApiClient(configuration) return client.CoreV1Api(api)