Skip to content

Commit

Permalink
[kueuectl] Added list workloads command.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbobrovskyi committed May 14, 2024
1 parent 5f455b0 commit 2846e49
Show file tree
Hide file tree
Showing 12 changed files with 907 additions and 31 deletions.
7 changes: 6 additions & 1 deletion cmd/kueuectl/app/list/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func addAllNamespacesFlagVar(cmd *cobra.Command, p *bool) {

func addClusterQueueFilterFlagVar(cmd *cobra.Command, p *string) {
cmd.Flags().StringVarP(p, "clusterqueue", "c", "",
"Filter by cluster queue name which associated with the local queue.")
"Filter by cluster queue name which associated with resource.")
}

func addLocalQueueFilterFlagVar(cmd *cobra.Command, p *string) {
cmd.Flags().StringVarP(p, "localqueue", "q", "",
"Filter by local queue name which associated with resource.")
}

func addActiveFilterFlagVar(cmd *cobra.Command, p *[]bool) {
Expand Down
9 changes: 8 additions & 1 deletion cmd/kueuectl/app/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ import (

const (
listExample = ` # List LocalQueue
kueuectl list localqueue`
kueuectl list localqueue
# List ClusterQueue
kueuectl list clusterqueue
# List Workload
kueuectl list workload`
)

func NewListCmd(clientGetter util.ClientGetter, streams genericiooptions.IOStreams) *cobra.Command {
Expand All @@ -38,6 +44,7 @@ func NewListCmd(clientGetter util.ClientGetter, streams genericiooptions.IOStrea

cmd.AddCommand(NewLocalQueueCmd(clientGetter, streams))
cmd.AddCommand(NewClusterQueueCmd(clientGetter, streams))
cmd.AddCommand(NewWorkloadCmd(clientGetter, streams))

return cmd
}
8 changes: 1 addition & 7 deletions cmd/kueuectl/app/list/list_clusterqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"k8s.io/cli-runtime/pkg/printers"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/client-go/clientset/versioned"
"sigs.k8s.io/kueue/client-go/clientset/versioned/scheme"
kueuev1beta1 "sigs.k8s.io/kueue/client-go/clientset/versioned/typed/kueue/v1beta1"
"sigs.k8s.io/kueue/cmd/kueuectl/app/util"
Expand Down Expand Up @@ -104,12 +103,7 @@ func NewClusterQueueCmd(clientGetter util.ClientGetter, streams genericiooptions
func (o *ClusterQueueOptions) Complete(clientGetter util.ClientGetter, cmd *cobra.Command, args []string) error {
var err error

config, err := clientGetter.ToRESTConfig()
if err != nil {
return err
}

clientset, err := versioned.NewForConfig(config)
clientset, err := clientGetter.KueueClientSet()
if err != nil {
return err
}
Expand Down
18 changes: 7 additions & 11 deletions cmd/kueuectl/app/list/list_localqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
)

const defaultNamespace = "default"

func TestLocalQueueFilter(t *testing.T) {
testCases := map[string]struct {
options *LocalQueueOptions
Expand Down Expand Up @@ -130,13 +128,13 @@ lq1 cq1 1 1 60m
"should print local queue list with clusterqueue filter": {
args: []string{"--clusterqueue", "cq1"},
objs: []runtime.Object{
utiltesting.MakeLocalQueue("lq1", defaultNamespace).
utiltesting.MakeLocalQueue("lq1", cmdtesting.DefaultNamespace).
ClusterQueue("cq1").
PendingWorkloads(1).
AdmittedWorkloads(1).
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeLocalQueue("lq2", defaultNamespace).
utiltesting.MakeLocalQueue("lq2", cmdtesting.DefaultNamespace).
ClusterQueue("cq2").
PendingWorkloads(2).
AdmittedWorkloads(2).
Expand All @@ -150,14 +148,14 @@ lq1 cq1 1 1 60m
"should print local queue list with label selector filter": {
args: []string{"--selector", "key=value1"},
objs: []runtime.Object{
utiltesting.MakeLocalQueue("lq1", defaultNamespace).
utiltesting.MakeLocalQueue("lq1", cmdtesting.DefaultNamespace).
ClusterQueue("cq1").
PendingWorkloads(1).
AdmittedWorkloads(1).
Label("key", "value1").
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeLocalQueue("lq2", defaultNamespace).
utiltesting.MakeLocalQueue("lq2", cmdtesting.DefaultNamespace).
ClusterQueue("cq2").
PendingWorkloads(2).
AdmittedWorkloads(2).
Expand All @@ -172,14 +170,14 @@ lq1 cq1 1 1 60m
"should print local queue list with label selector filter (short flag)": {
args: []string{"-l", "foo=bar"},
objs: []runtime.Object{
utiltesting.MakeLocalQueue("lq1", defaultNamespace).
utiltesting.MakeLocalQueue("lq1", cmdtesting.DefaultNamespace).
ClusterQueue("cq1").
PendingWorkloads(1).
AdmittedWorkloads(1).
Label("foo", "bar").
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeLocalQueue("lq2", defaultNamespace).
utiltesting.MakeLocalQueue("lq2", cmdtesting.DefaultNamespace).
ClusterQueue("cq2").
PendingWorkloads(2).
AdmittedWorkloads(2).
Expand All @@ -191,7 +189,7 @@ lq1 cq1 1 1 60m
`,
},
"should print not found error": {
wantOutErr: fmt.Sprintf("No resources found in %s namespace.\n", defaultNamespace),
wantOutErr: fmt.Sprintf("No resources found in %s namespace.\n", cmdtesting.DefaultNamespace),
},
"should print not found error with all-namespaces filter": {
args: []string{"-A"},
Expand All @@ -205,8 +203,6 @@ lq1 cq1 1 1 60m
tf := cmdtesting.NewTestClientGetter()
if len(tc.ns) > 0 {
tf.WithNamespace(tc.ns)
} else {
tf.WithNamespace(defaultNamespace)
}

tf.ClientSet = fake.NewSimpleClientset(tc.objs...)
Expand Down
95 changes: 91 additions & 4 deletions cmd/kueuectl/app/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/client-go/clientset/versioned/fake"
cmdtesting "sigs.k8s.io/kueue/cmd/kueuectl/app/testing"
utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
Expand All @@ -26,6 +29,7 @@ func TestListCmd(t *testing.T) {
wantErr error
}{
"should print local queue list with all namespaces": {
args: []string{"localqueue", "--all-namespaces"},
objs: []runtime.Object{
utiltesting.MakeLocalQueue("lq1", "ns1").
ClusterQueue("cq1").
Expand All @@ -40,10 +44,95 @@ func TestListCmd(t *testing.T) {
Creation(testStartTime.Add(-2 * time.Hour).Truncate(time.Second)).
Obj(),
},
args: []string{"--all-namespaces"},
wantOut: `NAMESPACE NAME CLUSTERQUEUE PENDING WORKLOADS ADMITTED WORKLOADS AGE
ns1 lq1 cq1 1 1 60m
ns2 lq2 cq2 2 2 120m
`,
},
"should print local queue list with all namespaces (short command and flag)": {
args: []string{"lq", "-A"},
objs: []runtime.Object{
utiltesting.MakeLocalQueue("lq1", "ns1").
ClusterQueue("cq1").
PendingWorkloads(1).
AdmittedWorkloads(1).
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeLocalQueue("lq2", "ns2").
ClusterQueue("cq2").
PendingWorkloads(2).
AdmittedWorkloads(2).
Creation(testStartTime.Add(-2 * time.Hour).Truncate(time.Second)).
Obj(),
},
wantOut: `NAMESPACE NAME CLUSTERQUEUE PENDING WORKLOADS ADMITTED WORKLOADS AGE
ns1 lq1 cq1 1 1 60m
ns2 lq2 cq2 2 2 120m
`,
},
"should print cluster queue list": {
args: []string{"clusterqueue"},
objs: []runtime.Object{
utiltesting.MakeClusterQueue("cq1").
Condition(v1beta1.ClusterQueueActive, metav1.ConditionTrue, "", "").
Cohort("cohort").
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeClusterQueue("cq2").
Condition(v1beta1.ClusterQueueActive, metav1.ConditionFalse, "", "").
Cohort("cohort").
Creation(testStartTime.Add(-2 * time.Hour).Truncate(time.Second)).
Obj(),
},
wantOut: `NAME COHORT PENDING WORKLOADS ADMITTED WORKLOADS ACTIVE AGE
cq1 cohort 0 0 true 60m
cq2 cohort 0 0 false 120m
`,
},
"should print workload list with all namespaces": {
args: []string{"workload", "--all-namespaces"},
objs: []runtime.Object{
utiltesting.MakeWorkload("wl1", "ns1").
OwnerReference(schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}, "j1", "test-uid").
Queue("lq1").
Active(true).
Admission(utiltesting.MakeAdmission("cq1").Obj()).
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeWorkload("wl2", "ns2").
OwnerReference(schema.GroupVersionKind{Group: "ray.io", Version: "v1", Kind: "RayJob"}, "j2", "test-uid").
Queue("lq2").
Active(true).
Admission(utiltesting.MakeAdmission("cq2").Obj()).
Creation(testStartTime.Add(-2 * time.Hour).Truncate(time.Second)).
Obj(),
},
wantOut: `NAMESPACE NAME CRD TYPE CRD NAME LOCALQUEUE CLUSTERQUEUE STATUS POSITION IN QUEUE AGE
ns1 wl1 j1 lq1 cq1 true 60m
ns2 wl2 j2 lq2 cq2 true 120m
`,
},
"should print workload list with all namespaces (short command and flag)": {
args: []string{"wl", "-A"},
objs: []runtime.Object{
utiltesting.MakeWorkload("wl1", "ns1").
OwnerReference(schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}, "j1", "test-uid").
Queue("lq1").
Active(true).
Admission(utiltesting.MakeAdmission("cq1").Obj()).
Creation(testStartTime.Add(-1 * time.Hour).Truncate(time.Second)).
Obj(),
utiltesting.MakeWorkload("wl2", "ns2").
OwnerReference(schema.GroupVersionKind{Group: "ray.io", Version: "v1", Kind: "RayJob"}, "j2", "test-uid").
Queue("lq2").
Active(true).
Admission(utiltesting.MakeAdmission("cq2").Obj()).
Creation(testStartTime.Add(-2 * time.Hour).Truncate(time.Second)).
Obj(),
},
wantOut: `NAMESPACE NAME CRD TYPE CRD NAME LOCALQUEUE CLUSTERQUEUE STATUS POSITION IN QUEUE AGE
ns1 wl1 j1 lq1 cq1 true 60m
ns2 wl2 j2 lq2 cq2 true 120m
`,
},
}
Expand All @@ -54,13 +143,11 @@ ns2 lq2 cq2 2 2 120m
tf := cmdtesting.NewTestClientGetter()
if len(tc.ns) > 0 {
tf.WithNamespace(tc.ns)
} else {
tf.WithNamespace(defaultNamespace)
}

tf.ClientSet = fake.NewSimpleClientset(tc.objs...)

cmd := NewLocalQueueCmd(tf, streams)
cmd := NewListCmd(tf, streams)
cmd.SetArgs(tc.args)

gotErr := cmd.Execute()
Expand Down
Loading

0 comments on commit 2846e49

Please sign in to comment.