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

[refactor][kubectl-plugin] use cobra's posargs length validator #2985

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidxia
Copy link
Contributor

@davidxia davidxia commented Feb 8, 2025

We're currently validating the number of positional arguments ourselves.
We can do this with cobra.Command{Args: cobra.MaximumNArgs(1)} instead
to simplify logic. There are no functional changes. The error message changes
slightly but still conveys the same information.

Before

$ kubectl ray --context gke_kubeflow-platform_europe-west4-b_ml-compute-1 get cluster foo bar
Error: too many arguments, either one or no arguments are allowed

After

$ kubectl ray get cluster foo bar
Error: accepts at most 1 arg(s), received 2

Signed-off-by: David Xia david@davidxia.com

Checks

  • I've made sure the tests are passing.
  • Testing Strategy
    • Unit tests
    • Manual tests
    • This PR is not tested :(

@@ -22,7 +22,7 @@ import (
type GetClusterOptions struct {
configFlags *genericclioptions.ConfigFlags
ioStreams *genericclioptions.IOStreams
args []string
cluster string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to be more obvious what this variable is for

@@ -43,6 +43,7 @@ func NewGetClusterCommand(streams genericclioptions.IOStreams) *cobra.Command {
Short: "Get cluster information.",
SilenceUsage: true,
ValidArgsFunction: completion.RayClusterCompletionFunc(cmdFactory),
Args: cobra.MaximumNArgs(1),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the important part

@@ -68,7 +69,10 @@ func (options *GetClusterOptions) Complete(args []string) error {
options.AllNamespaces = true
}

options.args = args
if len(args) >= 1 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should never be more than one positional argument, but just ignore the rest instead of erroring if there is for some reason

Comment on lines -84 to -87
if len(options.args) > 1 {
return fmt.Errorf("too many arguments, either one or no arguments are allowed")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we don't need this

@@ -21,22 +21,62 @@ import (
rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake"
)

// This is to test Complete() and ensure that it is setting the namespace and arguments correctly
// This is to test Complete() and ensure that it is setting the namespace and cluster correctly
// No validation test is done here
func TestRayClusterGetComplete(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test all the cases

@@ -98,24 +138,6 @@ func TestRayClusterGetValidate(t *testing.T) {
ioStreams: &testStreams,
},
},
{
name: "Test validation when more than 1 arg",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test no longer needed

@davidxia davidxia marked this pull request as ready for review February 9, 2025 20:11
@kevin85421
Copy link
Member

@davidxia would you mind resolving the conflict?

We're currently validating the number of positional arguments ourselves.
We can do this with `cobra.Command{Args: cobra.MaximumNArgs(1)}` instead
to simplify logic. There are no functional changes. The error message changes
slightly but still conveys the same information.

## Before

```console
$ kubectl ray --context gke_kubeflow-platform_europe-west4-b_ml-compute-1 get cluster foo bar
Error: too many arguments, either one or no arguments are allowed
```

## After

```console
$ kubectl ray get cluster foo bar
Error: accepts at most 1 arg(s), received 2
```

Signed-off-by: David Xia <david@davidxia.com>
@davidxia
Copy link
Contributor Author

thanks, resolved the conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants