-
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
Conversation
d66474e
to
44d2c5a
Compare
Skipping CI for Draft Pull Request. |
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.
a81df8a
to
676563a
Compare
Can I tackle this one as a follow-up task? |
aba563f
to
d3375d7
Compare
Yup definitely! |
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.
minor comments left, looks good to me!
pkg/os/shell/shell_unix.go
Outdated
) | ||
|
||
var ( | ||
ErrUnknownShell = errors.New("Error: Unknown shell") | ||
getPid = getCurrentProcess |
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.
With the changes you did, getPid
is no longer very accurate :-/
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 for the leftover typo, I've renamed it.
pkg/os/shell/shell_unix.go
Outdated
// process.Process object. This implementation is used in production code. | ||
type RealProcess struct { | ||
realProcess *process.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.
If you don't give a name to realProcess
, you don't need to reimplement Name
, go will reuse the implementation from *process.Process
:
type RealProcess struct {
*process.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.
Thanks, I've updated it as per your suggestion.
pkg/os/shell/shell_unix.go
Outdated
return &RealProcess{parentProcess}, nil | ||
} | ||
|
||
func NewRealProcess(pid int32) (*RealProcess, 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.
getCurrentProcess
is the single user of NewRealProcess
, you could consider merging the 2 especially as most of their code is error-handling.
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.
You're right. I've merged both methods into single one
…-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]>
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]>
d3375d7
to
dfcf769
Compare
@rohanKanojia: The following tests 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. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: cfergeau The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
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