Skip to content

Commit

Permalink
update CodecFor for GroupVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Dec 15, 2015
1 parent 7de177b commit 6e33403
Show file tree
Hide file tree
Showing 25 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
groupMeta := latest.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion.String()),
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion),
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

var SchemeGroupVersion = unversioned.GroupVersion{Group: "testgroup", Version: "v1"}

var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
// Register the API.
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// Codec is the identity codec for this package - it can only convert itself
// to itself.
var Codec = runtime.CodecFor(Scheme, "")
var Codec = runtime.CodecFor(Scheme, unversioned.GroupVersion{})

func init() {
Scheme.AddDefaultingFuncs(
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {
groupMeta := latest.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion.String()),
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion),
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/testing/compat/compatibility_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/validation/field"
Expand All @@ -39,7 +40,7 @@ import (
// keys in the resulting JSON.
func TestCompatibility(
t *testing.T,
version string,
version unversioned.GroupVersion,
input []byte,
validator func(obj runtime.Object) field.ErrorList,
expectedKeys map[string]string,
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/v1/backward_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testing/compat"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/validation/field"
Expand Down Expand Up @@ -223,6 +224,6 @@ func TestCompatibility_v1_PodSecurityContext(t *testing.T) {

for _, tc := range cases {
t.Logf("Testing 1.0.0 backward compatibility for %v", tc.name)
compat.TestCompatibility(t, "v1", []byte(tc.input), validator, tc.expectedKeys, tc.absentKeys)
compat.TestCompatibility(t, v1.SchemeGroupVersion, []byte(tc.input), validator, tc.expectedKeys, tc.absentKeys)
}
}
2 changes: 1 addition & 1 deletion pkg/api/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const GroupName = ""
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1"}

// Codec encodes internal objects to the v1 scheme
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
// Check if v1 is in the list of supported API versions.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/abac/v0/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
var GroupVersion = unversioned.GroupVersion{Group: api.Group, Version: "v0"}

// Codec encodes internal objects to the v0 version for the abac group
var Codec = runtime.CodecFor(api.Scheme, GroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, GroupVersion)

func init() {
api.Scheme.AddKnownTypes(GroupVersion,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/abac/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
var GroupVersion = unversioned.GroupVersion{Group: api.Group, Version: "v1beta1"}

// Codec encodes internal objects to the v1beta1 version for the abac group
var Codec = runtime.CodecFor(api.Scheme, GroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, GroupVersion)

func init() {
api.Scheme.AddKnownTypes(GroupVersion,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/componentconfig/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {
groupMeta := latest.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion.String()),
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion),
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/componentconfig/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const GroupName = "componentconfig"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1alpha1"}

var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
addKnownTypes()
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/extensions/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {
groupMeta := latest.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion.String()),
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion),
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/extensions/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const GroupName = "extensions"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1beta1"}

var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
addKnownTypes()
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/metrics/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {
groupMeta := latest.GroupMeta{
GroupVersion: preferredExternalVersion,
GroupVersions: externalVersions,
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion.String()),
Codec: runtime.CodecFor(api.Scheme, preferredExternalVersion),
RESTMapper: newRESTMapper(externalVersions),
SelfLinker: runtime.SelfLinker(accessor),
InterfacesFor: interfacesFor,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/metrics/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const GroupName = "metrics"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1alpha1"}

var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
// Register the API.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func serviceErrorHandler(requestResolver *RequestInfoResolver, apiVersions []str
for _, version := range apiVersions {
if requestInfo.APIVersion == version {
// valid api version.
codec = runtime.CodecFor(api.Scheme, requestInfo.APIVersion)
codec = runtime.CodecFor(api.Scheme, unversioned.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion})
break
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ var prefix = "apis"

var grouplessGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"}
var grouplessPrefix = "api"
var grouplessCodec = runtime.CodecFor(api.Scheme, grouplessGroupVersion.String())
var grouplessCodec = runtime.CodecFor(api.Scheme, grouplessGroupVersion)

var groupVersions = []unversioned.GroupVersion{grouplessGroupVersion, testGroupVersion, newGroupVersion}
var codec = runtime.CodecFor(api.Scheme, testGroupVersion.String())
var newCodec = runtime.CodecFor(api.Scheme, newGroupVersion.String())
var codec = runtime.CodecFor(api.Scheme, testGroupVersion)
var newCodec = runtime.CodecFor(api.Scheme, newGroupVersion)

var accessor = meta.NewAccessor()
var versioner runtime.ResourceVersioner = accessor
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/clientcmd/api/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"}

// Codec encodes internal objects to the v1 scheme
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
api.Scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
6 changes: 3 additions & 3 deletions pkg/conversion/unversioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
"k8s.io/kubernetes/pkg/runtime"
)

Expand Down Expand Up @@ -66,9 +67,8 @@ func TestV1EncodeDecodeStatus(t *testing.T) {
func TestExperimentalEncodeDecodeStatus(t *testing.T) {
// TODO: caesarxuchao: use the testapi.Extensions.Codec() once the PR that
// moves experimental from v1 to v1beta1 got merged.
// expCodec := testapi.Extensions.Codec()
expCodec := runtime.CodecFor(api.Scheme, "extensions/v1beta1")
encoded, err := runtime.Encode(expCodec, status)
expCodec := runtime.CodecFor(api.Scheme, v1beta1.SchemeGroupVersion)
encoded, err := expCodec.Encode(status)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
scheme.AddKnownTypeWithName(validVersionGV.WithKind("Type"), &ExternalType2{})

codec := runtime.CodecFor(scheme, unlikelyGV.String())
codec := runtime.CodecFor(scheme, unlikelyGV)
mapper := meta.NewDefaultRESTMapper([]unversioned.GroupVersion{unlikelyGV, validVersionGV}, func(version unversioned.GroupVersion) (*meta.VersionInterfaces, error) {
return &meta.VersionInterfaces{
Codec: runtime.CodecFor(scheme, version.String()),
Codec: runtime.CodecFor(scheme, version),
ObjectConvertor: scheme,
MetadataAccessor: meta.NewAccessor(),
}, versionErrIfFalse(version == validVersionGV || version == unlikelyGV)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestGetUnknownSchemaObjectListGeneric(t *testing.T) {
},
}
for k, test := range testCases {
apiCodec := runtime.CodecFor(api.Scheme, testapi.Default.GroupVersion().String())
apiCodec := runtime.CodecFor(api.Scheme, *testapi.Default.GroupVersion())
regularClient := &fake.RESTClient{
Codec: apiCodec,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/runtime/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package runtime
import (
"io"

"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/util/yaml"
)

Expand All @@ -41,7 +42,7 @@ func DecodeInto(d Decoder, data []byte, into Object) error {
}

// CodecFor returns a Codec that invokes Encode with the provided version.
func CodecFor(codec ObjectCodec, version string) Codec {
func CodecFor(codec ObjectCodec, version unversioned.GroupVersion) Codec {
return &codecWrapper{codec, version}
}

Expand Down Expand Up @@ -91,19 +92,19 @@ func EncodeOrDie(codec Codec, obj Object) string {
// default version for a scheme.
type codecWrapper struct {
ObjectCodec
version string
version unversioned.GroupVersion
}

// codecWrapper implements Decoder
var _ Decoder = &codecWrapper{}

// Encode implements Codec
func (c *codecWrapper) Encode(obj Object) ([]byte, error) {
return c.EncodeToVersion(obj, c.version)
return c.EncodeToVersion(obj, c.version.String())
}

func (c *codecWrapper) EncodeToStream(obj Object, stream io.Writer) error {
return c.EncodeToVersionStream(obj, c.version, stream)
return c.EncodeToVersionStream(obj, c.version.String(), stream)
}

// TODO: Make this behaviour default when we move everyone away from
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestEncode(t *testing.T) {
scheme.AddInternalGroupVersion(internalGV)
scheme.AddKnownTypeWithName(internalGV.WithKind("Simple"), &InternalSimple{})
scheme.AddKnownTypeWithName(externalGV.WithKind("Simple"), &ExternalSimple{})
codec := runtime.CodecFor(scheme, externalGV.String())
codec := runtime.CodecFor(scheme, externalGV)
test := &InternalSimple{
TestString: "I'm the same",
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/etcd/etcd_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
scheme = runtime.NewScheme()
scheme.AddKnownTypes(testapi.Default.InternalGroupVersion(), &storagetesting.TestResource{})
scheme.AddKnownTypes(*testapi.Default.GroupVersion(), &storagetesting.TestResource{})
codec = runtime.CodecFor(scheme, testapi.Default.GroupVersion().String())
codec = runtime.CodecFor(scheme, *testapi.Default.GroupVersion())
scheme.AddConversionFuncs(
func(in *storagetesting.TestResource, out *storagetesting.TestResource, s conversion.Scope) error {
*out = *in
Expand Down
2 changes: 1 addition & 1 deletion plugin/pkg/scheduler/api/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"}

// Codec encodes internal objects to the v1 scheme
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion.String())
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)

func init() {
api.Scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down

0 comments on commit 6e33403

Please sign in to comment.