Skip to content

Commit ed39331

Browse files
author
Shawn Hurley
committed
util/k8sutil: add ability to configure decoder
Creates a typed function for getting the decoder from the codecs Adds method to set decoder func Adds package var for the decoder fun fixes #352
1 parent 88bcf1b commit ed39331

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

pkg/util/k8sutil/k8sutil.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var (
3434
// scheme tracks the type registry for the sdk
3535
// This scheme is used to decode json data into the correct Go type based on the object's GVK
3636
// All types that the operator watches must be added to this scheme
37-
scheme = runtime.NewScheme()
38-
codecs = serializer.NewCodecFactory(scheme)
37+
scheme = runtime.NewScheme()
38+
codecs = serializer.NewCodecFactory(scheme)
39+
decoderFunc = decoder
3940
)
4041

4142
func init() {
@@ -45,7 +46,17 @@ func init() {
4546
cgoscheme.AddToScheme(scheme)
4647
}
4748

48-
func decoder(gv schema.GroupVersion) runtime.Decoder {
49+
// UtilDecoderFunc retrieve the correct decoder from a GroupVersion
50+
// and the schemes codec factory.
51+
type UtilDecoderFunc func(schema.GroupVersion, serializer.CodecFactory) runtime.Decoder
52+
53+
// SetDecoderFunc sets a non default decoder function
54+
// This is used as a work around to add support for unstructured objects
55+
func SetDecoderFunc(u UtilDecoderFunc) {
56+
decoderFunc = u
57+
}
58+
59+
func decoder(gv schema.GroupVersion, codecs serializer.CodecFactory) runtime.Decoder {
4960
codec := codecs.UniversalDecoder(gv)
5061
return codec
5162
}
@@ -60,7 +71,7 @@ func AddToSDKScheme(addToScheme addToSchemeFunc) {
6071
// RuntimeObjectFromUnstructured converts an unstructured to a runtime object
6172
func RuntimeObjectFromUnstructured(u *unstructured.Unstructured) runtime.Object {
6273
gvk := u.GroupVersionKind()
63-
decoder := decoder(gvk.GroupVersion())
74+
decoder := decoderFunc(gvk.GroupVersion(), codecs)
6475

6576
b, err := u.MarshalJSON()
6677
if err != nil {
@@ -91,7 +102,7 @@ func UnstructuredFromRuntimeObject(ro runtime.Object) *unstructured.Unstructured
91102
// TODO: https://github.com/operator-framework/operator-sdk/issues/127
92103
func UnstructuredIntoRuntimeObject(u *unstructured.Unstructured, into runtime.Object) error {
93104
gvk := u.GroupVersionKind()
94-
decoder := decoder(gvk.GroupVersion())
105+
decoder := decoderFunc(gvk.GroupVersion(), codecs)
95106

96107
b, err := u.MarshalJSON()
97108
if err != nil {
@@ -111,7 +122,7 @@ func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) er
111122
return err
112123
}
113124
gvk := from.GetObjectKind().GroupVersionKind()
114-
decoder := decoder(gvk.GroupVersion())
125+
decoder := decoderFunc(gvk.GroupVersion(), codecs)
115126
_, _, err = decoder.Decode(b, &gvk, into)
116127
if err != nil {
117128
return fmt.Errorf("failed to decode json data with gvk(%v): %v", gvk.String(), err)

0 commit comments

Comments
 (0)