mirror of
https://github.com/rancher/steve.git
synced 2025-09-08 18:59:58 +00:00
* added timestamp convertion to metadata.fields * fixed duration parsing * fixed tests * removed tags file * added comments * added better error handling * changed ParseHumanDuration to use Fscanf * added builtins handling * adding mock updates * fixing tests * another try * added timestamp convertion to metadata.fields * addressing comments from @ericpromislow * converting error to warning * added template options
This commit is contained in:
@@ -4,7 +4,10 @@ package virtual
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
rescommon "github.com/rancher/steve/pkg/resources/common"
|
||||
"github.com/rancher/steve/pkg/resources/virtual/clusters"
|
||||
"github.com/rancher/steve/pkg/resources/virtual/common"
|
||||
"github.com/rancher/steve/pkg/resources/virtual/events"
|
||||
@@ -13,6 +16,8 @@ import (
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
var now = time.Now()
|
||||
|
||||
// TransformBuilder builds transform functions for specified GVKs through GetTransformFunc
|
||||
type TransformBuilder struct {
|
||||
defaultFields *common.DefaultFields
|
||||
@@ -28,13 +33,52 @@ func NewTransformBuilder(cache common.SummaryCache) *TransformBuilder {
|
||||
}
|
||||
|
||||
// GetTransformFunc returns the func to transform a raw object into a fixed object, if needed
|
||||
func (t *TransformBuilder) GetTransformFunc(gvk schema.GroupVersionKind) cache.TransformFunc {
|
||||
func (t *TransformBuilder) GetTransformFunc(gvk schema.GroupVersionKind, columns []rescommon.ColumnDefinition) cache.TransformFunc {
|
||||
converters := make([]func(*unstructured.Unstructured) (*unstructured.Unstructured, error), 0)
|
||||
if gvk.Kind == "Event" && gvk.Group == "" && gvk.Version == "v1" {
|
||||
converters = append(converters, events.TransformEventObject)
|
||||
} else if gvk.Kind == "Cluster" && gvk.Group == "management.cattle.io" && gvk.Version == "v3" {
|
||||
converters = append(converters, clusters.TransformManagedCluster)
|
||||
}
|
||||
|
||||
// Detecting if we need to convert date fields
|
||||
for _, col := range columns {
|
||||
gvkDateFields, gvkFound := rescommon.DateFieldsByGVKBuiltins[gvk]
|
||||
if col.Type == "date" || (gvkFound && slices.Contains(gvkDateFields, col.Name)) {
|
||||
converters = append(converters, func(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
index := rescommon.GetIndexValueFromString(col.Field)
|
||||
if index == -1 {
|
||||
return nil, fmt.Errorf("field index not found at column.Field struct variable: %s", col.Field)
|
||||
}
|
||||
|
||||
curValue, got, err := unstructured.NestedSlice(obj.Object, "metadata", "fields")
|
||||
if !got {
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
value, cast := curValue[index].(string)
|
||||
if !cast {
|
||||
return nil, fmt.Errorf("could not cast metadata.fields %d to string", index)
|
||||
}
|
||||
duration, err := rescommon.ParseHumanReadableDuration(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
curValue[index] = fmt.Sprintf("%d", now.Add(-duration).UnixMilli())
|
||||
if err := unstructured.SetNestedSlice(obj.Object, curValue, "metadata", "fields"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
converters = append(converters, t.defaultFields.TransformCommon)
|
||||
|
||||
return func(raw interface{}) (interface{}, error) {
|
||||
|
Reference in New Issue
Block a user