Skip to content

Commit

Permalink
[kueuectl] Added list resourceflavor command.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbobrovskyi committed Jul 3, 2024
1 parent 33b9efb commit 738a3b4
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/kueuectl/app/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewListCmd(clientGetter util.ClientGetter, streams genericiooptions.IOStrea
cmd.AddCommand(NewLocalQueueCmd(clientGetter, streams, clock))
cmd.AddCommand(NewClusterQueueCmd(clientGetter, streams, clock))
cmd.AddCommand(NewWorkloadCmd(clientGetter, streams, clock))
cmd.AddCommand(NewResourceFlavorCmd(clientGetter, streams, clock))

return cmd
}
164 changes: 164 additions & 0 deletions cmd/kueuectl/app/list/list_resourceflavor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package list

import (
"context"
"fmt"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/utils/clock"

"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"
)

const (
rfExample = ` # List ResourceFlavor
kueuectl list resourceflavor`
)

type ResourceFlavorOptions struct {
Clock clock.Clock
PrintFlags *genericclioptions.PrintFlags

Limit int64
FieldSelector string
LabelSelector string

Client kueuev1beta1.KueueV1beta1Interface

genericiooptions.IOStreams
}

func NewResourceFlavorOptions(streams genericiooptions.IOStreams, clock clock.Clock) *ResourceFlavorOptions {
return &ResourceFlavorOptions{
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme),
IOStreams: streams,
Clock: clock,
}
}

func NewResourceFlavorCmd(clientGetter util.ClientGetter, streams genericiooptions.IOStreams, clock clock.Clock) *cobra.Command {
o := NewResourceFlavorOptions(streams, clock)

cmd := &cobra.Command{
Use: "resourceflavor [--selector KEY=VALUE] [--field-selector FIELD_NAME=VALUE]",
DisableFlagsInUseLine: true,
Aliases: []string{"rf"},
Short: "List ResourceFlavor",
Example: rfExample,
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.Complete(clientGetter))
cobra.CheckErr(o.Run(cmd.Context()))
},
}

o.PrintFlags.AddFlags(cmd)

addFieldSelectorFlagVar(cmd, &o.FieldSelector)
addLabelSelectorFlagVar(cmd, &o.LabelSelector)

return cmd
}

// Complete completes all the required options
func (o *ResourceFlavorOptions) Complete(clientGetter util.ClientGetter) error {
var err error

o.Limit, err = listRequestLimit()
if err != nil {
return err
}

clientset, err := clientGetter.KueueClientSet()
if err != nil {
return err
}

o.Client = clientset.KueueV1beta1()

return nil
}

func (o *ResourceFlavorOptions) ToPrinter(headers bool) (printers.ResourcePrinterFunc, error) {
if !o.PrintFlags.OutputFlagSpecified() {
printer := newResourceFlavorTablePrinter().WithHeaders(headers).WithClock(o.Clock)
return printer.PrintObj, nil
}

printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return nil, err
}

return printer.PrintObj, nil
}

// Run performs the list operation.
func (o *ResourceFlavorOptions) Run(ctx context.Context) error {
var totalCount int

opts := metav1.ListOptions{
LabelSelector: o.LabelSelector,
FieldSelector: o.FieldSelector,
Limit: o.Limit,
}

tabWriter := printers.GetNewTabWriter(o.Out)

for {
headers := totalCount == 0

list, err := o.Client.ResourceFlavors().List(ctx, opts)
if err != nil {
return err
}

totalCount += len(list.Items)

printer, err := o.ToPrinter(headers)
if err != nil {
return err
}

if err := printer.PrintObj(list, tabWriter); err != nil {
return err
}

if list.Continue != "" {
opts.Continue = list.Continue
continue
}

if totalCount == 0 {
fmt.Fprintln(o.ErrOut, "No resources found")
return nil
}

if err := tabWriter.Flush(); err != nil {
return err
}

return nil
}
}
89 changes: 89 additions & 0 deletions cmd/kueuectl/app/list/list_resourceflavor_printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package list

import (
"errors"
"io"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/utils/clock"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
)

type listResourceFlavorPrinter struct {
clock clock.Clock
printOptions printers.PrintOptions
}

var _ printers.ResourcePrinter = (*listResourceFlavorPrinter)(nil)

func (p *listResourceFlavorPrinter) PrintObj(obj runtime.Object, out io.Writer) error {
printer := printers.NewTablePrinter(p.printOptions)

list, ok := obj.(*v1beta1.ResourceFlavorList)
if !ok {
return errors.New("invalid object type")
}

table := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name"},
{Name: "Age", Type: "string"},
},
Rows: p.printResourceFlavorList(list),
}

return printer.PrintObj(table, out)
}

func (p *listResourceFlavorPrinter) WithHeaders(f bool) *listResourceFlavorPrinter {
p.printOptions.NoHeaders = !f
return p
}

func (p *listResourceFlavorPrinter) WithClock(c clock.Clock) *listResourceFlavorPrinter {
p.clock = c
return p
}

func newResourceFlavorTablePrinter() *listResourceFlavorPrinter {
return &listResourceFlavorPrinter{
clock: clock.RealClock{},
}
}

func (p *listResourceFlavorPrinter) printResourceFlavorList(list *v1beta1.ResourceFlavorList) []metav1.TableRow {
rows := make([]metav1.TableRow, len(list.Items))
for index := range list.Items {
rows[index] = p.printResourceFlavor(&list.Items[index])
}
return rows
}

func (p *listResourceFlavorPrinter) printResourceFlavor(resourceFlavor *v1beta1.ResourceFlavor) metav1.TableRow {
row := metav1.TableRow{Object: runtime.RawExtension{Object: resourceFlavor}}
row.Cells = []any{
resourceFlavor.Name,
duration.HumanDuration(p.clock.Since(resourceFlavor.CreationTimestamp.Time)),
}
return row
}
70 changes: 70 additions & 0 deletions cmd/kueuectl/app/list/list_resourceflavor_printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package list

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
testingclock "k8s.io/utils/clock/testing"
utiltesting "sigs.k8s.io/kueue/pkg/util/testing"

"sigs.k8s.io/kueue/apis/kueue/v1beta1"
)

func TestResourceFlavorPrint(t *testing.T) {
testStartTime := time.Now()

testCases := map[string]struct {
options *ResourceFlavorOptions
in *v1beta1.ResourceFlavorList
out []metav1.TableRow
}{
"should print resource flavor list": {
options: &ResourceFlavorOptions{},
in: &v1beta1.ResourceFlavorList{
Items: []v1beta1.ResourceFlavor{
*utiltesting.MakeResourceFlavor("rf").
Creation(testStartTime.Add(-time.Hour).Truncate(time.Second)).
Obj(),
},
},
out: []metav1.TableRow{
{
Cells: []any{"rf", "60m"},
Object: runtime.RawExtension{
Object: utiltesting.MakeResourceFlavor("rf").
Creation(testStartTime.Add(-time.Hour).Truncate(time.Second)).
Obj(),
},
},
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
p := newResourceFlavorTablePrinter().WithClock(testingclock.NewFakeClock(testStartTime))
out := p.printResourceFlavorList(tc.in)
if diff := cmp.Diff(tc.out, out); diff != "" {
t.Errorf("Unexpected result (-want,+got):\n%s", diff)
}
})
}
}
Loading

0 comments on commit 738a3b4

Please sign in to comment.