Skip to content
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

[fix][kubectl-plugin] set worker group CPU limit #2958

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions kubectl-plugin/pkg/cmd/create/create_workergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"k8s.io/kubectl/pkg/util/templates"
)

const (
resourceNvidiaGPU = "nvidia.com/gpu"
)

type CreateWorkerGroupOptions struct {
configFlags *genericclioptions.ConfigFlags
ioStreams *genericclioptions.IOStreams
Expand Down Expand Up @@ -132,6 +136,19 @@ func (options *CreateWorkerGroupOptions) Run(ctx context.Context, factory cmduti
}

newRayCluster := rayCluster.DeepCopy()

newRayCluster.Spec.WorkerGroupSpecs = append(newRayCluster.Spec.WorkerGroupSpecs, createWorkerGroupSpec(options))

newRayCluster, err = k8sClient.RayClient().RayV1().RayClusters(*options.configFlags.Namespace).Update(ctx, newRayCluster, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error updating Ray cluster with new worker group: %w", err)
}

fmt.Printf("Updated Ray cluster %s/%s with new worker group\n", newRayCluster.Namespace, newRayCluster.Name)
return nil
}

func createWorkerGroupSpec(options *CreateWorkerGroupOptions) rayv1.WorkerGroupSpec {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a function for unit testing

podTemplate := corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
Expand All @@ -155,25 +172,16 @@ func (options *CreateWorkerGroupOptions) Run(ctx context.Context, factory cmduti

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
podTemplate.Spec.Containers[0].Resources.Requests[corev1.ResourceName(resourceNvidiaGPU)] = gpuResource
podTemplate.Spec.Containers[0].Resources.Limits[corev1.ResourceName(resourceNvidiaGPU)] = gpuResource
}

workerGroup := rayv1.WorkerGroupSpec{
return 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 Ray cluster with new worker group: %w", err)
}

fmt.Printf("Updated Ray cluster %s/%s with new worker group\n", newRayCluster.Namespace, newRayCluster.Name)
return nil
}
57 changes: 57 additions & 0 deletions kubectl-plugin/pkg/cmd/create/create_workergroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package create

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"

rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
)

func TestCreateWorkerGroupSpec(t *testing.T) {
options := &CreateWorkerGroupOptions{
groupName: "example-group",
image: "DEADBEEF",
workerReplicas: 3,
workerMinReplicas: 1,
workerMaxReplicas: 5,
workerCPU: "2",
workerMemory: "5Gi",
workerGPU: "1",
}

expected := rayv1.WorkerGroupSpec{
RayStartParams: map[string]string{},
GroupName: "example-group",
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "ray-worker",
Image: "DEADBEEF",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
resourceNvidiaGPU: resource.MustParse("1"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
resourceNvidiaGPU: resource.MustParse("1"),
},
},
},
},
},
},
Replicas: ptr.To[int32](3),
MinReplicas: ptr.To[int32](1),
MaxReplicas: ptr.To[int32](5),
}

assert.Equal(t, expected, createWorkerGroupSpec(options))
}
Loading