Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preflight check for vsock SSH port #3965

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions pkg/crc/preflight/preflight_checks_nonlinux.go
Original file line number Diff line number Diff line change
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 port %d to run SSH", constants.VsockSSHPort),
flags: NoFix,
labels: None,
}
}

func checkSSHPortFree() func() error {
return func() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this indirection? the preflights use the pattern func foo() func() error when we need to pass arguments to foo. In this case, I think you could directly use func checkSSHPortFree() error, and change check: checkSSHPortFree(), to check: checkSSHPortFree, ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about it, it actually would make sense to only run the preset when usermode networking is use (the network-mode config option). In which case you'd need additional arg to the check, and using this pattern would make sense :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this was closed as resolved, but this was neither discussed nor resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)
}
praveenkumar marked this conversation as resolved.
Show resolved Hide resolved

// we successfully used and closed the port
return server.Close()
}
}
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)
}
Loading