label_resource
Add labels to Kubernetes resources like pods, services, or deployments to organize and identify them within your cluster.
Instructions
Label a Kubernetes resource with the specified label
Args:
resource_type: Type of the resource (e.g., pod, service, deployment)
resource_name: Name of the resource to label
label: Label to add (e.g., key=value)
namespace: Namespace of the resource
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resource_type | Yes | ||
| resource_name | Yes | ||
| label | Yes | ||
| namespace | No | default |
Implementation Reference
- kubernetes.py:280-295 (handler)The main handler function for the 'label_resource' tool, decorated with @mcp.tool() for registration. It uses kubectl label to add a label to a Kubernetes resource.async def label_resource(resource_type: str, resource_name: str, label: str, namespace: str = "default") -> dict: """Label a Kubernetes resource with the specified label Args: resource_type: Type of the resource (e.g., pod, service, deployment) resource_name: Name of the resource to label label: Label to add (e.g., key=value) namespace: Namespace of the resource """ try: cmd = ["kubectl", "label", resource_type, resource_name, label, "-n", namespace, "--overwrite"] result = subprocess.run(cmd, capture_output=True, text=True, check=True) return {"message": f"Resource {resource_type}/{resource_name} labeled successfully in namespace {namespace}", "details": result.stdout} except subprocess.CalledProcessError as e: return {"error": f"Failed to label resource: {str(e)}"}