Automate Namespace Creation in vSphere with Tanzu Using Python


๐งฉ Introduction
Creating Supervisor Namespaces is a foundational step in running modern Kubernetes workloads on VMware vSphere with Tanzu. Manually creating namespaces via the vSphere UI can be time-consuming and error-prone, especially in large-scale or automated environments.
In this post, Iโll walk you through a Python automation script I wrote that programmatically creates a namespace in a vSphere Supervisor Cluster using vCenter REST APIs.
๐ Script on GitHub โ create_namespace.py
๐ ๏ธ Prerequisites
Before using the script, ensure:
โ
vSphere 7+ with Workload Management enabled
โ
A configured Supervisor Cluster
โ
Python 3.x environment
โ
The requests
module installed
โ
Appropriate permissions for the user (at least NSX-T and VM operator roles)
๐งช Script Overview
Hereโs what the script does:
Authenticates with the vCenter server using a session-based login.
Retrieves the cluster ID for the given Supervisor Cluster.
Creates a new namespace using the vCenter REST API.
Applies optional configurations like:
storage policies
VM service configurations
content library
resource limits
๐ฆ Script Parameters
The script expects the following inputs:
bashCopyEditpython create_namespace.py \
--vc-ip <vcenter_ip_or_fqdn> \
--username <administrator@vsphere.local> \
--password <your_password> \
--cluster-name <your_supervisor_cluster_name> \
--namespace <desired_namespace_name>
๐งฌ Key Functions Explained
๐ get_session_id(vc_ip, username, password)
Establishes a session with vCenter.
Returns a session token used for authentication in subsequent API calls.
๐ get_cluster_id(vc_ip, session_id, cluster_name)
Queries the
/vcenter/cluster
API.Retrieves the cluster ID that matches the provided cluster name.
๐งฑ create_namespace(vc_ip, session_id, cluster_id, namespace_name)
Sends a
POST
request to/vcenter/namespaces/instances
.Creates a namespace on the selected Supervisor Cluster.
๐ Source Code
You can find the full script on my GitHub:
๐ create_namespace.py
Feel free to clone, fork, or contribute.
๐ Conclusion
Using the vCenter REST API, itโs possible to automate the lifecycle of Kubernetes namespaces in a consistent and repeatable way. This script is just the beginning โ stay tuned for more vSphere automation tools!
If you have any questions or want to suggest improvements, feel free to comment below or raise an issue on GitHub.
Subscribe to my newsletter
Read articles from Ravi Shankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
