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

refactor (shell) : Refactor CRC unix/linux shell detection logic using gopsutil (#4562) #4572

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rohanKanojia
Copy link
Contributor

Description

Fixes: #4562

Relates to: #4526 (comment)

Refactor CRC Shell detection for UNIX/Linux Systems to rely on parent process name. I was only able to get this approach working for Linux/Unix systems where gopsutil library is correctly detecting parent PIDs. On WSL, it's not able to see beyond windows processes.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Proposed changes

  • Move parsing ps output and picking recent pid logic to shell_windows. It would only be used in case of WSL.
  • Instead of parsing ps output and picking up recent pid. Inspect parent process of current process and keep going up until we find a shell process. Pick that shell process as currently active shell.
    • As of now I've set a depth limit of 10 levels, if we're not able to find any shell process after going this deep, we return blank value.

Testing

Start CRC cluster and run oc-env / podman-env commands on bash, zsh or fish shells.

Contribution Checklist

  • I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Which platform have you tested the code changes on?
    • Linux
    • Windows
    • MacOS (QE Lab instance)

Copy link

openshift-ci bot commented Jan 16, 2025

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Copy link

openshift-ci bot commented Jan 16, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign praveenkumar for approval. For more information see the 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

@rohanKanojia rohanKanojia changed the title refactor (shell) : Refactor unix/linux shell detection mechanism (#4562) refactor (shell) : Refactor CRC unix/linux shell detection logic using gopsutil (#4562) Jan 16, 2025
@rohanKanojia rohanKanojia force-pushed the pr/issue4562 branch 4 times, most recently from 1e916f7 to 75de20c Compare January 17, 2025 05:50
@rohanKanojia rohanKanojia marked this pull request as ready for review January 17, 2025 08:58
@cfergeau
Copy link
Contributor

On WSL, it's not able to see beyond windows processes.

What about Windows with powershell or cmd?

@rohanKanojia
Copy link
Contributor Author

@cfergeau : In this PR I had kept changes limited to parts I had touched on in #4526 . In my previous pull request for Shell detection, I had only made changes for WSL and Unix, Linux platforms.

I just tested this approach on PowerShell and CMD. I can verify that this approach is able to detect shell processes from parent tree.

Some Debug logs on PowerShell:

PS C:\Users\rokum\go\src\github.com\crc-org\crc> C:\Users\rokum\go\bin\crc.exe oc-env
process name : crc.exe
parent process id : 8060
process name : powershell.exe
parent process id : 15508
process name : WindowsTerminal.exe
parent process id : 8592
process name : explorer.exe
parent process id : 8476
process name :
 detectShellByCheckingProcessTree
$Env:PATH = "C:\Users\rokum\.crc\bin\oc;$Env:PATH"
# Run this command to configure your shell:
# & crc oc-env | Invoke-Expression

Some Debug logs on CMD:

C:\Users\rokum>C:\Users\rokum\go\bin\crc.exe oc-env
process name : crc.exe
parent process id : 14804
process name : cmd.exe
parent process id : 8592
process name : explorer.exe
parent process id : 8476
process name :
 detectShellByCheckingProcessTree
SET PATH=C:\Users\rokum\.crc\bin\oc;%PATH%
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('crc oc-env') DO @call %i

Shall I modify this PR to cover PowerShell and CMD as well?

@rohanKanojia rohanKanojia force-pushed the pr/issue4562 branch 2 times, most recently from a509dee to a81df8a Compare January 23, 2025 04:21
Copy link
Contributor

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

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

Another potential follow-up is to use detectShellByCheckingProcessTree in detect() in shell_windows.go as both seem to have similar functionalities.

@@ -101,3 +105,83 @@ func detect() (string, error) {

return shellType(shell, "cmd"), nil
}

// detectShellByInvokingCommand is a utility method that tries to detect current shell in use by invoking `ps` command.
// This method is extracted so that it could be used by unix systems as well as Windows (in case of WSL). It executes
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 now it is only used on Windows in the WSL case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I forgot to update this comment after moving the method. Thanks for noticing! It should be okay now.

)

var (
ErrUnknownShell = errors.New("Error: Unknown shell")
ErrUnknownShell = errors.New("Error: Unknown shell")
getCurrentProcessID = os.Getpid
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, it is slightly easier to follow if you name this getPid, in a way this removes one level of indirection.

Copy link
Contributor Author

@rohanKanojia rohanKanojia Jan 24, 2025

Choose a reason for hiding this comment

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

Thanks, I've renamed the getter to getPid as you suggested.

"path/filepath"

"github.com/shirou/gopsutil/v3/process"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not v4? We probably should switch crc to the latest version, but this can be done in a follow-up commit if you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a new commit on top of this one to upgrade to v4

type AbstractProcess interface {
Name() (string, error)
Ppid() (int32, error)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems this code could be simpler if you use Parent() instead of Ppid() ?

And I think it's possible to remove getCurrentProcessID = os.Getpid and AbstractProcessSupplier and to replace both with var newProcess = process.NewProcess.

Or will this prevent some of the "failure" tests that you have from being doable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Regardless of the previous suggestion, I'd add a comment saying these types are needed for the unit tests.
You could add var _ AbstractProcess = process.Process to make it clear that this type implements the interface.

Copy link
Contributor Author

@rohanKanojia rohanKanojia Jan 24, 2025

Choose a reason for hiding this comment

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

Thanks for the suggestion, I've updated it to use Parent(), as you suggested. It's much clearer now!

…-org#4562)

+ Move parsing ps output and picking recent pid approach to shell_windows. It would only
  be used in case of WSL.
+ Instead of parsing ps output and picking up recent pid. Inspect parent process
  of current process and keep going up until we find a shell process. Pick that
  shell process as currently active shell.

Signed-off-by: Rohan Kumar <[email protected]>
@rohanKanojia
Copy link
Contributor Author

Another potential follow-up is to use detectShellByCheckingProcessTree in detect() in shell_windows.go as both seem to have similar functionalities.

Can I tackle this one as a follow-up task?

We're currently using an old version of gopsutil library. Upgrade to the latest
version in order keep dependency up to date. This commit includes changes to update the
dependency version in project and also vendor directory updates

Signed-off-by: Rohan Kumar <[email protected]>
@cfergeau
Copy link
Contributor

Another potential follow-up is to use detectShellByCheckingProcessTree in detect() in shell_windows.go as both seem to have similar functionalities.

Can I tackle this one as a follow-up task?

Yup definitely!

Copy link

openshift-ci bot commented Jan 24, 2025

@rohanKanojia: 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 d3375d7 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-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor CRC shell detection mechanism to use process parent programatically
2 participants