Package io.jenkins.plugins.kubevirt
Class KubeVirtWebSocketTunnel
java.lang.Object
io.jenkins.plugins.kubevirt.KubeVirtWebSocketTunnel
- All Implemented Interfaces:
Closeable,AutoCloseable
Establishes a WebSocket tunnel to the KubeVirt subresource API and exposes it
as a local TCP port that
SSHLauncher can connect to.
This replaces the previous approach of finding virt-launcher pods and using Kubernetes core pod port-forward. Instead, it uses the KubeVirt-native portforward subresource endpoint:
wss://<api>/apis/subresources.kubevirt.io/v1/namespaces/{ns}/virtualmachineinstances/{name}/portforward/{port}/tcp
The tunnel accepts multiple sequential TCP connections on the local port. Each accepted connection triggers a new WebSocket to virt-api, which in turn opens a direct TCP connection to the VM's IP. This supports both the SSH readiness probe (short-lived) and the actual SSH session (long-lived).
Data flow:
SSHLauncher ──► 127.0.0.1:localPort ──► WebSocket ──► virt-api ──► net.Dial(VM_IP:22)
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the tunnel, stopping the accept loop and cleaning up all resources.intReturns the local TCP port that clients (e.g.booleanisAlive()Returns whether the tunnel is still running and accepting connections.static KubeVirtWebSocketTunnelopen(String serverUrl, Secret token, boolean ignoreSsl, String namespace, String vmiName, int targetPort) Opens a new WebSocket tunnel to a KubeVirt VMI's port.
-
Method Details
-
open
public static KubeVirtWebSocketTunnel open(String serverUrl, Secret token, boolean ignoreSsl, String namespace, String vmiName, int targetPort) throws IOException Opens a new WebSocket tunnel to a KubeVirt VMI's port.This method binds a local TCP server socket on an ephemeral port and starts an accept loop in the background. Each accepted TCP connection will trigger a new WebSocket connection to the KubeVirt subresource API.
- Parameters:
serverUrl- Kubernetes API server URL (e.g.https://api.cluster:6443)token- OAuth bearer token for Kubernetes API authenticationignoreSsl- Whether to skip TLS certificate verificationnamespace- Kubernetes namespace of the VMIvmiName- Name of the VirtualMachineInstancetargetPort- Target port on the VM (e.g. 22 for SSH)- Returns:
- A tunnel instance with a local port ready for connections
- Throws:
IOException- If the local server socket cannot be created
-
getLocalPort
public int getLocalPort()Returns the local TCP port that clients (e.g. SSHLauncher) should connect to.- Returns:
- The local port number
-
isAlive
public boolean isAlive()Returns whether the tunnel is still running and accepting connections.- Returns:
trueif the tunnel is active
-
close
public void close()Closes the tunnel, stopping the accept loop and cleaning up all resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-