Automate Namespace Creation in vSphere with Tanzu Using Python

Ravi ShankarRavi Shankar
2 min read

๐Ÿงฉ 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:

  1. Authenticates with the vCenter server using a session-based login.

  2. Retrieves the cluster ID for the given Supervisor Cluster.

  3. Creates a new namespace using the vCenter REST API.

  4. 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.

0
Subscribe to my newsletter

Read articles from Ravi Shankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ravi Shankar
Ravi Shankar