-
Notifications
You must be signed in to change notification settings - Fork 524
[Kubectl-plugin] ray session --kill-all #3422
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
Open
troychiu
wants to merge
6
commits into
ray-project:master
Choose a base branch
from
troychiu:kubectl-ray-session-kill-all
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,21 @@ package session | |
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util" | ||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util/client" | ||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util/completion" | ||
"github.com/shirou/gopsutil/v4/process" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericiooptions" | ||
cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util" | ||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util/client" | ||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util/completion" | ||
) | ||
|
||
type appPort struct { | ||
|
@@ -30,9 +33,13 @@ type SessionOptions struct { | |
ResourceName string | ||
namespace string | ||
Verbose bool | ||
killAll bool | ||
} | ||
|
||
const reconnectDelay = 3 * time.Second | ||
const ( | ||
reconnectDelay = 3 * time.Second | ||
raySessionCommand = "kubectl-ray session" | ||
) | ||
|
||
var ( | ||
dashboardPort = appPort{ | ||
|
@@ -68,6 +75,9 @@ var ( | |
|
||
# Forward local ports to the Ray cluster used for the RayService resource | ||
kubectl ray session rayservice/my-rayservice | ||
|
||
# Kill all existing Ray sessions started by kubectl-ray session command | ||
kubectl ray session --kill-all | ||
`) | ||
) | ||
|
||
|
@@ -87,8 +97,8 @@ func NewSessionCommand(cmdFactory cmdutil.Factory, streams genericiooptions.IOSt | |
Long: sessionLong, | ||
Example: sessionExample, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return cmdutil.UsageErrorf(cmd, "accepts 1 arg, received %d\n%s", len(args), cmd.Use) | ||
if len(args) > 1 { | ||
return cmdutil.UsageErrorf(cmd, "accepts at most 1 arg, received %d\n%s", len(args), cmd.Use) | ||
} | ||
return nil | ||
}, | ||
|
@@ -102,11 +112,22 @@ func NewSessionCommand(cmdFactory cmdutil.Factory, streams genericiooptions.IOSt | |
} | ||
|
||
cmd.Flags().BoolVarP(&options.Verbose, "verbose", "v", false, "verbose output") | ||
|
||
cmd.Flags().BoolVarP(&options.killAll, "kill-all", "", false, "kill all existing Ray sessions started by kubectl-ray session command") | ||
return cmd | ||
} | ||
|
||
func (options *SessionOptions) Complete(cmd *cobra.Command, args []string) error { | ||
if options.killAll { | ||
if len(args) > 0 { | ||
return cmdutil.UsageErrorf(cmd, "accepts no args when killAll flag is set") | ||
} | ||
return nil | ||
} | ||
|
||
if len(args) == 0 { | ||
return cmdutil.UsageErrorf(cmd, "killAll flag is not set, but no args provided") | ||
} | ||
|
||
namespace, err := cmd.Flags().GetString("namespace") | ||
if err != nil { | ||
return fmt.Errorf("failed to get namespace: %w", err) | ||
|
@@ -153,6 +174,16 @@ func (options *SessionOptions) Complete(cmd *cobra.Command, args []string) error | |
} | ||
|
||
func (options *SessionOptions) Run(ctx context.Context, factory cmdutil.Factory) error { | ||
if options.killAll { | ||
if options.Verbose { | ||
fmt.Println("killAll flag is set, killing all existing Ray sessions...") | ||
} | ||
if err := killAllRaySessions(ctx, options.Verbose); err != nil { | ||
return fmt.Errorf("failed to kill all ray sessions: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
k8sClient, err := client.NewClient(factory) | ||
if err != nil { | ||
return fmt.Errorf("failed to create client: %w", err) | ||
|
@@ -216,3 +247,45 @@ func (options *SessionOptions) Run(ctx context.Context, factory cmdutil.Factory) | |
wg.Wait() | ||
return nil | ||
} | ||
|
||
func killAllRaySessions(ctx context.Context, verbose bool) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this function? |
||
procs, err := process.Processes() | ||
if err != nil { | ||
return fmt.Errorf("failed to get processes: %w", err) | ||
} | ||
|
||
for _, p := range procs { | ||
cmdline, err := p.CmdlineWithContext(ctx) | ||
if err != nil { | ||
// Skip the process if we can't get its command line. It might be permission issue. | ||
continue | ||
} | ||
if strings.Contains(cmdline, raySessionCommand) && int(p.Pid) != os.Getpid() { | ||
if verbose { | ||
fmt.Printf("Found ray session: %s\n", cmdline) | ||
} | ||
// Since ray session spawn child processes to run the actual commands, | ||
// we need to kill all child processes first. | ||
children, err := p.ChildrenWithContext(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to get children of process %d: %w", p.Pid, err) | ||
} | ||
for _, child := range children { | ||
if verbose { | ||
fmt.Printf("Killing subprocess with PID %d\n", child.Pid) | ||
} | ||
if err := child.Kill(); err != nil { | ||
return fmt.Errorf("failed to kill child process %d: %w", child.Pid, err) | ||
} | ||
} | ||
// Then kill the parent process. | ||
if verbose { | ||
fmt.Printf("Killing process with PID %d\n", p.Pid) | ||
} | ||
if err := p.Kill(); err != nil { | ||
return fmt.Errorf("failed to kill process %d: %w", p.Pid, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Create another function called
Validate
and put the validation log there. You can use other files for reference. The order isComplete
,Validate
,Run
.