Class PortForwardCleanupTracker

java.lang.Object
io.jenkins.plugins.kubevirt.PortForwardCleanupTracker

@Extension public class PortForwardCleanupTracker extends Object
Tracks active WebSocket tunnel resources to ensure cleanup even when normal disconnection callbacks are not invoked (e.g., Jenkins restart, abnormal termination).

This tracker solves the problem of transient resources in VirtctlPortForwardLauncher being lost after serialization or when connections terminate abnormally without afterDisconnect being called.

Resources are tracked using WeakReferences to the launcher, allowing garbage collection while still enabling cleanup of the underlying network resources.

  • Constructor Details

    • PortForwardCleanupTracker

      public PortForwardCleanupTracker()
  • Method Details

    • register

      public static void register(String vmName, KubeVirtWebSocketTunnel tunnel, io.fabric8.kubernetes.client.KubernetesClient client, VirtctlPortForwardLauncher launcher)
      Registers tunnel resources for tracking. Call this after successfully establishing a WebSocket tunnel.
      Parameters:
      vmName - The name of the VM
      tunnel - The WebSocket tunnel connection
      client - The Kubernetes client
      launcher - The launcher that owns these resources
    • unregister

      public static void unregister(String vmName)
      Unregisters tunnel resources after normal cleanup. Call this from the launcher's cleanup method.
      Parameters:
      vmName - The name of the VM
    • cleanupOrphaned

      public static int cleanupOrphaned()
      Cleans up all orphaned resources. A resource is considered orphaned if its launcher WeakReference has been cleared (indicating the launcher was garbage collected without proper cleanup).
      Returns:
      The number of orphaned resources that were cleaned up
    • cleanupAll

      public static void cleanupAll()
      Cleans up all tracked resources. Called during Jenkins shutdown.
    • getActiveCount

      public static int getActiveCount()
      Gets the current count of tracked resources. Useful for monitoring and debugging.
      Returns:
      The number of active tracked resources
    • hasResources

      public static boolean hasResources(String vmName)
      Checks if a specific VM has tracked resources.
      Parameters:
      vmName - The VM name to check
      Returns:
      true if resources are being tracked for this VM
    • onShutdown

      @Terminator public static void onShutdown()
      Jenkins Terminator that runs during shutdown. This ensures all tunnel resources are cleaned up even if agents are not properly disconnected.