Merge pull request #38330 from caesarxuchao/fix-direct-encoder

Automatic merge from submit-queue (batch tested with PRs 36419, 38330, 37718, 38244, 38375)

Let DirectEncoder take a hint of what gvk to set during its construction

Fix https://github.com/kubernetes/kubeadm/issues/52.

The issue was that when the kubeadm binary executed `c.Extensions().Deployments().Delete(&v1.DeleteOptions{})`, the DeleteOptions.APIVersion is set as `kubeadm.k8s.io/v1alpha` in the serialized format. API server couldn't decode that.

With this PR, `DeleteOptions.APIVersion` will be set to `extensions.v1beta1` in the serialized format.

cc @mikedanese @luxas 

@kubernetes/sig-api-machinery
This commit is contained in:
Kubernetes Submit Queue 2016-12-08 17:13:55 -08:00 committed by GitHub
commit 7c09b56494
3 changed files with 42 additions and 5 deletions

View File

@ -220,9 +220,10 @@ type DirectCodecFactory struct {
CodecFactory
}
// EncoderForVersion returns an encoder that does not do conversion. gv is ignored.
func (f DirectCodecFactory) EncoderForVersion(serializer runtime.Encoder, _ runtime.GroupVersioner) runtime.Encoder {
// EncoderForVersion returns an encoder that does not do conversion.
func (f DirectCodecFactory) EncoderForVersion(serializer runtime.Encoder, version runtime.GroupVersioner) runtime.Encoder {
return versioning.DirectEncoder{
Version: version,
Encoder: serializer,
ObjectTyper: f.CodecFactory.scheme,
}

View File

@ -227,6 +227,7 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
// DirectEncoder serializes an object and ensures the GVK is set.
type DirectEncoder struct {
Version runtime.GroupVersioner
runtime.Encoder
runtime.ObjectTyper
}
@ -242,7 +243,14 @@ func (e DirectEncoder) Encode(obj runtime.Object, stream io.Writer) error {
}
kind := obj.GetObjectKind()
oldGVK := kind.GroupVersionKind()
kind.SetGroupVersionKind(gvks[0])
gvk := gvks[0]
if e.Version != nil {
preferredGVK, ok := e.Version.KindForGroupVersionKinds(gvks)
if ok {
gvk = preferredGVK
}
}
kind.SetGroupVersionKind(gvk)
err = e.Encoder.Encode(obj, stream)
kind.SetGroupVersionKind(oldGVK)
return err

View File

@ -329,8 +329,9 @@ func (c *checkConvertor) ConvertFieldLabel(version, kind, label, value string) (
}
type mockSerializer struct {
err error
obj runtime.Object
err error
obj runtime.Object
encodingObjGVK schema.GroupVersionKind
defaults, actual *schema.GroupVersionKind
into runtime.Object
@ -344,6 +345,7 @@ func (s *mockSerializer) Decode(data []byte, defaults *schema.GroupVersionKind,
func (s *mockSerializer) Encode(obj runtime.Object, w io.Writer) error {
s.obj = obj
s.encodingObjGVK = obj.GetObjectKind().GroupVersionKind()
return s.err
}
@ -369,3 +371,29 @@ func (t *mockTyper) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind,
func (t *mockTyper) Recognizes(_ schema.GroupVersionKind) bool {
return true
}
func TestDirectCodecEncode(t *testing.T) {
serializer := mockSerializer{}
typer := mockTyper{
gvks: []schema.GroupVersionKind{
{
Group: "wrong_group",
Kind: "some_kind",
},
{
Group: "expected_group",
Kind: "some_kind",
},
},
}
c := DirectEncoder{
Version: schema.GroupVersion{Group: "expected_group"},
Encoder: &serializer,
ObjectTyper: &typer,
}
c.Encode(&testDecodable{}, ioutil.Discard)
if e, a := "expected_group", serializer.encodingObjGVK.Group; e != a {
t.Errorf("expected group to be %v, got %v", e, a)
}
}