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

Fix cmctl renew flags validation #20

Merged

Conversation

jace-ys
Copy link
Contributor

@jace-ys jace-ys commented Feb 5, 2024

Hey folks 👋🏻

While using cmctl renew earlier today, we noticed some bugs in the flags that caused an error when running cmctl renew --all-namespaces -l app=my-service as documented in the CLI help.

The error we saw:

please supply one or more Certificate resource names or use the --all flag to renew all Certificate resources

I made an attempt to fix this bug in the renew validation function, so keen to hear your thoughts on it 😄

  • The first commit outlines the main change
  • The second commit proposes a refactor to make the mutually exclusive options (hopefully) clearer

I've also updated the tests accordingly!

Reasoning

Based on my understanding of the actual Run command, I believe these hold true:

  • Mutually exclusive flags:
    • --all and --label-selectors
  • If no arguments are specified:
    • We need either --all or --label-selectors
  • If arguments are specified:
    • They can't be used with --all, or --label-selectors
    • You would get an Error from server (NotFound): error with --all-namespaces, unless the first namespace that is listed happens to contain the certificate(s) specified in the arguments.
      • This behaviour doesn't sound ideal so I think it's more intuitive to disallow use of arguments with --all-namespaces; arguments should really only be used with --namespace.

@jetstack-bot jetstack-bot added dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 5, 2024
@jetstack-bot
Copy link
Contributor

Hi @jace-ys. Thanks for your PR.

I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@jetstack-bot jetstack-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 5, 2024
Comment on lines +44 to +50
"If there are arguments, as well as --all-namespaces, error": {
options: &Options{
AllNamespaces: true,
},
args: []string{"abc"},
expErr: true,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If arguments are specified:

  • You would get an Error from server (NotFound): error with --all-namespaces, unless the first namespace that is listed happens to contain the certificate(s) specified in the arguments.
    • This behaviour doesn't sound ideal so I think it's more intuitive to disallow use of arguments with --all-namespaces; arguments should really only be used with --namespace.

@jace-ys jace-ys force-pushed the jace-ys/fix-renew-flags-validation branch from cd921cf to beb912f Compare February 5, 2024 18:17
@jace-ys jace-ys changed the title Fix cmdctl renew flags validation Fix cmctl renew flags validation Feb 5, 2024
"If there are all certificates selected, as well as label selector, error": {
options: &Options{
LabelSelector: "foo=bar",
All: true,
},
args: []string{""},
Copy link
Contributor Author

@jace-ys jace-ys Feb 5, 2024

Choose a reason for hiding this comment

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

This looks like a minor bug in the test case, as providing []string{""} as args is triggering a different error cannot specify Certificate names in conjunction with label selectors than intended (cannot specify label selectors in conjunction with --all flag)

@jace-ys jace-ys force-pushed the jace-ys/fix-renew-flags-validation branch 3 times, most recently from d85427f to 5ded621 Compare February 5, 2024 18:39
Signed-off-by: jace-ys <jace@duffel.com>
Signed-off-by: jace-ys <jace@duffel.com>
Signed-off-by: jace-ys <jace@duffel.com>
@jace-ys jace-ys force-pushed the jace-ys/fix-renew-flags-validation branch from 5ded621 to 5babd3e Compare February 5, 2024 18:41
@jetstack-bot jetstack-bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. and removed dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. labels Feb 5, 2024
Comment on lines -66 to -73
"If --namespace and --all namespace specified, error": {
"If --namespace and --all specified, don't error": {
options: &Options{
All: true,
},
setStringFlags: []stringFlag{
{name: "namespace", value: "foo"},
},
expErr: true,
Copy link
Contributor Author

@jace-ys jace-ys Feb 5, 2024

Choose a reason for hiding this comment

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

This test case doesn't seem right, as looking at the description for --all as well as the actual Run function, they seem to suggest that --all should work with --namespace.

I've updated the validation function accordingly too.

@inteon inteon self-requested a review February 14, 2024 20:40
@inteon
Copy link
Member

inteon commented Feb 14, 2024

/ok-to-test

@jetstack-bot jetstack-bot added ok-to-test and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 14, 2024
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
@inteon
Copy link
Member

inteon commented Feb 15, 2024

Thanks @jace-ys, I added some comments that explain in what cases the Validation function should fail.
Also, I tried to dynamically generate the error message, so the Validate logic becomes a bit easier to understand.
PTAL and let me know what you think.

@jace-ys
Copy link
Contributor Author

jace-ys commented Feb 15, 2024

Thanks @jace-ys, I added some comments that explain in what cases the Validation function should fail.
Also, I tried to dynamically generate the error message, so the Validate logic becomes a bit easier to understand.
PTAL and let me know what you think.

Thanks! Looks great and is much cleaner now👍🏻

@inteon
Copy link
Member

inteon commented Feb 15, 2024

/approve
/lgtm

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Feb 15, 2024
@jetstack-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: inteon

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@jetstack-bot jetstack-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 15, 2024
@jetstack-bot jetstack-bot merged commit e634358 into cert-manager:main Feb 15, 2024
5 checks passed
@jace-ys jace-ys deleted the jace-ys/fix-renew-flags-validation branch February 15, 2024 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. lgtm Indicates that a PR is ready to be merged. ok-to-test size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants