k8s¶
Kubernetes module.
- exception spicerack.k8s.KubernetesApiError[source]¶
Bases:
SpicerackError
Custom error class for errors interacting with the kubernetes api.
- exception spicerack.k8s.KubernetesApiTooManyRequests[source]¶
Bases:
KubernetesApiError
Custom error class for HTTP TooManyRequest errors when interacting with the kubernetes api.
- exception spicerack.k8s.KubernetesCheckError[source]¶
Bases:
SpicerackCheckError
Custom error class for errors checking kubernetes resources.
- exception spicerack.k8s.KubernetesError[source]¶
Bases:
SpicerackError
Custom error class for errors in running the kubernetes module.
- class spicerack.k8s.Kubernetes(group: str, cluster: str, *, dry_run: bool = True)[source]¶
Bases:
object
High-level interface for interacting with the kubernetes api from spicerack.
Initialize the instance.
- Parameters:
- get_node(name: str) spicerack.k8s.KubernetesNode [source]¶
Get a kubernetes node.
- Parameters:
name (
str
) -- the name of the node.- Raises:
spicerack.k8s.KubernetesApiError -- if the node is not found on the cluster.
- Return type:
- get_pod(namespace: str, name: str) spicerack.k8s.KubernetesPod [source]¶
Get a kubernetes pod.
- Parameters:
- Raises:
spicerack.k8s.KubernetesApiError -- if the pod is not found on the cluster.
- Return type:
- class spicerack.k8s.KubernetesApiFactory(cluster: str)[source]¶
Bases:
object
Provides kubernetes object classes easy access to the API.
Initialize the instance.
- Parameters:
cluster (
str
) -- the cluster we're operating on.
- batch(*, user: str = 'admin') kubernetes.client.api.batch_v1_api.BatchV1Api [source]¶
Return an instance of the batch api correctly configured.
- Parameters:
user (
str
, default:'admin'
) -- the user to use for authentication.- Return type:
kubernetes.client.api.batch_v1_api.BatchV1Api
- configuration(user: str) kubernetes.client.configuration.Configuration [source]¶
Get the configuration for a specific user.
- Parameters:
user (
str
) -- the user to fetch the configuration for.- Raises:
spicerack.k8s.KubernetesError -- if the user or the configuration are invalid.
- Return type:
kubernetes.client.configuration.Configuration
- core(*, user: str = 'admin') kubernetes.client.api.core_v1_api.CoreV1Api [source]¶
Return an instance of the core api correctly configured.
- Parameters:
user (
str
, default:'admin'
) -- the user to use for authentication.- Return type:
kubernetes.client.api.core_v1_api.CoreV1Api
-
API_CLASSES:
dict
[str
,typing.Any
] = {'batch': <class 'kubernetes.client.api.batch_v1_api.BatchV1Api'>, 'core': <class 'kubernetes.client.api.core_v1_api.CoreV1Api'>}¶ The different kubernetes APIs supported.
- class spicerack.k8s.KubernetesNode(fqdn: str, api: spicerack.k8s.KubernetesApiFactory, dry_run: bool = True, init_obj: kubernetes.client.models.v1_node.V1Node | None = None)[source]¶
Bases:
object
Encapsulates actions on a kubernetes node.
Initialize the instance.
- Parameters:
fqdn (
str
) -- the fqdn of the node.api (
spicerack.k8s.KubernetesApiFactory
) -- the api factory we're going to use.dry_run (
bool
, default:True
) -- if true, no write operations will happen.init_obj (
typing.Optional
[kubernetes.client.models.v1_node.V1Node
], default:None
) -- if notNone
, this api object will be used, instead of fetching it from the api.
- cordon() None [source]¶
Makes the node unschedulable.
- Raises:
spicerack.k8s.KubernetesApiError -- if the call to the api failed.
spicerack.k8s.KubernetesCheckError -- if the node wasn't set to unschedulable.
- Return type:
- drain() None [source]¶
Drains the node, analogous to kubectl drain.
- Raises:
spicerack.k8s.KubernetesCheckError -- if we can't evict all pods.
- Return type:
- get_pods() list[KubernetesPod] [source]¶
Get the pods running on this node.
- Return type:
list
[KubernetesPod]
- uncordon() None [source]¶
Makes a node schedulable.
- Raises:
spicerack.k8s.KubernetesApiError -- if the call to the api failed.
spicerack.k8s.KubernetesCheckError -- if the node wasn't set to unschedulable.
- Return type:
- class spicerack.k8s.KubernetesPod(namespace: str, name: str, api: spicerack.k8s.KubernetesApiFactory, dry_run: bool = True, init_obj: kubernetes.client.models.v1_pod.V1Pod | None = None)[source]¶
Bases:
object
Encapsulates actions on a kubernetes pod.
Initialize the pod isntance.
- Parameters:
namespace (
str
) -- the namespace where the pod is located.name (
str
) -- the name of the pod.api (
spicerack.k8s.KubernetesApiFactory
) -- the api factory we're going to use.dry_run (
bool
, default:True
) -- if true, no write operations will happen.init_obj (
typing.Optional
[kubernetes.client.models.v1_pod.V1Pod
], default:None
) -- if not None, this api object will be used, instead of fetching it from the api.
- evict() None [source]¶
Submit an eviction request to the kubernetes api for this pod.
- Raises:
spicerack.k8s.KubernetesApiTooManyRequests -- in case of a persistent HTTP 429 from the server.
spicerack.k8s.KubernetesApiError -- in case of a bad response from the server.
spicerack.k8s.KubernetesError -- if the pod is not evictable.
- Return type:
- property spec: V1PodSpec¶
Get the pod's spec.