Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StoragePolicy and ContentLibrary when creating VC Namespace #975

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,40 @@ func (data *TestData) createVCNamespace(namespace string) error {
}()

svID, _ := data.vcClient.getSupervisorID()
storagePolicyID, _ := data.vcClient.getStoragePolicyID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will content library be heavy load or have performance for Namespace, can we add a parameter in createVCNamespace to add storage policy and content library only if they are needed in specific test cases?

log.V(1).Info("Get storage policy", "storagePolicyID", storagePolicyID)
contentLibraryID, _ := data.vcClient.getContentLibraryID()
log.V(1).Info("Get content library", "contentLibraryID", contentLibraryID)
vcNamespace := &VCNamespaceCreateSpec{
Supervisor: svID,
Namespace: namespace,
StorageSpecs: []InstancesStorageSpec{
{
Policy: storagePolicyID,
},
},
ContentLibraries: []InstancesContentLibrarySpec{
{
ContentLibrary: contentLibraryID,
},
},
NetworkSpec: InstancesNetworkConfigInfo{
NetworkProvider: "NSX_VPC",
VpcNetwork: InstancesVpcNetworkInfo{
DefaultSubnetSize: 16,
},
},
VmServiceSpec: &InstancesVMServiceSpec{
ContentLibraries: map[string]bool{
contentLibraryID: true,
},
VmClasses: map[string]bool{
"best-effort-xsmall": true,
},
},
}
dataJson, err := json.Marshal(vcNamespace)
log.V(1).Info("Data json", "dataJson", string(dataJson))
if err != nil {
log.Error(err, "Unable convert vcNamespace object to json bytes", "namespace", namespace)
return fmt.Errorf("unable convert vcNamespace object to json bytes: %v", err)
Expand Down
76 changes: 73 additions & 3 deletions test/e2e/vclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"

Expand All @@ -27,6 +28,12 @@ type supervisorInfo struct {
K8sStatus string `json:"kubernetes_status"`
}

type storagePolicyInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Policy string `json:"policy"`
}

type supervisorSummary struct {
ID string `json:"supervisor"`
Info supervisorInfo `json:"info"`
Expand All @@ -52,10 +59,29 @@ type InstancesNetworkConfigInfo struct {
VpcNetwork InstancesVpcNetworkInfo `json:"vpc_network"`
}

type InstancesStorageSpec struct {
Policy string `json:"policy"`
Limit *int64 `json:"limit"`
}

type InstancesContentLibrarySpec struct {
ContentLibrary string `json:"content_library"`
Writable *bool `json:"writable"`
AllowImport *bool `json:"allow_import"`
}

type InstancesVMServiceSpec struct {
ContentLibraries map[string]bool `json:"content_libraries"`
VmClasses map[string]bool `json:"vm_classes"`
}

type VCNamespaceCreateSpec struct {
Supervisor string `json:"supervisor"`
Namespace string `json:"namespace"`
NetworkSpec InstancesNetworkConfigInfo `json:"network_spec"`
Supervisor string `json:"supervisor"`
Namespace string `json:"namespace"`
NetworkSpec InstancesNetworkConfigInfo `json:"network_spec"`
StorageSpecs []InstancesStorageSpec `json:"storage_specs"`
ContentLibraries []InstancesContentLibrarySpec `json:"content_libraries"`
VmServiceSpec *InstancesVMServiceSpec `json:"vm_service_specs"`
}

type VCNamespaceGetInfo struct {
Expand Down Expand Up @@ -143,13 +169,56 @@ func (c *vcClient) getSupervisorID() (string, error) {
}

for _, sv := range response.Items {
log.Info("Checking supervisor", "supervisor", sv.Info.Name, "status", sv.Info.ConfigStatus)
if sv.Info.ConfigStatus == "RUNNING" {
return sv.ID, nil
}
}
return "", fmt.Errorf("no valid supervisor found on vCenter")
}

func (c *vcClient) getStoragePolicyID() (string, error) {
urlPath := "/api/vcenter/storage/policies"
request, err := c.prepareRequest(http.MethodGet, urlPath, nil)
if err != nil {
return "", err
}
// response is a list of storage policy info
var response []storagePolicyInfo
if _, err = c.handleRequest(request, &response); err != nil {
return "", err
}

for _, po := range response {
log.Info("Checking storage policy", "policy", po.Name, "description", po.Description)
if strings.Contains(po.Name, "global") || strings.Contains(po.Name, "local") {
return po.Name, nil
}
}
return "", fmt.Errorf("no valid storage policy found on vCenter")
}

// Get the first content library ID by default
func (c *vcClient) getContentLibraryID() (string, error) {
zhengxiexie marked this conversation as resolved.
Show resolved Hide resolved
urlPath := "/api/content/library"
request, err := c.prepareRequest(http.MethodGet, urlPath, nil)
if err != nil {
return "", err
}
var response []string
if _, err = c.handleRequest(request, &response); err != nil {
return "", err
}

for _, cl := range response {
log.Info("Checking content library", "content library", cl)
if cl != "" {
return cl, nil
}
}
return "", fmt.Errorf("no valid content library found on vCenter")
}

func (c *vcClient) createNamespaceWithPreCreatedVPC(namespace string, vpcPath string, supervisorID string) error {
vcNamespace := createVCNamespaceSpec(namespace, supervisorID, vpcPath)
data, err := json.Marshal(vcNamespace)
Expand Down Expand Up @@ -208,6 +277,7 @@ func createVCNamespaceSpec(namespace string, svID string, vpcPath string) *VCNam

func (c *vcClient) prepareRequest(method string, urlPath string, data []byte) (*http.Request, error) {
url := fmt.Sprintf("%s://%s%s", c.url.Scheme, c.url.Host, urlPath)
log.Info("Requesting", "url", url)
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
Expand Down
Loading