diff --git a/pkg/crc/machine/client.go b/pkg/crc/machine/client.go index adee769cb2..b9cf4a2c5d 100644 --- a/pkg/crc/machine/client.go +++ b/pkg/crc/machine/client.go @@ -34,19 +34,20 @@ type client struct { debug bool config crcConfig.Storage virtualMachine VirtualMachine + vSockClient AbstractVSock diskDetails *memoize.Memoizer ramDetails *memoize.Memoizer } func NewClient(name string, debug bool, config crcConfig.Storage) Client { - return newClientWithVirtualMachine(name, debug, config, nil) + return newClientWithVirtualMachineAndVSock(name, debug, config, nil, &VSock{}) } -// newClientWithVirtualMachine creates a Client instance with a overridden VirtualMachine implementation. +// newClientWithVirtualMachineAndVSock creates a Client instance with an overridden VirtualMachine and AbstractVSock implementation. // It would not create a new VirtualMachine object. This method is primarily created for usage in tests so -// that we can pass a fake VirtualMachine implementation. -func newClientWithVirtualMachine(name string, debug bool, config crcConfig.Storage, vm VirtualMachine) Client { +// that we can pass a fake VirtualMachine and AbstractVSock implementation. +func newClientWithVirtualMachineAndVSock(name string, debug bool, config crcConfig.Storage, vm VirtualMachine, vSock AbstractVSock) Client { return &client{ name: name, debug: debug, @@ -54,6 +55,7 @@ func newClientWithVirtualMachine(name string, debug bool, config crcConfig.Stora diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute), ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute), virtualMachine: vm, + vSockClient: vSock, } } diff --git a/pkg/crc/machine/delete.go b/pkg/crc/machine/delete.go index 2983e8231f..190549e63a 100644 --- a/pkg/crc/machine/delete.go +++ b/pkg/crc/machine/delete.go @@ -22,7 +22,7 @@ func (client *client) Delete() error { // In case usermode networking make sure all the port bind on host should be released if client.useVSock() { - if err := unexposePorts(); err != nil { + if err := client.vSockClient.UnExposePorts(); err != nil { return err } } diff --git a/pkg/crc/machine/fakemachine/vsock.go b/pkg/crc/machine/fakemachine/vsock.go new file mode 100644 index 0000000000..dd92ae982d --- /dev/null +++ b/pkg/crc/machine/fakemachine/vsock.go @@ -0,0 +1,23 @@ +package fakemachine + +import crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset" + +type FakeVSock struct { + PortsExposed bool +} + +func (v *FakeVSock) ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error { + v.PortsExposed = true + return nil +} + +func (v *FakeVSock) UnExposePorts() error { + v.PortsExposed = false + return nil +} + +func NewFakeVSock() *FakeVSock { + return &FakeVSock{ + PortsExposed: false, + } +} diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go index cc7af70530..80f993e5e4 100644 --- a/pkg/crc/machine/start.go +++ b/pkg/crc/machine/start.go @@ -368,7 +368,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig) logging.Infof("Starting CRC VM for %s %s...", startConfig.Preset, vm.Bundle().GetVersion()) if client.useVSock() { - if err := exposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil { + if err := client.vSockClient.ExposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil { return nil, err } } diff --git a/pkg/crc/machine/stop.go b/pkg/crc/machine/stop.go index f25a2e18f8..fc07aa7c60 100644 --- a/pkg/crc/machine/stop.go +++ b/pkg/crc/machine/stop.go @@ -45,7 +45,7 @@ func (client *client) Stop() (state.State, error) { } // In case usermode networking make sure all the port bind on host should be released if client.useVSock() { - return status, unexposePorts() + return status, client.vSockClient.UnExposePorts() } return status, nil } diff --git a/pkg/crc/machine/stop_test.go b/pkg/crc/machine/stop_test.go index a4ae6a1907..fc38dc2ab3 100644 --- a/pkg/crc/machine/stop_test.go +++ b/pkg/crc/machine/stop_test.go @@ -20,7 +20,8 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) { _, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true") assert.NoError(t, err) virtualMachine := fakemachine.NewFakeVirtualMachine(false, false) - client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine) + fakeVSock := fakemachine.NewFakeVSock() + client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock) // When clusterState, stopErr := client.Stop() @@ -31,6 +32,7 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) { assert.Equal(t, virtualMachine.IsStopped, true) assert.Equal(t, virtualMachine.FakeSSHClient.LastExecutedCommand, "sudo -- sh -c 'crictl stop $(crictl ps -q)'") assert.Equal(t, virtualMachine.FakeSSHClient.IsSSHClientClosed, true) + assert.Equal(t, fakeVSock.PortsExposed, false) } func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) { @@ -41,7 +43,8 @@ func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) { _, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true") assert.NoError(t, err) virtualMachine := fakemachine.NewFakeVirtualMachine(true, false) - client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine) + fakeVSock := fakemachine.NewFakeVSock() + client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock) // When _, stopErr := client.Stop() @@ -60,7 +63,8 @@ func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) { virtualMachine := fakemachine.NewFakeVirtualMachine(false, false) err = virtualMachine.Stop() assert.NoError(t, err) - client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine) + fakeVSock := fakemachine.NewFakeVSock() + client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock) // When clusterState, stopErr := client.Stop() diff --git a/pkg/crc/machine/vsock.go b/pkg/crc/machine/vsock.go index 7d9c72613a..e94703666f 100644 --- a/pkg/crc/machine/vsock.go +++ b/pkg/crc/machine/vsock.go @@ -16,7 +16,14 @@ import ( "github.com/pkg/errors" ) -func exposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error { +type AbstractVSock interface { + ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error + UnExposePorts() error +} + +type VSock struct{} + +func (v *VSock) ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error { portsToExpose := vsockPorts(preset, ingressHTTPPort, ingressHTTPSPort) daemonClient := daemonclient.New() alreadyOpenedPorts, err := listOpenPorts(daemonClient) @@ -47,7 +54,7 @@ func isOpened(exposed []types.ExposeRequest, port types.ExposeRequest) bool { return false } -func unexposePorts() error { +func (v *VSock) UnExposePorts() error { var mErr crcErrors.MultiError daemonClient := daemonclient.New() alreadyOpenedPorts, err := listOpenPorts(daemonClient)