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

[kjobctl] Add list job #2413

Merged
merged 1 commit into from
Jul 10, 2024
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
8 changes: 8 additions & 0 deletions cmd/experimental/kjobctl/pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/utils/clock"

"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/completion"
"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/create"
"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/list"
"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/util"
)

type KjobctlOptions struct {
Clock clock.Clock
ConfigFlags *genericclioptions.ConfigFlags

genericiooptions.IOStreams
Expand All @@ -52,6 +55,10 @@ func NewKjobctlCmd(o KjobctlOptions) *cobra.Command {
Short: "ML/AI/Batch Jobs Made Easy",
}

if o.Clock == nil {
o.Clock = clock.RealClock{}
}

flags := cmd.PersistentFlags()

configFlags := o.ConfigFlags
Expand All @@ -68,6 +75,7 @@ func NewKjobctlCmd(o KjobctlOptions) *cobra.Command {
cobra.CheckErr(cmd.RegisterFlagCompletionFunc("user", completion.UsersFunc(clientGetter)))

cmd.AddCommand(create.NewCreateCmd(clientGetter, o.IOStreams))
cmd.AddCommand(list.NewListCmd(clientGetter, o.IOStreams, o.Clock))

return cmd
}
47 changes: 47 additions & 0 deletions cmd/experimental/kjobctl/pkg/cmd/list/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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"
"os"
"strconv"
)

const (
defaultListRequestLimit = 100
KjobctlListRequestLimitEnvName = "KJOBCTL_LIST_REQUEST_LIMIT"
)

var (
invalidListRequestLimitError = errors.New("invalid list request limit")
)

func listRequestLimit() (int64, error) {
listRequestLimitEnv := os.Getenv(KjobctlListRequestLimitEnvName)

if len(listRequestLimitEnv) == 0 {
return defaultListRequestLimit, nil
}

limit, err := strconv.ParseInt(listRequestLimitEnv, 10, 64)
if err != nil {
return 0, invalidListRequestLimitError
}

return limit, nil
}
43 changes: 43 additions & 0 deletions cmd/experimental/kjobctl/pkg/cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/utils/clock"

"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/util"
)

const (
listExample = ` # List Job
kjobctl list job`
)

func NewListCmd(clientGetter util.ClientGetter, streams genericiooptions.IOStreams, clock clock.Clock) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Display resources",
Example: listExample,
SuggestFor: []string{"ps"},
}

cmd.AddCommand(NewJobCmd(clientGetter, streams, clock))

return cmd
}
194 changes: 194 additions & 0 deletions cmd/experimental/kjobctl/pkg/cmd/list/list_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
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/client-go/kubernetes/scheme"
batchv1 "k8s.io/client-go/kubernetes/typed/batch/v1"
"k8s.io/utils/clock"

"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/cmd/util"
"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/constants"
)

const (
jobExample = ` # List Job
kjobctl list job`
)

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

Limit int64
AllNamespaces bool
Namespace string
ProfileFilter string
FieldSelector string
LabelSelector string
ClusterQueueFilter string

Client batchv1.BatchV1Interface

genericiooptions.IOStreams
}

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

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

cmd := &cobra.Command{
Use: "job [--selector key1=value1] [--field-selector key1=value1] [--all-namespaces]",
DisableFlagsInUseLine: true,
Short: "List Job",
Example: jobExample,
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.Complete(clientGetter))
cobra.CheckErr(o.Run(cmd.Context()))
},
}

o.PrintFlags.AddFlags(cmd)

util.AddAllNamespacesFlagVar(cmd, &o.AllNamespaces)
util.AddFieldSelectorFlagVar(cmd, &o.FieldSelector)
util.AddLabelSelectorFlagVar(cmd, &o.LabelSelector)
util.AddProfileFlagVar(cmd, &o.ProfileFilter)

return cmd
}

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

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

o.Namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

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

o.Client = clientset.BatchV1()

return nil
}

func (o *JobOptions) ToPrinter(headers bool) (printers.ResourcePrinterFunc, error) {
if !o.PrintFlags.OutputFlagSpecified() {
printer := newJobTablePrinter().
WithNamespace(o.AllNamespaces).
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 *JobOptions) Run(ctx context.Context) error {
var totalCount int

namespace := o.Namespace
if o.AllNamespaces {
namespace = ""
}

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

if len(o.ProfileFilter) > 0 {
opts.LabelSelector = fmt.Sprintf("%s,%s=%s", opts.LabelSelector, constants.ProfileLabel, o.ProfileFilter)
}
if len(o.LabelSelector) > 0 {
opts.LabelSelector = fmt.Sprintf("%s,%s", opts.LabelSelector, o.LabelSelector)
}

tabWriter := printers.GetNewTabWriter(o.Out)

for {
headers := totalCount == 0

list, err := o.Client.Jobs(namespace).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 {
if !o.AllNamespaces {
fmt.Fprintf(o.ErrOut, "No resources found in %s namespace.\n", o.Namespace)
} else {
fmt.Fprintln(o.ErrOut, "No resources found")
}
return nil
}

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

return nil
}
}
Loading