Class KubeVirt

java.lang.Object
io.jenkins.plugins.kubevirt.internal.KubeVirt
All Implemented Interfaces:
AutoCloseable

public class KubeVirt extends Object implements AutoCloseable
Helper class for interacting with KubeVirt VirtualMachines on OpenShift/Kubernetes. Uses the generic Kubernetes resource API to work with KubeVirt CRDs.
  • Constructor Details

    • KubeVirt

      public KubeVirt(String serverUrl, String token, String namespace, boolean ignoreSsl)
    • 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 URL
      token - The authentication token
      namespace - The Kubernetes namespace
      ignoreSsl - Whether to ignore SSL certificate errors
      connectionTimeoutMs - 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

      public String getNamespace()
    • verifyConnection

      public void verifyConnection() throws Exception
      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

      public KubeVirt.DataSourceStatus checkDataSource(String dataSourceNamespace, String dataSourceName)
      Checks if a DataSource exists and is ready for cloning.
      Parameters:
      dataSourceNamespace - Namespace of the DataSource
      dataSourceName - 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 DataSource
      dataSourceName - Name of the DataSource
      imageUrl - 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 created
      dataSourceName - DataSource name
      imageUrl - container image URL
      diskSize - requested disk size
      accessMode - PVC access mode
      storageClass - Kubernetes storage class, or null to use the cluster default
      callback - 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 DataSource
      dataSourceName - Name of the DataSource
      timeoutSeconds - Maximum time to wait in seconds
      callback - 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 name
      cloudName - The cloud name for labeling
      templateName - The template name for labeling
      image - 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 null
      memoryLimit - Memory limit (maximum), may be null
      userData - Cloud-init user data
      injectTmpDiskConfig - Whether to inject /tmp disk configuration into cloud-init
      callback - 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 name
      cloudName - cloud name used for labeling
      templateName - template name used for labeling
      image - container disk image
      diskSize - requested disk size
      accessMode - PVC access mode
      storageClass - Kubernetes storage class, or null to use the cluster default
      cpu - CPU request
      memory - memory request
      cpuLimit - CPU limit, or null
      memoryLimit - memory limit, or null
      userData - cloud-init user data
      injectTmpDiskConfig - whether to inject the ephemeral /tmp disk configuration
      callback - 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 name
      cloudName - The cloud name for labeling
      templateName - The template name for labeling
      dataSourceNamespace - Namespace of the DataSource
      dataSourceName - Name of the DataSource
      diskSize - Size of the cloned disk
      accessMode - PVC access mode (ReadWriteOnce or ReadWriteMany)
      cpu - CPU requests (reserved)
      memory - Memory requests (reserved)
      cpuLimit - CPU limit (maximum), may be null
      memoryLimit - Memory limit (maximum), may be null
      userData - Cloud-init user data
      injectTmpDiskConfig - Whether to inject /tmp disk configuration into cloud-init
      callback - 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 name
      cloudName - cloud name used for labeling
      templateName - template name used for labeling
      dataSourceNamespace - namespace containing the DataSource
      dataSourceName - DataSource name
      diskSize - requested disk size
      accessMode - PVC access mode
      storageClass - Kubernetes storage class, or null to use the cluster default
      cpu - CPU request
      memory - memory request
      cpuLimit - CPU limit, or null
      memoryLimit - memory limit, or null
      userData - cloud-init user data
      injectTmpDiskConfig - whether to inject the ephemeral /tmp disk configuration
      callback - 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 name
      cloudName - The cloud name for labeling
      templateName - The template name for labeling
      dataSourceNamespace - Namespace where the DataSource will be created or looked up
      dataSourceName - Name of the DataSource to create or reuse
      image - 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 null
      memoryLimit - Memory limit (maximum), may be null
      userData - Cloud-init user data
      injectTmpDiskConfig - Whether to inject /tmp disk configuration into cloud-init
      callback - 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 name
      cloudName - cloud name used for labeling
      templateName - template name used for labeling
      dataSourceNamespace - namespace where the DataSource is created or found
      dataSourceName - DataSource name
      image - container disk image imported when the DataSource does not exist
      diskSize - requested disk size
      accessMode - PVC access mode
      storageClass - Kubernetes storage class, or null to use the cluster default
      cpu - CPU request
      memory - memory request
      cpuLimit - CPU limit, or null
      memoryLimit - memory limit, or null
      userData - cloud-init user data
      injectTmpDiskConfig - whether to inject the ephemeral /tmp disk configuration
      callback - callback for progress reporting
    • getVMStatus

      public String getVMStatus(String name)
      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

      public KubeVirt.DataVolumeStatus getDataVolumeStatus(String dvName)
      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

      public KubeVirt.DataVolumeStatus getDataVolumeStatus(String dvName, String dvNamespace)
      Gets the status of a DataVolume in a specific namespace.
      Parameters:
      dvName - The DataVolume name
      dvNamespace - The namespace where the DataVolume resides
      Returns:
      A status object with phase, progress, and any error message
    • deleteVM

      public void deleteVM(String name, String cloudName, String templateName)
      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 delete
      cloudName - Expected cloud name for ownership validation
      templateName - Expected template name for ownership validation
      Throws:
      IllegalStateException - if the VM doesn't belong to this instance
    • countVMsByCloud

      public int countVMsByCloud(String cloudName)
      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

      public int countVMsByCloudAndTemplate(String cloudName, String templateName)
      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 by
      templateName - The template name to filter by
      Returns:
      The number of VMs with matching cloud and template labels
    • getVMCreationTimestamp

      public String getVMCreationTimestamp(String name)
      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

      public String getVMTemplateName(String name)
      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

      public List<String> listVMsByCloud(String cloudName)
      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

      public String getVMIp(String name)
      Gets the IP address of a running VirtualMachineInstance.
    • getNetworkDiagnostics

      public String getNetworkDiagnostics(String name)
      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

      public String getFullVMDiagnostics(String name)
      Gets a comprehensive status dump of a VM, useful for debugging provisioning failures.
      Parameters:
      name - The VM name
      Returns:
      Detailed status information
    • isVMReady

      public boolean isVMReady(String name)
      Checks if a VirtualMachineInstance is in Running state.
    • testConnection

      public String testConnection() throws Exception
      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:
      close in interface AutoCloseable