Skip to content

Commit 75c4451

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

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pkg/util/k8sutil/k8sutil.go

Lines changed: 21 additions & 5 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,6 +46,21 @@ func init() {
4546
cgoscheme.AddToScheme(scheme)
4647
}
4748

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+
4864
func decoder(gv schema.GroupVersion) runtime.Decoder {
4965
codec := codecs.UniversalDecoder(gv)
5066
return codec
@@ -60,7 +76,7 @@ func AddToSDKScheme(addToScheme addToSchemeFunc) {
6076
// RuntimeObjectFromUnstructured converts an unstructured to a runtime object
6177
func RuntimeObjectFromUnstructured(u *unstructured.Unstructured) runtime.Object {
6278
gvk := u.GroupVersionKind()
63-
decoder := decoder(gvk.GroupVersion())
79+
decoder := decoderFunc(gvk.GroupVersion())
6480

6581
b, err := u.MarshalJSON()
6682
if err != nil {
@@ -91,7 +107,7 @@ func UnstructuredFromRuntimeObject(ro runtime.Object) *unstructured.Unstructured
91107
// TODO: https://github.com/operator-framework/operator-sdk/issues/127
92108
func UnstructuredIntoRuntimeObject(u *unstructured.Unstructured, into runtime.Object) error {
93109
gvk := u.GroupVersionKind()
94-
decoder := decoder(gvk.GroupVersion())
110+
decoder := decoderFunc(gvk.GroupVersion())
95111

96112
b, err := u.MarshalJSON()
97113
if err != nil {
@@ -111,7 +127,7 @@ func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) er
111127
return err
112128
}
113129
gvk := from.GetObjectKind().GroupVersionKind()
114-
decoder := decoder(gvk.GroupVersion())
130+
decoder := decoderFunc(gvk.GroupVersion())
115131
_, _, err = decoder.Decode(b, &gvk, into)
116132
if err != nil {
117133
return fmt.Errorf("failed to decode json data with gvk(%v): %v", gvk.String(), err)

0 commit comments

Comments
 (0)