get_namespaces
Retrieve a summary of Kubernetes cluster namespaces, including name, status, and age, similar to kubectl get namespace. Simplifies namespace monitoring without manual API calls.
Instructions
Return a summary of the namespaces for this Kubernetes cluster, similar to that
returned by kubectl get namespace.
Parameters
----------
None
This function does not take any parameters.
Returns
-------
list of NamespaceSummary
List of namespace summary objects. Each NamespaceSummary has the following fields:
name : str
Name of the namespace.
status : str
Status phase of the namespace.
age : datetime.timedelta
Age of the namespace (current time minus creation timestamp).
Raises
------
K8sConfigError
If unable to initialize the K8S API.
K8sApiError
If the API call to list namespaces fails.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/k8stools/k8s_tools.py:79-118 (handler)The core handler function for the 'get_namespaces' tool. It initializes the Kubernetes API client if needed, lists all namespaces, computes their age, and returns a list of NamespaceSummary objects.def get_namespaces() -> list[NamespaceSummary]: """Return a summary of the namespaces for this Kubernetes cluster, similar to that returned by `kubectl get namespace`. Parameters ---------- None This function does not take any parameters. Returns ------- list of NamespaceSummary List of namespace summary objects. Each NamespaceSummary has the following fields: name : str Name of the namespace. status : str Status phase of the namespace. age : datetime.timedelta Age of the namespace (current time minus creation timestamp). Raises ------ K8sConfigError If unable to initialize the K8S API. K8sApiError If the API call to list namespaces fails. """ global K8S if K8S is None: K8S = _get_api_client() logging.info(f"get_namespaces()") namespaces = K8S.list_namespace().items now = datetime.datetime.now(datetime.timezone.utc) return [ NamespaceSummary(name=namespace.metadata.name, status=namespace.status.phase, age=now-namespace.metadata.creation_timestamp) for namespace in namespaces ]
- src/k8stools/k8s_tools.py:72-77 (schema)Pydantic BaseModel defining the output schema for the get_namespaces tool, with fields for name, status, and age of each namespace.class NamespaceSummary(BaseModel): """Summary information about a namespace, like returned by `kubectl get namespace`""" name: str status: str age: datetime.timedelta
- src/k8stools/k8s_tools.py:1101-1111 (registration)Registration of the get_namespaces tool (and others) in the TOOLS list, which is likely used by the MCP server to expose these functions as tools.TOOLS = [ get_namespaces, get_node_summaries, get_pod_summaries, get_pod_container_statuses, get_pod_events, get_pod_spec, get_logs_for_pod_and_container, get_deployment_summaries, get_service_summaries ]