Skip to content

Commit

Permalink
v1beta1 should return Minion as kind, rather than Node
Browse files Browse the repository at this point in the history
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 kubernetes#3010
  • Loading branch information
smarterclayton committed Dec 18, 2014
1 parent edfae86 commit 7fde458
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
23 changes: 23 additions & 0 deletions pkg/api/v1beta1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1_test

import (
"encoding/json"
"reflect"
"testing"

Expand All @@ -27,6 +28,15 @@ import (
var Convert = newer.Scheme.Convert

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"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -47,6 +57,19 @@ func TestNodeConversion(t *testing.T) {
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`), obj); err != nil {
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) {
Expand Down
8 changes: 2 additions & 6 deletions pkg/api/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (
var Codec = runtime.CodecFor(api.Scheme, "v1beta1")

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",
&Pod{},
&PodContainerInfo{},
Expand Down Expand Up @@ -58,6 +52,8 @@ func init() {
// Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta1", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta1", "OperationList", &ServerOpList{})
}

func (*Pod) IsAnAPIObject() {}
Expand Down
25 changes: 24 additions & 1 deletion pkg/api/v1beta2/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package v1beta2_test

import (
"encoding/json"
"testing"

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) {
Expand Down Expand Up @@ -56,6 +57,15 @@ func TestServiceEmptySelector(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"}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -76,4 +86,17 @@ func TestNodeConversion(t *testing.T) {
if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`), obj); err != nil {
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))
}
}
8 changes: 2 additions & 6 deletions pkg/api/v1beta2/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (
var Codec = runtime.CodecFor(api.Scheme, "v1beta2")

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",
&Pod{},
&PodContainerInfo{},
Expand Down Expand Up @@ -58,6 +52,8 @@ func init() {
// Future names are supported
api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{})
api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{})
api.Scheme.AddKnownTypeWithName("v1beta2", "Operation", &ServerOp{})
api.Scheme.AddKnownTypeWithName("v1beta2", "OperationList", &ServerOpList{})
}

func (*Pod) IsAnAPIObject() {}
Expand Down
6 changes: 6 additions & 0 deletions pkg/conversion/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (s *Scheme) EncodeToVersion(obj interface{}, destVersion string) (data []by
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.
err = s.SetVersionAndKind(destVersion, objKind, obj)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/conversion/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ func (s *Scheme) Log(l DebugLogger) {
s.converter.Debug = l
}

// nameFunc returns the name of the type that we wish to use for encoding. Defaults to
// the go name of the type if the type is not registered.
// nameFunc returns the name of the type that we wish to use to determine when two types attempt
// a conversion. Defaults to the go name of the type if the type is not registered.
func (s *Scheme) nameFunc(t reflect.Type) string {
// find the preferred names for this type
names, ok := s.typeToKind[t]
if !ok {
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]
}

Expand Down

0 comments on commit 7fde458

Please sign in to comment.