Kubernetes Service Reference
- class smarter.apps.infrastructure.services.kubernetes.KubernetesService(provider_name, *args, **kwargs)[source]
Bases:
InfrastructureServiceThe platform’s Kubernetes cluster.
Implementations provide the primitives:
apply_manifest(),get_resource(),list_resources(),delete_resource(),delete_resources()andget_pod_logs(), and optionallyfind_resources(), for the inventory. The ingress operations that LLMClient deployments use are built on them.- abstractmethod apply_manifest(manifest)[source]
Create or update the resources of a manifest.
- Raises:
KubernetesServiceError – If the cluster rejects them.
- Return type:
- certificate_wait_seconds: float = 60
Seconds between the checks of a cert-manager certificate, in
verify_ingress_resources().
- delete_ingress_resources(hostname, namespace)[source]
Delete a host’s Ingress, its cert-manager Certificate, and its TLS Secret.
- abstractmethod delete_resource(kind, name, namespace)[source]
Delete a resource by name.
A resource that does not exist counts as deleted.
- Return type:
- abstractmethod delete_resources(kinds, namespace, selector)[source]
Delete the resources of several kinds that match a label selector.
Idempotent. A selector is required, so that a namespace is never emptied by mistake.
- Return type:
- error_class
alias of
KubernetesServiceError
- find_resources(kind, namespace=None)[source]
Return every resource of a kind, for the inventory.
Unlike
list_resources(), it tells a kind that has no resources from one that could not be listed, so that the inventory never mistakes an unavailable cluster for an empty one. This implementation cannot list, and returns None.- Parameters:
- Return type:
- Returns:
The resources, or None if they could not be listed, e.g. because the cluster is unavailable, or the kind’s CustomResourceDefinition is not installed.
- abstractmethod get_pod_logs(namespace, selector, container=None, tail=200)[source]
Return the most recent log lines of the pods that match a label selector.
- abstractmethod get_resource(kind, name, namespace)[source]
Return a resource, or None if it does not exist or the cluster is unavailable.
- get_resources(resources, namespace)[source]
Return several resources at once.
Implementations may override it to fetch them in one request. This one gets each in turn.
- abstractmethod list_resources(kind, namespace, selector=None)[source]
Return the resources of a kind, optionally those that match a label selector.
- service_name: str = 'kubernetes'
The name of the service in signals and logs, see
InfrastructureServiceNames.
- verify_certificate(name, namespace)[source]
Whether a cert-manager Certificate exists, and is Ready, i.e. issued.
- verify_ingress_resources(hostname, namespace, max_attempts=30)[source]
Verify that a host’s Ingress, its cert-manager Certificate, and its TLS Secret exist.
The Ingress is named after the host, and the Certificate and Secret
<host>-tls.- Parameters:
- Return type:
- Returns:
Whether the Ingress, the Certificate, and the Secret are verified.
- class smarter.apps.infrastructure.services.kubernetes.KubectlKubernetesService(provider=None, allow_in_tests=False, **kwargs)[source]
Bases:
KubernetesServiceThe platform’s Kubernetes cluster, through kubectl.
The cluster is ready once kubectl is configured, with the provider’s
update_kubeconfig(), and the environment’s namespace,smarter_settings.environment_namespace, exists.- Parameters:
provider (
CloudProvider|None) – The cloud provider that writes the kubeconfig. None for a cluster whose kubeconfig is already in place.allow_in_tests (
bool) – Allow kubectl in the unit tests, e.g. when subprocess is mocked.
- __init__(provider=None, allow_in_tests=False, **kwargs)[source]
- apply_manifest(manifest)[source]
Create or update the resources of a manifest, with
kubectl apply.Resources that provision billable cloud resources, see
billable_resources(), are announced with the billable resource signals. Nothing is applied if the cluster is not ready.- Parameters:
manifest (
str) – The manifest, which may have several YAML documents.- Raises:
KubernetesServiceError – If the cluster rejects it.
- Return type:
- property configured: bool
Whether kubectl is configured for the cluster.
- delete_resource(kind, name, namespace)[source]
Delete a resource by name.
A resource that does not exist counts as deleted.
- Return type:
- delete_resources(kinds, namespace, selector)[source]
Delete the resources of several kinds that match a label selector.
Idempotent. A selector is required, so that a namespace is never emptied by mistake.
- Return type:
- find_resources(kind, namespace=None)[source]
Return every resource of a kind, for the inventory.
Unlike
list_resources(), it tells a kind that has no resources from one that could not be listed, so that the inventory never mistakes an unavailable cluster for an empty one. This implementation cannot list, and returns None.- Parameters:
- Return type:
- Returns:
The resources, or None if they could not be listed, e.g. because the cluster is unavailable, or the kind’s CustomResourceDefinition is not installed.
- get_pod_logs(namespace, selector, container=None, tail=200)[source]
Return the most recent log lines of the pods that match a label selector.
- get_resource(kind, name, namespace)[source]
Return a resource, or None if it does not exist or the cluster is unavailable.
- get_resources(resources, namespace)[source]
Return several resources with one
kubectl get.Each kubectl call fetches a cluster token, e.g. with
aws eks get-token, which takes seconds, so one call rather than several frees a Celery worker sooner. If the call fails, e.g. because a kind’s CustomResourceDefinition is not installed, each resource is fetched in turn.
- property kubeconfig_path: str
The path of the platform’s kubeconfig file.
- list_resources(kind, namespace, selector=None)[source]
Return the resources of a kind, optionally those that match a label selector.
- property namespace_verified: bool
Whether the environment’s namespace exists.
- property ready: bool
Whether the service is authenticated and connected, i.e. it can be used.