Skip to content

Fix/vmware vm discovery#1983

Open
OmkarDeshpande7 wants to merge 4 commits into
mainfrom
fix/vmware-vm-discovery
Open

Fix/vmware vm discovery#1983
OmkarDeshpande7 wants to merge 4 commits into
mainfrom
fix/vmware-vm-discovery

Conversation

@OmkarDeshpande7
Copy link
Copy Markdown
Collaborator

@OmkarDeshpande7 OmkarDeshpande7 commented May 31, 2026

What this PR does / why we need it

Problem
VMwareMachine objects were not being created for VMs in VMware environments. Controller logs showed "vm '*' not found" errors per datacenter.

Root cause: finder.VirtualMachineList(ctx, "*") only searches the root VM folder of a datacenter. VMs placed inside cluster or resource-pool sub-folders are invisible to this call — govmomi returns notFoundError, the code silently skipped the datacenter, and no VMs were discovered.

Changes
Recursive VM enumeration (k8s/migration/pkg/utils/credutils.go)

Extracted collectVMsFromDatacenters helper
Replaced finder.VirtualMachineList("*") with view.ContainerView (recursive=true) — the correct govmomi idiom for enumerating all VMs across nested folders
PropertyCollector fix (same file)

v.Retrieve(..., []string{"self"}, ...) caused ServerFaultCode: InvalidProperty — "self" is not a vSphere property name; govmomi auto-populates mo.Self from the object reference
Fixed by passing nil properties instead

Which issue(s) this PR fixes

(optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged)

fixes #1985

Special notes for your reviewer

Testing done

please add testing details (logs, screenshots, etc.)


Open in Devin Review

@github-actions
Copy link
Copy Markdown
Contributor

🚨 Security Vulnerability Summary

Security posture degraded

📊 Overall Changes

Metric Count
Total Added 3
Total Fixed 0
Net Change +3

🔍 Detailed Breakdown

📦 Gosec (Static Analysis)

Current Baseline Added Fixed Method
0 0 0 0 artifact

📦 Trivy (Dependency Scan)

Current Baseline Added Fixed Method
12 9 3 0 artifact

📋 Baseline Methods

  • 📦 artifact: Used stored report from main branch
  • 🔄 live_scan: Scanned base branch in real-time
  • ⚠️ no_baseline: No baseline available (all vulnerabilities treated as new)

🚨 Added Vulnerabilities

Trivy (Dependencies) - 3 Added

Target: ui/yarn.lock
Package: axios 1.13.2
Vulnerability: CVE-2026-44492
Severity: HIGH
Title: axios's shouldBypassProxy does not recognize IPv4-mapped IPv6 addresses, allowing NO_PROXY bypass (incomplete fix for CVE-2025-62718)

Target: ui/yarn.lock
Package: axios 1.13.2
Vulnerability: CVE-2026-44494
Severity: HIGH
Title: axios Vulnerable to Full Man-in-the-Middle via Prototype Pollution Gadget in config.proxy

Target: ui/yarn.lock
Package: axios 1.13.2
Vulnerability: CVE-2026-44495
Severity: HIGH
Title: axios Vulnerable to Credential Theft and Response Hijacking via Prototype Pollution Gadget in Config Merge


Only HIGH and CRITICAL severity vulnerabilities are tracked
Baseline: 505eecc6e70bea0563e5244b139e7243b4b2140c

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

View 4 additional findings in Devin Review.

Open in Devin Review

Comment on lines +639 to +641
for i := range moVMs {
vm := object.NewVirtualMachine(c, moVMs[i].Self)
vmToDatacenter[moVMs[i].Self.Value] = dcName
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 vm.Name() returns "." for all VMs because InventoryPath is not set on objects created via object.NewVirtualMachine

The new collectVMsFromDatacenters function creates VM objects via object.NewVirtualMachine(c, moVMs[i].Self) at line 640, which sets the managed object reference but leaves InventoryPath empty. In govmomi, object.Common.Name() returns path.Base(o.InventoryPath), and Go's path.Base("") returns ".". The downstream processSingleVM function calls vm.Name() extensively — most critically in RDM disk ownership tracking at k8s/migration/pkg/utils/credutils.go:1881 (rdmInfo.Spec.OwnerVMs = AppendUnique(rdmInfo.Spec.OwnerVMs, vm.Name())) and inside processVMDisk at line 982 (rdmDisk.Spec.OwnerVMs = []string{vmName}). This corrupts the OwnerVMs field stored in Kubernetes RDMDisk CRDs — all VMs will be recorded as "." instead of their actual names. The old code used finder.VirtualMachineList(ctx, "*") which returned VM objects with InventoryPath populated.

Prompt for agents
The bug is in collectVMsFromDatacenters (credutils.go around line 640). When creating VMs via object.NewVirtualMachine(c, moVMs[i].Self), the InventoryPath is not set on the resulting object. This means vm.Name() (which calls path.Base(o.InventoryPath)) returns "." for all VMs.

The fix needs to set the VM name on the object. Options:
1. After creating the VM object, fetch its name from the moVMs properties. Change the Retrieve call to also fetch the "name" property (not just "self"), then use vm.SetInventoryPath() or store the name separately.
2. Alternatively, retrieve ["self", "name"] in the ContainerView Retrieve call, and then set a path on the object: e.g. object.NewVirtualMachine returns a *VirtualMachine that embeds Common — you could set vm.InventoryPath to something that ends with the VM name.
3. Or pass vmProps.Config.Name (which IS correctly fetched later in processSingleVM) to processVMDisk instead of vm.Name(), and fix the other vm.Name() usages in processSingleVM to use vmProps.Config.Name.

Option 3 is probably simplest: in processSingleVM, replace all uses of vm.Name() with vmProps.Config.Name (which is populated from VM properties and is guaranteed correct). This requires moving the vm.Name() calls that happen before vmProps.Config is available (lines 1822, 1827) to use vm.Reference().Value or a fallback.
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@OmkarDeshpande7 OmkarDeshpande7 force-pushed the fix/vmware-vm-discovery branch from e0138eb to 463686a Compare June 1, 2026 05:58
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

✅ Security Vulnerability Summary

No change in security posture

📊 Overall Changes

Metric Count
Total Added 0
Total Fixed 0
Net Change 0

🔍 Detailed Breakdown

📦 Gosec (Static Analysis)

Current Baseline Added Fixed Method
0 0 0 0 artifact

📦 Trivy (Dependency Scan)

Current Baseline Added Fixed Method
12 12 0 0 artifact

📋 Baseline Methods

  • 📦 artifact: Used stored report from main branch
  • 🔄 live_scan: Scanned base branch in real-time
  • ⚠️ no_baseline: No baseline available (all vulnerabilities treated as new)

Only HIGH and CRITICAL severity vulnerabilities are tracked
Baseline: f834cba07bf03c81626828e880844026de70ada5

@OmkarDeshpande7 OmkarDeshpande7 force-pushed the fix/vmware-vm-discovery branch from 463686a to 7dff3db Compare June 1, 2026 07:01
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

✅ Security Vulnerability Summary

No change in security posture

📊 Overall Changes

Metric Count
Total Added 0
Total Fixed 0
Net Change 0

🔍 Detailed Breakdown

📦 Gosec (Static Analysis)

Current Baseline Added Fixed Method
0 0 0 0 artifact

📦 Trivy (Dependency Scan)

Current Baseline Added Fixed Method
12 12 0 0 artifact

📋 Baseline Methods

  • 📦 artifact: Used stored report from main branch
  • 🔄 live_scan: Scanned base branch in real-time
  • ⚠️ no_baseline: No baseline available (all vulnerabilities treated as new)

Only HIGH and CRITICAL severity vulnerabilities are tracked
Baseline: f834cba07bf03c81626828e880844026de70ada5

OmkarDeshpande7 and others added 4 commits June 1, 2026 14:27
Replace finder.VirtualMachineList("*") with a ContainerView-based
recursive enumeration so VMs inside cluster and resource-pool
sub-folders are discovered. "*" only searched the root VM folder,
causing "vm '*' not found" errors when all VMs lived in sub-folders.

Extract the datacenter-scanning loop into collectVMsFromDatacenters
to make the logic independently testable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
TestCollectVMsFromDatacenters_WithVMs: verifies VMs in cluster
sub-folders are found and mapped to the correct datacenter.

TestCollectVMsFromDatacenters_EmptyDatacenter: verifies an empty
datacenter returns an empty list without error, covering the
notFoundError handling that previously caused spurious ERROR logs
and skipped valid datacenters.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Require tests for every touched Go file (not just new code), and add
govmomi simulator + fake k8s client guidance for VMware/k8s tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… of "self"

vSphere PropertyCollector does not recognise "self" as a property name;
govmomi auto-populates mo.Self from the object reference when nil is
passed. Passing []string{"self"} caused ServerFaultCode: InvalidProperty
and silently skipped every datacenter, so no VMwareMachine objects were
created.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@meghansh-pf9 meghansh-pf9 force-pushed the fix/vmware-vm-discovery branch from 7dff3db to 65ff593 Compare June 1, 2026 08:57
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

✅ Security Vulnerability Summary

No change in security posture

📊 Overall Changes

Metric Count
Total Added 0
Total Fixed 0
Net Change 0

🔍 Detailed Breakdown

📦 Gosec (Static Analysis)

Current Baseline Added Fixed Method
0 0 0 0 artifact

📦 Trivy (Dependency Scan)

Current Baseline Added Fixed Method
12 12 0 0 artifact

📋 Baseline Methods

  • 📦 artifact: Used stored report from main branch
  • 🔄 live_scan: Scanned base branch in real-time
  • ⚠️ no_baseline: No baseline available (all vulnerabilities treated as new)

Only HIGH and CRITICAL severity vulnerabilities are tracked
Baseline: f834cba07bf03c81626828e880844026de70ada5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No VMs listed for a datacenter

2 participants