create_namespace
Create a new namespace in Kubernetes clusters with 'k8s-pilot'. Specify the context, namespace name, and optional labels to manage resources efficiently.
Instructions
Create a new namespace.
Args: context_name: The Kubernetes context name namespace: The name for the new namespace labels: Optional dictionary of labels to apply to the namespace
Returns: JSON string containing information about the created namespace
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context_name | Yes | ||
| labels | No | ||
| namespace | Yes |
Implementation Reference
- tools/namespace.py:67-110 (handler)The MCP tool handler for create_namespace, decorated with @mcp.tool(). It checks if the namespace exists, creates it using Kubernetes API with optional labels, and returns JSON result.@mcp.tool() @use_current_context @check_readonly_permission def create_namespace(context_name: str, namespace: str, labels: Optional[Dict[str, str]] = None): """ Create a new namespace. Args: context_name: The Kubernetes context name namespace: The name for the new namespace labels: Optional dictionary of labels to apply to the namespace Returns: JSON string containing information about the created namespace """ core_v1: CoreV1Api = get_api_clients(context_name)["core"] try: # Check if namespace already exists try: core_v1.read_namespace(namespace) return json.dumps({"error": f"Namespace '{namespace}' already exists"}) except ApiException as e: if e.status != 404: return json.dumps({"error": f"API error: {str(e)}"}) # 404 means namespace doesn't exist, so we can proceed # Create namespace object ns_metadata = V1ObjectMeta(name=namespace, labels=labels) ns_body = V1Namespace(metadata=ns_metadata) # Create the namespace created_ns = core_v1.create_namespace(body=ns_body) result = { "name": created_ns.metadata.name, "status": created_ns.status.phase, "labels": created_ns.metadata.labels if created_ns.metadata.labels else {}, "message": f"Namespace '{namespace}' created successfully" } return json.dumps(result) except ApiException as e: return json.dumps({"error": f"Failed to create namespace: {str(e)}"})
- tools/namespace.py:67-67 (registration)The @mcp.tool() decorator registers the create_namespace function as an MCP tool.@mcp.tool()