Skip to content

Commit 267681b

Browse files
committed
Enhanced GetPortGroupsAttachedToVMsInFailureDomain to only look at VMs related to cluster
1 parent 960a84f commit 267681b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

test/e2e/vsphere/multi-nic.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ func failIfNodeNotInMachineNetwork(nodes corev1.NodeList, machineNetworks []stri
4141

4242
func failIfIncorrectPortgroupsAttachedToVMs(
4343
ctx context.Context,
44-
infra configv1.PlatformSpec,
44+
infra *configv1.Infrastructure,
4545
nodeList *corev1.NodeList,
4646
vsphereCreds *corev1.Secret) {
4747

4848
By("checking if VMs have the correct portgroups attached")
4949

50-
for _, failureDomain := range infra.VSphere.FailureDomains {
51-
nodes, err := getNodesInFailureDomain(infra.VSphere, failureDomain, nodeList)
50+
providerSpec := infra.Spec.PlatformSpec
51+
for _, failureDomain := range providerSpec.VSphere.FailureDomains {
52+
nodes, err := getNodesInFailureDomain(providerSpec.VSphere, failureDomain, nodeList)
5253
fmt.Printf("nodes: %d", len(nodes))
5354
Expect(err).NotTo(HaveOccurred())
5455

55-
vmPortgroupMap, err := GetPortGroupsAttachedToVMsInFailureDomain(ctx, failureDomain, vsphereCreds, infra.VSphere.VCenters)
56+
vmPortgroupMap, err := GetPortGroupsAttachedToVMsInFailureDomain(ctx, infra.Status.InfrastructureName, failureDomain, vsphereCreds, providerSpec.VSphere.VCenters)
5657
if err != nil {
5758
Expect(err).NotTo(HaveOccurred())
5859
}
@@ -212,7 +213,7 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:VSphereMultiNetworks][p
212213
})
213214

214215
It("node VMs should have all specified portgroups attached which are associated with their failure domain [apigroup:machine.openshift.io][Suite:openshift/conformance/parallel]", func() {
215-
failIfIncorrectPortgroupsAttachedToVMs(ctx, infra.Spec.PlatformSpec, nodes, vsphereCreds)
216+
failIfIncorrectPortgroupsAttachedToVMs(ctx, infra, nodes, vsphereCreds)
216217
})
217218

218219
It("new machines should pass multi network tests [apigroup:machine.openshift.io][Suite:openshift/conformance/serial]", Label("Serial"), func() {
@@ -252,6 +253,6 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:VSphereMultiNetworks][p
252253
failIfNodeNotInMachineNetwork(*nodes, machineNetworks)
253254
failIfNodeNetworkingInconsistentWithMachineNetwork(infra.Spec.PlatformSpec, machineNetworks)
254255
failIfMachinesDoNotHaveAllPortgroups(infra.Spec.PlatformSpec, machines)
255-
failIfIncorrectPortgroupsAttachedToVMs(ctx, infra.Spec.PlatformSpec, nodes, vsphereCreds)
256+
failIfIncorrectPortgroupsAttachedToVMs(ctx, infra, nodes, vsphereCreds)
256257
})
257258
})

test/e2e/vsphere/util.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import (
66
"net"
77
"net/url"
88
"path"
9+
"slices"
910
"strconv"
1011
"time"
1112

1213
. "github.com/onsi/ginkgo/v2"
1314
. "github.com/onsi/gomega"
14-
1515
configv1 "github.com/openshift/api/config/v1"
1616
"github.com/openshift/api/machine/v1beta1"
1717
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
18-
util "github.com/openshift/machine-api-operator/test/e2e"
1918
"github.com/pkg/errors"
2019
"github.com/vmware/govmomi"
2120
"github.com/vmware/govmomi/find"
2221
"github.com/vmware/govmomi/object"
2322
vapirest "github.com/vmware/govmomi/vapi/rest"
23+
"github.com/vmware/govmomi/vapi/tags"
2424
"github.com/vmware/govmomi/vim25"
2525
"github.com/vmware/govmomi/vim25/mo"
2626
"github.com/vmware/govmomi/vim25/soap"
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/client-go/rest"
3030

3131
"github.com/openshift/machine-api-operator/pkg/controller/vsphere"
32+
util "github.com/openshift/machine-api-operator/test/e2e"
3233
)
3334

3435
func isIpInCidrRange(ip string, cidr string) (bool, error) {
@@ -255,6 +256,7 @@ func getCredentialsForVCenter(ctx context.Context, vsphereCreds *corev1.Secret,
255256

256257
func GetPortGroupsAttachedToVMsInFailureDomain(
257258
ctx context.Context,
259+
clusterID string,
258260
failureDomain configv1.VSpherePlatformFailureDomainSpec,
259261
vsphereCreds *corev1.Secret,
260262
vCenters []configv1.VSpherePlatformVCenterSpec) (map[string][]string, error) {
@@ -281,7 +283,7 @@ func GetPortGroupsAttachedToVMsInFailureDomain(
281283

282284
portGroupMap := make(map[string][]string)
283285

284-
client, _, logout, err := CreateVSphereClients(ctx, vcenterUrl, user, pass)
286+
client, rClient, logout, err := CreateVSphereClients(ctx, vcenterUrl, user, pass)
285287
defer logout()
286288

287289
if err != nil {
@@ -290,17 +292,31 @@ func GetPortGroupsAttachedToVMsInFailureDomain(
290292

291293
finder := NewFinder(client)
292294

295+
// Get tagged objects and then grab VMs
296+
tm := tags.NewManager(rClient)
297+
refs, err := tm.ListAttachedObjects(ctx, clusterID)
298+
var vmList []types.ManagedObjectReference
299+
for _, ref := range refs {
300+
if ref.Reference().Type == "VirtualMachine" {
301+
vmList = append(vmList, ref.Reference())
302+
}
303+
}
304+
293305
vms, err := finder.VirtualMachineList(ctx, fmt.Sprintf("/%s/vm/...", failureDomain.Topology.Datacenter))
294306
if err != nil {
295307
return nil, fmt.Errorf("unable to get VMs in %s. %v", failureDomain.Topology.ComputeCluster, err)
296308
}
297309

298310
for _, vm := range vms {
311+
// Make sure vm has the cluster id tag. Note: template will be included.
312+
if !slices.Contains(vmList, vm.Reference()) {
313+
continue
314+
}
315+
299316
vm, err := finder.VirtualMachine(ctx, vm.InventoryPath)
300317
if err != nil {
301318
return nil, fmt.Errorf("unable to get VMs in %s. %v", failureDomain.Topology.ComputeCluster, err)
302319
}
303-
fmt.Printf("UUID: %s\n", vm.UUID(ctx))
304320
virtualDevices, err := vm.Device(ctx)
305321

306322
var portGroups []string

0 commit comments

Comments
 (0)