add_namespace_label
Add or update labels on Kubernetes namespaces to organize and manage cluster resources using the k8s-pilot server.
Instructions
Add or update a label on a namespace.
Args: context_name: The Kubernetes context name namespace: The name of the namespace label_key: The label key to add label_value: The label value to set
Returns: JSON string containing the updated namespace labels
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context_name | Yes | ||
| namespace | Yes | ||
| label_key | Yes | ||
| label_value | Yes |
Implementation Reference
- tools/namespace.py:153-202 (handler)The @mcp.tool()-decorated handler function that implements adding or updating a label on a Kubernetes namespace by patching its metadata.labels using the CoreV1Api.@mcp.tool() @use_current_context @check_readonly_permission def add_namespace_label(context_name: str, namespace: str, label_key: str, label_value: str): """ Add or update a label on a namespace. Args: context_name: The Kubernetes context name namespace: The name of the namespace label_key: The label key to add label_value: The label value to set Returns: JSON string containing the updated namespace labels """ core_v1: CoreV1Api = get_api_clients(context_name)["core"] try: # Get the current namespace ns = core_v1.read_namespace(namespace) # Prepare the patch if not ns.metadata.labels: ns.metadata.labels = {} # Update the labels labels = dict(ns.metadata.labels) labels[label_key] = label_value # Apply the patch body = { "metadata": { "labels": labels } } patched_ns = core_v1.patch_namespace(namespace, body) result = { "name": patched_ns.metadata.name, "labels": patched_ns.metadata.labels } return json.dumps(result) except ApiException as e: if e.status == 404: return json.dumps({"error": f"Namespace '{namespace}' not found"}) else: return json.dumps({"error": f"Failed to add label: {str(e)}"})
- tools/namespace.py:153-153 (registration)The @mcp.tool() decorator registers the add_namespace_label function as an MCP tool.@mcp.tool()