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

🌱 Move vcsim object naming utils to vcsim helpers #2690

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
83 changes: 83 additions & 0 deletions internal/test/helpers/vcsim/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vcsim

import "fmt"

const (
// DefaultNetworkName is the name of the default network that exists when starting a new vcsim instance.
DefaultNetworkName = "VM Network"

// DefaultStoragePolicyName is the name of the default storage policy that exists when starting a new vcsim instance.
DefaultStoragePolicyName = "vSAN Default Storage Policy"

// DefaultVMTemplateName is the name of the default VM template the vcsim controller adds to new vcsim instance.
// Note: There are no default templates when starting a new vcsim instance.
// Note: For the sake of testing with vcsim the template doesn't really matter (nor the version of K8s hosted on it)
// so the vcsim controller creates only a VM template with a well-known name.
DefaultVMTemplateName = "ubuntu-2204-kube-vX"
)

// DatacenterName provide a function to compute vcsim datacenter names given its index.
func DatacenterName(datacenter int) string {
return fmt.Sprintf("DC%d", datacenter)
}

// ClusterName provide a function to compute vcsim cluster names given its index and the index of a datacenter.
func ClusterName(datacenter, cluster int) string {
return fmt.Sprintf("%s_C%d", DatacenterName(datacenter), cluster)
}

// ClusterPath provides the path for a vcsim cluster given its index and the index of a datacenter.
func ClusterPath(datacenter, cluster int) string {
return fmt.Sprintf("/%s/host/%s", DatacenterName(datacenter), ClusterName(datacenter, cluster))
}

// DatastoreName provide a function to compute vcsim datastore names given its index.
func DatastoreName(datastore int) string {
return fmt.Sprintf("LocalDS_%d", datastore)
}

// DatastorePath provides the path for a vcsim datastore given its index and the index of a datacenter.
func DatastorePath(datacenter, datastore int) string {
return fmt.Sprintf("/%s/datastore/%s", DatacenterName(datacenter), DatastoreName(datastore))
}

// ResourcePoolPath provides the path for a vcsim Resources folder given the index of a datacenter and the index of a cluster.
func ResourcePoolPath(datacenter, cluster int) string {
return fmt.Sprintf("/%s/host/%s/Resources", DatacenterName(datacenter), ClusterName(datacenter, cluster))
}

// VMFolderName provide a function to compute vcsim vm folder name names given the index of a datacenter.
func VMFolderName(datacenter int) string {
return fmt.Sprintf("%s/vm", DatacenterName(datacenter))
}

// VMPath provides the path for a vcsim VM given the index of a datacenter and the vm name.
func VMPath(datacenter int, vm string) string {
return fmt.Sprintf("/%s/%s", VMFolderName(datacenter), vm)
}

// NetworkFolderName provide a function to compute vcsim network folder name names given the index of a datacenter.
func NetworkFolderName(datacenter int) string {
return fmt.Sprintf("%s/network", DatacenterName(datacenter))
}

// NetworkPath provides the path for a vcsim network given the index of a datacenter and the network name.
func NetworkPath(datacenter int, network string) string {
return fmt.Sprintf("/%s/%s", NetworkFolderName(datacenter), network)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vcsim

import (
"testing"
Expand All @@ -29,16 +29,14 @@ func Test_vcsim_NamesAndPath(t *testing.T) {
cluster := 3
datastore := 7

g.Expect(vcsimDatacenterName(datacenter)).To(Equal("DC5"))
g.Expect(vcsimClusterName(datacenter, cluster)).To(Equal("DC5_C3"))
g.Expect(vcsimClusterPath(datacenter, cluster)).To(Equal("/DC5/host/DC5_C3"))
g.Expect(vcsimDatastoreName(datastore)).To(Equal("LocalDS_7"))
g.Expect(vcsimDatastorePath(datacenter, datastore)).To(Equal("/DC5/datastore/LocalDS_7"))
g.Expect(vcsimResourcePoolPath(datacenter, cluster)).To(Equal("/DC5/host/DC5_C3/Resources"))
g.Expect(vcsimVMFolderName(datacenter)).To(Equal("DC5/vm"))
g.Expect(vcsimVMPath(datacenter, "my-mv")).To(Equal("/DC5/vm/my-mv"))
}

func Test_createVMTemplate(_ *testing.T) {
// TODO: implement
g.Expect(DatacenterName(datacenter)).To(Equal("DC5"))
g.Expect(ClusterName(datacenter, cluster)).To(Equal("DC5_C3"))
g.Expect(ClusterPath(datacenter, cluster)).To(Equal("/DC5/host/DC5_C3"))
g.Expect(DatastoreName(datastore)).To(Equal("LocalDS_7"))
g.Expect(DatastorePath(datacenter, datastore)).To(Equal("/DC5/datastore/LocalDS_7"))
g.Expect(ResourcePoolPath(datacenter, cluster)).To(Equal("/DC5/host/DC5_C3/Resources"))
g.Expect(VMFolderName(datacenter)).To(Equal("DC5/vm"))
g.Expect(VMPath(datacenter, "my-mv")).To(Equal("/DC5/vm/my-mv"))
g.Expect(NetworkFolderName(datacenter)).To(Equal("DC5/network"))
g.Expect(NetworkPath(datacenter, "my-network")).To(Equal("/DC5/network/my-network"))
}
9 changes: 5 additions & 4 deletions test/infrastructure/vcsim/controllers/envvar_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

vcsimhelpers "sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vcsim"
vcsimv1 "sigs.k8s.io/cluster-api-provider-vsphere/test/infrastructure/vcsim/api/v1alpha1"
)

Expand Down Expand Up @@ -191,13 +192,13 @@ func (r *EnvVarReconciler) reconcileNormal(ctx context.Context, envVar *vcsimv1.
// cluster template variables about the vcsim instance.
envVar.Status.Variables["VSPHERE_SERVER"] = fmt.Sprintf("https://%s", vCenterSimulator.Status.Host)
envVar.Status.Variables["VSPHERE_TLS_THUMBPRINT"] = vCenterSimulator.Status.Thumbprint
envVar.Status.Variables["VSPHERE_DATACENTER"] = vcsimDatacenterName(int(ptr.Deref(envVar.Spec.Cluster.Datacenter, 0)))
envVar.Status.Variables["VSPHERE_DATASTORE"] = vcsimDatastoreName(int(ptr.Deref(envVar.Spec.Cluster.Datastore, 0)))
envVar.Status.Variables["VSPHERE_DATACENTER"] = vcsimhelpers.DatacenterName(int(ptr.Deref(envVar.Spec.Cluster.Datacenter, 0)))
envVar.Status.Variables["VSPHERE_DATASTORE"] = vcsimhelpers.DatastoreName(int(ptr.Deref(envVar.Spec.Cluster.Datastore, 0)))
envVar.Status.Variables["VSPHERE_FOLDER"] = fmt.Sprintf("/DC%d/vm", ptr.Deref(envVar.Spec.Cluster.Datacenter, 0))
envVar.Status.Variables["VSPHERE_NETWORK"] = fmt.Sprintf("/DC%d/network/VM Network", ptr.Deref(envVar.Spec.Cluster.Datacenter, 0))
envVar.Status.Variables["VSPHERE_RESOURCE_POOL"] = fmt.Sprintf("/DC%d/host/DC%[1]d_C%d/Resources", ptr.Deref(envVar.Spec.Cluster.Datacenter, 0), ptr.Deref(envVar.Spec.Cluster.Cluster, 0))
envVar.Status.Variables["VSPHERE_STORAGE_POLICY"] = vcsimDefaultStoragePolicyName
envVar.Status.Variables["VSPHERE_TEMPLATE"] = fmt.Sprintf("/DC%d/vm/%s", ptr.Deref(envVar.Spec.Cluster.Datacenter, 0), vcsimDefaultVMTemplateName)
envVar.Status.Variables["VSPHERE_STORAGE_POLICY"] = vcsimhelpers.DefaultStoragePolicyName
envVar.Status.Variables["VSPHERE_TEMPLATE"] = fmt.Sprintf("/DC%d/vm/%s", ptr.Deref(envVar.Spec.Cluster.Datacenter, 0), vcsimhelpers.DefaultVMTemplateName)

return nil
}
Expand Down
102 changes: 0 additions & 102 deletions test/infrastructure/vcsim/controllers/vcsim.go

This file was deleted.

51 changes: 41 additions & 10 deletions test/infrastructure/vcsim/controllers/vcsim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

_ "github.com/dougm/pretty" // NOTE: this is required to add commands vm.* to cli.Run
"github.com/pkg/errors"
"github.com/vmware/govmomi/govc/cli"
_ "github.com/vmware/govmomi/govc/vm" // NOTE: this is required to add commands vm.* to cli.Run
pbmsimulator "github.com/vmware/govmomi/pbm/simulator"
"github.com/vmware/govmomi/simulator"
Expand All @@ -46,13 +47,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vcsim"
vcsimhelpers "sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vcsim"
"sigs.k8s.io/cluster-api-provider-vsphere/test/framework/vmoperator"
vcsimv1 "sigs.k8s.io/cluster-api-provider-vsphere/test/infrastructure/vcsim/api/v1alpha1"
"sigs.k8s.io/cluster-api-provider-vsphere/test/infrastructure/vcsim/controllers/images"
)

const (
vcsimMinVersionForCAPV = "7.0.0"

// vmIP reconciler secret.

netConfigMapName = "vsphere.provider.config.netoperator.vmware.com"
Expand All @@ -67,7 +70,7 @@ type VCenterSimulatorReconciler struct {
Client client.Client
SupervisorMode bool

vcsimInstances map[string]*vcsim.Simulator
vcsimInstances map[string]*vcsimhelpers.Simulator
lock sync.RWMutex

// WatchFilterValue is the label value used to filter events prior to reconciliation.
Expand Down Expand Up @@ -136,7 +139,7 @@ func (r *VCenterSimulatorReconciler) reconcileNormal(ctx context.Context, vCente
defer r.lock.Unlock()

if r.vcsimInstances == nil {
r.vcsimInstances = map[string]*vcsim.Simulator{}
r.vcsimInstances = map[string]*vcsimhelpers.Simulator{}
}

key := klog.KObj(vCenterSimulator).String()
Expand Down Expand Up @@ -176,7 +179,7 @@ func (r *VCenterSimulatorReconciler) reconcileNormal(ctx context.Context, vCente
}

// Start the vcsim instance
vcsimInstance, err := vcsim.NewBuilder().
vcsimInstance, err := vcsimhelpers.NewBuilder().
WithModel(model).
SkipModelCreate().
WithURL(vcsimURL).
Expand Down Expand Up @@ -236,16 +239,16 @@ func (r *VCenterSimulatorReconciler) reconcileNormal(ctx context.Context, vCente
Username: vCenterSimulator.Status.Username,
Password: vCenterSimulator.Status.Password,
Thumbprint: vCenterSimulator.Status.Thumbprint,
Datacenter: vcsimDatacenterName(datacenter),
Cluster: vcsimClusterPath(datacenter, cluster),
Folder: vcsimVMFolderName(datacenter),
ResourcePool: vcsimResourcePoolPath(datacenter, cluster),
StoragePolicyID: vcsimDefaultStoragePolicyName,
Datacenter: vcsimhelpers.DatacenterName(datacenter),
Cluster: vcsimhelpers.ClusterPath(datacenter, cluster),
Folder: vcsimhelpers.VMFolderName(datacenter),
ResourcePool: vcsimhelpers.ResourcePoolPath(datacenter, cluster),
StoragePolicyID: vcsimhelpers.DefaultStoragePolicyName,

// Those are settings for a fake content library we are going to create given that it doesn't exists in vcsim by default.
ContentLibrary: vmoperator.ContentLibraryConfig{
Name: "kubernetes",
Datastore: vcsimDatastorePath(datacenter, datastore),
Datastore: vcsimhelpers.DatastorePath(datacenter, datastore),
Item: vmoperator.ContentLibraryItemConfig{
Name: "test-image-ovf",
Files: []vmoperator.ContentLibraryItemFilesConfig{ // TODO: check if we really need both
Expand Down Expand Up @@ -286,6 +289,34 @@ func (r *VCenterSimulatorReconciler) reconcileNormal(ctx context.Context, vCente
return nil
}

func createVMTemplate(ctx context.Context, vCenterSimulator *vcsimv1.VCenterSimulator) error {
log := ctrl.LoggerFrom(ctx)
govcURL := fmt.Sprintf("https://%s:%s@%s/sdk", vCenterSimulator.Status.Username, vCenterSimulator.Status.Password, vCenterSimulator.Status.Host)

// TODO: Investigate how template are supposed to work
// we create a template in a datastore, what if many?
// we create a template in a cluster, but the generated vm doesn't have the cluster in the path. What if I have many clusters?
cluster := 0
datastore := 0
datacenters := 1
if vCenterSimulator.Spec.Model != nil {
datacenters = int(ptr.Deref(vCenterSimulator.Spec.Model.Datacenter, int32(simulator.VPX().Datacenter))) // VPX is the same base model used when creating vcsim
}
for dc := 0; dc < datacenters; dc++ {
exit := cli.Run([]string{"vm.create", fmt.Sprintf("-ds=%s", vcsimhelpers.DatastoreName(datastore)), fmt.Sprintf("-cluster=%s", vcsimhelpers.ClusterName(dc, cluster)), fmt.Sprintf("-net=%s", vcsimhelpers.DefaultNetworkName), "-disk=20G", "-on=false", "-k=true", fmt.Sprintf("-u=%s", govcURL), vcsimhelpers.DefaultVMTemplateName})
if exit != 0 {
return errors.New("failed to create vm template")
}

exit = cli.Run([]string{"vm.markastemplate", "-k=true", fmt.Sprintf("-u=%s", govcURL), vcsimhelpers.VMPath(dc, vcsimhelpers.DefaultVMTemplateName)})
if exit != 0 {
return errors.New("failed to mark vm template")
}
log.Info("Created VM template", "name", vcsimhelpers.DefaultVMTemplateName)
}
return nil
}

func addPreRequisitesForVMIPreconciler(ctx context.Context, c client.Client, config vmoperator.Dependencies) error {
log := ctrl.LoggerFrom(ctx)
log.Info("Reconciling requirements for the Fake net-operator Deployment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
vcsimhelpers "sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vcsim"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
"sigs.k8s.io/cluster-api-provider-vsphere/test/framework/vmoperator"
Expand Down Expand Up @@ -242,7 +243,7 @@ func (r *VirtualMachineReconciler) getVMIpReconciler(cluster *clusterv1.Cluster,
GetVMPath: func() string {
// The vm operator always create VMs under a sub-folder with named like the cluster.
datacenter := 0
return vcsimVMPath(datacenter, path.Join(cluster.Name, virtualMachine.Name))
return vcsimhelpers.VMPath(datacenter, path.Join(cluster.Name, virtualMachine.Name))
},
}
}
Expand Down
Loading