Skip to content

Commit

Permalink
To support read the timestamp fields of custom resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dontan001 committed Jun 28, 2022
1 parent 3bc6557 commit 889d7b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"strconv"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -499,6 +500,9 @@ func getNum(value interface{}) (float64, error) {
}
return 0, nil
case string:
if t, e := time.Parse(time.RFC3339, value.(string)); e == nil {
return float64(t.Unix()), nil
}
return strconv.ParseFloat(value.(string), 64)
case byte:
v = float64(vv)
Expand Down
37 changes: 37 additions & 0 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func init() {
"qux": "quxx",
"bar": "baz",
},
"creationTimestamp": "2022-06-28T00:00:00Z",
},
})
if err != nil {
Expand Down Expand Up @@ -197,6 +198,42 @@ func Test_compiledEach_Values(t *testing.T) {
}
}

func Test_compiledEach_Timestamp(t *testing.T) {
type tc struct {
name string
each compiledEach
wantResult []eachValue
wantErrors []error
}
val := func(value float64, labels ...string) eachValue {
t.Helper()
if len(labels)%2 != 0 {
t.Fatalf("labels must be even: %v", labels)
}
m := make(map[string]string)
for i := 0; i < len(labels); i += 2 {
m[labels[i]] = labels[i+1]
}
return eachValue{
Value: value,
Labels: m,
}
}

tests := []tc{
{name: "single_timestamp", each: compiledEach{
Path: mustCompilePath(t, "metadata", "creationTimestamp"),
}, wantResult: []eachValue{val(1656374400)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult, gotErrors := tt.each.Values(cr)
assert.Equal(t, tt.wantResult, gotResult)
assert.Equal(t, tt.wantErrors, gotErrors)
})
}
}

func Test_compiledFamily_BaseLabels(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 889d7b4

Please sign in to comment.