Skip to content
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

handle watch event serialization for third party resources #25938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/registry/thirdpartyresourcedata/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,6 @@ func encodeToJSON(obj *extensions.ThirdPartyResourceData, stream io.Writer) erro

func (t *thirdPartyResourceDataEncoder) Encode(obj runtime.Object, stream io.Writer) (err error) {
switch obj := obj.(type) {
case *versioned.InternalEvent:
return t.delegate.Encode(obj, stream)
case *extensions.ThirdPartyResourceData:
return encodeToJSON(obj, stream)
case *extensions.ThirdPartyResourceDataList:
Expand All @@ -502,6 +500,20 @@ func (t *thirdPartyResourceDataEncoder) Encode(obj runtime.Object, stream io.Wri
}
gv := t.gvk.GroupVersion()
fmt.Fprintf(stream, template, t.gvk.Kind+"List", gv.String(), strings.Join(dataStrings, ","))
return nil
case *versioned.InternalEvent:
event := &versioned.Event{}
err := versioned.Convert_versioned_InternalEvent_to_versioned_Event(obj, event, nil)
if err != nil {
return err
}

enc := json.NewEncoder(stream)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use t.delegate.EncodeToStream(...) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brendandburns I dont think this is possible as it currently stands, attempting it throws an error e.g. no kind "WatchEvent" is registered for version "stable.sitepod.io/v1"

err = enc.Encode(event)
if err != nil {
return err
}

return nil
case *unversioned.Status, *unversioned.APIResourceList:
return t.delegate.Encode(obj, stream)
Expand Down
27 changes: 27 additions & 0 deletions pkg/registry/thirdpartyresourcedata/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package thirdpartyresourcedata

import (
"bytes"
"encoding/json"
"reflect"
"testing"
Expand All @@ -29,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch/versioned"
)

type Foo struct {
Expand Down Expand Up @@ -219,3 +221,28 @@ func TestCreater(t *testing.T) {

}
}

func TestEncodeToStreamForInternalEvent(t *testing.T) {
e := &thirdPartyResourceDataEncoder{gvk: unversioned.GroupVersionKind{
Group: "company.com",
Version: "v1",
Kind: "Foo",
}, delegate: testapi.Extensions.Codec()}
buf := bytes.NewBuffer([]byte{})
expected := &versioned.Event{
Type: "Added",
}
err := e.Encode(&versioned.InternalEvent{
Type: "Added",
}, buf)

jBytes, _ := json.Marshal(expected)

if string(jBytes) == buf.String() {
t.Errorf("unexpected encoding expected %s got %s", string(jBytes), buf.String())
}

if err != nil {
t.Errorf("unexpected error encoding: %v", err)
}
}