Skip to content

Commit

Permalink
[Feature][kubectl-plugin]'ray log command' Add check and cleanup dire…
Browse files Browse the repository at this point in the history
…ctory when no ray node exist
  • Loading branch information
chiayi committed Oct 29, 2024
1 parent 8b61b73 commit f0d34fa
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion kubectl-plugin/pkg/cmd/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var (
# Download all (worker node and head node) the logs from a RayCluster
kubectl ray log my-raycluster --node-type all
`)

// flag to check if output directory is generated and needs to be deleted
deleteOutputDir = false
)

func NewClusterLogOptions(streams genericclioptions.IOStreams) *ClusterLogOptions {
Expand Down Expand Up @@ -123,12 +126,13 @@ func (options *ClusterLogOptions) Validate() error {
}

if options.outputDir == "" {
fmt.Fprintln(options.ioStreams.Out, "No output directory specified, creating dir under current directory using cluster name.")
fmt.Fprintln(options.ioStreams.Out, "No output directory specified, creating dir under current directory using resource name.")
options.outputDir = options.ResourceName
err := os.MkdirAll(options.outputDir, 0o755)
if err != nil {
return fmt.Errorf("could not create directory with cluster name %s: %w", options.outputDir, err)
}
deleteOutputDir = true
}

switch options.nodeType {
Expand Down Expand Up @@ -180,6 +184,13 @@ func (options *ClusterLogOptions) Run(ctx context.Context, factory cmdutil.Facto
if err != nil {
return fmt.Errorf("failed to retrieve head node for RayCluster %s: %w", options.ResourceName, err)
}
if len(rayNodes.Items) == 0 {
// Clean up the empty directory if the directory was generated. Since it will always be in current dir, only Remove() is used.
if deleteOutputDir {
os.Remove(options.outputDir)
}
return fmt.Errorf("No ray nodes found for resource %s", options.ResourceName)
}

// Get a list of logs of the ray nodes.
var logList []*bytes.Buffer
Expand Down

0 comments on commit f0d34fa

Please sign in to comment.