1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

Added a time_add() filter to add duration and absolute time (#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>
This commit is contained in:
Vishal Choudhary 2023-01-06 22:18:13 +05:30 committed by GitHub
parent c10649fc2f
commit bb6005a7a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 144 additions and 0 deletions

View file

@ -81,6 +81,8 @@ var (
objectFromLists = "object_from_lists"
random = "random"
x509_decode = "x509_decode"
timeAdd = "time_add"
timeParse = "time_parse"
timeToCron = "time_to_cron"
)
@ -502,6 +504,31 @@ func GetFunctions() []*FunctionEntry {
ReturnType: []JpType{JpString},
Note: "converts an absolute time (RFC 3339) to a cron expression (string).",
},
{
Entry: &gojmespath.FunctionEntry{
Name: timeAdd,
Arguments: []ArgSpec{
{Types: []JpType{JpString}},
{Types: []JpType{JpString}},
{Types: []JpType{JpString}},
},
Handler: jpTimeAdd,
},
ReturnType: []JpType{JpString},
Note: "adds duration (third string) to a time value (second string) of a specified layout (first string)",
},
{
Entry: &gojmespath.FunctionEntry{
Name: timeParse,
Arguments: []ArgSpec{
{Types: []JpType{JpString}},
{Types: []JpType{JpString}},
},
Handler: jpTimeParse,
},
ReturnType: []JpType{JpString},
Note: "changes a time value of a given layout to RFC 3339",
},
}
}
@ -1131,3 +1158,60 @@ func jpTimeToCron(arguments []interface{}) (interface{}, error) {
return cron, nil
}
func jpTimeAdd(arguments []interface{}) (interface{}, error) {
var err error
layout, err := validateArg("", arguments, 0, reflect.String)
if err != nil {
return nil, err
}
ts, err := validateArg("", arguments, 1, reflect.String)
if err != nil {
return nil, err
}
dr, err := validateArg("", arguments, 2, reflect.String)
if err != nil {
return nil, err
}
var t time.Time
if layout.String() != "" {
t, err = time.Parse(layout.String(), ts.String())
} else {
t, err = time.Parse(time.RFC3339, ts.String())
}
if err != nil {
return nil, err
}
var d time.Duration
d, err = time.ParseDuration(dr.String())
if err != nil {
return nil, err
}
return t.Add(d).Format(time.RFC3339), nil
}
func jpTimeParse(arguments []interface{}) (interface{}, error) {
var err error
layout, err := validateArg("", arguments, 0, reflect.String)
if err != nil {
return nil, err
}
ts, err := validateArg("", arguments, 1, reflect.String)
if err != nil {
return nil, err
}
var t time.Time
t, err = time.Parse(layout.String(), ts.String())
if err != nil {
return nil, err
}
return t.Format(time.RFC3339), nil
}

View file

@ -1579,3 +1579,63 @@ func Test_TimeToCron(t *testing.T) {
})
}
}
func Test_TimeAdd(t *testing.T) {
testCases := []struct {
test string
expectedResult string
}{
{
test: "time_add('', '2021-01-02T15:04:05-07:00', '3h')",
expectedResult: "2021-01-02T18:04:05-07:00",
},
{
test: "time_add('Mon Jan 02 15:04:05 MST 2006', 'Sat Jan 02 15:04:05 MST 2021', '5h30m40s')",
expectedResult: "2021-01-02T20:34:45Z",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := New(tc.test)
assert.NilError(t, err)
res, err := query.Search("")
assert.NilError(t, err)
result, ok := res.(string)
assert.Assert(t, ok)
assert.Equal(t, result, tc.expectedResult)
})
}
}
func Test_TimeParse(t *testing.T) {
testCases := []struct {
test string
expectedResult string
}{
{
test: "time_parse('2006-01-02T15:04:05Z07:00', '2021-01-02T15:04:05-07:00')",
expectedResult: "2021-01-02T15:04:05-07:00",
},
{
test: "time_parse('Mon Jan 02 15:04:05 MST 2006', 'Sat Jan 02 15:04:05 MST 2021')",
expectedResult: "2021-01-02T15:04:05Z",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := New(tc.test)
assert.NilError(t, err)
res, err := query.Search("")
assert.NilError(t, err)
result, ok := res.(string)
assert.Assert(t, ok)
assert.Equal(t, result, tc.expectedResult)
})
}
}