generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kueuectl] Add list LocalQueue command.
- Loading branch information
1 parent
5054b04
commit 9e40be7
Showing
14 changed files
with
821 additions
and
40 deletions.
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,41 @@ | ||
/* | ||
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" | ||
) | ||
|
||
func addFieldSelectorFlagVar(cmd *cobra.Command, p *string) { | ||
cmd.Flags().StringVar(p, "field-selector", "", | ||
"Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.") | ||
} | ||
|
||
func addLabelSelectorFlagVar(cmd *cobra.Command, p *string) { | ||
cmd.Flags().StringVarP(p, "selector", "l", "", | ||
"Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.") | ||
} | ||
|
||
func addAllNamespacesFlagVar(cmd *cobra.Command, p *bool) { | ||
cmd.Flags().BoolVarP(p, "all-namespaces", "A", false, | ||
"If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.") | ||
} | ||
|
||
func addClusterQueueFilterFlagVar(cmd *cobra.Command, p *string) { | ||
cmd.Flags().StringVarP(p, "clusterqueue", "c", "", | ||
"Filter by cluster queue name which associated with the local queue.") | ||
} |
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,40 @@ | ||
/* | ||
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/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/genericiooptions" | ||
) | ||
|
||
const ( | ||
listExample = ` # List LocalQueue | ||
kueuectl list localqueue` | ||
) | ||
|
||
func NewListCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Display resources", | ||
Example: listExample, | ||
} | ||
|
||
cmd.AddCommand(NewLocalQueueCmd(clientGetter, streams)) | ||
|
||
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,217 @@ | ||
/* | ||
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" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/duration" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/genericiooptions" | ||
"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" | ||
) | ||
|
||
const ( | ||
lqLong = `Lists LocalQueues that match the given criteria: point to a specific CQ, | ||
being active/inactive, belonging to the specified namespace, matching | ||
the label selector or the field selector.` | ||
lqExample = ` # List LocalQueue | ||
kueuectl list localqueue` | ||
) | ||
|
||
type LocalQueueOptions struct { | ||
PrintFlags *genericclioptions.PrintFlags | ||
|
||
AllNamespaces bool | ||
EnforceNamespace bool | ||
Namespace string | ||
FieldSelector string | ||
LabelSelector string | ||
ClusterQueueFilter string | ||
|
||
Client kueuev1beta1.KueueV1beta1Interface | ||
|
||
TablePrintObj printers.ResourcePrinterFunc | ||
DefaultPrintObj printers.ResourcePrinterFunc | ||
|
||
genericiooptions.IOStreams | ||
} | ||
|
||
func NewLocalQueueOptions(streams genericiooptions.IOStreams) *LocalQueueOptions { | ||
return &LocalQueueOptions{ | ||
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme), | ||
IOStreams: streams, | ||
} | ||
} | ||
|
||
func NewLocalQueueCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command { | ||
o := NewLocalQueueOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "localqueue [-–clusterqueue CLUSTER_QUEUE_NAME] [--selector key1=value1] [--field-selector key1=value1] [--all-namespaces]", | ||
// To do not add "[flags]" suffix on the end of usage line | ||
DisableFlagsInUseLine: true, | ||
Aliases: []string{"lq"}, | ||
Short: "List LocalQueue", | ||
Long: lqLong, | ||
Example: lqExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cobra.CheckErr(o.Complete(clientGetter, cmd, args)) | ||
cobra.CheckErr(o.Run(cmd.Context())) | ||
}, | ||
} | ||
|
||
o.PrintFlags.AddFlags(cmd) | ||
|
||
addAllNamespacesFlagVar(cmd, &o.AllNamespaces) | ||
addFieldSelectorFlagVar(cmd, &o.FieldSelector) | ||
addLabelSelectorFlagVar(cmd, &o.LabelSelector) | ||
addClusterQueueFilterFlagVar(cmd, &o.ClusterQueueFilter) | ||
|
||
return cmd | ||
} | ||
|
||
// Complete completes all the required options | ||
func (o *LocalQueueOptions) Complete(clientGetter genericclioptions.RESTClientGetter, cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
o.Namespace, o.EnforceNamespace, err = clientGetter.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config, err := clientGetter.ToRESTConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientset, err := versioned.NewForConfig(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.Client = clientset.KueueV1beta1() | ||
|
||
defaultPrinter, err := o.PrintFlags.ToPrinter() | ||
if err != nil { | ||
return err | ||
} | ||
o.DefaultPrintObj = defaultPrinter.PrintObj | ||
|
||
tablePrinter := printers.NewTablePrinter(printers.PrintOptions{WithNamespace: o.AllNamespaces}) | ||
o.TablePrintObj = tablePrinter.PrintObj | ||
|
||
return nil | ||
} | ||
|
||
// Run performs the list operation. | ||
func (o *LocalQueueOptions) Run(ctx context.Context) error { | ||
namespace := o.Namespace | ||
if o.AllNamespaces { | ||
namespace = "" | ||
} | ||
|
||
opts := metav1.ListOptions{LabelSelector: o.LabelSelector, FieldSelector: o.FieldSelector} | ||
list, err := o.Client.LocalQueues(namespace).List(ctx, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.filterList(list) | ||
|
||
if len(list.Items) > 0 { | ||
return o.print(list) | ||
} | ||
|
||
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 | ||
} | ||
|
||
func (o *LocalQueueOptions) filterList(list *v1beta1.LocalQueueList) { | ||
if len(o.ClusterQueueFilter) > 0 { | ||
filteredItems := make([]v1beta1.LocalQueue, 0, len(o.ClusterQueueFilter)) | ||
for _, lq := range list.Items { | ||
if lq.Spec.ClusterQueue == v1beta1.ClusterQueueReference(o.ClusterQueueFilter) { | ||
filteredItems = append(filteredItems, lq) | ||
} | ||
} | ||
list.Items = filteredItems | ||
} | ||
} | ||
|
||
func (o *LocalQueueOptions) print(list *v1beta1.LocalQueueList) error { | ||
if len(*o.PrintFlags.OutputFormat) == 0 { | ||
return o.printTable(list) | ||
} else { | ||
return o.DefaultPrintObj(list, o.Out) | ||
} | ||
} | ||
|
||
func (o *LocalQueueOptions) printTable(list *v1beta1.LocalQueueList) error { | ||
table := &metav1.Table{ | ||
ColumnDefinitions: localQueueColumnDefinitions(), | ||
Rows: printLocalQueueList(list), | ||
} | ||
return o.TablePrintObj(table, o.Out) | ||
} | ||
|
||
func localQueueColumnDefinitions() []metav1.TableColumnDefinition { | ||
return []metav1.TableColumnDefinition{ | ||
{Name: "Name", Type: "string", Format: "name"}, | ||
{Name: "ClusterQueue", Type: "string"}, | ||
{Name: "Pending Workloads", Type: "integer"}, | ||
{Name: "Admitted Workloads", Type: "integer"}, | ||
{Name: "Age", Type: "string"}, | ||
} | ||
} | ||
|
||
func printLocalQueueList(list *v1beta1.LocalQueueList) []metav1.TableRow { | ||
rows := make([]metav1.TableRow, len(list.Items)) | ||
for index := range list.Items { | ||
rows[index] = printLocalQueue(&list.Items[index]) | ||
} | ||
return rows | ||
} | ||
|
||
func printLocalQueue(localQueue *v1beta1.LocalQueue) metav1.TableRow { | ||
row := metav1.TableRow{ | ||
Object: runtime.RawExtension{Object: localQueue}, | ||
} | ||
row.Cells = []any{ | ||
localQueue.Name, | ||
localQueue.Spec.ClusterQueue, | ||
localQueue.Status.PendingWorkloads, | ||
localQueue.Status.AdmittedWorkloads, | ||
duration.HumanDuration(time.Since(localQueue.CreationTimestamp.Time)), | ||
} | ||
return row | ||
} |
Oops, something went wrong.