Kubesphere kubesphere-multi-tenant-management
KubeSphere multi-tenant management Skill. Use when user requests to create users, workspaces, projects, or assign roles/permissions. Supports user lifecycle management, workspace configuration, project creation, role binding. Do not perform any delete operations, do not create custom roles.
git clone https://github.com/kubesphere/kubesphere
T=$(mktemp -d) && git clone --depth=1 https://github.com/kubesphere/kubesphere "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/kubesphere-multi-tenant-management" ~/.claude/skills/kubesphere-kubesphere-kubesphere-multi-tenant-management && rm -rf "$T"
skills/kubesphere-multi-tenant-management/SKILL.mdKubeSphere Multi-Tenant Management
Security Guidelines
-
Never use kubectl edit/delete - Do NOT use
,kubectl edit
, or similar commands to modify or delete workspaces, projects, users, roles, or role bindings. These operations are sensitive and should be performed via KubeSphere Console with proper approval workflow.kubectl delete -
Never perform delete operations via API - Do NOT delete users, workspaces, projects, roles, or role bindings via API. These operations must be performed manually via KubeSphere Console with proper approval workflow. Only use this skill for creating and querying resources.
-
Never create custom roles - Do NOT create custom roles (Role, WorkspaceRole, GlobalRole). Only use built-in roles provided by KubeSphere. If custom permissions are needed, instruct the user to configure them via KubeSphere Console.
-
Default to least privilege - When creating users or assigning permissions, always use the minimum required access level:
- New user creation: default to
(not platform-admin)platform-regular - Inviting user to workspace: default to
(not admin)<workspace-name>-regular - Inviting user to project: default to
(not admin)viewer - Only escalate permissions when explicitly requested
- New user creation: default to
Core Concepts
Workspace
The top-level organizational unit in KubeSphere, representing a team, department, or business unit. A workspace can contain multiple projects and serves as the basic boundary for resource grouping and access control. Workspaces can span multiple clusters, enabling centralized management of resources distributed across different clusters.
Project
KubeSphere's enhanced Kubernetes namespace, representing a specific application, environment, or workload within a workspace. Each project maps to a separate namespace.
User & Role
- User: KubeSphere account entity, can be platform admin, workspace member, or project member
- Role: Permission set defined in KubeSphere's three-tier RBAC:
Project Roles (
roles.iam.kubesphere.io):
: Full access to all resourcesadmin
: Create/update/delete resources, cannot manage rolesoperator
: Read-only accessviewer
Workspace Roles (WorkspaceRole,
workspaceroles.iam.kubesphere.io):
: Full access to workspace and all projects<workspace-name>-admin
: Limited workspace access<workspace-name>-regular
: Create projects in workspace<workspace-name>-self-provisioner
: Read-only access to workspace<workspace-name>-viewer
Platform Roles (GlobalRole,
globalroles.iam.kubesphere.io):
: Full access to all resourcesplatform-admin
: Limited platform accessplatform-regular
: Can create workspacesplatform-self-provisioner
Role Binding (KubeSphere API endpoints, binds roles to Users):
- Project-level:
API, binds/namespacemembers
to Userroles.iam.kubesphere.io - Workspace-level:
API, binds/workspacemembers
to Userworkspaceroles.iam.kubesphere.io - Platform-level:
API, binds/users/<username>
to User via annotationglobalroles.iam.kubesphere.io
Step-by-Step Guide
Prerequisites
Set up authentication using the provided CLI tool. First, navigate to the scripts directory:
# Navigate to the skill's scripts directory # Example path (replace with your actual kubesphere-skills location): cd ~/kubesphere-skills/core/kubesphere-core/scripts # Install required Python package pip install requests # Set host endpoint (optional, defaults to http://ks-apiserver.kubesphere-system) export KUBESPHERE_HOST="http://<kubesphere-host>" # Login to get token (token will be cached) python ks_api.py --login --username admin --password <your-password> # Token is cached in ~/.kubesphere_token and auto-refreshed # Optional: Clear cached token python ks_api.py --clear-cache
1. Create Workspace
Required parameters:
: Name for the workspace (maps toworkspace-name
)metadata.name
: Workspace manager (maps tomanager
, default to current login user)spec.template.spec.manager
: Creator name (maps tocreator
)metadata.annotations["kubesphere.io/creator"]
: List of cluster names to host this workspace (maps toclusters
)spec.placement.clusters
# Create workspace via Python CLI python ks_api.py POST /kapis/tenant.kubesphere.io/v1beta1/workspacetemplates '{ "apiVersion": "iam.kubesphere.io/v1beta1", "kind": "WorkspaceTemplate", "metadata": { "name": "<workspace-name>", "annotations": { "kubesphere.io/creator": "<creator>" } }, "spec": { "template": { "spec": { "manager": "<manager>" }, "metadata": { "annotations": { "kubesphere.io/creator": "<creator>" } } }, "placement": { "clusters": [ {"name": "<cluster-name>"} ] } } }'
Note: Before creating a workspace, always ask the user for:
- Workspace name (required)
- Manager (required, default to current login user)
- Clusters (required) - which cluster(s) to assign the workspace to
2. Create Project within Workspace
Required parameters:
: Name for the project (maps toproject-name
)metadata.name
: Name of the workspace to create the project in (maps toworkspace-name
)metadata.labels["kubesphere.io/workspace"]
: Cluster name to create the project in (maps to URI path andcluster-name
field)cluster
: Creator name (maps tocreator
)metadata.annotations["kubesphere.io/creator"]
# Create project within workspace via Python CLI python ks_api.py POST /clusters/<cluster-name>/kapis/tenant.kubesphere.io/v1beta1/workspaces/<workspace-name>/namespaces '{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "labels": { "kubesphere.io/workspace": "<workspace-name>", "kubesphere.io/managed": "true" }, "name": "<project-name>", "annotations": { "kubesphere.io/creator": "<creator>" } }, "cluster": "<cluster-name>" }'
Note: Before creating a project, always ask the user for:
- Project name (required)
- Workspace name (required) - which workspace to create the project in
- Cluster name (required) - which cluster to create the project in
3. Create User
Required parameters:
: Username for the new userusername
: User's email addressemail
: User's password (must meet KubeSphere password policy)password
Optional parameters:
: Platform role (default:globalrole
)platform-regular
# Create user via Python CLI python ks_api.py POST /kapis/iam.kubesphere.io/v1beta1/users '{ "apiVersion": "iam.kubesphere.io/v1beta1", "kind": "User", "metadata": { "annotations": { "iam.kubesphere.io/uninitialized": "true", "iam.kubesphere.io/globalrole": "platform-regular", "kubesphere.io/creator": "admin" }, "name": "<username>" }, "spec": { "email": "<email>", "password": "<password>" } }'
Note: Before creating a user, always ask the user for:
- Username (required)
- Email address (required)
- Platform role: If not specified, default to
platform-regular
4. Invite User to Workspace/Project
For Workspace invitation:
: Username to invite (required)username
: Target workspace name (required)workspace-name
: Workspace role (default:role
)<workspace-name>-regular
For Project invitation:
: Username to invite (required)username
: Target project name (required)project-name
: Cluster name (required)cluster-name
: Project role (default:role
)viewer
# Invite user to workspace (default role: <workspace-name>-regular) python ks_api.py POST /kapis/iam.kubesphere.io/v1beta1/workspaces/<workspace-name>/workspacemembers '[{"username":"<username>","roleRef":"<workspace-name>-regular"}]'
# Invite user to project (default role: viewer) python ks_api.py POST /clusters/<cluster-name>/kapis/iam.kubesphere.io/v1beta1/namespaces/<project-name>/namespacemembers '[{"username":"<username>","roleRef":"viewer"}]'
Note: Before inviting a user, always ask the user for:
- Username to invite (required)
- Target: workspace or project (required)
- Role: If not specified, default to
for workspace or<workspace-name>-regular
for projectviewer
5. Modify User Permissions
Modify user roles at three levels: platform, workspace, and project.
For Platform Role (global role):
: Username to modify (required)username
: New platform role (required)globalrole- Note: Must first GET the user to get current metadata, then PUT with updated annotation
# Step 1: Get current user info (required before modification) python ks_api.py GET /kapis/iam.kubesphere.io/v1beta1/users/<username> # Step 2: Update global role annotation python ks_api.py PUT /kapis/iam.kubesphere.io/v1beta1/users/<username> '{ "apiVersion": "iam.kubesphere.io/v1beta1", "kind": "User", "metadata": { "name": "<username>", "annotations": { "iam.kubesphere.io/globalrole": "<new-global-role>" } } }'
For Workspace Role:
: Username to modify (required)username
: Target workspace name (required)workspace-name
: New workspace role (required)roleRef
# Modify user role in workspace python ks_api.py PUT /kapis/iam.kubesphere.io/v1beta1/workspaces/<workspace-name>/workspacemembers/<username> '{"username":"<username>","roleRef":"<workspace-name>-<role>"}'
For Project Role:
: Username to modify (required)username
: Target project name (required)project-name
: Cluster name (required)cluster-name
: New project role (required)roleRef
# Modify user role in project python ks_api.py PUT /clusters/<cluster-name>/kapis/iam.kubesphere.io/v1beta1/namespaces/<project-name>/namespacemembers/<username> '{"username":"<username>","roleRef":"<role>"}'
Note: Before modifying permissions, always ask the user for:
- Username to modify (required)
- Scope: platform / workspace / project (required)
- New role: Only use built-in roles provided by KubeSphere
6. Query Resources
List Workspaces
python ks_api.py GET /kapis/tenant.kubesphere.io/v1beta1/workspacetemplates
List Users
python ks_api.py GET /kapis/iam.kubesphere.io/v1beta1/users
List Workspace Members
python ks_api.py GET /kapis/iam.kubesphere.io/v1beta1/workspaces/<workspace-name>/workspacemembers
List Project Members
python ks_api.py GET /clusters/<cluster-name>/kapis/iam.kubesphere.io/v1beta1/namespaces/<project-name>/namespacemembers
List Projects in Workspace
python ks_api.py GET /clusters/<cluster-name>/kapis/tenant.kubesphere.io/v1beta1/workspaces/<workspace-name>/namespaces
Get User Details
python ks_api.py GET /kapis/iam.kubesphere.io/v1beta1/users/<username>
Error Handling
| Error Code | Cause | Solution |
|---|---|---|
| Token expired | |
| No permission | Use admin account |
| Resource already exists | Use different name |
| Resource not found | Verify name/workspace/cluster is correct |
| Invalid parameters | Check error message for details (email format, password policy, naming rules) |
| Connection refused/timeout | API unreachable | Verify KUBESPHERE_HOST is correct |
Debugging:
flag for cleaner output:--quietpython ks_api.py GET /users --quiet- Check token:
python ks_api.py
References
Related Skills
- Core platform architecturekubesphere-core
- Cluster operationskubesphere-cluster-management