kubevirt

package
v0.0.59 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultInstancetypeLabel = "instancetype.kubevirt.io/default-instancetype"
	DefaultPreferenceLabel   = "instancetype.kubevirt.io/default-preference"
)

Variables

View Source
var (
	// VirtualMachineGVK is the GroupVersionKind for VirtualMachine resources
	VirtualMachineGVK = schema.GroupVersionKind{
		Group:   "kubevirt.io",
		Version: "v1",
		Kind:    "VirtualMachine",
	}

	// VirtualMachineGVR is the GroupVersionResource for VirtualMachine resources
	VirtualMachineGVR = schema.GroupVersionResource{
		Group:    "kubevirt.io",
		Version:  "v1",
		Resource: "virtualmachines",
	}

	// VirtualMachineInstanceGVR is the GroupVersionResource for VirtualMachineInstance resources
	VirtualMachineInstanceGVR = schema.GroupVersionResource{
		Group:    "kubevirt.io",
		Version:  "v1",
		Resource: "virtualmachineinstances",
	}
)

KubeVirt core resources

View Source
var (
	// DataVolumeGVR is the GroupVersionResource for DataVolume resources
	DataVolumeGVR = schema.GroupVersionResource{
		Group:    "cdi.kubevirt.io",
		Version:  "v1beta1",
		Resource: "datavolumes",
	}

	// DataSourceGVR is the GroupVersionResource for DataSource resources
	DataSourceGVR = schema.GroupVersionResource{
		Group:    "cdi.kubevirt.io",
		Version:  "v1beta1",
		Resource: "datasources",
	}
)

CDI (Containerized Data Importer) resources

View Source
var (
	// VirtualMachineClusterInstancetypeGVR is the GroupVersionResource for cluster-scoped VirtualMachineClusterInstancetype resources
	VirtualMachineClusterInstancetypeGVR = schema.GroupVersionResource{
		Group:    "instancetype.kubevirt.io",
		Version:  "v1beta1",
		Resource: "virtualmachineclusterinstancetypes",
	}

	// VirtualMachineInstancetypeGVR is the GroupVersionResource for namespaced VirtualMachineInstancetype resources
	VirtualMachineInstancetypeGVR = schema.GroupVersionResource{
		Group:    "instancetype.kubevirt.io",
		Version:  "v1beta1",
		Resource: "virtualmachineinstancetypes",
	}
)

Instancetype resources

View Source
var (
	// VirtualMachineClusterPreferenceGVR is the GroupVersionResource for cluster-scoped VirtualMachineClusterPreference resources
	VirtualMachineClusterPreferenceGVR = schema.GroupVersionResource{
		Group:    "instancetype.kubevirt.io",
		Version:  "v1beta1",
		Resource: "virtualmachineclusterpreferences",
	}

	// VirtualMachinePreferenceGVR is the GroupVersionResource for namespaced VirtualMachinePreference resources
	VirtualMachinePreferenceGVR = schema.GroupVersionResource{
		Group:    "instancetype.kubevirt.io",
		Version:  "v1beta1",
		Resource: "virtualmachinepreferences",
	}
)

Preference resources

View Source
var (
	// PersistentVolumeClaimGVR is the GroupVersionResource for PersistentVolumeClaim resources
	PersistentVolumeClaimGVR = schema.GroupVersionResource{
		Group:    "",
		Version:  "v1",
		Resource: "persistentvolumeclaims",
	}

	// PodGVR is the GroupVersionResource for Pod resources
	PodGVR = schema.GroupVersionResource{
		Group:    "",
		Version:  "v1",
		Resource: "pods",
	}
)

Kubernetes core resources

View Source
var (
	// VirtualMachineCloneGVR is the GroupVersionResource for VirtualMachineClone resources
	VirtualMachineCloneGVR = schema.GroupVersionResource{
		Group:    "clone.kubevirt.io",
		Version:  "v1beta1",
		Resource: "virtualmachineclones",
	}
)

Clone resources

Functions

func CloneVM added in v0.0.58

func CloneVM(ctx context.Context, dynamicClient dynamic.Interface, namespace, sourceName, targetName string) (*unstructured.Unstructured, error)

CloneVM creates a VirtualMachineClone CR to clone a source VM to a target VM

func ExtractDataSourceInfo

func ExtractDataSourceInfo(obj *unstructured.Unstructured) string

ExtractDataSourceInfo extracts source information from a DataSource object.

Supports multiple source types:

  • PVC: Returns "PVC: namespace/name" or "PVC: name"
  • Registry: Returns "Registry: url"
  • HTTP: Returns "HTTP: url"

Parameters:

  • obj: Unstructured DataSource object

Returns a human-readable string describing the source, or "unknown source"/"DataSource (type unknown)" if the source cannot be determined.

func GetVirtualMachine added in v0.0.56

func GetVirtualMachine(ctx context.Context, client dynamic.Interface, namespace, name string) (*unstructured.Unstructured, error)

GetVirtualMachine retrieves a VirtualMachine by namespace and name

func MatchInstancetypeBySize

func MatchInstancetypeBySize(instancetypes []InstancetypeInfo, size, performance string) string

MatchInstancetypeBySize finds an instancetype that matches the size and performance hints.

Matching strategy:

  1. Filter instancetypes by size (e.g., "medium" matches "*.medium")
  2. Try to match by performance family prefix (e.g., "c1" matches "c1.medium")
  3. Try to match by performance family label (instancetype.kubevirt.io/class)
  4. Fall back to first instancetype that matches size

Parameters:

  • instancetypes: List of available instancetypes
  • size: Size hint (e.g., "small", "medium", "large")
  • performance: Performance class hint (e.g., "u1", "c1", "m1")

Returns the matched instancetype name, or empty string if no match found.

func RestartVM added in v0.0.56

func RestartVM(ctx context.Context, dynamicClient dynamic.Interface, namespace, name string) (*unstructured.Unstructured, error)

RestartVM restarts a VirtualMachine by temporarily setting runStrategy to Halted then back to Always

func SearchDataSources

func SearchDataSources(ctx context.Context, dynamicClient dynamic.Interface) map[string]DataSourceInfo

SearchDataSources searches for DataSource resources in the cluster.

It searches in well-known namespaces first (openshift-virtualization-os-images, kubevirt-os-images), then performs a cluster-wide search. Duplicate DataSources are filtered by namespace/name key.

Returns a map of DataSourceInfo indexed by "namespace/name". If no DataSources are found, returns a placeholder entry indicating no sources are available.

func SetVMRunStrategy added in v0.0.56

func SetVMRunStrategy(vm *unstructured.Unstructured, strategy RunStrategy) error

SetVMRunStrategy sets the runStrategy on a VirtualMachine

func StartVM added in v0.0.56

func StartVM(ctx context.Context, dynamicClient dynamic.Interface, namespace, name string) (*unstructured.Unstructured, bool, error)

StartVM starts a VirtualMachine by updating its runStrategy to Always Returns the updated VM and true if the VM was started, false if it was already running

func StopVM added in v0.0.56

func StopVM(ctx context.Context, dynamicClient dynamic.Interface, namespace, name string) (*unstructured.Unstructured, bool, error)

StopVM stops a VirtualMachine by updating its runStrategy to Halted Returns the updated VM and true if the VM was stopped, false if it was already stopped

func UpdateVirtualMachine added in v0.0.56

func UpdateVirtualMachine(ctx context.Context, client dynamic.Interface, vm *unstructured.Unstructured) (*unstructured.Unstructured, error)

UpdateVirtualMachine updates a VirtualMachine in the cluster

Types

type DataSourceInfo

type DataSourceInfo struct {
	Name                string
	Namespace           string
	Source              string
	DefaultInstancetype string
	DefaultPreference   string
}

DataSourceInfo contains information about a KubeVirt DataSource

func MatchDataSource

func MatchDataSource(dataSources map[string]DataSourceInfo, workload string) *DataSourceInfo

MatchDataSource finds a DataSource that matches the workload input.

Matching strategy:

  1. Exact name match (case-insensitive)
  2. Partial match for DataSources with namespaces (real cluster resources) e.g., "rhel" matches "rhel9"

Built-in containerdisks (without namespaces) are excluded from partial matching to avoid ambiguous matches.

Parameters:

  • dataSources: Map of available DataSources keyed by "namespace/name"
  • workload: User input (OS name, DataSource name, or container image)

Returns a pointer to matched DataSourceInfo, or nil if no match found.

type InstancetypeInfo

type InstancetypeInfo struct {
	Name      string
	Namespace string // Empty for cluster-scoped instancetypes
	Labels    map[string]string
}

InstancetypeInfo contains information about a VirtualMachineInstancetype

func FilterInstancetypesBySize

func FilterInstancetypesBySize(instancetypes []InstancetypeInfo, normalizedSize string) []InstancetypeInfo

FilterInstancetypesBySize filters instancetypes that contain the size hint in their name.

Parameters:

  • instancetypes: List of available instancetypes
  • normalizedSize: Lowercase size hint (e.g., "small", "medium", "large")

Returns a filtered list of instancetypes whose names contain the size string. For example, "medium" matches "u1.medium", "c1.medium", etc.

func ResolveInstancetype

func ResolveInstancetype(instancetypes []InstancetypeInfo, explicitInstancetype, size, performance string, matchedDataSource *DataSourceInfo) *InstancetypeInfo

ResolveInstancetype determines the instancetype to use from DataSource defaults or size/performance hints.

Resolution priority:

  1. Explicit instancetype parameter (if provided)
  2. DataSource default instancetype (if DataSource matched, has default, and no size specified)
  3. Auto-match by size and performance hints e.g., size="large" + performance="c1" matches "c1.large"

Parameters:

  • instancetypes: List of available instancetypes from the cluster
  • explicitInstancetype: User-specified instancetype name (may be empty)
  • size: Size hint (e.g., "small", "medium", "large") - may be empty
  • performance: Performance class hint (e.g., "u1", "c1", "m1") - may be empty
  • matchedDataSource: Matched DataSource (may be nil)

Returns a pointer to InstancetypeInfo with name and scope, or nil if no match found. If an instancetype name is provided but not found in available instancetypes, assumes it's cluster-scoped.

func SearchInstancetypes

func SearchInstancetypes(ctx context.Context, dynamicClient dynamic.Interface, namespace string) []InstancetypeInfo

SearchInstancetypes searches for both cluster-wide and namespaced VirtualMachineInstancetype resources.

It queries both VirtualMachineClusterInstancetypes (cluster-scoped) and VirtualMachineInstancetypes (namespaced) resources. Each InstancetypeInfo includes a Namespace field that is empty for cluster-scoped resources, plus Labels for filtering by performance class.

Parameters:

  • ctx: Context for the API calls
  • dynamicClient: Kubernetes dynamic client
  • namespace: Namespace to search for namespaced instancetypes

Returns a list of InstancetypeInfo objects. Returns empty list if no instancetypes found or if API calls fail.

type PreferenceInfo

type PreferenceInfo struct {
	Name      string
	Namespace string // Empty for cluster-scoped preferences
}

PreferenceInfo contains information about a VirtualMachinePreference

func ResolvePreference

func ResolvePreference(preferences []PreferenceInfo, explicitPreference, workload string, matchedDataSource *DataSourceInfo) *PreferenceInfo

ResolvePreference determines the preference to use from DataSource defaults or cluster resources.

Resolution priority:

  1. Explicit preference parameter (if provided)
  2. DataSource default preference (if DataSource matched and has default)
  3. Auto-match preference name against workload input e.g., "rhel" matches "rhel.9"

Parameters:

  • preferences: List of available preferences from the cluster
  • explicitPreference: User-specified preference name (may be empty)
  • workload: Workload/OS name used for auto-matching
  • matchedDataSource: Matched DataSource (may be nil)

Returns a pointer to PreferenceInfo with name and scope, or nil if no match found. If a preference name is provided but not found in available preferences, assumes it's cluster-scoped.

func SearchPreferences

func SearchPreferences(ctx context.Context, dynamicClient dynamic.Interface, namespace string) []PreferenceInfo

SearchPreferences searches for both cluster-wide and namespaced VirtualMachinePreference resources.

It queries both VirtualMachineClusterPreferences (cluster-scoped) and VirtualMachinePreferences (namespaced) resources. Each PreferenceInfo includes a Namespace field that is empty for cluster-scoped resources.

Parameters:

  • ctx: Context for the API calls
  • dynamicClient: Kubernetes dynamic client
  • namespace: Namespace to search for namespaced preferences

Returns a list of PreferenceInfo objects. Returns empty list if no preferences found or if API calls fail.

type RunStrategy added in v0.0.56

type RunStrategy string

RunStrategy represents the run strategy for a VirtualMachine

const (
	RunStrategyAlways RunStrategy = "Always"
	RunStrategyHalted RunStrategy = "Halted"
)

func GetVMRunStrategy added in v0.0.56

func GetVMRunStrategy(vm *unstructured.Unstructured) (RunStrategy, bool, error)

GetVMRunStrategy retrieves the current runStrategy from a VirtualMachine Returns the strategy, whether it was found, and any error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL