Skip to content

Commit b0153df

Browse files
authored
Merge pull request #1766 from dontan001/timestamp_support
To support read the timestamp fields of custom resources
2 parents 3bc6557 + 889d7b4 commit b0153df

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pkg/customresourcestate/registry_factory.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sort"
2424
"strconv"
2525
"strings"
26+
"time"
2627

2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -499,6 +500,9 @@ func getNum(value interface{}) (float64, error) {
499500
}
500501
return 0, nil
501502
case string:
503+
if t, e := time.Parse(time.RFC3339, value.(string)); e == nil {
504+
return float64(t.Unix()), nil
505+
}
502506
return strconv.ParseFloat(value.(string), 64)
503507
case byte:
504508
v = float64(vv)

pkg/customresourcestate/registry_factory_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func init() {
8181
"qux": "quxx",
8282
"bar": "baz",
8383
},
84+
"creationTimestamp": "2022-06-28T00:00:00Z",
8485
},
8586
})
8687
if err != nil {
@@ -197,6 +198,42 @@ func Test_compiledEach_Values(t *testing.T) {
197198
}
198199
}
199200

201+
func Test_compiledEach_Timestamp(t *testing.T) {
202+
type tc struct {
203+
name string
204+
each compiledEach
205+
wantResult []eachValue
206+
wantErrors []error
207+
}
208+
val := func(value float64, labels ...string) eachValue {
209+
t.Helper()
210+
if len(labels)%2 != 0 {
211+
t.Fatalf("labels must be even: %v", labels)
212+
}
213+
m := make(map[string]string)
214+
for i := 0; i < len(labels); i += 2 {
215+
m[labels[i]] = labels[i+1]
216+
}
217+
return eachValue{
218+
Value: value,
219+
Labels: m,
220+
}
221+
}
222+
223+
tests := []tc{
224+
{name: "single_timestamp", each: compiledEach{
225+
Path: mustCompilePath(t, "metadata", "creationTimestamp"),
226+
}, wantResult: []eachValue{val(1656374400)}},
227+
}
228+
for _, tt := range tests {
229+
t.Run(tt.name, func(t *testing.T) {
230+
gotResult, gotErrors := tt.each.Values(cr)
231+
assert.Equal(t, tt.wantResult, gotResult)
232+
assert.Equal(t, tt.wantErrors, gotErrors)
233+
})
234+
}
235+
}
236+
200237
func Test_compiledFamily_BaseLabels(t *testing.T) {
201238
tests := []struct {
202239
name string

0 commit comments

Comments
 (0)