-
Notifications
You must be signed in to change notification settings - Fork 480
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
[kubectl-plugin] add create workergroup command #2673
Merged
Merged
Changes from all commits
Commits
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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
|
||
"github.com/ray-project/kuberay/kubectl-plugin/pkg/util/client" | ||
|
||
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
type CreateWorkerGroupOptions struct { | ||
configFlags *genericclioptions.ConfigFlags | ||
ioStreams *genericclioptions.IOStreams | ||
clusterName string | ||
groupName string | ||
rayVersion string | ||
image string | ||
workerCPU string | ||
workerGPU string | ||
workerMemory string | ||
workerReplicas int32 | ||
workerMinReplicas int32 | ||
workerMaxReplicas int32 | ||
} | ||
|
||
var ( | ||
createWorkerGroupLong = templates.LongDesc(` | ||
Adds a worker group to an existing RayCluster. | ||
`) | ||
|
||
createWorkerGroupExample = templates.Examples(` | ||
# Create a worker group in an existing RayCluster | ||
kubectl ray create worker-group example-group --cluster sample-cluster --image rayproject/ray:2.39.0 --worker-cpu=2 --worker-memory=5Gi | ||
`) | ||
) | ||
|
||
func NewCreateWorkerGroupOptions(streams genericclioptions.IOStreams) *CreateWorkerGroupOptions { | ||
return &CreateWorkerGroupOptions{ | ||
configFlags: genericclioptions.NewConfigFlags(true), | ||
ioStreams: &streams, | ||
} | ||
} | ||
|
||
func NewCreateWorkerGroupCommand(streams genericclioptions.IOStreams) *cobra.Command { | ||
options := NewCreateWorkerGroupOptions(streams) | ||
cmdFactory := cmdutil.NewFactory(options.configFlags) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "workergroup [WORKERGROUP]", | ||
Short: "Create worker group in an existing RayCluster", | ||
Long: createWorkerGroupLong, | ||
Example: createWorkerGroupExample, | ||
SilenceUsage: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := options.Complete(cmd, args); err != nil { | ||
return err | ||
} | ||
if err := options.Validate(); err != nil { | ||
return err | ||
} | ||
return options.Run(cmd.Context(), cmdFactory) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&options.clusterName, "ray-cluster", "", "The name of the RayCluster to add a worker group.") | ||
cmd.Flags().StringVar(&options.rayVersion, "ray-version", "2.39.0", "Ray Version to use in the Ray Cluster yaml. Default to 2.39.0") | ||
cmd.Flags().StringVar(&options.image, "image", options.image, "Ray image to use in the Ray Cluster yaml") | ||
cmd.Flags().Int32Var(&options.workerReplicas, "worker-replicas", 1, "Number of the worker group replicas. Default of 1") | ||
cmd.Flags().Int32Var(&options.workerMinReplicas, "worker-min-replicas", 1, "Number of the worker group replicas. Default of 10") | ||
cmd.Flags().Int32Var(&options.workerMaxReplicas, "worker-max-replicas", 10, "Number of the worker group replicas. Default of 10") | ||
cmd.Flags().StringVar(&options.workerCPU, "worker-cpu", "2", "Number of CPU for the ray worker. Default to 2") | ||
cmd.Flags().StringVar(&options.workerGPU, "worker-gpu", "0", "Number of GPU for the ray worker. Default to 0") | ||
cmd.Flags().StringVar(&options.workerMemory, "worker-memory", "4Gi", "Amount of memory to use for the ray worker. Default to 4Gi") | ||
|
||
options.configFlags.AddFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
func (options *CreateWorkerGroupOptions) Complete(cmd *cobra.Command, args []string) error { | ||
if *options.configFlags.Namespace == "" { | ||
*options.configFlags.Namespace = "default" | ||
} | ||
|
||
if len(args) != 1 { | ||
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use) | ||
} | ||
options.groupName = args[0] | ||
|
||
if options.image == "" { | ||
options.image = fmt.Sprintf("rayproject/ray:%s", options.rayVersion) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (options *CreateWorkerGroupOptions) Validate() error { | ||
config, err := options.configFlags.ToRawKubeConfigLoader().RawConfig() | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving raw config: %w", err) | ||
} | ||
if len(config.CurrentContext) == 0 { | ||
return fmt.Errorf("no context is currently set, use %q to select a new one", "kubectl config use-context <context>") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (options *CreateWorkerGroupOptions) Run(ctx context.Context, factory cmdutil.Factory) error { | ||
k8sClient, err := client.NewClient(factory) | ||
if err != nil { | ||
return fmt.Errorf("failed to create client: %w", err) | ||
} | ||
|
||
rayCluster, err := k8sClient.RayClient().RayV1().RayClusters(*options.configFlags.Namespace).Get(ctx, options.clusterName, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error getting RayCluster: %w", err) | ||
} | ||
|
||
newRayCluster := rayCluster.DeepCopy() | ||
podTemplate := corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "ray-worker", | ||
Image: options.image, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse(options.workerCPU), | ||
corev1.ResourceMemory: resource.MustParse(options.workerMemory), | ||
}, | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceMemory: resource.MustParse(options.workerMemory), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
gpuResource := resource.MustParse(options.workerGPU) | ||
if !gpuResource.IsZero() { | ||
podTemplate.Spec.Containers[0].Resources.Requests[corev1.ResourceName("nvidia.com/gpu")] = gpuResource | ||
podTemplate.Spec.Containers[0].Resources.Limits[corev1.ResourceName("nvidia.com/gpu")] = gpuResource | ||
} | ||
|
||
workerGroup := rayv1.WorkerGroupSpec{ | ||
GroupName: options.groupName, | ||
Replicas: &options.workerReplicas, | ||
MinReplicas: &options.workerMinReplicas, | ||
MaxReplicas: &options.workerMaxReplicas, | ||
RayStartParams: map[string]string{}, | ||
Template: podTemplate, | ||
} | ||
newRayCluster.Spec.WorkerGroupSpecs = append(newRayCluster.Spec.WorkerGroupSpecs, workerGroup) | ||
|
||
newRayCluster, err = k8sClient.RayClient().RayV1().RayClusters(*options.configFlags.Namespace).Update(ctx, newRayCluster, metav1.UpdateOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error updating RayCluster with new worker group: %w", err) | ||
} | ||
|
||
fmt.Printf("Updated RayCluster %s/%s with new worker group\n", newRayCluster.Namespace, newRayCluster.Name) | ||
return nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
I can't comment on the other parts but in the job submit examples section, we still have the worker-grp-name. Line 98 and 101
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, removed from examples