Python kubernetes.config.load_incluster_config() Examples
The following are 30
code examples of kubernetes.config.load_incluster_config().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
kubernetes.config
, or try the search function
.
Example #1
Source File: k8s.py From eks-rolling-update with Apache License 2.0 | 8 votes |
def get_k8s_nodes(exclude_node_label_key=app_config["EXCLUDE_NODE_LABEL_KEY"]): """ Returns a list of kubernetes nodes """ try: config.load_incluster_config() except config.ConfigException: try: config.load_kube_config() except config.ConfigException: raise Exception("Could not configure kubernetes python client") k8s_api = client.CoreV1Api() logger.info("Getting k8s nodes...") response = k8s_api.list_node() if exclude_node_label_key is not None: nodes = [] for node in response.items: if exclude_node_label_key not in node.metadata.labels: nodes.append(node) response.items = nodes logger.info("Current k8s node count is {}".format(len(response.items))) return response.items
Example #2
Source File: _k8s_job_helper.py From pipelines with Apache License 2.0 | 7 votes |
def _configure_k8s(self): k8s_config_file = os.environ.get('KUBECONFIG') if k8s_config_file: try: logging.info('Loading kubernetes config from the file %s', k8s_config_file) config.load_kube_config(config_file=k8s_config_file) except Exception as e: raise RuntimeError('Can not load kube config from the file %s, error: %s', k8s_config_file, e) else: try: config.load_incluster_config() logging.info('Initialized with in-cluster config.') except: logging.info('Cannot find in-cluster config, trying the local kubernetes config. ') try: config.load_kube_config() logging.info('Found local kubernetes config. Initialized with kube_config.') except: raise RuntimeError('Forgot to run the gcloud command? Check out the link: \ https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl for more information') self._api_client = k8s_client.ApiClient() self._corev1 = k8s_client.CoreV1Api(self._api_client) return True
Example #3
Source File: k8s.py From eks-rolling-update with Apache License 2.0 | 7 votes |
def delete_node(node_name): """ Deletes a kubernetes node from the cluster """ try: config.load_incluster_config() except config.ConfigException: try: config.load_kube_config() except config.ConfigException: raise Exception("Could not configure kubernetes python client") configuration = client.Configuration() # create an instance of the API class k8s_api = client.CoreV1Api(client.ApiClient(configuration)) logger.info("Deleting k8s node {}...".format(node_name)) try: if not app_config['DRY_RUN']: k8s_api.delete_node(node_name) else: k8s_api.delete_node(node_name, dry_run="true") logger.info("Node deleted") except ApiException as e: logger.info("Exception when calling CoreV1Api->delete_node: {}".format(e))
Example #4
Source File: reflector.py From kubespawner with BSD 3-Clause "New" or "Revised" License | 7 votes |
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Load kubernetes config here, since this is a Singleton and # so this __init__ will be run way before anything else gets run. try: config.load_incluster_config() except config.ConfigException: config.load_kube_config() self.api = shared_client(self.api_group_name) # FIXME: Protect against malicious labels? self.label_selector = ','.join(['{}={}'.format(k, v) for k, v in self.labels.items()]) self.field_selector = ','.join(['{}={}'.format(k, v) for k, v in self.fields.items()]) self.first_load_future = Future() self._stop_event = threading.Event() self.start()
Example #5
Source File: kubernetes.py From airflow with Apache License 2.0 | 7 votes |
def get_conn(self): """ Returns kubernetes api session for use with requests """ connection = self.get_connection(self.conn_id) extras = connection.extra_dejson if extras.get("extra__kubernetes__in_cluster"): self.log.debug("loading kube_config from: in_cluster configuration") config.load_incluster_config() elif extras.get("extra__kubernetes__kube_config") is None: self.log.debug("loading kube_config from: default file") config.load_kube_config() else: with tempfile.NamedTemporaryFile() as temp_config: self.log.debug("loading kube_config from: connection kube_config") temp_config.write(extras.get("extra__kubernetes__kube_config").encode()) temp_config.flush() config.load_kube_config(temp_config.name) return client.ApiClient()
Example #6
Source File: manager.py From polyaxon with Apache License 2.0 | 7 votes |
def __init__(self, k8s_config=None, namespace="default", in_cluster=False): if not k8s_config: if in_cluster: config.load_incluster_config() else: config.load_kube_config() self.api_client = None else: self.api_client = client.api_client.ApiClient(configuration=k8s_config) self._k8s_api = None self._k8s_batch_api = None self._k8s_apps_api = None self._k8s_beta_api = None self._networking_v1_beta1_api = None self._k8s_custom_object_api = None self._k8s_version_api = None self.namespace = namespace self.in_cluster = in_cluster
Example #7
Source File: __init__.py From insights-core with Apache License 2.0 | 6 votes |
def __init__(self, ctx=None, cfg=None): cfg = cfg or os.environ.get("KUBECONFIG") if cfg: k8s_client = config.new_client_from_config(cfg) else: config.load_incluster_config() # makes a singleton config behind the scenes k8cfg = Configuration() # gets a copy from what was populated in the line above # NOTE this is required due to https://github.com/openshift/origin/issues/22125 k8cfg.verify_ssl = False k8s_client = ApiClient(configuration=k8cfg) # this should use the singleton produced above self.k8s = DynamicClient(k8s_client) # stole this from config.new_client_from_config
Example #8
Source File: core.py From mars with Apache License 2.0 | 6 votes |
def __init__(self, k8s_config=None, k8s_namespace=None, label_selector=None): from kubernetes import config, client from gevent.threadpool import ThreadPool if k8s_config is not None: self._k8s_config = k8s_config elif os.environ.get('KUBE_API_ADDRESS'): self._k8s_config = client.Configuration() self._k8s_config.host = os.environ['KUBE_API_ADDRESS'] else: self._k8s_config = config.load_incluster_config() self._k8s_namespace = k8s_namespace or os.environ.get('MARS_K8S_POD_NAMESPACE') or 'default' self._label_selector = label_selector self._client = client.CoreV1Api(client.ApiClient(self._k8s_config)) self._pool = ThreadPool(1) self._pod_to_ep = None
Example #9
Source File: _kfp_execution_context.py From pipelines with Apache License 2.0 | 6 votes |
def _load_kfp_environment(self): self._pod_name = os.environ.get(KFP_POD_ENV_NAME, None) self._namespace = os.environ.get(KFP_NAMESPACE_ENV_NAME, DEFAULT_NAMESPACE) if not self._pod_name: self._k8s_client = None else: try: config.load_incluster_config() self._k8s_client = client.CoreV1Api() except Exception as e: logging.warning('Failed to load kubernetes client:' ' {}.'.format(e)) self._k8s_client = None if self._pod_name and self._k8s_client: self._argo_node_name = self._get_argo_node_name() if not self.under_kfp_environment(): logging.warning('Running without KFP context.')
Example #10
Source File: kubernetes_resources.py From inference-model-manager with Apache License 2.0 | 6 votes |
def get_k8s_configuration(): try: configuration = config.load_kube_config() except Exception: configuration = config.load_incluster_config() return configuration
Example #11
Source File: k8s.py From armada with Apache License 2.0 | 6 votes |
def __init__(self): ''' Initialize connection to Kubernetes ''' try: config.load_incluster_config() except config.config_exception.ConfigException: config.load_kube_config() self.client = client.CoreV1Api() self.batch_api = client.BatchV1Api() self.batch_v1beta1_api = client.BatchV1beta1Api() self.extension_api = client.ExtensionsV1beta1Api()
Example #12
Source File: kubernetes_orchestrator.py From coach with Apache License 2.0 | 6 votes |
def __init__(self, params: KubernetesParameters): """ :param params: The Kubernetes parameters which are used for deploying the components in Coach. These parameters include namespace and kubeconfig. """ super().__init__(params) self.params = params if self.params.kubeconfig: k8sconfig.load_kube_config() else: k8sconfig.load_incluster_config() if not self.params.namespace: _, current_context = k8sconfig.list_kube_config_contexts() self.params.namespace = current_context['context']['namespace'] if os.environ.get('http_proxy'): k8sclient.Configuration._default.proxy = os.environ.get('http_proxy') self.params.memory_backend_parameters.orchestrator_params = {'namespace': self.params.namespace} self.memory_backend = get_memory_backend(self.params.memory_backend_parameters) self.params.data_store_params.orchestrator_params = {'namespace': self.params.namespace} self.params.data_store_params.namespace = self.params.namespace self.data_store = get_data_store(self.params.data_store_params) if self.params.data_store_params.store_type == "s3": self.s3_access_key = None self.s3_secret_key = None if self.params.data_store_params.creds_file: s3config = ConfigParser() s3config.read(self.params.data_store_params.creds_file) try: self.s3_access_key = s3config.get('default', 'aws_access_key_id') self.s3_secret_key = s3config.get('default', 'aws_secret_access_key') except Error as e: print("Error when reading S3 credentials file: %s", e) else: self.s3_access_key = os.environ.get('ACCESS_KEY_ID') self.s3_secret_key = os.environ.get('SECRET_ACCESS_KEY')
Example #13
Source File: k8s.py From eks-rolling-update with Apache License 2.0 | 6 votes |
def modify_k8s_autoscaler(action): """ Pauses or resumes the Kubernetes autoscaler """ try: config.load_incluster_config() except config.ConfigException: try: config.load_kube_config() except config.ConfigException: raise Exception("Could not configure kubernetes python client") # Configure API key authorization: BearerToken configuration = client.Configuration() # create an instance of the API class k8s_api = client.AppsV1Api(client.ApiClient(configuration)) if action == 'pause': logger.info('Pausing k8s autoscaler...') body = {'spec': {'replicas': 0}} elif action == 'resume': logger.info('Resuming k8s autoscaler...') body = {'spec': {'replicas': app_config['K8S_AUTOSCALER_REPLICAS']}} else: logger.info('Invalid k8s autoscaler option') sys.exit(1) try: k8s_api.patch_namespaced_deployment( app_config['K8S_AUTOSCALER_DEPLOYMENT'], app_config['K8S_AUTOSCALER_NAMESPACE'], body ) logger.info('K8s autoscaler modified to replicas: {}'.format(body['spec']['replicas'])) except ApiException as e: logger.info('Scaling of k8s autoscaler failed. Error code was {}, {}. Exiting.'.format(e.reason, e.body)) sys.exit(1)
Example #14
Source File: kube_client.py From airflow with Apache License 2.0 | 6 votes |
def _get_kube_config(in_cluster: bool, cluster_context: Optional[str], config_file: Optional[str]) -> Optional[Configuration]: if in_cluster: # load_incluster_config set default configuration with config populated by k8s config.load_incluster_config() return None else: # this block can be replaced with just config.load_kube_config once # refresh_config module is replaced with upstream fix cfg = RefreshConfiguration() load_kube_config( client_configuration=cfg, config_file=config_file, context=cluster_context) return cfg
Example #15
Source File: k8s.py From armada with Apache License 2.0 | 6 votes |
def __init__(self, bearer_token=None): ''' Initialize connection to Kubernetes ''' self.bearer_token = bearer_token api_client = None try: config.load_incluster_config() except config.config_exception.ConfigException: config.load_kube_config() if self.bearer_token: # Configure API key authorization: Bearer Token configuration = client.Configuration() configuration.api_key_prefix['authorization'] = 'Bearer' configuration.api_key['authorization'] = self.bearer_token api_client = client.ApiClient(configuration) self.client = client.CoreV1Api(api_client) self.batch_api = client.BatchV1Api(api_client) self.batch_v1beta1_api = client.BatchV1beta1Api(api_client) self.custom_objects = client.CustomObjectsApi(api_client) self.api_extensions = client.ApiextensionsV1beta1Api(api_client) self.extension_api = client.ExtensionsV1beta1Api(api_client) self.apps_v1_api = client.AppsV1Api(api_client)
Example #16
Source File: kube_client_1_10_2.py From telemetry-airflow with Mozilla Public License 2.0 | 5 votes |
def _load_kube_config(in_cluster, cluster_context, config_file): if not has_kubernetes: raise _import_err if in_cluster: config.load_incluster_config() else: config.load_kube_config(config_file=config_file, context=cluster_context) if PY2: # For connect_get_namespaced_pod_exec from kubernetes.client import Configuration configuration = Configuration() configuration.assert_hostname = False Configuration.set_default(configuration) return client.CoreV1Api()
Example #17
Source File: k8s.py From CPU-Manager-for-Kubernetes with Apache License 2.0 | 5 votes |
def extensions_client_from_config(config): if config is None: k8sconfig.load_incluster_config() return k8sclient.ExtensionsV1beta1Api() else: client = k8sclient.ApiClient(configuration=config) return k8sclient.ExtensionsV1beta1Api(api_client=client)
Example #18
Source File: sentry-kubernetes.py From sentry-kubernetes with Apache License 2.0 | 5 votes |
def main(): parser = argparse.ArgumentParser() parser.add_argument("--log-level", default=LOG_LEVEL) args = parser.parse_args() log_level = args.log_level.upper() logging.basicConfig(format="%(asctime)s %(message)s", level=log_level) logging.debug("log_level: %s" % log_level) try: config.load_incluster_config() except: config.load_kube_config() while True: try: watch_loop() except ApiException as e: logging.error( "Exception when calling CoreV1Api->list_event_for_all_namespaces: %s\n" % e ) time.sleep(5) except ProtocolError: logging.warning("ProtocolError exception. Continuing...") except Exception as e: logging.exception("Unhandled exception occurred.")
Example #19
Source File: k8s_preflight_checks_operator.py From shipyard with Apache License 2.0 | 5 votes |
def execute(self, context): logging.info("Running Basic Kubernetes Cluster Health Check:") # Note that we are using 'in_cluster_config' config.load_incluster_config() v1 = client.CoreV1Api() ret = v1.list_pod_for_all_namespaces(watch=False) # Loop through items to get the status of all pods # Note that the current health check only checks to # ensure that all pods are either in 'Succeeded' or # in 'Running' state. The k8s health checks can be # expanded in future if need be. for i in ret.items: logging.info("%s is in %s state", i.metadata.name, i.status.phase) if i.status.phase not in ['Succeeded', 'Running']: # NOTE: Kubelet receives information about the pods # and node from etcd after a restart. It seems that # it is possible for kubelet to set the pod status to # 'MatchNodeSelector' after a hard reboot of the node. # This might happen if the labels in the initial node # info is different from the node info in etcd, which # will in turn cause the pod admission to fail. # # As the system does recover after a hard reboot with # new pods created for various services, there is a need # to ignore the failed pods with 'MatchNodeSelector' status # to avoid false alarms. Hence the approach that we will # be taking in such situation will be to log warning messages # printing the current state of these pods as opposed to # failing the health checks. if (i.status.phase == 'Failed' and i.status.container_statuses is None and i.status.reason == 'MatchNodeSelector'): logging.warning("%s is in %s state with status", i.metadata.name, i.status.phase) logging.warning(i.status) else: raise AirflowException("Kubernetes Health Checks Failed!")
Example #20
Source File: app.py From pipelines with Apache License 2.0 | 5 votes |
def load_kube_config(params): # from six import PY3 # PY3 = sys.version_info.major == 3 # # # apply monkey-patch for kubernetes client OIDC authentication issue 525 ("binascii.Error: Incorrect padding") # # before importing client and config from kubernetes # if PY3: # apply_oid_token_monkey_patch() from kubernetes import config # kube_config_file = "kube/%s/kube-config.yml" % params["public_ip"] config.load_incluster_config()
Example #21
Source File: kube_utils.py From tfx with Apache License 2.0 | 5 votes |
def _LoadConfig(self) -> None: # pylint: disable=invalid-name """Load the kubernetes client config. Depending on the environment (whether it is inside the running kubernetes cluster or remote host), different location will be searched for the config file. The loaded config will be used as a default value for the clients this factory is creating. If config is already loaded, it is a no-op. Raises: kubernetes.config.ConfigException: If fails to locate configuration in current environment. """ try: # If this code is running inside Kubernetes Pod, service account admission # controller [1] sets volume mounts in which the service account tokens # and certificates exists, and it can be loaded using # `load_incluster_config()`. # # [1] # https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#service-account-admission-controller self._inside_cluster = True k8s_config.load_incluster_config() except k8s_config.ConfigException: # If loading incluster config fails, it means we're not running the code # inside Kubernetes cluster. We try to load ~/.kube/config file, or the # filename from the KUBECONFIG environment variable. # It will raise kubernetes.config.ConfigException if no kube config file # is found. self._inside_cluster = False k8s_config.load_kube_config() self._config_loaded = True
Example #22
Source File: kube_namespace.py From wc-devops-utilities with Apache License 2.0 | 5 votes |
def NS_list(parser_args): if (parser_args.mode == "inCluster") : config.load_incluster_config() elif (parser_args.mode == "outCluster"): config.load_kube_config() else: print("error mode configuration") sys.exit(1) v1 = client.CoreV1Api() namespace_list = v1.list_namespace() for i in namespace_list.items: print(i.metadata.name + ",")
Example #23
Source File: kube_factory.py From wc-devops-utilities with Apache License 2.0 | 5 votes |
def Factory_InitKubeClient(configtype=None,configfile=None,context=None,apiversion=None): if configtype == "InCluster" : config.load_incluster_config() elif configtype == "OutCluster": if configfile == None: configfile = dirname(os.path.realpath(__file__)) + os.sep + "kube_config.yml" config.load_kube_config(config_file=configfile,context=context) else: print("Input cofigtype not support!") exit(1) CoreV1Api = client.CoreV1Api() #Store CoreV1Api Client in global module globalvars.set_value('KubCoreV1Api', CoreV1Api)
Example #24
Source File: installRunner.py From ks-installer with Apache License 2.0 | 5 votes |
def generateConfig(): config.load_incluster_config() api = client.CustomObjectsApi() resource = api.get_namespaced_custom_object( group="installer.kubesphere.io", version="v1alpha1", name="ks-installer", namespace="kubesphere-system", plural="clusterconfigurations", ) try: with open(configFile, 'w', encoding='utf-8') as f: json.dump(resource['spec'], f, ensure_ascii=False, indent=4) except: with open(configFile, 'w', encoding='utf-8') as f: json.dump({"config": "new"}, f, ensure_ascii=False, indent=4) try: with open(statusFile, 'w', encoding='utf-8') as f: json.dump({"status": resource['status']}, f, ensure_ascii=False, indent=4) except: with open(statusFile, 'w', encoding='utf-8') as f: json.dump({"status": {"enabledComponents": []}}, f, ensure_ascii=False, indent=4) # cmdGetConfig = r"kubectl get cm -n kubesphere-system ks-installer -o jsonpath='{.data}' | grep -v '\[\|\]' > /kubesphere/config/ks-config.yaml" # os.system(cmdGetConfig)
Example #25
Source File: check_k8s_node_status.py From shipyard with Apache License 2.0 | 5 votes |
def _get_all_k8s_node_status(): """Invoke Kubernetes and return the status response object""" # Note that we are using 'in_cluster_config' try: config.load_incluster_config() v1 = client.CoreV1Api() return v1.list_node(watch=False) except Exception: # Log some diagnostics and return None. logging.warning("There was an error retrieving the cluster status", exc_info=True)
Example #26
Source File: main.py From acs-keyvault-agent with MIT License | 5 votes |
def _get_kubernetes_api_instance(self): if self._api_instance is None: config.load_incluster_config() client.configuration.assert_hostname = False self._api_instance = client.CoreV1Api() return self._api_instance
Example #27
Source File: pelorus.py From pelorus with Apache License 2.0 | 5 votes |
def load_kube_config(): if "OPENSHIFT_BUILD_NAME" in os.environ: config.load_incluster_config() file_namespace = open( "/run/secrets/kubernetes.io/serviceaccount/namespace", "r" ) if file_namespace.mode == "r": namespace = file_namespace.read() print("namespace: %s\n" % (namespace)) else: config.load_kube_config()
Example #28
Source File: pelorus.py From pelorus with Apache License 2.0 | 5 votes |
def load_kube_config(): if "OPENSHIFT_BUILD_NAME" in os.environ: config.load_incluster_config() file_namespace = open( "/run/secrets/kubernetes.io/serviceaccount/namespace", "r" ) if file_namespace.mode == "r": namespace = file_namespace.read() print("namespace: %s\n" % (namespace)) else: config.load_kube_config()
Example #29
Source File: pelorus.py From pelorus with Apache License 2.0 | 5 votes |
def load_kube_config(): if "OPENSHIFT_BUILD_NAME" in os.environ: config.load_incluster_config() file_namespace = open( "/run/secrets/kubernetes.io/serviceaccount/namespace", "r" ) if file_namespace.mode == "r": namespace = file_namespace.read() print("namespace: %s\n" % (namespace)) else: config.load_kube_config()
Example #30
Source File: pelorus.py From pelorus with Apache License 2.0 | 5 votes |
def load_kube_config(): if "OPENSHIFT_BUILD_NAME" in os.environ: config.load_incluster_config() file_namespace = open( "/run/secrets/kubernetes.io/serviceaccount/namespace", "r" ) if file_namespace.mode == "r": namespace = file_namespace.read() print("namespace: %s\n" % (namespace)) else: config.load_kube_config()