1
0
mirror of https://github.com/rancher/steve.git synced 2025-08-11 19:21:36 +00:00

Fix parsing of metadata.fields when age is already a duration (#700)

* Fix parsing of metadata.fields when age is already a duration

* Fix frozen "now"

* Use ParseTimestampOrHumanReadableDuration instead
This commit is contained in:
Alejandro Ruiz 2025-07-01 18:53:51 +02:00 committed by GitHub
parent c782fea615
commit 61173104af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 12 deletions

View File

@ -262,14 +262,17 @@ func convertMetadataTimestampFields(request *types.APIRequest, gvk schema2.Group
return return
} }
millis, err := strconv.ParseInt(timeValue, 10, 64) dur, ok := isDuration(timeValue)
if err != nil { if !ok {
logrus.Warnf("convert timestamp value: %s failed with error: %s", timeValue, err.Error()) millis, err := strconv.ParseInt(timeValue, 10, 64)
return if err != nil {
} logrus.Warnf("convert timestamp value: %s failed with error: %s", timeValue, err.Error())
return
}
timestamp := time.Unix(0, millis*int64(time.Millisecond)) timestamp := time.Unix(0, millis*int64(time.Millisecond))
dur := time.Since(timestamp) dur = time.Since(timestamp)
}
humanDuration := duration.HumanDuration(dur) humanDuration := duration.HumanDuration(dur)
if humanDuration == "<invalid>" { if humanDuration == "<invalid>" {
@ -287,6 +290,11 @@ func convertMetadataTimestampFields(request *types.APIRequest, gvk schema2.Group
} }
} }
func isDuration(value string) (time.Duration, bool) {
d, err := ParseTimestampOrHumanReadableDuration(value)
return d, err == nil
}
func excludeValues(request *types.APIRequest, unstr *unstructured.Unstructured) { func excludeValues(request *types.APIRequest, unstr *unstructured.Unstructured) {
if values, ok := request.Query["excludeValues"]; ok { if values, ok := request.Query["excludeValues"]; ok {
for _, f := range values { for _, f := range values {

View File

@ -11,13 +11,14 @@ import (
"github.com/rancher/steve/pkg/resources/virtual/clusters" "github.com/rancher/steve/pkg/resources/virtual/clusters"
"github.com/rancher/steve/pkg/resources/virtual/common" "github.com/rancher/steve/pkg/resources/virtual/common"
"github.com/rancher/steve/pkg/resources/virtual/events" "github.com/rancher/steve/pkg/resources/virtual/events"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
) )
var now = time.Now() var now = time.Now
// TransformBuilder builds transform functions for specified GVKs through GetTransformFunc // TransformBuilder builds transform functions for specified GVKs through GetTransformFunc
type TransformBuilder struct { type TransformBuilder struct {
@ -72,7 +73,7 @@ func (t *TransformBuilder) GetTransformFunc(gvk schema.GroupVersionKind, columns
return obj, nil return obj, nil
} }
curValue[index] = fmt.Sprintf("%d", now.Add(-duration).UnixMilli()) curValue[index] = fmt.Sprintf("%d", now().Add(-duration).UnixMilli())
if err := unstructured.SetNestedSlice(obj.Object, curValue, "metadata", "fields"); err != nil { if err := unstructured.SetNestedSlice(obj.Object, curValue, "metadata", "fields"); err != nil {
return nil, err return nil, err
} }

View File

@ -17,7 +17,7 @@ import (
) )
func TestTransformChain(t *testing.T) { func TestTransformChain(t *testing.T) {
now = func() time.Time { return time.Date(1992, 9, 2, 0, 0, 0, 0, time.UTC) }() now = func() time.Time { return time.Date(1992, 9, 2, 0, 0, 0, 0, time.UTC) }
noColumns := []rescommon.ColumnDefinition{} noColumns := []rescommon.ColumnDefinition{}
tests := []struct { tests := []struct {
name string name string
@ -154,7 +154,7 @@ func TestTransformChain(t *testing.T) {
"message": "", "message": "",
}, },
"fields": []interface{}{ "fields": []interface{}{
fmt.Sprintf("%d", now.Add(-24*time.Hour).UnixMilli()), fmt.Sprintf("%d", now().Add(-24*time.Hour).UnixMilli()),
}, },
}, },
"id": "test-ns/testobj", "id": "test-ns/testobj",
@ -216,7 +216,7 @@ func TestTransformChain(t *testing.T) {
"message": "", "message": "",
}, },
"fields": []interface{}{ "fields": []interface{}{
fmt.Sprintf("%d", now.Add(-24*time.Hour).UnixMilli()), fmt.Sprintf("%d", now().Add(-24*time.Hour).UnixMilli()),
}, },
}, },
"id": "test-ns/testobj", "id": "test-ns/testobj",