generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180e25f
commit 73a1ee0
Showing
10 changed files
with
947 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.