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

Added a time_add() filter to add duration and absolute time #5817

Merged
merged 11 commits into from
Jan 6, 2023

Conversation

vishal-chdhry
Copy link
Member

Signed-off-by: Vishal Choudhary contactvishaltech@gmail.com

Explanation

Adds a custom JMESPath filter to support inputs in the form of RFC3339 + scalar (duration) with the sum being RFC3339.

Related issue

Closes #5815

Milestone of this PR

What type of PR is this

/kind feature

Proposed Changes

Proof Manifests

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.
  • This is a feature and I have added CLI tests that are applicable.
  • My PR needs to be cherry picked to a specific release branch which is .
  • My PR contains new or altered behavior to Kyverno and
    • CLI support should be added and my PR doesn't contain that functionality.
    • I have added or changed the documentation myself in an existing PR and the link is:
    • I have raised an issue in kyverno/website to track the documentation update and the link is:

Further Comments

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
@codecov
Copy link

codecov bot commented Dec 30, 2022

Codecov Report

Merging #5817 (11046fb) into main (c10649f) will increase coverage by 0.10%.
The diff coverage is 65.71%.

@@            Coverage Diff             @@
##             main    #5817      +/-   ##
==========================================
+ Coverage   35.75%   35.85%   +0.10%     
==========================================
  Files         188      188              
  Lines       20884    20954      +70     
==========================================
+ Hits         7467     7513      +46     
- Misses      12598    12614      +16     
- Partials      819      827       +8     
Impacted Files Coverage Δ
pkg/engine/jmespath/functions.go 71.81% <65.71%> (-0.49%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@eddycharly
Copy link
Member

eddycharly commented Jan 2, 2023

I don't really like the format argument, how about a time_convert filter ?
cc @chipzoller

@realshuting
Copy link
Member

I don't really like the format argument, how about a time_convert filter ? cc @chipzoller

+1, time_convert seems more clear to me.

@chipzoller
Copy link
Member

The arguments here are similar to time_since(). I don't mind the idea of this time_add() filter being able to accommodate multiple time formats, but if that's not working here then it either needs to be fixed, removed and the filter renamed, or something else.

@eddycharly
Copy link
Member

IMHO we should support only one format internally and use conversion when input is in another format.

@@ -461,6 +462,18 @@ func GetFunctions() []*FunctionEntry {
ReturnType: []JpType{JpObject},
Note: "decodes an x.509 certificate to an object. you may also use this in conjunction with `base64_decode` jmespath function to decode a base64-encoded certificate",
},
{
Copy link
Member

Choose a reason for hiding this comment

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

@vishal-chdhry can you add a note to the function descriptor ?

@realshuting
Copy link
Member

@vishal-chdhry - can you address comment here? We'd like to get this merged to 1.9 and cut beta.2 by Friday.

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
@vishal-chdhry
Copy link
Member Author

@realshuting @eddycharly done!

@chipzoller
Copy link
Member

Thanks for these, @vishal-chdhry !

@eddycharly
Copy link
Member

Thanks @vishal-chdhry !

We were discussing the possibility to add a time_convert filter to allow removing the format argument from all related filters.
What do you think ?
Do you want to work on this ? Or we can merge this PR and take care of it in follow up PRs ?

@vishal-chdhry
Copy link
Member Author

@eddycharly I can add it to this PR

@eddycharly
Copy link
Member

@vishal-chdhry it would be awesome ❤️

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
@vishal-chdhry
Copy link
Member Author

vishal-chdhry commented Jan 5, 2023

should I convert the times to utc or keep the given timezone? (currently time_convert preserves the timezone)
@eddycharly can you please approve the change so that I can add the changes to existing functions

@eddycharly
Copy link
Member

should I convert the times to utc or keep the given timezone? (currently time_convert preserves the timezone)

@vishal-chdhry isn't it covered by the format ?

can you please approve the change so that I can add the changes to existing functions

we don't want to change time_since as it would be a breaking change, we can add a new time_diff func and deprecate time_since instead.

expectedResult string
}{
{
test: "time_convert('', '2021-01-02T15:04:05-07:00')",
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to call time_convert with an empty format ?

Copy link
Member Author

Choose a reason for hiding this comment

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

empty string everywhere else means we are using RFC3339, I kept it for consistency

and if the layout is empty and the time isn't in RFC3339 format then it will throw an error

Copy link
Member

Choose a reason for hiding this comment

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

I understand the idea but why one would call time_convert if the input is already in RFC3339 ?

Copy link
Member

Choose a reason for hiding this comment

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

I would rather not default the format, sounds error prone.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, I should remove it then

@vishal-chdhry
Copy link
Member Author

isn't it covered by the format ?

@eddycharly time.format() doesn't change the timezone to UTC, for example, in the first test case

@eddycharly
Copy link
Member

time.format() doesn't change the timezone to UTC

what are the pros/cons of converting to UTC ?

do you have an opinion @JimBugwadia @chipzoller @realshuting ?

@eddycharly
Copy link
Member

@vishal-chdhry what would be the best name time_convert or time_parse ?

@vishal-chdhry
Copy link
Member Author

vishal-chdhry commented Jan 5, 2023

@eddycharlytime_parse maybe because we are not converting time, just parsing it and its the same value in the end

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
@chipzoller
Copy link
Member

time.format() doesn't change the timezone to UTC

what are the pros/cons of converting to UTC ?

do you have an opinion @JimBugwadia @chipzoller @realshuting ?

The pros are pretty clear:

  • Consistent time format
  • Avoids potential problems with multiple replicas on hosts in different time zones

Cons are less clear but seem to be few from what I can think of.

@eddycharly
Copy link
Member

IMHO it's better to not enforce UTC.
We need RFC3339 format but UTC is not mandatory.

@eddycharly
Copy link
Member

@vishal-chdhry can you resolve conflicts ?

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
@vishal-chdhry
Copy link
Member Author

@eddycharly Done!

@chipzoller
Copy link
Member

One thing I ran into testing CronJobs is to be mindful that the cron format given is interpreted by the time zone of the Node on which the Pod will execute. In 1.26 there's a way to specify a time zone, but that's obviously not something we can adopt ourselves now.

@eddycharly
Copy link
Member

@chipzoller IIRC it's the TZ of the master nodes, not the TZ of the node where the pod executes.

Copy link
Member

@eddycharly eddycharly left a comment

Choose a reason for hiding this comment

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

Good job @vishal-chdhry !

@eddycharly eddycharly enabled auto-merge (squash) January 6, 2023 13:55
@eddycharly eddycharly merged commit bb6005a into kyverno:main Jan 6, 2023
@eddycharly
Copy link
Member

/cherry-pick release-1.9

2 similar comments
@eddycharly
Copy link
Member

/cherry-pick release-1.9

@eddycharly
Copy link
Member

/cherry-pick release-1.9

eddycharly added a commit to eddycharly/kyverno that referenced this pull request Jan 9, 2023
…#5817)

* time_add

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* output is RFC3339 only now

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added note to timeadd

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added time_convert

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* removed blank string timezone

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* renamed to time_parse

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@eddycharly eddycharly added the cherry-pick-completed The PR was cherry-picked (or merged) to required release branches label Jan 9, 2023
eddycharly added a commit that referenced this pull request Jan 9, 2023
…5946)

* time_add

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* output is RFC3339 only now

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added note to timeadd

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added time_convert

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* removed blank string timezone

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* renamed to time_parse

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 11, 2023
…#5817)

* time_add

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* output is RFC3339 only now

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added note to timeadd

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added time_convert

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* removed blank string timezone

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* renamed to time_parse

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: MdSahil-oss <Mohdssahil1@gmail.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 11, 2023
…#5817)

* time_add

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* output is RFC3339 only now

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added note to timeadd

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added time_convert

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* removed blank string timezone

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* renamed to time_parse

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: MdSahil-oss <Mohdssahil1@gmail.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 13, 2023
…#5817)

* time_add

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* output is RFC3339 only now

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added note to timeadd

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* added time_convert

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* removed blank string timezone

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

* renamed to time_parse

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>

Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: MdSahil-oss <Mohdssahil1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick-completed The PR was cherry-picked (or merged) to required release branches cherry-pick-required milestone 1.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] Update add() filter to add duration and absolute time
4 participants