remove_namespace_label
Remove a specific label from a Kubernetes namespace by providing the context name, namespace, and label key. Returns updated namespace labels in JSON format, simplifying label management in multi-cluster environments.
Instructions
Remove a label from a namespace.
Args: context_name: The Kubernetes context name namespace: The name of the namespace label_key: The label key to remove
Returns: JSON string containing the updated namespace labels
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context_name | Yes | ||
| label_key | Yes | ||
| namespace | Yes |
Implementation Reference
- tools/namespace.py:205-258 (handler)The core handler function for the 'remove_namespace_label' MCP tool. It is decorated with @mcp.tool() which registers it, @use_current_context for context management, and @check_readonly_permission for access control. The function reads the namespace, removes the specified label if present, patches the namespace via Kubernetes API, and returns JSON with updated labels or error.@mcp.tool() @use_current_context @check_readonly_permission def remove_namespace_label(context_name: str, namespace: str, label_key: str): """ Remove a label from a namespace. Args: context_name: The Kubernetes context name namespace: The name of the namespace label_key: The label key to remove 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) # Check if the namespace has labels if not ns.metadata.labels or label_key not in ns.metadata.labels: result = { "name": namespace, "labels": ns.metadata.labels, "message": f"Label '{label_key}' not found on namespace" } return json.dumps(result) # Update the labels labels = dict(ns.metadata.labels) del labels[label_key] # 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 remove label: {str(e)}"})