diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 5218b05fa6..55f86b80a3 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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" @@ -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) diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index 311d2ea16a..166399ac28 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -81,6 +81,7 @@ func init() { "qux": "quxx", "bar": "baz", }, + "creationTimestamp": "2022-06-28T00:00:00Z", }, }) if err != nil { @@ -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