@@ -6,21 +6,21 @@ import (
6
6
"net"
7
7
"net/url"
8
8
"path"
9
+ "slices"
9
10
"strconv"
10
11
"time"
11
12
12
13
. "github.com/onsi/ginkgo/v2"
13
14
. "github.com/onsi/gomega"
14
-
15
15
configv1 "github.com/openshift/api/config/v1"
16
16
"github.com/openshift/api/machine/v1beta1"
17
17
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
18
- util "github.com/openshift/machine-api-operator/test/e2e"
19
18
"github.com/pkg/errors"
20
19
"github.com/vmware/govmomi"
21
20
"github.com/vmware/govmomi/find"
22
21
"github.com/vmware/govmomi/object"
23
22
vapirest "github.com/vmware/govmomi/vapi/rest"
23
+ "github.com/vmware/govmomi/vapi/tags"
24
24
"github.com/vmware/govmomi/vim25"
25
25
"github.com/vmware/govmomi/vim25/mo"
26
26
"github.com/vmware/govmomi/vim25/soap"
@@ -29,6 +29,7 @@ import (
29
29
"k8s.io/client-go/rest"
30
30
31
31
"github.com/openshift/machine-api-operator/pkg/controller/vsphere"
32
+ util "github.com/openshift/machine-api-operator/test/e2e"
32
33
)
33
34
34
35
func isIpInCidrRange (ip string , cidr string ) (bool , error ) {
@@ -255,6 +256,7 @@ func getCredentialsForVCenter(ctx context.Context, vsphereCreds *corev1.Secret,
255
256
256
257
func GetPortGroupsAttachedToVMsInFailureDomain (
257
258
ctx context.Context ,
259
+ clusterID string ,
258
260
failureDomain configv1.VSpherePlatformFailureDomainSpec ,
259
261
vsphereCreds * corev1.Secret ,
260
262
vCenters []configv1.VSpherePlatformVCenterSpec ) (map [string ][]string , error ) {
@@ -281,7 +283,7 @@ func GetPortGroupsAttachedToVMsInFailureDomain(
281
283
282
284
portGroupMap := make (map [string ][]string )
283
285
284
- client , _ , logout , err := CreateVSphereClients (ctx , vcenterUrl , user , pass )
286
+ client , rClient , logout , err := CreateVSphereClients (ctx , vcenterUrl , user , pass )
285
287
defer logout ()
286
288
287
289
if err != nil {
@@ -290,17 +292,31 @@ func GetPortGroupsAttachedToVMsInFailureDomain(
290
292
291
293
finder := NewFinder (client )
292
294
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
+
293
305
vms , err := finder .VirtualMachineList (ctx , fmt .Sprintf ("/%s/vm/..." , failureDomain .Topology .Datacenter ))
294
306
if err != nil {
295
307
return nil , fmt .Errorf ("unable to get VMs in %s. %v" , failureDomain .Topology .ComputeCluster , err )
296
308
}
297
309
298
310
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
+
299
316
vm , err := finder .VirtualMachine (ctx , vm .InventoryPath )
300
317
if err != nil {
301
318
return nil , fmt .Errorf ("unable to get VMs in %s. %v" , failureDomain .Topology .ComputeCluster , err )
302
319
}
303
- fmt .Printf ("UUID: %s\n " , vm .UUID (ctx ))
304
320
virtualDevices , err := vm .Device (ctx )
305
321
306
322
var portGroups []string
0 commit comments