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 <[email protected]>
  • Loading branch information
evidolob committed Jan 19, 2024
1 parent 6d23b6a commit ee58bc8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
42 changes: 42 additions & 0 deletions pkg/crc/preflight/preflight_checks_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package preflight

import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"

"github.com/crc-org/crc/v2/pkg/crc/adminhelper"
"github.com/crc-org/crc/v2/pkg/crc/cluster"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/daemonclient"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/crc-org/crc/v2/pkg/crc/machine/bundle"
crcpreset "github.com/crc-org/crc/v2/pkg/crc/preset"
Expand Down Expand Up @@ -45,6 +48,17 @@ func memoryCheck(preset crcpreset.Preset) Check {
}
}

func sshPortCheck() Check {

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (windows-2019, 1.20)

func `sshPortCheck` is unused (unused)

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (macOS-13, 1.20)

func `sshPortCheck` is unused (unused)

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (macOS-12, 1.20)

func `sshPortCheck` is unused (unused)

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (windows-2022, 1.20)

func `sshPortCheck` is unused (unused)

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.20)

func `sshPortCheck` is unused (unused)

Check failure on line 51 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 1.20)

func `sshPortCheck` is unused (unused)
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,
}
}

var genericCleanupChecks = []Check{
{
cleanupDescription: "Removing CRC Machine Instance directory",
Expand Down Expand Up @@ -83,6 +97,34 @@ var genericCleanupChecks = []Check{
},
}

func checkSSHPortFree() func() error {

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (windows-2019, 1.20)

func `checkSSHPortFree` is unused (unused)

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (macOS-13, 1.20)

func `checkSSHPortFree` is unused (unused)

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (macOS-12, 1.20)

func `checkSSHPortFree` is unused (unused)

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (windows-2022, 1.20)

func `checkSSHPortFree` is unused (unused)

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.20)

func `checkSSHPortFree` is unused (unused)

Check failure on line 100 in pkg/crc/preflight/preflight_checks_common.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 1.20)

func `checkSSHPortFree` is unused (unused)
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)
}
server.Close()

// we successfully used and closed the port
return nil
}
}

func checkBundleExtracted(bundlePath string) func() error {
return func() error {
logging.Infof("Checking if %s exists", bundlePath)
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/preflight/preflight_darwin.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ee58bc8

Please sign in to comment.