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

feat: add more time jmespath filters #5950

Merged
merged 8 commits into from
Jan 10, 2023

Conversation

eddycharly
Copy link
Member

@eddycharly eddycharly commented Jan 9, 2023

Signed-off-by: Charles-Edouard Brétéché charles.edouard@nirmata.com

Explanation

This PR adds a couple more time related jmespath filters:

  • time_utc
  • time_now_utc
  • time_diff (the ultimate replacement for time_since)
  • time_before
  • time_after
  • time_between
  • time_truncate

I also took the opportunity to refactor existing funcs and restructure the package to put all time related filters in a separate file.

Proof manifests

  • time_utc

time_utc('2023-02-02T15:04:05-07:00') -> 2023-02-02T22:04:05Z

  • time_now_utc

same as time_now but result is in UTC

  • time_diff

time_diff('2021-01-02T15:04:05-07:00', '2021-01-10T03:14:05-07:00') -> 180h10m0s

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@eddycharly
Copy link
Member Author

cc @vishal-chdhry

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@eddycharly eddycharly marked this pull request as ready for review January 9, 2023 16:44
@codecov
Copy link

codecov bot commented Jan 9, 2023

Codecov Report

Merging #5950 (3b228c8) into main (e323e69) will increase coverage by 0.14%.
The diff coverage is 61.32%.

@@            Coverage Diff             @@
##             main    #5950      +/-   ##
==========================================
+ Coverage   35.71%   35.85%   +0.14%     
==========================================
  Files         188      189       +1     
  Lines       21043    21147     +104     
==========================================
+ Hits         7515     7582      +67     
- Misses      12699    12730      +31     
- Partials      829      835       +6     
Impacted Files Coverage Δ
pkg/engine/jmespath/time.go 36.92% <36.92%> (ø)
pkg/engine/jmespath/functions.go 77.37% <100.00%> (+4.15%) ⬆️

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

@chipzoller
Copy link
Member

Couldn't time_since() be used where time_diff() is as shown?

@eddycharly
Copy link
Member Author

eddycharly commented Jan 9, 2023

time_diff does not have a format argument, apart from that it's doing the same as time_since.

@chipzoller
Copy link
Member

Ok, so this is the replacement for time_since() that we discussed a few days ago, right?

@eddycharly
Copy link
Member Author

@chipzoller yes, the goal is to deprecate time_since.

@chipzoller
Copy link
Member

Ok, so we need a deprecation notice printed if time_since() is found in a rule. That could be in this PR or another.

@eddycharly
Copy link
Member Author

eddycharly commented Jan 9, 2023

Probably in another PR, this is not going to be easy :(

I'm trying to find a way to analyse the AST of a jmespath expression to get better validation but it's not easy, especially because we can have nest jmespath expressions.

@chipzoller
Copy link
Member

Since we're talking about a warning, we could start with being "dumb" and just looking for that as a substring anywhere. Not ideal but it's better than nothing.

@eddycharly
Copy link
Member Author

Since we're talking about a warning, we could start with being "dumb" and just looking for that as a substring anywhere. Not ideal but it's better than nothing.

Detecting a function call should be relatively easy.

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: shuting <shutting06@gmail.com>
@realshuting realshuting enabled auto-merge (squash) January 10, 2023 04:08
@realshuting
Copy link
Member

/cherry-pick release-1.9

@realshuting realshuting merged commit 91f2622 into kyverno:main Jan 10, 2023
@gcp-cherry-pick-bot
Copy link

Cherry-pick failed with Merge error 91f2622384ad807e9ac34248857f1636529396bf into temp-cherry-pick-6d3a93-release-1.9

realshuting pushed a commit to realshuting/kyverno that referenced this pull request Jan 10, 2023
Signed-off-by: ShutingZhao <shuting@nirmata.com>
@realshuting realshuting added the cherry-pick-completed The PR was cherry-picked (or merged) to required release branches label Jan 10, 2023
realshuting added a commit that referenced this pull request Jan 10, 2023
Signed-off-by: ShutingZhao <shuting@nirmata.com>

Signed-off-by: ShutingZhao <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
@eddycharly eddycharly deleted the more-time-filters branch January 10, 2023 08:02
vishal-chdhry pushed a commit to vishal-chdhry/kyverno that referenced this pull request Jan 10, 2023
* feat: add more time jmespath filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_utc and time_now_utc

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_diff

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more time filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* Update pkg/engine/jmespath/functions.go

Signed-off-by: shuting <shutting06@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: shuting <shutting06@gmail.com>
Co-authored-by: shuting <shutting06@gmail.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 11, 2023
* feat: add more time jmespath filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_utc and time_now_utc

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_diff

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more time filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* Update pkg/engine/jmespath/functions.go

Signed-off-by: shuting <shutting06@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: shuting <shutting06@gmail.com>
Co-authored-by: shuting <shutting06@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
* feat: add more time jmespath filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_utc and time_now_utc

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_diff

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more time filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* Update pkg/engine/jmespath/functions.go

Signed-off-by: shuting <shutting06@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: shuting <shutting06@gmail.com>
Co-authored-by: shuting <shutting06@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
* feat: add more time jmespath filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_utc and time_now_utc

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* add time_diff

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more time filters

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* Update pkg/engine/jmespath/functions.go

Signed-off-by: shuting <shutting06@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: shuting <shutting06@gmail.com>
Co-authored-by: shuting <shutting06@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.

None yet

3 participants