Skip to content

Commit

Permalink
add preflight check for vsock SSH port
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob authored and praveenkumar committed Feb 12, 2024
1 parent 465df52 commit 2c34671
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/crc/constants/constants.go
Expand Up @@ -38,6 +38,7 @@ const (

VSockGateway = "192.168.127.1"
VsockSSHPort = 2222
LocalIP = "127.0.0.1"

OkdPullSecret = `{"auths":{"fake":{"auth": "Zm9vOmJhcgo="}}}` // #nosec G101

Expand Down
7 changes: 3 additions & 4 deletions pkg/crc/machine/vsock.go
Expand Up @@ -78,7 +78,6 @@ const (
virtualMachineIP = "192.168.127.2"
hostVirtualIP = "192.168.127.254"
internalSSHPort = "22"
localIP = "127.0.0.1"
remoteHTTPPort = "80"
remoteHTTPSPort = "443"
apiPort = "6443"
Expand All @@ -95,7 +94,7 @@ func vsockPorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint)
exposeRequest := []types.ExposeRequest{
{
Protocol: "tcp",
Local: net.JoinHostPort(localIP, strconv.Itoa(constants.VsockSSHPort)),
Local: net.JoinHostPort(constants.LocalIP, strconv.Itoa(constants.VsockSSHPort)),
Remote: net.JoinHostPort(virtualMachineIP, internalSSHPort),
},
{
Expand All @@ -110,7 +109,7 @@ func vsockPorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint)
exposeRequest = append(exposeRequest,
types.ExposeRequest{
Protocol: "tcp",
Local: net.JoinHostPort(localIP, apiPort),
Local: net.JoinHostPort(constants.LocalIP, apiPort),
Remote: net.JoinHostPort(virtualMachineIP, apiPort),
},
types.ExposeRequest{
Expand All @@ -127,7 +126,7 @@ func vsockPorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint)
exposeRequest = append(exposeRequest,
types.ExposeRequest{
Protocol: "tcp",
Local: net.JoinHostPort(localIP, cockpitPort),
Local: net.JoinHostPort(constants.LocalIP, cockpitPort),
Remote: net.JoinHostPort(virtualMachineIP, cockpitPort),
})
default:
Expand Down
41 changes: 41 additions & 0 deletions pkg/crc/preflight/preflight_checks_nonlinux.go
Expand Up @@ -6,9 +6,12 @@ package preflight
import (
"context"
"fmt"
"net"
"runtime"
"strconv"
"time"

"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/daemonclient"
crcerrors "github.com/crc-org/crc/v2/pkg/crc/errors"
"github.com/crc-org/crc/v2/pkg/crc/logging"
Expand Down Expand Up @@ -72,3 +75,41 @@ func waitForDaemonRunning() error {
return nil
}, 2*time.Second)
}

func sshPortCheck() Check {
return Check{
configKeySuffix: "check-ssh-port",
checkDescription: "Checking SSH port availability",
check: checkSSHPortFree(),
fixDescription: fmt.Sprintf("crc uses %d to run SSH", constants.VsockSSHPort),
flags: NoFix,
labels: None,
}
}

func checkSSHPortFree() func() error {
return func() error {

host := net.JoinHostPort(constants.LocalIP, strconv.Itoa(constants.VsockSSHPort))

daemonClient := daemonclient.New()
exposed, err := daemonClient.NetworkClient.List()
if err == nil {
// if port already exported by vsock we could proceed
for _, e := range exposed {
if e.Local == host {
return nil
}
}
}

server, err := net.Listen("tcp", host)
// if it fails then the port is likely taken
if err != nil {
return fmt.Errorf("port %d already in use: %s", constants.VsockSSHPort, err)
}

// we successfully used and closed the port
return server.Close()
}
}
1 change: 1 addition & 0 deletions pkg/crc/preflight/preflight_darwin.go
Expand Up @@ -114,6 +114,7 @@ func getChecks(_ network.Mode, bundlePath string, preset crcpreset.Preset, enabl
checks = append(checks, bundleCheck(bundlePath, preset, enableBundleQuayFallback))
checks = append(checks, trayLaunchdCleanupChecks...)
checks = append(checks, daemonLaunchdChecks...)
checks = append(checks, sshPortCheck())

return checks
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/crc/preflight/preflight_darwin_test.go
Expand Up @@ -13,13 +13,13 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 11)
assert.Len(t, cfg.AllConfigs(), 12)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 17)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 17)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 18)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 18)

assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 16)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 16)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 17)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 17)
}
1 change: 1 addition & 0 deletions pkg/crc/preflight/preflight_windows.go
Expand Up @@ -221,6 +221,7 @@ func getChecks(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallb
checks = append(checks, cleanupCheckRemoveCrcVM)
checks = append(checks, daemonTaskChecks...)
checks = append(checks, adminHelperServiceCheks...)
checks = append(checks, sshPortCheck())
return checks
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/crc/preflight/preflight_windows_test.go
Expand Up @@ -13,13 +13,13 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 14)
assert.Len(t, cfg.AllConfigs(), 15)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22)
assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 23)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 23)

assert.Len(t, getPreflightChecks(false, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 21)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 21)
assert.Len(t, getPreflightChecks(false, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22)
}

0 comments on commit 2c34671

Please sign in to comment.