Skip to content

Conversation

@whatever125
Copy link

Added detection of the virtualization platform on x86/arm64 archs using klauspost/cpuid lib.

  • Preserved existing s390x implementation using /proc/sysinfo.
  • On x86 and arm64 cpu.model.hypervisor now reports hypervisor type, or none for bare-metal, and raw CPUID string for unknown hypervisors.

Resolves #2267

@netlify
Copy link

netlify bot commented Dec 7, 2025

Deploy Preview for kubernetes-sigs-nfd ready!

Name Link
🔨 Latest commit 6a0f931
🔍 Latest deploy log https://app.netlify.com/projects/kubernetes-sigs-nfd/deploys/693da432e831550008375fe4
😎 Deploy Preview https://deploy-preview-2388--kubernetes-sigs-nfd.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 7, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @whatever125!

It looks like this is your first PR to kubernetes-sigs/node-feature-discovery 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/node-feature-discovery has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 7, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @whatever125. Thanks for your PR.

I'm waiting for a github.com member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 7, 2025
Copy link
Contributor

@marquiz marquiz left a comment

Choose a reason for hiding this comment

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

Thanks @whatever125. A few comments below

Comment on lines 313 to 315
hv := strings.ToLower(cpuid.CPU.HypervisorVendorID.String())

switch hv {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
hv := strings.ToLower(cpuid.CPU.HypervisorVendorID.String())
switch hv {
switch hv := strings.ToLower(cpuid.CPU.HypervisorVendorID.String()); hv {

Comment on lines 326 to 335
case "qemu":
return "qemu", nil
case "apple":
return "apple", nil
case "qnx":
return "qnx", nil
case "acrn":
return "acrn", nil
case "sre":
return "sre", nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't we put all these cases where we don't mangle the value under the default case?

Suggested change
case "qemu":
return "qemu", nil
case "apple":
return "apple", nil
case "qnx":
return "qnx", nil
case "acrn":
return "acrn", nil
case "sre":
return "sre", nil
default:
return hv, nil

Comment on lines 283 to 284
if _, err := os.Stat("/proc/sysinfo"); err == nil {
data, err := os.ReadFile("/proc/sysinfo")
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would make the code more maintainable and readable to split it into multiple functions, something like:

Suggested change
if _, err := os.Stat("/proc/sysinfo"); err == nil {
data, err := os.ReadFile("/proc/sysinfo")
if _, err := os.Stat("/proc/sysinfo"); err == nil {
return getHypervisorFromProcSysinfo
} else {
klog.Error(err, "failed to stat /proc/sysinfo")
}
if cpuid.CPU.VM() {
return getHyperVisorFromCPUID(), nil
}
return "none", nil

Also, if the stat fails, we might still want to fall back to the cpuid method (like above). Thoughts?

@marquiz
Copy link
Contributor

marquiz commented Dec 9, 2025

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 9, 2025
@whatever125
Copy link
Author

Thank you for the feedback @marquiz. I've updated the code as you suggested:

  1. Split into helper functions, updated fallback logic
  2. Used default in switch case. Now only cpuid.VendorUnknown is treated separately to get vendor from cpuid.CPU.HypervisorVendorString. Otherwise, the default cpuid.CPU.HypervisorVendorID.String() is used
  3. Got rid of string handling for simplicity and maintainability

hv, discovered := getHypervisorFromProcSysinfo()
if discovered {
return hv, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If this does not find anything (discovered == false), do we want to fall back to getHypervisorFromCPUID()? Probably not, or WDYT?

Copy link
Author

@whatever125 whatever125 Dec 9, 2025

Choose a reason for hiding this comment

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

after digging deeper, i've reconsidered the logic:

  • PR/SM virtualization is always present on s390x, so it should be the default fallback value (consistent with the original code)
  • /proc/sysinfo exists only on s390x. its absence means another arch -> go to cpuid
  • not being able to read it -> just log and return the error
  • cpuid doesn't support s390x, so we shouldn't fall back to it in any case
  • parsing /proc/sysinfo and not finding "Control Program:" means PR/SM virtualization by default -> return "PR/SM", not a blank string

so this is what i propose:

func getHypervisor() (string, error) {
	// use /proc/sysinfo for s390x
	if _, err := os.Stat("/proc/sysinfo"); err == nil {
		return getHypervisorFromProcSysinfo()
	}
	// use cpuid lib for x86/arm
	if cpuid.CPU.VM() {
		return getHypervisorFromCPUID()
	}
	// no hv discovered
	return "none", nil
}

and then the getHypervisorFromProcSysinfo() is just the old getHypervisor().

maybe we could use runtime.GOARCH == "s390x" for a cleaner arch detection instead of doing stat on /proc/sysinfo. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we could use runtime.GOARCH == "s390x" for a cleaner arch detection instead of doing stat on /proc/sysinfo. what do you think?

That works 👍 Would make sense with the fact that /proc/sysinfo only exists on s390x

@whatever125
Copy link
Author

@marquiz thanks! i've updated the code to use runtime.GOARCH and implemented the logic we've discussed

@whatever125 whatever125 force-pushed the feat/cpu-detect-hypervisor-via-cpuid branch from 52a922f to 6a0f931 Compare December 13, 2025 17:36
Copy link
Contributor

@marquiz marquiz left a comment

Choose a reason for hiding this comment

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

Nice @whatever125. This became really clean and simple.

/assign @ArangoGutierrez @fmuyassarov

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: marquiz, whatever125

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] list virtualization type

5 participants