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:
  • group (str) -- the cluster group we want to operate on.

  • cluster (str) -- the cluster we're operating on.

  • dry_run (bool, default: True) -- if true, no write operations will happen.

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:

spicerack.k8s.KubernetesNode

get_pod(namespace: str, name: str) spicerack.k8s.KubernetesPod[source]

Get a kubernetes pod.

Parameters:
  • name (str) -- the name of the pod.

  • namespace (str) -- the namespace the pod is in.

Raises:

spicerack.k8s.KubernetesApiError -- if the pod is not found on the cluster.

Return type:

spicerack.k8s.KubernetesPod

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.

CONFIG_BASE: str = '/etc/kubernetes'

The base path for the kubernetes clusters configurations files.

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 not None, this api object will be used, instead of fetching it from the api.

cordon() None[source]

Makes the node unschedulable.

Raises:
Return type:

None

drain() None[source]

Drains the node, analogous to kubectl drain.

Raises:

spicerack.k8s.KubernetesCheckError -- if we can't evict all pods.

Return type:

None

get_pods() list[KubernetesPod][source]

Get the pods running on this node.

Return type:

list[KubernetesPod]

is_schedulable() bool[source]

Checks if a node is schedulable or not.

Return type:

bool

Returns:

True if payloads can be scheduled on the node, False otherwise.

refresh() None[source]

Refresh the api object from the kubernetes api server.

Return type:

None

uncordon() None[source]

Makes a node schedulable.

Raises:
Return type:

None

property name: str

The name of the node.

property taints: list[V1Taint]

The taints of the node.

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:
Return type:

None

is_daemonset() bool[source]

Checks if the pod is part of a daemonset.

Return type:

bool

is_evictable() bool[source]

Check if the pod can be evicted.

Return type:

bool

is_mirror() bool[source]

Check if the pod is a mirror pod.

Return type:

bool

is_terminated() bool[source]

Checks if the pod is terminated.

Return type:

bool

refresh() None[source]

Refresh the api object from the kubernetes api server.

Return type:

None

property controller: V1OwnerReference | None

Get the reference to the controlling object, if any.

property spec: V1PodSpec

Get the pod's spec.