34
34
// scheme tracks the type registry for the sdk
35
35
// This scheme is used to decode json data into the correct Go type based on the object's GVK
36
36
// 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
39
40
)
40
41
41
42
func init () {
@@ -45,7 +46,17 @@ func init() {
45
46
cgoscheme .AddToScheme (scheme )
46
47
}
47
48
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 {
49
60
codec := codecs .UniversalDecoder (gv )
50
61
return codec
51
62
}
@@ -60,7 +71,7 @@ func AddToSDKScheme(addToScheme addToSchemeFunc) {
60
71
// RuntimeObjectFromUnstructured converts an unstructured to a runtime object
61
72
func RuntimeObjectFromUnstructured (u * unstructured.Unstructured ) runtime.Object {
62
73
gvk := u .GroupVersionKind ()
63
- decoder := decoder (gvk .GroupVersion ())
74
+ decoder := decoderFunc (gvk .GroupVersion (), codecs )
64
75
65
76
b , err := u .MarshalJSON ()
66
77
if err != nil {
@@ -91,7 +102,7 @@ func UnstructuredFromRuntimeObject(ro runtime.Object) *unstructured.Unstructured
91
102
// TODO: https://github.com/operator-framework/operator-sdk/issues/127
92
103
func UnstructuredIntoRuntimeObject (u * unstructured.Unstructured , into runtime.Object ) error {
93
104
gvk := u .GroupVersionKind ()
94
- decoder := decoder (gvk .GroupVersion ())
105
+ decoder := decoderFunc (gvk .GroupVersion (), codecs )
95
106
96
107
b , err := u .MarshalJSON ()
97
108
if err != nil {
@@ -111,7 +122,7 @@ func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) er
111
122
return err
112
123
}
113
124
gvk := from .GetObjectKind ().GroupVersionKind ()
114
- decoder := decoder (gvk .GroupVersion ())
125
+ decoder := decoderFunc (gvk .GroupVersion (), codecs )
115
126
_ , _ , err = decoder .Decode (b , & gvk , into )
116
127
if err != nil {
117
128
return fmt .Errorf ("failed to decode json data with gvk(%v): %v" , gvk .String (), err )
0 commit comments