mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-30 19:35:06 +00:00
Adds JMESPath filter for returning current time (#5813)
* time_current Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com> * name change Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com> * added note Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com> Signed-off-by: Vishal Choudhary <contactvishaltech@gmail.com> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
79b96025a3
commit
8572d53cc9
1 changed files with 35 additions and 0 deletions
|
@ -71,6 +71,7 @@ var (
|
|||
base64Decode = "base64_decode"
|
||||
base64Encode = "base64_encode"
|
||||
timeSince = "time_since"
|
||||
timeNow = "time_now"
|
||||
pathCanonicalize = "path_canonicalize"
|
||||
truncate = "truncate"
|
||||
semverCompare = "semver_compare"
|
||||
|
@ -374,6 +375,17 @@ func GetFunctions() []*FunctionEntry {
|
|||
ReturnType: []JpType{JpString},
|
||||
Note: "calculate the difference between a start and end period of time where the end may either be a static definition or the then-current time",
|
||||
},
|
||||
{
|
||||
Entry: &gojmespath.FunctionEntry{
|
||||
Name: timeNow,
|
||||
Arguments: []ArgSpec{
|
||||
{Types: []JpType{JpString}},
|
||||
},
|
||||
Handler: jpTimeNow,
|
||||
},
|
||||
ReturnType: []JpType{JpString},
|
||||
Note: "returns current time in RFC 3339 format",
|
||||
},
|
||||
{
|
||||
Entry: &gojmespath.FunctionEntry{
|
||||
Name: pathCanonicalize,
|
||||
|
@ -825,6 +837,29 @@ func jpTimeSince(arguments []interface{}) (interface{}, error) {
|
|||
return t2.Sub(t1).String(), nil
|
||||
}
|
||||
|
||||
func jpTimeNow(arguments []interface{}) (interface{}, error) {
|
||||
var err error
|
||||
layout, err := validateArg("", arguments, 0, reflect.String)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
|
||||
t = time.Now()
|
||||
if layout.String() != "" {
|
||||
t, err = time.Parse(layout.String(), t.String())
|
||||
} else {
|
||||
t, err = time.Parse(time.RFC3339, t.String())
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return t.String(), nil
|
||||
}
|
||||
|
||||
func jpPathCanonicalize(arguments []interface{}) (interface{}, error) {
|
||||
var err error
|
||||
str, err := validateArg(pathCanonicalize, arguments, 0, reflect.String)
|
||||
|
|
Loading…
Add table
Reference in a new issue