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

Conversation

evidolob
Copy link
Contributor

Relates to: First part for #3855

Solution/Idea

PR's add preflight check for SSH port(2222) in not used before start.

Testing

  1. Occupy 2222 port on your laptop( you may use nc -lkv 127.0.0.1 2222 command)
  2. Start daemon
  3. Try to start CRC VM
  4. Ensure that you have error during preflight checks
  5. Free port 2222
  6. Try to start CRC again
  7. Ensure that CRC starts normally

@anjannath
Copy link
Member

working as expected, tested on macOS:

INFO Using bundle path /Users/anath/.crc/cache/crc_vfkit_4.14.8_arm64.crcbundle
INFO Checking if running as non-root
INFO Checking if crc-admin-helper executable is cached
INFO Checking if running on a supported CPU architecture
INFO Checking if crc executable symlink exists
INFO Checking minimum RAM requirements
INFO Checking if running emulated on Apple silicon
INFO Checking if vfkit is installed
INFO Checking if old launchd config for tray and/or daemon exists
INFO Checking if crc daemon plist file is present and loaded
INFO Checking SSH port availability
WARN Preflight checks failed during `crc start`, please try to run `crc setup` first in case you haven't done so yet
port 2222 already in use: listen tcp 127.0.0.1:2222: bind: address already in use

Copy link
Contributor

@vyasgun vyasgun left a comment

Choose a reason for hiding this comment

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

Just added a few minor comments

pkg/crc/preflight/preflight_checks_nonlinux.go Outdated Show resolved Hide resolved
if err != nil {
return fmt.Errorf("port %d already in use: %s", constants.VsockSSHPort, err)
}
server.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
server.Close()
defer func() {
err = server.Close()
}()

Copy link
Contributor

Choose a reason for hiding this comment

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

Could be just return server.Close() ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's more conventional to use defer for closing connections.

server.Close()

// we successfully used and closed the port
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return nil
return err

Copy link
Contributor

Choose a reason for hiding this comment

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

If the intent is to return the error from the deferred server.Close() call, I don't think this will work as expected:
https://go.dev/play/p/HyKboZE2dka

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right. We can define the err variable as part of the function signature to set the correct return value in defer.
https://go.dev/play/p/8kaB3HQlfk6

}

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.

server.Close()

// we successfully used and closed the port
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

If the intent is to return the error from the deferred server.Close() call, I don't think this will work as expected:
https://go.dev/play/p/HyKboZE2dka

if err != nil {
return fmt.Errorf("port %d already in use: %s", constants.VsockSSHPort, err)
}
server.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be just return server.Close() ?

evidolob and others added 2 commits February 8, 2024 09:32
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
Co-authored-by: Gunjan Vyas <vyasgun20@gmail.com>
@praveenkumar praveenkumar added the has-to-be-in-release This PR need to go in coming release. label Feb 8, 2024
Copy link

openshift-ci bot commented Feb 8, 2024

@evidolob: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-crc 7e92185 link true /test e2e-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@vyasgun
Copy link
Contributor

vyasgun commented Feb 12, 2024

/approve

Copy link

openshift-ci bot commented Feb 12, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vyasgun
Once this PR has been reviewed and has the lgtm label, please ask for approval from evidolob. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@praveenkumar praveenkumar merged commit 7929737 into crc-org:main Feb 12, 2024
18 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-to-be-in-release This PR need to go in coming release.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants