Package io.jenkins.plugins.kubevirt
Class PortForwardCleanupTracker
java.lang.Object
io.jenkins.plugins.kubevirt.PortForwardCleanupTracker
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHolds the resources associated with a WebSocket tunnel connection. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCleans up all tracked resources.static intCleans up all orphaned resources.static intGets the current count of tracked resources.static booleanhasResources(String vmName) Checks if a specific VM has tracked resources.static voidJenkins Terminator that runs during shutdown.static voidregister(String vmName, KubeVirtWebSocketTunnel tunnel, io.fabric8.kubernetes.client.KubernetesClient client, VirtctlPortForwardLauncher launcher) Registers tunnel resources for tracking.static voidunregister(String vmName) Unregisters tunnel resources after normal cleanup.
-
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 VMtunnel- The WebSocket tunnel connectionclient- The Kubernetes clientlauncher- The launcher that owns these resources
-
unregister
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
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
Jenkins Terminator that runs during shutdown. This ensures all tunnel resources are cleaned up even if agents are not properly disconnected.
-