-
Notifications
You must be signed in to change notification settings - Fork 21
Add authenticated host capabilities endpoint #338
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
Open
rgarcia
wants to merge
9
commits into
main
Choose a base branch
from
oss/host-capabilities-safe-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9bad15c
Add host capabilities endpoint and secret-safe diagnostics
rgarcia 56ba0f0
Zero runtime features when the default runtime can't run on the host
rgarcia 8223b51
Enforce owner-only build configs on rewrite and at startup
rgarcia 62db5d3
Treat all-sentinel env patches as no env update
rgarcia ba280a6
Sweep legacy guest config disks to owner-only at startup
rgarcia 0f6f74e
Report the vz NAT gateway for macOS capabilities
rgarcia 63ca479
Chmod build metadata temp file before rename
rgarcia 7632089
Narrow PR to host-capability contract only
rgarcia 328e8a7
Remove unrelated endpoint compatibility tests
rgarcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime" | ||
| "slices" | ||
| "sync" | ||
|
|
||
| "github.com/kernel/hypeman/lib/hypervisor" | ||
| "github.com/kernel/hypeman/lib/images" | ||
| "github.com/kernel/hypeman/lib/logger" | ||
| "github.com/kernel/hypeman/lib/network" | ||
| "github.com/kernel/hypeman/lib/oapi" | ||
| ) | ||
|
|
||
| // Stable feature IDs reported by the capabilities endpoint. Clients gate | ||
| // behavior on these (and the structured runtime booleans) rather than on | ||
| // hypervisor names. | ||
| const ( | ||
| featureInstances = "instances" | ||
| featureImages = "images" | ||
| featureBuilds = "builds" | ||
| featureVolumes = "volumes" | ||
| featureIngress = "ingress" | ||
| featureExec = "exec" | ||
| featureLogs = "logs" | ||
| featureStandby = "standby" | ||
| featureSnapshots = "snapshots" | ||
| featureFork = "fork" | ||
| featurePause = "pause" | ||
| featureHotplugMemory = "hotplug-memory" | ||
| featureBalloonControl = "balloon-control" | ||
| featureVsock = "vsock" | ||
| featureGPUPassthrough = "gpu-passthrough" | ||
| featureDiskIOLimit = "disk-io-limit" | ||
| featureDiskResize = "disk-resize" | ||
| featureDevices = "devices" | ||
| featureRosettaEmulation = "rosetta-emulation" | ||
| ) | ||
|
|
||
| // apiVersion is the API contract version from the embedded OpenAPI document. | ||
| // The decoded spec is cached: decoding it per request is needlessly expensive. | ||
| var apiVersion = sync.OnceValue(func() string { | ||
| spec, err := oapi.GetSwagger() | ||
| if err != nil || spec.Info == nil { | ||
| return "unknown" | ||
| } | ||
| return spec.Info.Version | ||
| }) | ||
|
|
||
| // GetCapabilities reports host, runtime, network, and image capabilities. | ||
| func (s *ApiService) GetCapabilities(ctx context.Context, _ oapi.GetCapabilitiesRequestObject) (oapi.GetCapabilitiesResponseObject, error) { | ||
| log := logger.FromContext(ctx) | ||
|
|
||
| defaultRuntime := hypervisor.TypeCloudHypervisor | ||
| if s.InstanceManager != nil { | ||
| defaultRuntime = s.InstanceManager.DefaultHypervisor() | ||
| } | ||
| supported := supportedRuntimes(runtime.GOOS) | ||
| caps, capsKnown := hypervisor.CapabilitiesForType(defaultRuntime) | ||
| if capsKnown && !slices.Contains(supported, string(defaultRuntime)) { | ||
| // The configured default runtime cannot run on this host platform; | ||
| // advertising its features would overstate support. | ||
| capsKnown = false | ||
| } | ||
| if !capsKnown { | ||
| // Report zeroed features rather than guessing. | ||
| log.WarnContext(ctx, "default runtime is not usable on this host platform", | ||
| "runtime", string(defaultRuntime), "host_os", runtime.GOOS) | ||
| caps = hypervisor.Capabilities{} | ||
| } | ||
|
|
||
| emulation := emulationSupported(runtime.GOOS, runtime.GOARCH, defaultRuntime) | ||
|
|
||
| networkCaps, err := s.networkCapabilities(ctx) | ||
| if err != nil { | ||
| log.ErrorContext(ctx, "failed to resolve network capabilities", "error", err) | ||
| return oapi.GetCapabilities500JSONResponse{ | ||
| Code: "internal_error", | ||
| Message: "failed to resolve network capabilities", | ||
| }, nil | ||
| } | ||
|
|
||
| resp := oapi.Capabilities{ | ||
| Server: oapi.CapabilitiesServer{ | ||
| Version: s.Config.Version, | ||
| ApiVersion: apiVersion(), | ||
| }, | ||
| Host: oapi.CapabilitiesHost{ | ||
| Os: runtime.GOOS, | ||
| Arch: runtime.GOARCH, | ||
| }, | ||
| Runtime: oapi.CapabilitiesRuntime{ | ||
| Default: string(defaultRuntime), | ||
| Supported: supported, | ||
| Snapshot: caps.SupportsSnapshot, | ||
| Standby: standbySupported(caps), | ||
| Pause: caps.SupportsPause, | ||
| HotplugMemory: caps.SupportsHotplugMemory, | ||
| BalloonControl: caps.SupportsBalloonControl, | ||
| Vsock: caps.SupportsVsock, | ||
| GpuPassthrough: caps.SupportsGPUPassthrough, | ||
| DiskIoLimit: caps.SupportsDiskIOLimit, | ||
| DiskResize: caps.SupportsDiskResize, | ||
| }, | ||
| Network: *networkCaps, | ||
| Images: oapi.CapabilitiesImages{ | ||
| Platforms: imagePlatforms(runtime.GOARCH, emulation), | ||
| DefaultPlatform: images.HostPlatformString(), | ||
| }, | ||
| Features: assembleFeatures(runtime.GOOS, caps, emulation), | ||
| } | ||
|
|
||
| return oapi.GetCapabilities200JSONResponse(resp), nil | ||
| } | ||
|
|
||
| // networkCapabilities resolves the guest networking model and the | ||
| // guest-visible host gateway from the network manager's effective default | ||
| // network. | ||
| func (s *ApiService) networkCapabilities(ctx context.Context) (*oapi.CapabilitiesNetwork, error) { | ||
| caps := &oapi.CapabilitiesNetwork{ | ||
| Model: oapi.CapabilitiesNetworkModel(network.NetworkModel()), | ||
| GuestToGuest: false, | ||
| } | ||
| if s.NetworkManager == nil { | ||
| return caps, nil | ||
| } | ||
| nw, err := s.NetworkManager.DefaultNetwork(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if nw == nil { | ||
| return caps, nil | ||
| } | ||
| caps.Gateway = nw.Gateway | ||
| if nw.Subnet != "" { | ||
| subnet := nw.Subnet | ||
| caps.Subnet = &subnet | ||
| } | ||
| caps.GuestToGuest = network.GuestToGuestEnabled(nw) | ||
| return caps, nil | ||
| } | ||
|
|
||
| // standbySupported reports whether standby (pause + memory snapshot) and | ||
| // later restore are supported. Standby requires both snapshot and pause. | ||
| func standbySupported(caps hypervisor.Capabilities) bool { | ||
| return caps.SupportsSnapshot && caps.SupportsPause | ||
| } | ||
|
|
||
| // supportedRuntimes returns the runtime identifiers usable on a host OS. | ||
| // This is a platform floor, not a registry listing: a runtime whose package | ||
| // registered capabilities but that cannot run on the host (e.g. firecracker | ||
| // on macOS) is not listed. | ||
| func supportedRuntimes(goos string) []string { | ||
| switch goos { | ||
| case "darwin": | ||
| return []string{string(hypervisor.TypeVZ)} | ||
| default: | ||
| return []string{ | ||
| string(hypervisor.TypeCloudHypervisor), | ||
| string(hypervisor.TypeFirecracker), | ||
| string(hypervisor.TypeQEMU), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // emulationSupported reports whether the host can boot images built for the | ||
| // other CPU architecture. This mirrors the create-path rule for attaching | ||
| // the Rosetta share: vz on Apple Silicon macOS. | ||
| func emulationSupported(goos, goarch string, defaultRuntime hypervisor.Type) bool { | ||
| return defaultRuntime == hypervisor.TypeVZ && goos == "darwin" && goarch == "arm64" | ||
| } | ||
|
|
||
| // imagePlatforms returns the image platforms (os/arch) the host can run: | ||
| // the host-native platform plus the emulated one when available. | ||
| func imagePlatforms(goarch string, emulation bool) []string { | ||
| if goarch == "" { | ||
| goarch = runtime.GOARCH | ||
| } | ||
| platforms := []string{"linux/" + goarch} | ||
| if emulation { | ||
| switch goarch { | ||
| case "arm64": | ||
| platforms = append(platforms, "linux/amd64") | ||
| case "amd64": | ||
| platforms = append(platforms, "linux/arm64") | ||
| } | ||
| } | ||
| return platforms | ||
| } | ||
|
|
||
| // assembleFeatures builds the stable feature ID list: always-present base API | ||
| // surfaces plus conditional entries derived from the effective default | ||
| // runtime's capabilities and the host platform. | ||
| func assembleFeatures(goos string, caps hypervisor.Capabilities, emulation bool) []string { | ||
| features := []string{ | ||
| featureInstances, | ||
| featureImages, | ||
| featureBuilds, | ||
| featureVolumes, | ||
| featureIngress, | ||
| featureExec, | ||
| featureLogs, | ||
| } | ||
| if standbySupported(caps) { | ||
| features = append(features, featureStandby) | ||
| } | ||
| if caps.SupportsSnapshot { | ||
| features = append(features, featureSnapshots, featureFork) | ||
| } | ||
| if caps.SupportsPause { | ||
| features = append(features, featurePause) | ||
| } | ||
| if caps.SupportsHotplugMemory { | ||
| features = append(features, featureHotplugMemory) | ||
| } | ||
| if caps.SupportsBalloonControl { | ||
| features = append(features, featureBalloonControl) | ||
| } | ||
| if caps.SupportsVsock { | ||
| features = append(features, featureVsock) | ||
| } | ||
| if caps.SupportsGPUPassthrough { | ||
| features = append(features, featureGPUPassthrough) | ||
| } | ||
| if caps.SupportsDiskIOLimit { | ||
| features = append(features, featureDiskIOLimit) | ||
| } | ||
| if caps.SupportsDiskResize { | ||
| features = append(features, featureDiskResize) | ||
| } | ||
| // Device passthrough (GPU/PCI) is only meaningful on Linux hosts. | ||
| if goos == "linux" { | ||
| features = append(features, featureDevices) | ||
| } | ||
| if emulation { | ||
| features = append(features, featureRosettaEmulation) | ||
| } | ||
| return features | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.