Skip to content

util/k8sutil: add ability to configure decoder #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var (
// scheme tracks the type registry for the sdk
// This scheme is used to decode json data into the correct Go type based on the object's GVK
// All types that the operator watches must be added to this scheme
scheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(scheme)
scheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(scheme)
decoderFunc = decoder
)

func init() {
Expand All @@ -45,7 +46,17 @@ func init() {
cgoscheme.AddToScheme(scheme)
}

func decoder(gv schema.GroupVersion) runtime.Decoder {
// UtilDecoderFunc retrieve the correct decoder from a GroupVersion
// and the schemes codec factory.
type UtilDecoderFunc func(schema.GroupVersion, serializer.CodecFactory) runtime.Decoder

// SetDecoderFunc sets a non default decoder function
// This is used as a work around to add support for unstructured objects
func SetDecoderFunc(u UtilDecoderFunc) {
decoderFunc = u
}

func decoder(gv schema.GroupVersion, codecs serializer.CodecFactory) runtime.Decoder {
codec := codecs.UniversalDecoder(gv)
return codec
}
Expand All @@ -60,7 +71,7 @@ func AddToSDKScheme(addToScheme addToSchemeFunc) {
// RuntimeObjectFromUnstructured converts an unstructured to a runtime object
func RuntimeObjectFromUnstructured(u *unstructured.Unstructured) runtime.Object {
gvk := u.GroupVersionKind()
decoder := decoder(gvk.GroupVersion())
decoder := decoderFunc(gvk.GroupVersion(), codecs)

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

b, err := u.MarshalJSON()
if err != nil {
Expand All @@ -111,7 +122,7 @@ func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) er
return err
}
gvk := from.GetObjectKind().GroupVersionKind()
decoder := decoder(gvk.GroupVersion())
decoder := decoderFunc(gvk.GroupVersion(), codecs)
_, _, err = decoder.Decode(b, &gvk, into)
if err != nil {
return fmt.Errorf("failed to decode json data with gvk(%v): %v", gvk.String(), err)
Expand Down