remove_namespace_label
Remove a label from a Kubernetes namespace to manage cluster metadata and organization. This tool helps maintain clean namespace configurations by deleting specific labels.
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 | ||
| namespace | Yes | ||
| label_key | Yes |
Implementation Reference
- tools/namespace.py:205-258 (handler)The main handler function for the 'remove_namespace_label' MCP tool. It removes a specified label from a Kubernetes namespace using the CoreV1Api. Includes decorators for MCP tool registration (@mcp.tool()), context usage (@use_current_context), and permission checks (@check_readonly_permission). Returns JSON with updated labels or error messages.@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)}"})