Skip to content

Commit

Permalink
Pull event-object handling into a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpromislow committed Sep 24, 2024
1 parent c99d78a commit 88f64b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
35 changes: 2 additions & 33 deletions pkg/resources/virtual/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type DefaultFields struct {
Cache SummaryCache
}

type EventFields struct {
DF *DefaultFields
}

// GetTransform produces the default transformation func
func (d *DefaultFields) GetTransform() cache.TransformFunc {
return d.transform
Expand All @@ -42,28 +38,10 @@ func (d *DefaultFields) transform(obj any) (any, error) {
if err != nil {
return nil, err
}
return d.transformRest(raw)
}

// GetEventTransform produces the transformation func for events
func (e *EventFields) GetEventTransform() cache.TransformFunc {
return e.transformEventObject
}

// transformEventObjects does special-case handling on event objects
func (e *EventFields) transformEventObject(obj any) (any, error) {
raw, isSignal, err := getUnstructured(obj)
if isSignal {
return obj, nil
}
if err != nil {
return nil, err
}
raw = fixTypeField(raw)
return e.DF.transformRest(raw)
return d.transformCommon(raw)
}

func (d *DefaultFields) transformRest(raw *unstructured.Unstructured) (any, error) {
func (d *DefaultFields) transformCommon(raw *unstructured.Unstructured) (any, error) {
raw = addIDField(raw)
raw, err := addSummaryFields(raw, d.Cache)
if err != nil {
Expand Down Expand Up @@ -113,15 +91,6 @@ func addIDField(raw *unstructured.Unstructured) *unstructured.Unstructured {
return raw
}

// updateTypeField replaces the _type field with the contents of the field named "type", if it exists
func fixTypeField(raw *unstructured.Unstructured) *unstructured.Unstructured {
currentTypeValue, ok := raw.Object["type"]
if ok {
raw.Object["_type"] = currentTypeValue
}
return raw
}

func normalizeConditions(raw *unstructured.Unstructured) {
var (
obj data.Object
Expand Down
38 changes: 38 additions & 0 deletions pkg/resources/virtual/common/event_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Package common provides cache.TransformFunc's for /v1 Event objects
package common

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/tools/cache"
)

type EventFields struct {
DF *DefaultFields
}

// GetEventTransform produces the transformation func for events
func (e *EventFields) GetEventTransform() cache.TransformFunc {
return e.transformEventObject
}

// transformEventObjects does special-case handling on event objects
func (e *EventFields) transformEventObject(obj any) (any, error) {
raw, isSignal, err := getUnstructured(obj)
if isSignal {
return obj, nil
}
if err != nil {
return nil, err
}
raw = fixTypeField(raw)
return e.DF.transformCommon(raw)
}

// updateTypeField replaces the _type field with the contents of the field named "type", if it exists
func fixTypeField(raw *unstructured.Unstructured) *unstructured.Unstructured {
currentTypeValue, ok := raw.Object["type"]
if ok {
raw.Object["_type"] = currentTypeValue
}
return raw
}
4 changes: 3 additions & 1 deletion pkg/resources/virtual/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func NewTransformBuilder(cache common.SummaryCache) *TransformBuilder {
return t
}

// GetTransformFunc retrieves a TransformFunc for a given GVK. Currently only returns a transformFunc for defaultFields
// GetTransformFunc retrieves a TransformFunc for a given GVK.
// Currently it returns an event-specific transform func for /v1 Event objects,
// and the default transformFunc for all other objects.
func (t *TransformBuilder) GetTransformFunc(gvk schema.GroupVersionKind) cache.TransformFunc {
if gvk.Kind == "Event" && gvk.Group == "" && gvk.Version == "v1" {
return t.eventFields.GetEventTransform()
Expand Down

0 comments on commit 88f64b9

Please sign in to comment.