Skip to content

Commit

Permalink
[kueuectl] Added stop/resume workload commands. (#2134)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbobrovskyi committed May 10, 2024
1 parent 2ab58f6 commit c55f10c
Show file tree
Hide file tree
Showing 14 changed files with 619 additions and 39 deletions.
4 changes: 4 additions & 0 deletions cmd/kueuectl/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/cmd/kueuectl/app/create"
"sigs.k8s.io/kueue/cmd/kueuectl/app/resume"
"sigs.k8s.io/kueue/cmd/kueuectl/app/stop"
)

type KueuectlOptions struct {
Expand Down Expand Up @@ -59,6 +61,8 @@ func NewKueuectlCmd(o KueuectlOptions) *cobra.Command {
configFlags.AddFlags(flags)

cmd.AddCommand(create.NewCreateCmd(configFlags, o.IOStreams))
cmd.AddCommand(resume.NewResumeCmd(configFlags, o.IOStreams))
cmd.AddCommand(stop.NewStopCmd(configFlags, o.IOStreams))

return cmd
}
130 changes: 130 additions & 0 deletions cmd/kueuectl/app/options/update_workload_activation_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
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 options

import (
"context"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

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

type UpdateWorkloadActivationOptions struct {
PrintFlags *genericclioptions.PrintFlags

Active bool
DryRunStrategy util.DryRunStrategy
Name string
Namespace string
EnforceNamespace bool

Client kueuev1beta1.KueueV1beta1Interface

PrintObj printers.ResourcePrinterFunc

genericiooptions.IOStreams
}

func NewUpdateWorkloadActivationOptions(streams genericiooptions.IOStreams, operation string, active bool) *UpdateWorkloadActivationOptions {
return &UpdateWorkloadActivationOptions{
PrintFlags: genericclioptions.NewPrintFlags(operation).WithTypeSetter(scheme.Scheme),
Active: active,
IOStreams: streams,
}
}

// Complete completes all the required options
func (o *UpdateWorkloadActivationOptions) Complete(clientGetter genericclioptions.RESTClientGetter, cmd *cobra.Command, args []string) error {
o.Name = args[0]

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

o.DryRunStrategy, err = util.GetDryRunStrategy(cmd)
if err != nil {
return err
}

err = util.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
if err != nil {
return err
}

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

o.PrintObj = printer.PrintObj

return nil
}

// Run performs to update a Workload status operation.
func (o *UpdateWorkloadActivationOptions) Run(ctx context.Context) error {
wl, err := o.Client.Workloads(o.Namespace).Get(ctx, o.Name, metav1.GetOptions{})
if err != nil {
return err
}

wlOriginal := wl.DeepCopy()
wl.Spec.Active = ptr.To(o.Active)

if o.DryRunStrategy != util.DryRunClient {
opts := metav1.PatchOptions{}
if o.DryRunStrategy == util.DryRunServer {
opts.DryRun = []string{metav1.DryRunAll}
}
patch := client.MergeFrom(wlOriginal)
data, err := patch.Data(wl)
if err != nil {
return err
}
wl, err = o.Client.Workloads(o.Namespace).Patch(ctx, wl.Name, types.MergePatchType, data, opts)
if err != nil {
return err
}
}

return o.PrintObj(wl, o.Out)
}
44 changes: 44 additions & 0 deletions cmd/kueuectl/app/resume/resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 resume

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/cmd/kueuectl/app/util"
)

const (
resumeExample = ` # Resume the workload
kueuectl resume workload my-workload`
)

func NewResumeCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "resume",
Short: "Resume the resource",
Example: resumeExample,
}

util.AddDryRunFlag(cmd)

cmd.AddCommand(NewWorkloadCmd(clientGetter, streams))

return cmd
}
54 changes: 54 additions & 0 deletions cmd/kueuectl/app/resume/resume_workload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 resume

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/cmd/kueuectl/app/options"
)

const (
wlLong = `Resumes the Workload, allowing its admission according to regular ClusterQueue rules.`
wlExample = ` # Resume the workload
kueuectl resume workload my-workload`
)

func NewWorkloadCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command {
o := options.NewUpdateWorkloadActivationOptions(streams, "resumed", true)

cmd := &cobra.Command{
Use: "workload NAME [--namespace NAMESPACE] [--dry-run STRATEGY]",
// To do not add "[flags]" suffix on the end of usage line
DisableFlagsInUseLine: true,
Aliases: []string{"wl"},
Short: "Resume the Workload",
Long: wlLong,
Example: wlExample,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.Complete(clientGetter, cmd, args))
cobra.CheckErr(o.Run(cmd.Context()))
},
}

o.PrintFlags.AddFlags(cmd)

return cmd
}
44 changes: 44 additions & 0 deletions cmd/kueuectl/app/stop/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 stop

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/cmd/kueuectl/app/util"
)

const (
stopExample = ` # Stop the workload
kueuectl stop workload my-workload`
)

func NewStopCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
Short: "Stop the resource",
Example: stopExample,
}

util.AddDryRunFlag(cmd)

cmd.AddCommand(NewWorkloadCmd(clientGetter, streams))

return cmd
}
56 changes: 56 additions & 0 deletions cmd/kueuectl/app/stop/stop_workload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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 stop

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"

"sigs.k8s.io/kueue/cmd/kueuectl/app/options"
)

const (
wlLong = `Puts the given Workload on hold. The Workload will not be admitted and
if it is already admitted it will be put back to queue just as if it
was preempted (using .spec.active field).`
wlExample = ` # Stop the workload
kueuectl stop workload my-workload`
)

func NewWorkloadCmd(clientGetter genericclioptions.RESTClientGetter, streams genericiooptions.IOStreams) *cobra.Command {
o := options.NewUpdateWorkloadActivationOptions(streams, "stopped", false)

cmd := &cobra.Command{
Use: "workload NAME [--namespace NAMESPACE] [--dry-run STRATEGY]",
// To do not add "[flags]" suffix on the end of usage line
DisableFlagsInUseLine: true,
Aliases: []string{"wl"},
Short: "Stop the Workload",
Long: wlLong,
Example: wlExample,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.Complete(clientGetter, cmd, args))
cobra.CheckErr(o.Run(cmd.Context()))
},
}

o.PrintFlags.AddFlags(cmd)

return cmd
}
24 changes: 24 additions & 0 deletions site/content/en/docs/reference/kubectl-kueue/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "Kubectl Kueue Plugin"
linkTitle: "Kubectl Kueue Plugin"
date: 2024-05-09
weight: 10
description: >
The kubectl-kueue plugin, kueuectl, allows you to create, resume and stop kueue resources such as localqueue and workload.
---

## Syntax

Use the following syntax to run `kubectl kueue` commands from your terminal window:

```shell
kubectl kueue [OPERATION] [TYPE] [NAME] [flags]
```

or with shorter syntax `kueuectl`:

```shell
kueuectl [OPERATION] [TYPE] [NAME] [flags]
```

You can run `kubectl kueue help` in the terminal to get the full list of commands, along with all possible flags.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: "Commands"
linkTitle: "Commands"
date: 2024-05-10
weight: 30
---

Loading

0 comments on commit c55f10c

Please sign in to comment.