-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new cache.Options field to customize transform
- Loading branch information
Alexander Zielenski
committed
Apr 1, 2022
1 parent
4270760
commit 603031f
Showing
4 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package internal | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
type runtimeObjectKind int | ||
|
||
const ( | ||
// prefixed names since there are packages with these names that conflict | ||
runtimeObjectKindStructured runtimeObjectKind = iota | ||
runtimeObjectKindUnstructured | ||
runtimeObjectKindMetadata | ||
) | ||
|
||
func runtimeObjectKindForObject(obj runtime.Object) runtimeObjectKind { | ||
switch obj.(type) { | ||
case *unstructured.Unstructured: | ||
return runtimeObjectKindUnstructured | ||
case *unstructured.UnstructuredList: | ||
return runtimeObjectKindUnstructured | ||
case *metav1.PartialObjectMetadata: | ||
return runtimeObjectKindMetadata | ||
case *metav1.PartialObjectMetadataList: | ||
return runtimeObjectKindMetadata | ||
default: | ||
return runtimeObjectKindStructured | ||
} | ||
} | ||
|
||
type TransformFuncByObject map[runtimeObjectKind]map[schema.GroupVersionKind]cache.TransformFunc | ||
type TransformFuncByGVK map[schema.GroupVersionKind]cache.TransformFunc | ||
|
||
func NewTransformFuncByObject() TransformFuncByObject { | ||
return TransformFuncByObject{} | ||
} | ||
|
||
func (t TransformFuncByObject) Set(obj runtime.Object, scheme *runtime.Scheme, transformer cache.TransformFunc) error { | ||
kind := runtimeObjectKindForObject(obj) | ||
existing, ok := t[kind] | ||
if !ok { | ||
existing = TransformFuncByGVK{} | ||
t[kind] = existing | ||
} | ||
|
||
gvk, err := apiutil.GVKForObject(obj, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
existing[gvk] = transformer | ||
return nil | ||
} | ||
|
||
func (t TransformFuncByObject) Get(kind runtimeObjectKind) TransformFuncByGVK { | ||
if val, ok := t[kind]; ok { | ||
return val | ||
} | ||
return nil | ||
} | ||
|
||
func (t TransformFuncByGVK) Get(gvk schema.GroupVersionKind) cache.TransformFunc { | ||
if val, ok := t[gvk]; ok { | ||
return val | ||
} | ||
return nil | ||
} |