From 650aead4c420f6a48a1f10a0d369995e1fd9df80 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sun, 7 Dec 2014 22:31:46 -0500 Subject: [PATCH] Support Node/NodeList as a valid name in all versions Also refactor v1beta1/conversion_test.go `v1beta` -> `current` to allow easier cut and paste of tests. --- pkg/api/register.go | 3 ++ pkg/api/v1beta1/conversion_test.go | 73 ++++++++++++++++++++---------- pkg/api/v1beta1/register.go | 3 ++ pkg/api/v1beta2/conversion_test.go | 29 ++++++++++-- pkg/api/v1beta2/register.go | 3 ++ pkg/api/v1beta3/conversion_test.go | 47 +++++++++++++++++++ pkg/api/v1beta3/register.go | 3 ++ 7 files changed, 133 insertions(+), 28 deletions(-) create mode 100644 pkg/api/v1beta3/conversion_test.go diff --git a/pkg/api/register.go b/pkg/api/register.go index 14f7283c5c..d508d73b1e 100644 --- a/pkg/api/register.go +++ b/pkg/api/register.go @@ -46,6 +46,9 @@ func init() { &BoundPod{}, &BoundPods{}, ) + // Legacy names are supported + Scheme.AddKnownTypeWithName("", "Node", &Minion{}) + Scheme.AddKnownTypeWithName("", "NodeList", &MinionList{}) } func (*Pod) IsAnAPIObject() {} diff --git a/pkg/api/v1beta1/conversion_test.go b/pkg/api/v1beta1/conversion_test.go index 302d7aed19..23d9b6dbc2 100644 --- a/pkg/api/v1beta1/conversion_test.go +++ b/pkg/api/v1beta1/conversion_test.go @@ -21,13 +21,36 @@ import ( "testing" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" + current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" ) var Convert = newer.Scheme.Convert +func TestNodeConversion(t *testing.T) { + obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.Minion); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj, err = current.Codec.Decode([]byte(`{"kind":"NodeList","apiVersion":"v1beta1"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.MinionList); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj = &newer.Node{} + if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta1"}`), obj); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + func TestEnvConversion(t *testing.T) { - nonCanonical := []v1beta1.EnvVar{ + nonCanonical := []current.EnvVar{ {Key: "EV"}, {Key: "EV", Name: "EX"}, } @@ -48,7 +71,7 @@ func TestEnvConversion(t *testing.T) { // Test conversion the other way, too. for i := range canonical { - var got v1beta1.EnvVar + var got current.EnvVar err := Convert(&canonical[i], &got) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -65,15 +88,15 @@ func TestEnvConversion(t *testing.T) { func TestVolumeMountConversionToOld(t *testing.T) { table := []struct { in newer.VolumeMount - out v1beta1.VolumeMount + out current.VolumeMount }{ { in: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, - out: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/foo", ReadOnly: true}, + out: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/foo", ReadOnly: true}, }, } for _, item := range table { - got := v1beta1.VolumeMount{} + got := current.VolumeMount{} err := Convert(&item.in, &got) if err != nil { t.Errorf("Unexpected error: %v", err) @@ -87,17 +110,17 @@ func TestVolumeMountConversionToOld(t *testing.T) { func TestVolumeMountConversionToNew(t *testing.T) { table := []struct { - in v1beta1.VolumeMount + in current.VolumeMount out newer.VolumeMount }{ { - in: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, + in: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, }, { - in: v1beta1.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/bar", ReadOnly: true}, + in: current.VolumeMount{Name: "foo", MountPath: "/dev/foo", Path: "/dev/bar", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/foo", ReadOnly: true}, }, { - in: v1beta1.VolumeMount{Name: "foo", Path: "/dev/bar", ReadOnly: true}, + in: current.VolumeMount{Name: "foo", Path: "/dev/bar", ReadOnly: true}, out: newer.VolumeMount{Name: "foo", MountPath: "/dev/bar", ReadOnly: true}, }, } @@ -115,13 +138,13 @@ func TestVolumeMountConversionToNew(t *testing.T) { } func TestMinionListConversionToNew(t *testing.T) { - oldMinion := func(id string) v1beta1.Minion { - return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}} + oldMinion := func(id string) current.Minion { + return current.Minion{TypeMeta: current.TypeMeta{ID: id}} } newMinion := func(id string) newer.Minion { return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}} } - oldMinions := []v1beta1.Minion{ + oldMinions := []current.Minion{ oldMinion("foo"), oldMinion("bar"), } @@ -131,19 +154,19 @@ func TestMinionListConversionToNew(t *testing.T) { } table := []struct { - oldML *v1beta1.MinionList + oldML *current.MinionList newML *newer.MinionList }{ { - oldML: &v1beta1.MinionList{Items: oldMinions}, + oldML: ¤t.MinionList{Items: oldMinions}, newML: &newer.MinionList{Items: newMinions}, }, { - oldML: &v1beta1.MinionList{Minions: oldMinions}, + oldML: ¤t.MinionList{Minions: oldMinions}, newML: &newer.MinionList{Items: newMinions}, }, { - oldML: &v1beta1.MinionList{ + oldML: ¤t.MinionList{ Items: oldMinions, - Minions: []v1beta1.Minion{oldMinion("baz")}, + Minions: []current.Minion{oldMinion("baz")}, }, newML: &newer.MinionList{Items: newMinions}, }, @@ -162,13 +185,13 @@ func TestMinionListConversionToNew(t *testing.T) { } func TestMinionListConversionToOld(t *testing.T) { - oldMinion := func(id string) v1beta1.Minion { - return v1beta1.Minion{TypeMeta: v1beta1.TypeMeta{ID: id}} + oldMinion := func(id string) current.Minion { + return current.Minion{TypeMeta: current.TypeMeta{ID: id}} } newMinion := func(id string) newer.Minion { return newer.Minion{ObjectMeta: newer.ObjectMeta{Name: id}} } - oldMinions := []v1beta1.Minion{ + oldMinions := []current.Minion{ oldMinion("foo"), oldMinion("bar"), } @@ -178,12 +201,12 @@ func TestMinionListConversionToOld(t *testing.T) { } newML := &newer.MinionList{Items: newMinions} - oldML := &v1beta1.MinionList{ + oldML := ¤t.MinionList{ Items: oldMinions, Minions: oldMinions, } - got := &v1beta1.MinionList{} + got := ¤t.MinionList{} err := Convert(newML, got) if err != nil { t.Errorf("Unexpected error: %v", err) @@ -195,7 +218,7 @@ func TestMinionListConversionToOld(t *testing.T) { func TestServiceEmptySelector(t *testing.T) { // Nil map should be preserved - svc := &v1beta1.Service{Selector: nil} + svc := ¤t.Service{Selector: nil} data, err := newer.Scheme.EncodeToVersion(svc, "v1beta1") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -210,7 +233,7 @@ func TestServiceEmptySelector(t *testing.T) { } // Empty map should be preserved - svc2 := &v1beta1.Service{Selector: map[string]string{}} + svc2 := ¤t.Service{Selector: map[string]string{}} data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta1") if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/pkg/api/v1beta1/register.go b/pkg/api/v1beta1/register.go index 53fce718fb..75e6bfc848 100644 --- a/pkg/api/v1beta1/register.go +++ b/pkg/api/v1beta1/register.go @@ -51,6 +51,9 @@ func init() { &BoundPod{}, &BoundPods{}, ) + // Future names are supported + api.Scheme.AddKnownTypeWithName("v1beta1", "Node", &Minion{}) + api.Scheme.AddKnownTypeWithName("v1beta1", "NodeList", &MinionList{}) } func (*Pod) IsAnAPIObject() {} diff --git a/pkg/api/v1beta2/conversion_test.go b/pkg/api/v1beta2/conversion_test.go index 357435745d..c8bd7ac285 100644 --- a/pkg/api/v1beta2/conversion_test.go +++ b/pkg/api/v1beta2/conversion_test.go @@ -20,12 +20,12 @@ import ( "testing" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2" + current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" ) func TestServiceEmptySelector(t *testing.T) { // Nil map should be preserved - svc := &v1beta2.Service{Selector: nil} + svc := ¤t.Service{Selector: nil} data, err := newer.Scheme.EncodeToVersion(svc, "v1beta2") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -40,7 +40,7 @@ func TestServiceEmptySelector(t *testing.T) { } // Empty map should be preserved - svc2 := &v1beta2.Service{Selector: map[string]string{}} + svc2 := ¤t.Service{Selector: map[string]string{}} data, err = newer.Scheme.EncodeToVersion(svc2, "v1beta2") if err != nil { t.Fatalf("unexpected error: %v", err) @@ -54,3 +54,26 @@ func TestServiceEmptySelector(t *testing.T) { t.Errorf("unexpected selector: %#v", obj) } } + +func TestNodeConversion(t *testing.T) { + obj, err := current.Codec.Decode([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.Minion); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj, err = current.Codec.Decode([]byte(`{"kind":"NodeList","apiVersion":"v1beta2"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.MinionList); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj = &newer.Node{} + if err := current.Codec.DecodeInto([]byte(`{"kind":"Node","apiVersion":"v1beta2"}`), obj); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} diff --git a/pkg/api/v1beta2/register.go b/pkg/api/v1beta2/register.go index 60a2c9d5f6..4f70be9fb8 100644 --- a/pkg/api/v1beta2/register.go +++ b/pkg/api/v1beta2/register.go @@ -51,6 +51,9 @@ func init() { &BoundPod{}, &BoundPods{}, ) + // Future names are supported + api.Scheme.AddKnownTypeWithName("v1beta2", "Node", &Minion{}) + api.Scheme.AddKnownTypeWithName("v1beta2", "NodeList", &MinionList{}) } func (*Pod) IsAnAPIObject() {} diff --git a/pkg/api/v1beta3/conversion_test.go b/pkg/api/v1beta3/conversion_test.go new file mode 100644 index 0000000000..215e9f6cb2 --- /dev/null +++ b/pkg/api/v1beta3/conversion_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta3_test + +import ( + "testing" + + newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3" +) + +func TestNodeConversion(t *testing.T) { + obj, err := current.Codec.Decode([]byte(`{"kind":"Minion","apiVersion":"v1beta3"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.Minion); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj, err = current.Codec.Decode([]byte(`{"kind":"MinionList","apiVersion":"v1beta3"}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if _, ok := obj.(*newer.MinionList); !ok { + t.Errorf("unexpected type: %#v", obj) + } + + obj = &newer.Node{} + if err := current.Codec.DecodeInto([]byte(`{"kind":"Minion","apiVersion":"v1beta3"}`), obj); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} diff --git a/pkg/api/v1beta3/register.go b/pkg/api/v1beta3/register.go index db6c3957f0..6cfd094e5a 100644 --- a/pkg/api/v1beta3/register.go +++ b/pkg/api/v1beta3/register.go @@ -47,6 +47,9 @@ func init() { &Event{}, &EventList{}, ) + // Legacy names are supported + api.Scheme.AddKnownTypeWithName("v1beta3", "Minion", &Node{}) + api.Scheme.AddKnownTypeWithName("v1beta3", "MinionList", &NodeList{}) } func (*Pod) IsAnAPIObject() {}