def kubernetes_api_client(rancher_client, cluster_name): c = rancher_client.by_id_cluster(cluster_name) kc = c.generateKubeconfig() loader = KubeConfigLoader(config_dict=yaml.full_load(kc.config)) client_configuration = type.__call__(Configuration) loader.load_and_set(client_configuration) k8s_client = ApiClient(configuration=client_configuration) return k8s_client
def kubernetes_api_client(rancher_client, cluster_name): c = rancher_client.by_id_cluster(cluster_name) kc = c.generateKubeconfig() loader = KubeConfigLoader(config_dict=yaml.load(kc.config)) client_configuration = type.__call__(Configuration) loader.load_and_set(client_configuration) k8s_client = ApiClient(configuration=client_configuration) return k8s_client
def set_kubecfg(self, kubeconfig_dict: dict): not_none(kubeconfig_dict) configuration = kubernetes.client.Configuration() cfg_loader = KubeConfigLoader(dict(kubeconfig_dict)) cfg_loader.load_and_set(configuration) # pylint: disable=no-member kubernetes.client.Configuration.set_default(configuration) # pylint: enable=no-member self.kubeconfig = configuration
def set_kubecfg(self, kubeconfig_dict: typing.Union[dict, model.kubernetes.KubernetesConfig]): not_none(kubeconfig_dict) if isinstance(kubeconfig_dict, model.kubernetes.KubernetesConfig): kubeconfig_dict = kubeconfig_dict.kubeconfig() configuration = kubernetes.client.Configuration() cfg_loader = KubeConfigLoader(dict(kubeconfig_dict)) cfg_loader.load_and_set(configuration) # pylint: disable=no-member kubernetes.client.Configuration.set_default(configuration) # pylint: enable=no-member self.kubeconfig = configuration
def load_kube_config_from_dict(config_dict): """ Loads authentication and cluster information from dict. """ config_loader = KubeConfigLoader(config_dict=config_dict) client_configuration = Configuration() config_loader.load_and_set(client_configuration=client_configuration) Configuration.set_default(client_configuration) return config_loader
def _load_kubeconfig(self, path_to_kubeconfig: str): if not os.path.isfile(path_to_kubeconfig): print(f"No file found at '{path_to_kubeconfig}'") exit(1) with open(path_to_kubeconfig, 'r') as kubeconfig_file: kubeconfig = yaml.safe_load(kubeconfig_file.read()) k8s_config = kubernetes.client.Configuration() cfg_loader = KubeConfigLoader(kubeconfig) cfg_loader.load_and_set(k8s_config) return k8s_config
def load_kubeconf(config_file): with open(config_file, 'r') as f: config = yaml.load(f) user = [user for user in config['users'] if user['name'] == 'aws'][0]['user'] command = [user['exec']['command']] command.extend(user['exec']['args']) output = subprocess.check_output(command) c = json.loads(output.decode('utf-8')) user['token'] = c['status']['token'] del user['exec'] loader = KubeConfigLoader(config) config = type.__call__(Configuration) loader.load_and_set(config) Configuration.set_default(config)
def get_api_client(self): """ Create Kubernetes API client with configuration from string. Returns: str: Kubeconfig file path """ # This super-ugly code configuration is causes by wrong design of config-loading # functions in https://github.com/kubernetes-client/python client_config = type.__call__(client.Configuration) kubeconfig = self.cluster.get_kubeconfig() kcl = KubeConfigLoader(config_dict=kubeconfig, ) kcl.load_and_set(client_config) return client.ApiClient(configuration=client_config)
def k8s_api_client(client, cluster_name, kube_path=None): kube_path = None if kube_path is not None: kube_file = open(kube_path, "r") kube_config = kube_file.read() kube_file.close() else: cluster = client.rancher_api_client().by_id_cluster(cluster_name) kube_config = cluster.generateKubeconfig().config loader = KubeConfigLoader(config_dict=yaml.full_load(kube_config)) client_configuration = type.__call__(Configuration) loader.load_and_set(client_configuration) client_configuration.api_key = {} client_configuration.verify_ssl = False k8s_client = ApiClient(configuration=client_configuration) return CustomObjectsApi(api_client=k8s_client).api_client
def get_api_client(self): """ Create Kubernetes API client with configuration from string. Returns: str: Kubeconfig file path """ # This super-ugly code configuration is causes by wrong design of config-loading # functions in https://github.com/kubernetes-client/python client_config = type.__call__(client.Configuration) kubeconfig = self.cluster.get_kubeconfig() if kubeconfig is None: raise ValueError( "Could not create kubernetes API client: kubeconfig is not found " ) kcl = KubeConfigLoader(config_dict=kubeconfig, ) kcl.load_and_set(client_config) if self.cluster.provisioner.engine == 'kqueen.engines.OpenstackKubesprayEngine': client_config.assert_hostname = False return client.ApiClient(configuration=client_config)
def load_and_set(self, client_configuration): KubeConfigLoader.load_and_set(self, client_configuration) client_configuration.refresh_api_key = self.refresh_api_key
def _read_kube_config(self, config_file_path): with open(config_file_path, "r") as f: loader = KubeConfigLoader(config_dict=yaml.safe_load(f)) config = client.Configuration() loader.load_and_set(config) return config