Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(cli): extend duration to longer units #15040

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

Merged
merged 10 commits into from
Oct 16, 2024
Merged

Conversation

defelmnq
Copy link
Contributor

@defelmnq defelmnq commented Oct 10, 2024

This PR is a proposal to improve the situation described in #14750

For some precise commands - we would like to be able to use durations bigger than hours, minutes..

This PR extends the Duration proposed by Go with :

  • d - a day or 24hours.
  • y - a year or 365 days.

I also removed the default value for lifetime and instead fetch the maxLifetime value from codersdk - so by default if no value set we use the value defined in the config.

@defelmnq defelmnq self-assigned this Oct 10, 2024
@defelmnq defelmnq requested a review from mtojek October 10, 2024 23:47
{"10s", 10 * time.Second, true},
{"1m", 1 * time.Minute, true},
{"20h", 20 * time.Hour, true},
{"10y10d10s", 0, false},
Copy link
Member

Choose a reason for hiding this comment

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

I think users will expect durations like 1y1d to work here. It shouldn't be a huge lift to enable this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @johnstcn I tried to play with time.ParseDuration golang function to mimic the behavior as much as possible - and changed to indeed modify what you've raised.

@defelmnq defelmnq requested a review from johnstcn October 14, 2024 00:05
@matifali
Copy link
Member

matifali commented Oct 14, 2024

This is excellent @defelmnq. I suggest we stay uniform and check and update all durations of input throughput the product to support the string format across CLI/API/frontend.

It doesn't have to be in this PR and can be tracked as a separate issue.

cli/util.go Outdated
Comment on lines 188 to 189
// - d (days)
// - y (years)
Copy link
Member

Choose a reason for hiding this comment

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

It may make sense to explicitly note that this will not take into account things like DST or leap years or all that other assorted wibbly wobbley timey wimey stuff.

Suggested change
// - d (days)
// - y (years)
// - d (days, interpreted as 24 hours)
// - y (years, interpreted as 8_760 hours)

Copy link
Member

Choose a reason for hiding this comment

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

My first though was about leap years too :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep - indeed we can document it. About the behavior are we good to keep it this way ? I felt like it can quickly become complicated with time saving, leap years etc.. otherwise - maybe we can do it in the follow-up PR in serpent if we want to use it everywhere.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, definitely not a must-have

Copy link
Member

@johnstcn johnstcn Oct 14, 2024

Choose a reason for hiding this comment

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

I'm happy with a naive definition of days and years here. Adding DST / leap year awareness adds a lot of complications, as you need both a timezone and a relative time. We can add a follow-up issue if we think it's necessary though.

Comment on lines +51 to +59
{"1d", 24 * time.Hour, true},
{"1y", 365 * 24 * time.Hour, true},
{"10s", 10 * time.Second, true},
{"1m", 1 * time.Minute, true},
{"20h", 20 * time.Hour, true},
{"10y10d10s", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second, true},
{"10ms", 10 * time.Millisecond, true},
{"5y10d10s5y2ms8ms", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second + 10*time.Millisecond, true},
{"10yz10d10s", 0, false},
Copy link
Member

Choose a reason for hiding this comment

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

question: What happens if we try to parse a duration greater than 2540400h10m10.000000000s (e.g. 1000y)?
(see: https://github.com/golang/go/blob/master/src/time/time.go#L954C21-L954C45)

Comment on lines +78 to +81
parsedLifetime, err = extendedParseDuration(tokenLifetime)
if err != nil {
return xerrors.Errorf("parse lifetime: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: given that we have the token config available, we can check if the requested lifetime is greater than MaxTokenLifetime client-side. This may not be completely necessary though, as the API will disallow creating tokens with lifetime greater than MaximumTokenDuration. Dealer's choice!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah you're completely right - makes sense I just changed it a bit to handle the config fetched ! 👌

@@ -46,7 +46,7 @@ func (r *RootCmd) tokens() *serpent.Command {

func (r *RootCmd) createToken() *serpent.Command {
var (
tokenLifetime time.Duration
tokenLifetime string
Copy link
Member

Choose a reason for hiding this comment

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

@defelmnq @johnstcn WDYT about defining a new type in coder/serpent, time.LongDuration?

Copy link
Member

Choose a reason for hiding this comment

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

Could be done as a follow-up!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created a follow-up issue here - would definitely be a cool one to do indeed.

cli/util.go Outdated
Comment on lines 188 to 189
// - d (days)
// - y (years)
Copy link
Member

Choose a reason for hiding this comment

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

My first though was about leap years too :)

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

I see this being valuable outside this 1 case.

serpent.Duration is used in a few places in DeploymentValues. Can we make this a new time, called like serpent.ExtendedDuration?

Or maybe more controversial, just include this into serpent.Duration itself?

Screenshot from 2024-10-14 10-26-09

@defelmnq defelmnq requested review from mtojek and johnstcn October 14, 2024 16:26
@defelmnq
Copy link
Contributor Author

Hey @mtojek @johnstcn thanks a lot for the input here - really appreciate.

Since the last review, here are globally the points I tried to change and improve :

  • Handling & tests for negative values and overflow.
  • Stick to go time package to add 'segments' (2h2h10m is valid and equals 4h10m)
  • Verifying client-side if the defined token lifetime is valid compared to the one fetched in the config.

What I would love to do as a follow-up :

  • Handle cases like 1.2h
  • Potentially move the library to Serpent cc @Emyrk & this issue for broader adoption

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

Strong tests 👍

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Nice work! 👍

@defelmnq defelmnq requested a review from Emyrk October 16, 2024 13:09
@defelmnq defelmnq merged commit ccbb687 into main Oct 16, 2024
27 checks passed
@defelmnq defelmnq deleted the feat/improve-duration-flags branch October 16, 2024 15:02
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants