1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-04 10:36:40 +00:00
steve/pkg/resources/virtual/common/util.go
Eric Promislow 06c2eb50d1
Index more sqlite cache fields (#271)
* Add more fields to index when sql-caching is on.

Misc changes:
- Use the builtin Event class, not events.k8s.io (by looking at the dashboard client code)
- Specify full path to the management.cattle.io fields.
- Map `Event.type` to `Event._type` for indexing.

Use a compound transform-func to first check for a "signal",
and then to run all the relevant transformers until either
one fails or the list is exhausted.

- Includes moving the fakeSummaryCache type into a common area.

Use a simpler way of running transforms.

* Inline the function to get the gvk key.

* Create a '--sql-cache' flag to turn on caching for the steve CLI.

* Improve error-handling in object transformer.

* Drop the 'GetTransform' function.

* Inline the code that transforms a payload into a k8s-unstructured object.
2024-10-18 11:06:29 -07:00

41 lines
1.3 KiB
Go

package common
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/tools/cache"
)
// GetUnstructured retrieves an unstructured object from the provided input. If this is a signal
// object (like cache.DeletedFinalStateUnknown), returns true, indicating that this wasn't an
// unstructured object, but doesn't need to be processed by our transform function
func GetUnstructured(obj any) (*unstructured.Unstructured, bool, error) {
raw, ok := obj.(*unstructured.Unstructured)
if !ok {
_, isFinalUnknown := obj.(cache.DeletedFinalStateUnknown)
if isFinalUnknown {
// As documented in the TransformFunc interface
return nil, true, nil
}
return nil, false, fmt.Errorf("object was of type %T, not unstructured", raw)
}
return raw, false, nil
}
// toMap converts an object to a map[string]any which can be stored/retrieved from the cache. Currently
// uses json encoding to take advantage of tag names
func toMap(obj any) (map[string]any, error) {
bytes, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("unable to marshal object: %w", err)
}
var retObj map[string]any
err = json.Unmarshal(bytes, &retObj)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal object: %w", err)
}
return retObj, nil
}