Python kubernetes.client() Examples
The following are 30
code examples of kubernetes.client().
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
, or try the search function
.
Example #1
Source File: test_utils.py From rally-openstack with Apache License 2.0 | 6 votes |
def test_create_v1pod_timeout(self, mock__get_k8s_api_client, mock_random_choice, mock_time): k8s_api = mock__get_k8s_api_client.return_value manifest = ( {"apiVersion": "v1", "kind": "Pod", "metadata": {"name": "nginx"}}) k8s_api.create_namespaced_pod.return_value = self.pod mock_time.side_effect = [1, 2, 3, 4, 5, 1800, 1801] not_ready_pod = kubernetes_client.models.V1Pod() not_ready_status = kubernetes_client.models.V1PodStatus() not_ready_status.phase = "not_ready" not_ready_pod_metadata = kubernetes_client.models.V1ObjectMeta() not_ready_pod_metadata.uid = "123456789" not_ready_pod.status = not_ready_status not_ready_pod.metadata = not_ready_pod_metadata k8s_api.read_namespaced_pod = mock.MagicMock( side_effect=[not_ready_pod for i in range(4)]) self.assertRaises( exceptions.TimeoutException, self.scenario._create_v1pod, manifest)
Example #2
Source File: helper.py From cc-utils with Apache License 2.0 | 6 votes |
def delete_pod(self, name: str, namespace: str, grace_period_seconds: int=0): '''Delete a pod in the given namespace. grace_period_seconds: the duration in seconds before the object should be deleted. Value must be non-negative integer. The value zero indicates delete immediately. ''' not_empty(namespace) not_empty(name) body = kubernetes.client.V1DeleteOptions() try: self.core_api.delete_namespaced_pod( name, namespace, body=body, grace_period_seconds=grace_period_seconds ) except ApiException as ae: if ae.status == 404: return None raise ae
Example #3
Source File: clients.py From kubespawner with BSD 3-Clause "New" or "Revised" License | 6 votes |
def shared_client(ClientType, *args, **kwargs): """Return a single shared kubernetes client instance A weak reference to the instance is cached, so that concurrent calls to shared_client will all return the same instance until all references to the client are cleared. """ kwarg_key = tuple((key, kwargs[key]) for key in sorted(kwargs)) cache_key = (ClientType, args, kwarg_key) client = None if cache_key in _client_cache: # resolve cached weakref # client can still be None after this! client = _client_cache[cache_key]() if client is None: Client = getattr(kubernetes.client, ClientType) client = Client(*args, **kwargs) # cache weakref so that clients can be garbage collected _client_cache[cache_key] = weakref.ref(client) return client
Example #4
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 6 votes |
def run(self) -> None: """Performs watching""" kube_client: client.CoreV1Api = get_kube_client() if not self.worker_uuid: raise AirflowException(NOT_STARTED_MESSAGE) while True: try: self.resource_version = self._run(kube_client, self.resource_version, self.worker_uuid, self.kube_config) except ReadTimeoutError: self.log.warning("There was a timeout error accessing the Kube API. " "Retrying request.", exc_info=True) time.sleep(1) except Exception: self.log.exception('Unknown error in KubernetesJobWatcher. Failing') raise else: self.log.warning('Watch died gracefully, starting back up with: ' 'last resource_version: %s', self.resource_version)
Example #5
Source File: helper.py From cc-utils with Apache License 2.0 | 6 votes |
def replace_or_create_ingress(self, namespace: str, ingress: V1beta1Ingress): '''Create an ingress in a given namespace. If the ingress already exists, the previous version will be deleted beforehand. ''' not_empty(namespace) not_none(ingress) ingress_name = ingress.metadata.name existing_ingress = self.get_ingress(namespace=namespace, name=ingress_name) if existing_ingress: self.extensions_v1beta1_api.delete_namespaced_ingress( namespace=namespace, name=ingress_name, body=kubernetes.client.V1DeleteOptions() ) self.create_ingress(namespace=namespace, ingress=ingress)
Example #6
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 6 votes |
def __init__(self, kube_config: Any, task_queue: 'Queue[KubernetesJobType]', result_queue: 'Queue[KubernetesResultsType]', kube_client: client.CoreV1Api, worker_uuid: str): super().__init__() self.log.debug("Creating Kubernetes executor") self.kube_config = kube_config self.task_queue = task_queue self.result_queue = result_queue self.namespace = self.kube_config.kube_namespace self.log.debug("Kubernetes using namespace %s", self.namespace) self.kube_client = kube_client self.launcher = PodLauncher(kube_client=self.kube_client) self.worker_configuration_pod = WorkerConfiguration(kube_config=self.kube_config).as_pod() self._manager = multiprocessing.Manager() self.watcher_queue = self._manager.Queue() self.worker_uuid = worker_uuid self.kube_watcher = self._make_kube_watcher()
Example #7
Source File: helper.py From cc-utils with Apache License 2.0 | 6 votes |
def replace_or_create_deployment(self, namespace: str, deployment: V1Deployment): '''Create a deployment in a given namespace. If the deployment already exists, the previous version will be deleted beforehand. ''' not_empty(namespace) not_none(deployment) deployment_name = deployment.metadata.name existing_deployment = self.get_deployment(namespace=namespace, name=deployment_name) if existing_deployment: self.apps_api.delete_namespaced_deployment( namespace=namespace, name=deployment_name, body=kubernetes.client.V1DeleteOptions() ) self.create_deployment(namespace=namespace, deployment=deployment)
Example #8
Source File: helper.py From cc-utils with Apache License 2.0 | 6 votes |
def replace_or_create_service(self, namespace: str, service: V1Service): '''Create a service in a given namespace. If the service already exists, the previous version will be deleted beforehand ''' not_empty(namespace) not_none(service) service_name = service.metadata.name existing_service = self.get_service(namespace=namespace, name=service_name) if existing_service: delete_options = kubernetes.client.V1DeleteOptions() delete_options.grace_period_seconds = 0 self.core_api.delete_namespaced_service( namespace=namespace, name=service_name, body=delete_options, ) self.create_service(namespace=namespace, service=service)
Example #9
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 6 votes |
def start(self) -> None: """Starts the executor""" self.log.info('Start Kubernetes executor') self.worker_uuid = KubeWorkerIdentifier.get_or_create_current_kube_worker_uuid() if not self.worker_uuid: raise AirflowException("Could not get worker uuid") self.log.debug('Start with worker_uuid: %s', self.worker_uuid) # always need to reset resource version since we don't know # when we last started, note for behavior below # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs # /CoreV1Api.md#list_namespaced_pod KubeResourceVersion.reset_resource_version() self.kube_client = get_kube_client() self.kube_scheduler = AirflowKubernetesScheduler( self.kube_config, self.task_queue, self.result_queue, self.kube_client, self.worker_uuid ) self._inject_secrets() self.clear_not_launched_queued_tasks()
Example #10
Source File: deploy_to_kubernetes.py From takeoff with GNU General Public License v3.0 | 6 votes |
def _authenticate_with_kubernetes(self): """Authenticate with the defined AKS cluster and write the configuration to a file""" resource_group = get_resource_group_name(self.config, self.env) cluster_name = get_kubernetes_name(self.config, self.env) # get azure container service client credentials = ActiveDirectoryUserCredentials( vault_name=self.vault_name, vault_client=self.vault_client ).credentials(self.config) client = ContainerServiceClient( credentials=credentials, subscription_id=SubscriptionId(self.vault_name, self.vault_client).subscription_id(self.config), ) # authenticate with Kubernetes credential_results = client.managed_clusters.list_cluster_user_credentials( resource_group_name=resource_group, resource_name=cluster_name ) self._write_kube_config(credential_results)
Example #11
Source File: k8s.py From simpleflow with MIT License | 6 votes |
def schedule(self): """ Schedule a job from the given job template. See example of it here: https://github.com/kubernetes-incubator/client-python/blob/master/examples/create_deployment.py """ # build job definition job_definition = self.compute_job_definition() # load cluster config self.load_config() # schedule job api = kubernetes.client.BatchV1Api() namespace = os.getenv("K8S_NAMESPACE", "default") api.create_namespaced_job(body=job_definition, namespace=namespace)
Example #12
Source File: app.py From pipelines with Apache License 2.0 | 6 votes |
def delete_deployment(params): from kubernetes.client import V1DeleteOptions spec = get_seldon_spec(params) name = get_deployment_name(params) # spec["metadata"]["name"] namespace = "default" # TODO: the namespace should be configured or be figured out dynamically plural = spec["kind"].lower()+"s" # TODO: verify the "rule" for constructing plural group, version = spec["apiVersion"].split("/") del_opts = V1DeleteOptions() api_client = get_custom_objects_api_client() api_response = api_client.list_namespaced_custom_object(group, version, namespace, plural) if name in [deployment["metadata"]["name"] for deployment in api_response["items"]]: api_response = api_client.delete_namespaced_custom_object(group, version, namespace, plural, name, del_opts) else: LOG.error("Could not find the Seldon deployment '%s'" % name) return { "status": "Error", "details": "Could not find a Seldon deployment with name '%s'" % name } # api_response_filtered = {key: api_response[key] for key in ["apiVersion", "kind"]} LOG.info("%s ..." % str(api_response)[:160]) return api_response
Example #13
Source File: k8s_client.py From powerfulseal with Apache License 2.0 | 6 votes |
def delete_pods(self, pods): """ https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/docs/ CoreV1Api.md#delete_namespaced_pod Delete all pods synchronously """ for pod in pods: try: self.client_corev1api.delete_namespaced_pod( name=pod.name, namespace=pod.namespace, grace_period_seconds=0 ) except ApiException as e: self.logger.exception(e) return False return True
Example #14
Source File: test_ipip_spoofing.py From node with Apache License 2.0 | 5 votes |
def setUp(self): TestBase.setUp(self) self.ns_name = generate_unique_id(5, prefix="spoof") self.create_namespace(self.ns_name) # Create two client pods that live for the duration of the # test. We will use 'kubectl exec' to try sending/receiving # from these at particular times. # # We do it this way because it takes a # relatively long time (7 seconds?) in this test setup for # Calico routing and policy to be set up correctly for a newly # created pod. nodes, _, _ = node_info() kubectl("run --generator=run-pod/v1 " "access " "-n %s " "--image busybox " "--overrides='{\"spec\": {\"nodeName\":\"%s\"}}' " "--command /bin/sh -- -c \"nc -l -u -p 5000 &> /root/snoop.txt\"" % (self.ns_name, nodes[1])) kubectl("run --generator=run-pod/v1 " "scapy " "-n %s " "--image ehlers/scapy " "--overrides='{\"spec\": {\"nodeName\":\"%s\"}}' " "--command /bin/sleep -- 3600" % (self.ns_name, nodes[2])) kubectl("wait --timeout=2m --for=condition=ready" + " pod/scapy -n %s" % self.ns_name) kubectl("wait --timeout=2m --for=condition=ready" + " pod/access -n %s" % self.ns_name)
Example #15
Source File: image-cleaner.py From binderhub with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_docker_images(client): """Return list of docker images, sorted by size Untagged images will come first """ images = client.images.list(all=True) # create dict by image id for O(1) lookup by_id = {image.id: image for image in images} # graph contains a set of all descendant (not just immediate) # images for each image graph = defaultdict(set) for image in images: while image.attrs['Parent']: graph[image.attrs['Parent']].add(image) image = by_id[image.attrs['Parent']] def image_key(image): """Sort images topologically and by size - Prefer images with fewer descendants, so that we never try to delete an image before its children (fails with 409) - Prefer untagged images to tagged ones (delete build intermediates first) - Sort topological peers by size """ return (-len(graph[image.id]), not image.tags, image.attrs['Size']) images.sort(key=image_key, reverse=True) return images
Example #16
Source File: app.py From pipelines with Apache License 2.0 | 5 votes |
def apply_oid_token_monkey_patch(): LOG.warning("applying monkey-patch for https://github.com/kubernetes-client/python/issues/525") import base64 import json import kubernetes from datetime import datetime, timezone from kubernetes.config.kube_config import _is_expired def load_oid_token_patched(self, provider): if 'auth-provider' not in self._user: return provider = self._user['auth-provider'] if 'name' not in provider or 'config' not in provider or provider['name'] != 'oidc': return parts = provider['config']['id-token'].split('.') if len(parts) != 3: # Not a valid JWT return None padding = (4 - len(parts[1]) % 4) * '=' jwt_attributes = json.loads(base64.b64decode(parts[1] + padding).decode('utf-8')) expire = jwt_attributes.get('exp') if (expire is not None) and _is_expired(datetime.fromtimestamp(expire, tz=timezone.utc)): self._refresh_oidc(provider) if self._config_persister: self._config_persister(self._config.value) self.token = "Bearer %s" % provider['config']['id-token'] return self.token kubernetes.config.kube_config.KubeConfigLoader._load_oid_token = load_oid_token_patched
Example #17
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 #18
Source File: app.py From pipelines with Apache License 2.0 | 5 votes |
def get_api_client_v1(): import kubernetes api_client_v1 = kubernetes.client.CoreV1Api() return api_client_v1
Example #19
Source File: app.py From pipelines with Apache License 2.0 | 5 votes |
def get_custom_objects_api_client(): import kubernetes api_client = kubernetes.client.CustomObjectsApi() return api_client
Example #20
Source File: deployment_status_operator.py From shipyard with Apache License 2.0 | 5 votes |
def _get_k8s_client(): """Create and return a Kubernetes client :returns: A Kubernetes client object :rtype: kubernetes.client """ # Note that we are using 'in_cluster_config' LOG.debug("Loading Kubernetes config") kubernetes.config.load_incluster_config() LOG.debug("Creating Kubernetes client") return kubernetes.client.CoreV1Api()
Example #21
Source File: manifest.py From kubetest with GNU General Public License v3.0 | 5 votes |
def cast_value(value: Any, t: str) -> Any: """Cast the given value to the specified type. There are two general cases for possible casts: - A cast to a builtin type (int, str, etc.) - A cast to a Kubernetes object (V1ObjectMeta, etc) In either case, check to see if the specified type exists in the correct type pool. If so, cast to that type, otherwise fail. Args: value: The value to cast. t: The type to cast the value to. This can be a builtin type or a Kubernetes API object type. Returns: The value, casted to the appropriate type. Raises: ValueError: Unable to cast the value to the specified type. TypeError: Unable to cast the given value to the specified type. AttributeError: The value is an invalid Kubernetes type. """ # The config value should be cast to a built-in type builtin_type = builtins.__dict__.get(t) if builtin_type == object: return value if builtin_type is not None: return builtin_type(value) # The config value should be cast to a Kubernetes type k_type = kubernetes.client.__dict__.get(t) if k_type is not None: return new_object(k_type, value) raise ValueError(f'Unable to determine cast type behavior: {t}')
Example #22
Source File: cluster_monitor.py From KubeOperator with Apache License 2.0 | 5 votes |
def get_api_instance(self): self.cluster.change_to() master = self.cluster.group_set.get(name='master').hosts.first() if master is not None and master.ip is not None: configuration = kubernetes.client.Configuration() configuration.api_key_prefix['authorization'] = 'Bearer' configuration.api_key['authorization'] = self.token configuration.debug = True configuration.host = 'https://' + master.ip + ":6443" configuration.verify_ssl = False self.api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(configuration)) self.app_v1_api = kubernetes.client.AppsV1Api(kubernetes.client.ApiClient(configuration)) self.storage_v1_Api = kubernetes.client.StorageV1Api(kubernetes.client.ApiClient(configuration))
Example #23
Source File: k8s_client.py From powerfulseal with Apache License 2.0 | 5 votes |
def __init__(self, kube_config=None, logger=None): if kube_config: kubernetes.config.load_kube_config(config_file=kube_config) else: kubernetes.config.load_incluster_config() self.kube_config = kube_config self.client_corev1api = kubernetes.client.CoreV1Api() self.client_appsv1api = kubernetes.client.AppsV1Api() self.logger = logger or makeLogger(__name__) self.logger.info("Initializing with config: %s", kube_config)
Example #24
Source File: cluster_monitor.py From KubeOperator with Apache License 2.0 | 5 votes |
def check_authorization(self, retry_count): if retry_count == 0: raise Exception('init k8s client failed! retry_count=' + str(retry_count)) self.retry_count = retry_count - 1 try: self.api_instance.list_node() except ApiException as e: if e.status == 401: self.push_token_to_redis() else: logger.error(msg='init k8s client failed ' + e.reason, exc_info=True)
Example #25
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 5 votes |
def _run(self, kube_client: client.CoreV1Api, resource_version: Optional[str], worker_uuid: str, kube_config: Any) -> Optional[str]: self.log.info( 'Event: and now my watch begins starting at resource_version: %s', resource_version ) watcher = watch.Watch() kwargs = {'label_selector': 'airflow-worker={}'.format(worker_uuid)} if resource_version: kwargs['resource_version'] = resource_version if kube_config.kube_client_request_args: for key, value in kube_config.kube_client_request_args.items(): kwargs[key] = value last_resource_version: Optional[str] = None for event in watcher.stream(kube_client.list_pod_for_all_namespaces, **kwargs): task = event['object'] self.log.info( 'Event: %s had an event of type %s', task.metadata.name, event['type'] ) if event['type'] == 'ERROR': return self.process_error(event) self.process_status( pod_id=task.metadata.name, namespace=task.metadata.namespace, status=task.status.phase, labels=task.metadata.labels, resource_version=task.metadata.resource_version, event=event, ) last_resource_version = task.metadata.resource_version return last_resource_version
Example #26
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 5 votes |
def delete_pod(self, pod_id: str, namespace: str) -> None: """Deletes POD""" try: self.kube_client.delete_namespaced_pod( pod_id, namespace, body=client.V1DeleteOptions(**self.kube_config.delete_option_kwargs), **self.kube_config.kube_client_request_args) except ApiException as e: # If the pod is already deleted if e.status != 404: raise
Example #27
Source File: kubernetes_executor.py From airflow with Apache License 2.0 | 5 votes |
def __init__(self): self.kube_config = KubeConfig() self._manager = multiprocessing.Manager() self.task_queue: 'Queue[KubernetesJobType]' = self._manager.Queue() self.result_queue: 'Queue[KubernetesResultsType]' = self._manager.Queue() self.kube_scheduler: Optional[AirflowKubernetesScheduler] = None self.kube_client: Optional[client.CoreV1Api] = None self.worker_uuid: Optional[str] = None super().__init__(parallelism=self.kube_config.parallelism)
Example #28
Source File: create_from_yaml.py From python with Apache License 2.0 | 5 votes |
def create_from_yaml_single_item( k8s_client, yml_object, verbose=False, **kwargs): group, _, version = yml_object["apiVersion"].partition("/") if version == "": version = group group = "core" # Take care for the case e.g. api_type is "apiextensions.k8s.io" # Only replace the last instance group = "".join(group.rsplit(".k8s.io", 1)) # convert group name from DNS subdomain format to # python class name convention group = "".join(word.capitalize() for word in group.split('.')) fcn_to_call = "{0}{1}Api".format(group, version.capitalize()) k8s_api = getattr(client, fcn_to_call)(k8s_client) # Replace CamelCased action_type into snake_case kind = yml_object["kind"] kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind) kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower() # Expect the user to create namespaced objects more often if hasattr(k8s_api, "create_namespaced_{0}".format(kind)): # Decide which namespace we are going to put the object in, # if any if "namespace" in yml_object["metadata"]: namespace = yml_object["metadata"]["namespace"] kwargs['namespace'] = namespace resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))( body=yml_object, **kwargs) else: kwargs.pop('namespace', None) resp = getattr(k8s_api, "create_{0}".format(kind))( body=yml_object, **kwargs) if verbose: msg = "{0} created.".format(kind) if hasattr(resp, 'status'): msg += " status='{0}'".format(str(resp.status)) print(msg)
Example #29
Source File: test_utils.py From rally-openstack with Apache License 2.0 | 5 votes |
def test_get_k8s_api_client(self, mock_core_v1_api, mock_api_client): if hasattr(kubernetes_client, "ConfigurationObject"): # it is k8s-client < 4.0.0 m = mock.patch("kubernetes.client.ConfigurationObject") else: m = mock.patch("kubernetes.client.Configuration") mock_configuration_object = m.start() self.addCleanup(m.stop) self.context.update({ "tenant": { "id": "rally_tenant_id", "cluster": "rally_cluster_uuid" } }) self.scenario = utils.MagnumScenario(self.context) cluster_uuid = self.context["tenant"]["cluster"] client = self.clients("magnum") client.clusters.get.return_value = self.cluster cluster = self.scenario._get_cluster(cluster_uuid) self.cluster_template.tls_disabled = True client.cluster_templates.get.return_value = self.cluster_template config = mock_configuration_object.return_value config.host = cluster.api_address config.ssl_ca_cert = None config.cert_file = None config.key_file = None _api_client = mock_api_client.return_value self.scenario._get_k8s_api_client() mock_configuration_object.assert_called_once_with() if hasattr(kubernetes_client, "ConfigurationObject"): # k8s-python < 4.0.0 mock_api_client.assert_called_once_with(config=config) else: mock_api_client.assert_called_once_with(config) mock_core_v1_api.assert_called_once_with(_api_client)
Example #30
Source File: k8s_client.py From powerfulseal with Apache License 2.0 | 5 votes |
def list_nodes(self): """ https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/docs/ CoreV1Api.md##list_node """ try: resp = self.client_corev1api.list_node() return resp.items except ApiException as e: self.logger.exception(e) raise