-
Notifications
You must be signed in to change notification settings - Fork 243
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
base: main
Are you sure you want to change the base?
Conversation
d66474e
to
44d2c5a
Compare
Skipping CI for Draft Pull Request. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
1e916f7
to
75de20c
Compare
What about Windows with powershell or cmd? |
@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:
Some Debug logs on CMD:
Shall I modify this PR to cover PowerShell and CMD as well? |
a509dee
to
a81df8a
Compare
There was a problem hiding this 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.
pkg/os/shell/shell_windows.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
pkg/os/shell/shell_unix.go
Outdated
) | ||
|
||
var ( | ||
ErrUnknownShell = errors.New("Error: Unknown shell") | ||
ErrUnknownShell = errors.New("Error: Unknown shell") | ||
getCurrentProcessID = os.Getpid |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pkg/os/shell/shell_unix.go
Outdated
"path/filepath" | ||
|
||
"github.com/shirou/gopsutil/v3/process" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
a81df8a
to
676563a
Compare
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]>
aba563f
to
d3375d7
Compare
Yup definitely! |
@rohanKanojia: The following test failed, say
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. |
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
test, version modification, documentation, etc.)
Proposed changes
Testing
Start CRC cluster and run
oc-env
/podman-env
commands on bash, zsh or fish shells.Contribution Checklist