Skip to content
Open
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
28 changes: 28 additions & 0 deletions kubectl-plugin/pkg/cmd/get/get_workergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -46,6 +47,8 @@ type workerGroup struct {
cluster string
readyReplicas int32
desiredReplicas int32
minReplicas int32
maxReplicas int32
}

var getWorkerGroupsExample = templates.Examples(`
Expand Down Expand Up @@ -223,6 +226,19 @@ func getWorkerGroupDetails(ctx context.Context, enrichedWorkerGroupSpecs []enric

workerGroupResources := calculateDesiredResourcesForWorkerGroup(ewgs.spec)

var (
minReplicas int32
maxReplicas int32 = math.MaxInt32
)

if ewgs.spec.MinReplicas != nil {
minReplicas = *ewgs.spec.MinReplicas
}

if ewgs.spec.MaxReplicas != nil {
maxReplicas = *ewgs.spec.MaxReplicas
}

workerGroups = append(workerGroups, workerGroup{
namespace: ewgs.namespace,
name: ewgs.spec.GroupName,
Expand All @@ -233,6 +249,8 @@ func getWorkerGroupDetails(ctx context.Context, enrichedWorkerGroupSpecs []enric
totalTPU: workerGroupResources[corev1.ResourceName(util.ResourceGoogleTPU)],
totalMemory: *workerGroupResources.Memory(),
cluster: ewgs.cluster,
minReplicas: minReplicas,
maxReplicas: maxReplicas,
})
}

Expand Down Expand Up @@ -267,6 +285,8 @@ func printWorkerGroups(workerGroups []workerGroup, allNamespaces bool, output io

columns = append(columns, []v1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Min", Type: "string"},
{Name: "Max", Type: "string"},
{Name: "Replicas", Type: "string"},
{Name: "CPUs", Type: "string"},
{Name: "GPUs", Type: "string"},
Expand All @@ -283,8 +303,16 @@ func printWorkerGroups(workerGroups []workerGroup, allNamespaces bool, output io
row.Cells = append(row.Cells, wg.namespace)
}

minStr := fmt.Sprintf("%d", wg.minReplicas)
maxStr := "-"
if wg.maxReplicas != math.MaxInt32 {
maxStr = fmt.Sprintf("%d", wg.maxReplicas)
}

row.Cells = append(row.Cells, []interface{}{
wg.name,
minStr,
maxStr,
fmt.Sprintf("%d/%d", wg.readyReplicas, wg.desiredReplicas),
wg.totalCPU.String(),
wg.totalGPU.String(),
Expand Down
100 changes: 58 additions & 42 deletions kubectl-plugin/pkg/cmd/get/get_workergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"math"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -139,9 +140,11 @@ func TestRayWorkerGroupsGetRun(t *testing.T) {
Spec: rayv1.RayClusterSpec{
WorkerGroupSpecs: []rayv1.WorkerGroupSpec{
{
GroupName: "group-1",
Replicas: ptr.To(int32(1)),
Template: podTemplate,
GroupName: "group-1",
Replicas: ptr.To(int32(1)),
MinReplicas: ptr.To(int32(1)),
MaxReplicas: ptr.To(int32(5)),
Template: podTemplate,
},
},
},
Expand All @@ -154,9 +157,11 @@ func TestRayWorkerGroupsGetRun(t *testing.T) {
Spec: rayv1.RayClusterSpec{
WorkerGroupSpecs: []rayv1.WorkerGroupSpec{
{
GroupName: "group-2",
Replicas: ptr.To(int32(1)),
Template: podTemplate,
GroupName: "group-2",
Replicas: ptr.To(int32(1)),
MinReplicas: ptr.To(int32(0)),
MaxReplicas: ptr.To(int32(math.MaxInt32)),
Template: podTemplate,
},
},
},
Expand All @@ -169,14 +174,18 @@ func TestRayWorkerGroupsGetRun(t *testing.T) {
Spec: rayv1.RayClusterSpec{
WorkerGroupSpecs: []rayv1.WorkerGroupSpec{
{
GroupName: "group-1",
Replicas: ptr.To(int32(2)),
Template: podTemplate,
GroupName: "group-1",
Replicas: ptr.To(int32(2)),
MinReplicas: ptr.To(int32(1)),
MaxReplicas: ptr.To(int32(4)),
Template: podTemplate,
},
{
GroupName: "group-4",
Replicas: ptr.To(int32(0)),
Template: podTemplate,
GroupName: "group-4",
Replicas: ptr.To(int32(0)),
MinReplicas: ptr.To(int32(0)),
MaxReplicas: ptr.To(int32(3)),
Template: podTemplate,
},
},
},
Expand Down Expand Up @@ -263,11 +272,11 @@ func TestRayWorkerGroupsGetRun(t *testing.T) {
allNamespaces: true,
rayClusters: rayClusters,
pods: pods,
expected: `NAMESPACE NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1/1 2 1 1 1Gi cluster-1
namespace-1 group-2 1/1 2 1 1 1Gi cluster-2
namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
namespace-2 group-4 0/0 0 0 0 0 cluster-1
expected: `NAMESPACE NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1 5 1/1 2 1 1 1Gi cluster-1
namespace-1 group-2 0 - 1/1 2 1 1 1Gi cluster-2
namespace-2 group-1 1 4 1/2 4 2 2 2Gi cluster-1
namespace-2 group-4 0 3 0/0 0 0 0 0 cluster-1
`,
},
{
Expand All @@ -279,10 +288,10 @@ namespace-2 group-4 0/0 0 0 0 0 cluster-1
// See https://github.com/kubernetes/client-go/issues/326
rayClusters: []runtime.Object{rayClusters[0], rayClusters[2]},
pods: pods,
expected: `NAMESPACE NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
namespace-2 group-4 0/0 0 0 0 0 cluster-1
expected: `NAMESPACE NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1 5 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1 4 1/2 4 2 2 2Gi cluster-1
namespace-2 group-4 0 3 0/0 0 0 0 0 cluster-1
`,
},
{
Expand All @@ -291,9 +300,9 @@ namespace-2 group-4 0/0 0 0 0 0 cluster-1
allNamespaces: false,
rayClusters: rayClusters,
pods: pods,
expected: `NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1/1 2 1 1 1Gi cluster-1
group-2 1/1 2 1 1 1Gi cluster-2
expected: `NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1 5 1/1 2 1 1 1Gi cluster-1
group-2 0 - 1/1 2 1 1 1Gi cluster-2
`,
},
{
Expand All @@ -306,8 +315,8 @@ group-2 1/1 2 1 1 1Gi cluster-2
// See https://github.com/kubernetes/client-go/issues/326
rayClusters: rayClusters[:1],
pods: pods,
expected: `NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1/1 2 1 1 1Gi cluster-1
expected: `NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1 5 1/1 2 1 1 1Gi cluster-1
`,
},
{
Expand All @@ -316,9 +325,9 @@ group-1 1/1 2 1 1 1Gi cluster-1
workerGroup: "group-1",
rayClusters: rayClusters,
pods: pods,
expected: `NAMESPACE NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
expected: `NAMESPACE NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1 5 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1 4 1/2 4 2 2 2Gi cluster-1
`,
},
{
Expand All @@ -331,9 +340,9 @@ namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
// See https://github.com/kubernetes/client-go/issues/326
rayClusters: []runtime.Object{rayClusters[0], rayClusters[2]},
pods: pods,
expected: `NAMESPACE NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
expected: `NAMESPACE NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 group-1 1 5 1/1 2 1 1 1Gi cluster-1
namespace-2 group-1 1 4 1/2 4 2 2 2Gi cluster-1
`,
},
{
Expand All @@ -343,8 +352,8 @@ namespace-2 group-1 1/2 4 2 2 2Gi cluster-1
workerGroup: "group-1",
rayClusters: rayClusters,
pods: pods,
expected: `NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1/1 2 1 1 1Gi cluster-1
expected: `NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1 5 1/1 2 1 1 1Gi cluster-1
`,
},
{
Expand All @@ -358,8 +367,8 @@ group-1 1/1 2 1 1 1Gi cluster-1
// See https://github.com/kubernetes/client-go/issues/326
rayClusters: rayClusters[:1],
pods: pods,
expected: `NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1/1 2 1 1 1Gi cluster-1
expected: `NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
group-1 1 5 1/1 2 1 1 1Gi cluster-1
`,
},
{
Expand Down Expand Up @@ -624,6 +633,7 @@ func TestGetWorkerGroupDetails(t *testing.T) {
namespace: "namespace-1",
cluster: "cluster-1",
name: "group-1",
maxReplicas: math.MaxInt32,
readyReplicas: 1,
desiredReplicas: 1,
totalCPU: *resources.Cpu(),
Expand All @@ -635,6 +645,7 @@ func TestGetWorkerGroupDetails(t *testing.T) {
namespace: "namespace-1",
cluster: "cluster-2",
name: "group-2",
maxReplicas: math.MaxInt32,
readyReplicas: 1,
desiredReplicas: 1,
totalCPU: *resources.Cpu(),
Expand All @@ -646,6 +657,7 @@ func TestGetWorkerGroupDetails(t *testing.T) {
namespace: "namespace-2",
cluster: "cluster-1",
name: "group-1",
maxReplicas: math.MaxInt32,
readyReplicas: 1,
desiredReplicas: 1,
totalCPU: *resources.Cpu(),
Expand Down Expand Up @@ -742,6 +754,8 @@ func TestPrintWorkerGroups(t *testing.T) {
name: "pod-1",
readyReplicas: 1,
desiredReplicas: 2,
minReplicas: 0,
maxReplicas: math.MaxInt32,
totalCPU: *resources.Cpu(),
totalGPU: *resources.Name(util.ResourceNvidiaGPU, resource.DecimalSI),
totalTPU: *resources.Name(util.ResourceGoogleTPU, resource.DecimalSI),
Expand All @@ -753,6 +767,8 @@ func TestPrintWorkerGroups(t *testing.T) {
name: "pod-2",
readyReplicas: 3,
desiredReplicas: 3,
minReplicas: 1,
maxReplicas: 5,
totalCPU: *resources.Cpu(),
totalGPU: *resources.Name(util.ResourceNvidiaGPU, resource.DecimalSI),
totalTPU: *resources.Name(util.ResourceGoogleTPU, resource.DecimalSI),
Expand All @@ -768,17 +784,17 @@ func TestPrintWorkerGroups(t *testing.T) {
{
name: "one namespace",
allNamespaces: false,
expected: `NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
pod-1 1/2 1 1 1 1Gi cluster-1
pod-2 3/3 1 1 1 1Gi cluster-2
expected: `NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
pod-1 0 - 1/2 1 1 1 1Gi cluster-1
pod-2 1 5 3/3 1 1 1 1Gi cluster-2
`,
},
{
name: "all namespaces",
allNamespaces: true,
expected: `NAMESPACE NAME REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 pod-1 1/2 1 1 1 1Gi cluster-1
namespace-2 pod-2 3/3 1 1 1 1Gi cluster-2
expected: `NAMESPACE NAME MIN MAX REPLICAS CPUS GPUS TPUS MEMORY CLUSTER
namespace-1 pod-1 0 - 1/2 1 1 1 1Gi cluster-1
namespace-2 pod-2 1 5 3/3 1 1 1 1Gi cluster-2
`,
},
}
Expand Down
Loading
Loading