Class KubeVirt
java.lang.Object
io.jenkins.plugins.kubevirt.internal.KubeVirt
- All Implemented Interfaces:
AutoCloseable
Helper class for interacting with KubeVirt VirtualMachines on OpenShift/Kubernetes.
Uses the generic Kubernetes resource API to work with KubeVirt CRDs.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStatus information for a DataSource.static classStatus information for a DataVolume. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncheckDataSource(String dataSourceNamespace, String dataSourceName) Checks if a DataSource exists and is ready for cloning.voidclose()Closes the Kubernetes client.intcountVMsByCloud(String cloudName) Counts VMs in the namespace that belong to a specific cloud.intcountVMsByCloudAndTemplate(String cloudName, String templateName) Counts VMs in the namespace that belong to a specific cloud and template.voidcreateDataSource(String dataSourceNamespace, String dataSourceName, String imageUrl, String diskSize, String accessMode, ProvisioningCallback callback) Creates a DataSource from a container image registry URL.voidcreateDataSource(String dataSourceNamespace, String dataSourceName, String imageUrl, String diskSize, String accessMode, String storageClass, ProvisioningCallback callback) Creates a DataSource from a registry image using an optional Kubernetes storage class.voidcreateVM(String name, String cloudName, String templateName, String image, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine with a ContainerDisk image.voidcreateVM(String name, String cloudName, String templateName, String image, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine from a registry image using an optional storage class.voidcreateVMFromDataSource(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by cloning from a DataSource.voidcreateVMFromDataSource(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by cloning a DataSource using an optional storage class.voidcreateVMFromDataSourceImport(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String image, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by importing an image into a DataSource and then cloning from it.voidcreateVMFromDataSourceImport(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String image, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates or reuses an imported DataSource and clones a VM from it using an optional storage class.voidDeletes a KubeVirt VirtualMachine and its associated cloud-init Secret.io.fabric8.kubernetes.client.KubernetesClientgetDataVolumeStatus(String dvName) Gets the status of a DataVolume in the default (VM) namespace.getDataVolumeStatus(String dvName, String dvNamespace) Gets the status of a DataVolume in a specific namespace.getFullVMDiagnostics(String name) Gets a comprehensive status dump of a VM, useful for debugging provisioning failures.getNetworkDiagnostics(String name) Gets detailed diagnostic information about a VMI's network interfaces.getVMCreationTimestamp(String name) Gets the creation timestamp of a VM.Gets the IP address of a running VirtualMachineInstance.getVMStatus(String name) Gets the current status of a VirtualMachine as a human-readable string.getVMTemplateName(String name) Gets the template name from a VM's labels.booleanChecks if a VirtualMachineInstance is in Running state.listVMsByCloud(String cloudName) Lists VM names in the namespace that belong to a specific cloud and were created by this Jenkins controller.Tests the connection to the Kubernetes cluster and verifies KubeVirt is available.voidVerifies basic connectivity to the Kubernetes cluster.booleanwaitForDataSourceReady(String dataSourceNamespace, String dataSourceName, int timeoutSeconds, ProvisioningCallback callback) Waits for a DataSource to become ready with a timeout.
-
Constructor Details
-
KubeVirt
-
KubeVirt
public KubeVirt(String serverUrl, String token, String namespace, boolean ignoreSsl, int connectionTimeoutMs, int requestTimeoutMs) Creates a KubeVirt client with custom connection and request timeouts.- Parameters:
serverUrl- The Kubernetes API server URLtoken- The authentication tokennamespace- The Kubernetes namespaceignoreSsl- Whether to ignore SSL certificate errorsconnectionTimeoutMs- Connection timeout in milliseconds (0 = Fabric8 default)requestTimeoutMs- Request timeout in milliseconds (0 = Fabric8 default)
-
-
Method Details
-
getClient
public io.fabric8.kubernetes.client.KubernetesClient getClient() -
getNamespace
-
verifyConnection
Verifies basic connectivity to the Kubernetes cluster. This is a lightweight check that only fetches the server version.- Throws:
Exception- if the cluster is unreachable or not responding
-
checkDataSource
Checks if a DataSource exists and is ready for cloning.- Parameters:
dataSourceNamespace- Namespace of the DataSourcedataSourceName- Name of the DataSource- Returns:
- A status object indicating if the DataSource exists and is ready
-
createDataSource
public void createDataSource(String dataSourceNamespace, String dataSourceName, String imageUrl, String diskSize, String accessMode, ProvisioningCallback callback) Creates a DataSource from a container image registry URL. First creates a DataVolume to import the image, then creates a DataSource referencing it.- Parameters:
dataSourceNamespace- Namespace where to create the DataSourcedataSourceName- Name of the DataSourceimageUrl- Container image URL (e.g., "quay.io/containerdisks/fedora:40")diskSize- Size of the disk (e.g., "30Gi")accessMode- PVC access mode (e.g., "ReadWriteOnce" or "ReadWriteMany")callback- Callback for progress reporting
-
createDataSource
public void createDataSource(String dataSourceNamespace, String dataSourceName, String imageUrl, String diskSize, String accessMode, String storageClass, ProvisioningCallback callback) Creates a DataSource from a registry image using an optional Kubernetes storage class.- Parameters:
dataSourceNamespace- namespace where the DataSource is createddataSourceName- DataSource nameimageUrl- container image URLdiskSize- requested disk sizeaccessMode- PVC access modestorageClass- Kubernetes storage class, ornullto use the cluster defaultcallback- callback for progress reporting
-
waitForDataSourceReady
public boolean waitForDataSourceReady(String dataSourceNamespace, String dataSourceName, int timeoutSeconds, ProvisioningCallback callback) Waits for a DataSource to become ready with a timeout.- Parameters:
dataSourceNamespace- Namespace of the DataSourcedataSourceName- Name of the DataSourcetimeoutSeconds- Maximum time to wait in secondscallback- Callback for progress reporting- Returns:
- true if DataSource became ready, false if timeout
-
createVM
public void createVM(String name, String cloudName, String templateName, String image, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine with a ContainerDisk image. Uses a DataVolumeTemplate to import the container image into a PVC with configurable size.- Parameters:
name- VM namecloudName- The cloud name for labelingtemplateName- The template name for labelingimage- Container disk image (registry URL)diskSize- Size of the disk (e.g., "30Gi")accessMode- PVC access mode (ReadWriteOnce or ReadWriteMany)cpu- CPU requests (reserved)memory- Memory requests (reserved)cpuLimit- CPU limit (maximum), may be nullmemoryLimit- Memory limit (maximum), may be nulluserData- Cloud-init user datainjectTmpDiskConfig- Whether to inject /tmp disk configuration into cloud-initcallback- Callback for progress reporting
-
createVM
public void createVM(String name, String cloudName, String templateName, String image, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine from a registry image using an optional storage class.- Parameters:
name- VM namecloudName- cloud name used for labelingtemplateName- template name used for labelingimage- container disk imagediskSize- requested disk sizeaccessMode- PVC access modestorageClass- Kubernetes storage class, ornullto use the cluster defaultcpu- CPU requestmemory- memory requestcpuLimit- CPU limit, ornullmemoryLimit- memory limit, ornulluserData- cloud-init user datainjectTmpDiskConfig- whether to inject the ephemeral /tmp disk configurationcallback- callback for progress reporting
-
createVMFromDataSource
public void createVMFromDataSource(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by cloning from a DataSource.- Parameters:
name- VM namecloudName- The cloud name for labelingtemplateName- The template name for labelingdataSourceNamespace- Namespace of the DataSourcedataSourceName- Name of the DataSourcediskSize- Size of the cloned diskaccessMode- PVC access mode (ReadWriteOnce or ReadWriteMany)cpu- CPU requests (reserved)memory- Memory requests (reserved)cpuLimit- CPU limit (maximum), may be nullmemoryLimit- Memory limit (maximum), may be nulluserData- Cloud-init user datainjectTmpDiskConfig- Whether to inject /tmp disk configuration into cloud-initcallback- Callback for progress reporting
-
createVMFromDataSource
public void createVMFromDataSource(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by cloning a DataSource using an optional storage class.- Parameters:
name- VM namecloudName- cloud name used for labelingtemplateName- template name used for labelingdataSourceNamespace- namespace containing the DataSourcedataSourceName- DataSource namediskSize- requested disk sizeaccessMode- PVC access modestorageClass- Kubernetes storage class, ornullto use the cluster defaultcpu- CPU requestmemory- memory requestcpuLimit- CPU limit, ornullmemoryLimit- memory limit, ornulluserData- cloud-init user datainjectTmpDiskConfig- whether to inject the ephemeral /tmp disk configurationcallback- callback for progress reporting
-
createVMFromDataSourceImport
public void createVMFromDataSourceImport(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String image, String diskSize, String accessMode, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates a KubeVirt VirtualMachine by importing an image into a DataSource and then cloning from it. If a DataSource with the given name already exists and is ready, it is reused (cached). Otherwise, a new DataVolume is created to import the image, a DataSource is created referencing it, and the VM clones from that DataSource.- Parameters:
name- VM namecloudName- The cloud name for labelingtemplateName- The template name for labelingdataSourceNamespace- Namespace where the DataSource will be created or looked updataSourceName- Name of the DataSource to create or reuseimage- Container disk image (registry URL)diskSize- Size of the disk (e.g., "30Gi")accessMode- PVC access mode (ReadWriteOnce or ReadWriteMany)cpu- CPU requests (reserved)memory- Memory requests (reserved)cpuLimit- CPU limit (maximum), may be nullmemoryLimit- Memory limit (maximum), may be nulluserData- Cloud-init user datainjectTmpDiskConfig- Whether to inject /tmp disk configuration into cloud-initcallback- Callback for progress reporting
-
createVMFromDataSourceImport
public void createVMFromDataSourceImport(String name, String cloudName, String templateName, String dataSourceNamespace, String dataSourceName, String image, String diskSize, String accessMode, String storageClass, String cpu, String memory, String cpuLimit, String memoryLimit, String userData, boolean injectTmpDiskConfig, ProvisioningCallback callback) Creates or reuses an imported DataSource and clones a VM from it using an optional storage class.- Parameters:
name- VM namecloudName- cloud name used for labelingtemplateName- template name used for labelingdataSourceNamespace- namespace where the DataSource is created or founddataSourceName- DataSource nameimage- container disk image imported when the DataSource does not existdiskSize- requested disk sizeaccessMode- PVC access modestorageClass- Kubernetes storage class, ornullto use the cluster defaultcpu- CPU requestmemory- memory requestcpuLimit- CPU limit, ornullmemoryLimit- memory limit, ornulluserData- cloud-init user datainjectTmpDiskConfig- whether to inject the ephemeral /tmp disk configurationcallback- callback for progress reporting
-
getVMStatus
Gets the current status of a VirtualMachine as a human-readable string.- Parameters:
name- The VM name- Returns:
- Status string describing the current VM state
-
getDataVolumeStatus
Gets the status of a DataVolume in the default (VM) namespace.- Parameters:
dvName- The DataVolume name (typically vmName + "-disk")- Returns:
- A status object with phase, progress, and any error message
-
getDataVolumeStatus
Gets the status of a DataVolume in a specific namespace.- Parameters:
dvName- The DataVolume namedvNamespace- The namespace where the DataVolume resides- Returns:
- A status object with phase, progress, and any error message
-
deleteVM
Deletes a KubeVirt VirtualMachine and its associated cloud-init Secret. Validates ownership before deletion to prevent accidental removal of VMs belonging to other controllers, clouds, or templates.- Parameters:
name- VM name to deletecloudName- Expected cloud name for ownership validationtemplateName- Expected template name for ownership validation- Throws:
IllegalStateException- if the VM doesn't belong to this instance
-
countVMsByCloud
Counts VMs in the namespace that belong to a specific cloud. This counts VMs based on Kubernetes labels, which includes VMs that are still being provisioned and not yet registered as Jenkins nodes.- Parameters:
cloudName- The cloud name to filter by- Returns:
- The number of VMs with the matching cloud label
-
countVMsByCloudAndTemplate
Counts VMs in the namespace that belong to a specific cloud and template. This counts VMs based on Kubernetes labels, which includes VMs that are still being provisioned and not yet registered as Jenkins nodes.- Parameters:
cloudName- The cloud name to filter bytemplateName- The template name to filter by- Returns:
- The number of VMs with matching cloud and template labels
-
getVMCreationTimestamp
Gets the creation timestamp of a VM. Used for orphan detection to implement a grace period.- Parameters:
name- The VM name- Returns:
- The creation timestamp in ISO-8601 format, or null if not found
-
getVMTemplateName
Gets the template name from a VM's labels. Used for orphan detection to look up template-specific grace period.- Parameters:
name- The VM name- Returns:
- The template name from the VM's labels, or null if not found
-
listVMsByCloud
Lists VM names in the namespace that belong to a specific cloud and were created by this Jenkins controller. Both the cloud label and the controller label are required so that two Jenkins instances sharing the same KubeVirt cluster with the same cloud name cannot interfere with each other's VMs during orphan cleanup.If the controller label value cannot be determined (e.g. Jenkins root URL is not configured) the query falls back to filtering by cloud label only, which preserves the previous behaviour but logs a warning.
- Parameters:
cloudName- The cloud name to filter by- Returns:
- List of VM names matching both labels, empty list on error
-
getVMIp
Gets the IP address of a running VirtualMachineInstance. -
getNetworkDiagnostics
Gets detailed diagnostic information about a VMI's network interfaces. Useful for debugging why an IP address might not be assigned.- Parameters:
name- The VM name- Returns:
- Detailed interface information or a diagnostic message
-
getFullVMDiagnostics
Gets a comprehensive status dump of a VM, useful for debugging provisioning failures.- Parameters:
name- The VM name- Returns:
- Detailed status information
-
isVMReady
Checks if a VirtualMachineInstance is in Running state. -
testConnection
Tests the connection to the Kubernetes cluster and verifies KubeVirt is available. Returns a detailed message about the connection status.- Returns:
- A message describing the connection test result
- Throws:
Exception- if the connection test fails
-
close
public void close()Closes the Kubernetes client.- Specified by:
closein interfaceAutoCloseable
-