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,6 +46,21 @@ func init() {
45
46
cgoscheme .AddToScheme (scheme )
46
47
}
47
48
49
+ // UtilDecoderFunc finds the correct decoder from a GroupVersion
50
+ type UtilDecoderFunc func (gv schema.GroupVersion ) runtime.Decoder
51
+
52
+ // SetDecoderFunc sets a non default decoder function
53
+ // This is used as a work around to add support for unstructured objects
54
+ func SetDecoderFunc (u UtilDecoderFunc ) {
55
+ decoderFunc = u
56
+ }
57
+
58
+ // GetCodecs get the codec factory for the scheme
59
+ // Exposing the codecs factory so that a user can use the codecs for the scheme.
60
+ func GetCodecs () serializer.CodecFactory {
61
+ return codecs
62
+ }
63
+
48
64
func decoder (gv schema.GroupVersion ) runtime.Decoder {
49
65
codec := codecs .UniversalDecoder (gv )
50
66
return codec
@@ -60,7 +76,7 @@ func AddToSDKScheme(addToScheme addToSchemeFunc) {
60
76
// RuntimeObjectFromUnstructured converts an unstructured to a runtime object
61
77
func RuntimeObjectFromUnstructured (u * unstructured.Unstructured ) runtime.Object {
62
78
gvk := u .GroupVersionKind ()
63
- decoder := decoder (gvk .GroupVersion ())
79
+ decoder := decoderFunc (gvk .GroupVersion ())
64
80
65
81
b , err := u .MarshalJSON ()
66
82
if err != nil {
@@ -91,7 +107,7 @@ func UnstructuredFromRuntimeObject(ro runtime.Object) *unstructured.Unstructured
91
107
// TODO: https://github.com/operator-framework/operator-sdk/issues/127
92
108
func UnstructuredIntoRuntimeObject (u * unstructured.Unstructured , into runtime.Object ) error {
93
109
gvk := u .GroupVersionKind ()
94
- decoder := decoder (gvk .GroupVersion ())
110
+ decoder := decoderFunc (gvk .GroupVersion ())
95
111
96
112
b , err := u .MarshalJSON ()
97
113
if err != nil {
@@ -111,7 +127,7 @@ func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) er
111
127
return err
112
128
}
113
129
gvk := from .GetObjectKind ().GroupVersionKind ()
114
- decoder := decoder (gvk .GroupVersion ())
130
+ decoder := decoderFunc (gvk .GroupVersion ())
115
131
_ , _ , err = decoder .Decode (b , & gvk , into )
116
132
if err != nil {
117
133
return fmt .Errorf ("failed to decode json data with gvk(%v): %v" , gvk .String (), err )
0 commit comments