Commit 7fde4583 authored by Clayton Coleman's avatar Clayton Coleman

v1beta1 should return Minion as kind, rather than Node

This changes the internal name logic (for conversion) to prefer the internal registered preferred name for a resource, and then makes v1beta1 and v1beta2 prefer Minion. Fixes #3010
parent edfae866
...@@ -17,6 +17,7 @@ limitations under the License. ...@@ -17,6 +17,7 @@ limitations under the License.
package v1beta1_test package v1beta1_test
import ( import (
"encoding/json"
"reflect" "reflect"
"testing" "testing"
...@@ -27,6 +28,15 @@ import ( ...@@ -27,6 +28,15 @@ import (
var Convert = newer.Scheme.Convert var Convert = newer.Scheme.Convert
func TestNodeConversion(t *testing.T) { func TestNodeConversion(t *testing.T) {
version, kind, err := newer.Scheme.ObjectVersionAndKind(&current.Minion{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if version != "v1beta1" || kind != "Minion" {
t.Errorf("unexpected version and kind: %s %s", version, kind)
}
newer.Scheme.Log(t)
obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`)) obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`))
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
...@@ -47,6 +57,19 @@ func TestNodeConversion(t *testing.T) { ...@@ -47,6 +57,19 @@ func TestNodeConversion(t *testing.T) {
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`), obj); err != nil { if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`), obj); err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
obj = &newer.Node{}
data, err := current.Codec.Encode(obj)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
m := map[string]interface{}{}
if err := json.Unmarshal(data, &m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m["kind"] != "Minion" {
t.Errorf("unexpected encoding: %s - %#v", m["kind"], string(data))
}
} }
func TestEnvConversion(t *testing.T) { func TestEnvConversion(t *testing.T) {
......
...@@ -25,12 +25,6 @@ import ( ...@@ -25,12 +25,6 @@ import (
var Codec = runtime.CodecFor(api.Scheme, "v1beta1") var Codec = runtime.CodecFor(api.Scheme, "v1beta1")
func init() { func init() {
// Future names are supported, and declared first so they take precedence
api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta1", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta1", "OperationList", &ServerOpList{})
api.Scheme.AddKnownTypes("v1beta1", api.Scheme.AddKnownTypes("v1beta1",
&Pod{}, &Pod{},
&PodContainerInfo{}, &PodContainerInfo{},
...@@ -58,6 +52,8 @@ func init() { ...@@ -58,6 +52,8 @@ func init() {
// Future names are supported // Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{}) api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{}) api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta1", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta1", "OperationList", &ServerOpList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}
......
...@@ -17,10 +17,11 @@ limitations under the License. ...@@ -17,10 +17,11 @@ limitations under the License.
package v1beta2_test package v1beta2_test
import ( import (
"encoding/json"
"testing" "testing"
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
) )
func TestServiceEmptySelector(t *testing.T) { func TestServiceEmptySelector(t *testing.T) {
...@@ -56,6 +57,15 @@ func TestServiceEmptySelector(t *testing.T) { ...@@ -56,6 +57,15 @@ func TestServiceEmptySelector(t *testing.T) {
} }
func TestNodeConversion(t *testing.T) { func TestNodeConversion(t *testing.T) {
version, kind, err := newer.Scheme.ObjectVersionAndKind(&current.Minion{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if version != "v1beta2" || kind != "Minion" {
t.Errorf("unexpected version and kind: %s %s", version, kind)
}
newer.Scheme.Log(t)
obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`)) obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`))
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
...@@ -76,4 +86,17 @@ func TestNodeConversion(t *testing.T) { ...@@ -76,4 +86,17 @@ func TestNodeConversion(t *testing.T) {
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`), obj); err != nil { if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`), obj); err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
obj = &newer.Node{}
data, err := current.Codec.Encode(obj)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
m := map[string]interface{}{}
if err := json.Unmarshal(data, &m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m["kind"] != "Minion" {
t.Errorf("unexpected encoding: %s - %#v", m["kind"], string(data))
}
} }
...@@ -25,12 +25,6 @@ import ( ...@@ -25,12 +25,6 @@ import (
var Codec = runtime.CodecFor(api.Scheme, "v1beta2") var Codec = runtime.CodecFor(api.Scheme, "v1beta2")
func init() { func init() {
// Future names are supported, and declared first so they take precedence
api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta2", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta2", "OperationList", &ServerOpList{})
api.Scheme.AddKnownTypes("v1beta2", api.Scheme.AddKnownTypes("v1beta2",
&Pod{}, &Pod{},
&PodContainerInfo{}, &PodContainerInfo{},
...@@ -58,6 +52,8 @@ func init() { ...@@ -58,6 +52,8 @@ func init() {
// Future names are supported // Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{}) api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{}) api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta2", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta2", "OperationList", &ServerOpList{})
} }
func (*Pod) IsAnAPIObject() {} func (*Pod) IsAnAPIObject() {}
......
...@@ -74,6 +74,12 @@ func (s *Scheme) EncodeToVersion(obj interface{}, destVersion string) (data []by ...@@ -74,6 +74,12 @@ func (s *Scheme) EncodeToVersion(obj interface{}, destVersion string) (data []by
obj = objOut obj = objOut
} }
// ensure the output object name comes from the destination type
_, objKind, err = s.ObjectVersionAndKind(obj)
if err != nil {
return nil, err
}
// Version and Kind should be set on the wire. // Version and Kind should be set on the wire.
err = s.SetVersionAndKind(destVersion, objKind, obj) err = s.SetVersionAndKind(destVersion, objKind, obj)
if err != nil { if err != nil {
......
...@@ -72,14 +72,21 @@ func (s *Scheme) Log(l DebugLogger) { ...@@ -72,14 +72,21 @@ func (s *Scheme) Log(l DebugLogger) {
s.converter.Debug = l s.converter.Debug = l
} }
// nameFunc returns the name of the type that we wish to use for encoding. Defaults to // nameFunc returns the name of the type that we wish to use to determine when two types attempt
// the go name of the type if the type is not registered. // a conversion. Defaults to the go name of the type if the type is not registered.
func (s *Scheme) nameFunc(t reflect.Type) string { func (s *Scheme) nameFunc(t reflect.Type) string {
// find the preferred names for this type // find the preferred names for this type
names, ok := s.typeToKind[t] names, ok := s.typeToKind[t]
if !ok { if !ok {
return t.Name() return t.Name()
} }
if internal, ok := s.versionMap[""]; ok {
for _, name := range names {
if t, ok := internal[name]; ok {
return s.typeToKind[t][0]
}
}
}
return names[0] return names[0]
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment