1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-03 10:06:35 +00:00
steve/pkg/resources/virtual/virtual.go

56 lines
1.7 KiB
Go
Raw Normal View History

// Package virtual provides functions/resources to define virtual fields (fields which don't exist in k8s
// but should be visible in the API) on resources
package virtual
import (
"fmt"
"github.com/rancher/steve/pkg/resources/virtual/common"
"github.com/rancher/steve/pkg/resources/virtual/events"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
// TransformBuilder builds transform functions for specified GVKs through GetTransformFunc
type TransformBuilder struct {
defaultFields *common.DefaultFields
}
// NewTransformBuilder returns a TransformBuilder using the given summary cache
func NewTransformBuilder(cache common.SummaryCache) *TransformBuilder {
return &TransformBuilder{
defaultFields: &common.DefaultFields{
Cache: cache,
},
}
}
// GetTransformFunc returns the func to transform a raw object into a fixed object, if needed
func (t *TransformBuilder) GetTransformFunc(gvk schema.GroupVersionKind) 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)
}
converters = append(converters, t.defaultFields.TransformCommon)
return func(raw interface{}) (interface{}, error) {
obj, isSignal, err := common.GetUnstructured(raw)
if isSignal {
// isSignal= true overrides any error
return raw, err
}
if err != nil {
return nil, fmt.Errorf("GetUnstructured: failed to get underlying object: %w", err)
}
// Conversions are run in this loop:
for _, f := range converters {
obj, err = f(obj)
if err != nil {
return nil, err
}
}
return obj, nil
}
}