add_namespace_label
Adds or updates a label on a specified Kubernetes namespace using context name, label key, and value. Returns JSON with updated namespace labels.
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 | ||
| label_key | Yes | ||
| label_value | Yes | ||
| namespace | Yes |
Implementation Reference
- tools/namespace.py:153-202 (handler)The main handler function for the 'add_namespace_label' tool, decorated with @mcp.tool() for registration, @use_current_context, and @check_readonly_permission. It adds or updates a label on a Kubernetes namespace using the Kubernetes API.@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)}"})