Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 68b0572

Browse files
committed
internal versions
1 parent 6231404 commit 68b0572

File tree

17 files changed

+221
-107
lines changed

17 files changed

+221
-107
lines changed

pkg/api/conversion_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func BenchmarkPodConversion(b *testing.B) {
4141
if err != nil {
4242
b.Fatalf("Conversion error: %v", err)
4343
}
44-
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersion)
44+
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersions[testapi.Default.Group].String())
4545
if err != nil {
4646
b.Fatalf("Conversion error: %v", err)
4747
}
@@ -69,7 +69,7 @@ func BenchmarkNodeConversion(b *testing.B) {
6969
if err != nil {
7070
b.Fatalf("Conversion error: %v", err)
7171
}
72-
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersion)
72+
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersions[testapi.Default.Group].String())
7373
if err != nil {
7474
b.Fatalf("Conversion error: %v", err)
7575
}
@@ -97,7 +97,7 @@ func BenchmarkReplicationControllerConversion(b *testing.B) {
9797
if err != nil {
9898
b.Fatalf("Conversion error: %v", err)
9999
}
100-
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersion)
100+
obj, err := scheme.ConvertToVersion(versionedObj, scheme.InternalVersions[testapi.Default.Group].String())
101101
if err != nil {
102102
b.Fatalf("Conversion error: %v", err)
103103
}

pkg/api/unversioned/group_version.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ type GroupVersion struct {
4949
Version string
5050
}
5151

52+
func (gv GroupVersion) IsEmpty() bool {
53+
return len(gv.Group) == 0 && len(gv.Version) == 0
54+
}
55+
5256
// String puts "group" and "version" into a single "group/version" string. For the legacy v1
5357
// it returns "v1".
5458
func (gv GroupVersion) String() string {
5559
// special case the internal apiVersion for kube
56-
if len(gv.Group) == 0 && len(gv.Version) == 0 {
60+
if gv.IsEmpty() {
5761
return ""
5862
}
5963

pkg/apis/componentconfig/register.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func init() {
2525
}
2626

2727
func addKnownTypes() {
28-
api.Scheme.AddKnownTypes("",
28+
// TODO this will get cleaned up with the scheme types are fixed
29+
api.Scheme.AddKnownTypes("componentconfig/",
2930
&KubeProxyConfiguration{},
3031
)
3132
}

pkg/apis/extensions/register.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func init() {
2727

2828
// Adds the list of known types to api.Scheme.
2929
func addKnownTypes() {
30-
api.Scheme.AddKnownTypes("",
30+
// TODO this gets cleaned up when the types are fixed
31+
api.Scheme.AddKnownTypes("extensions/",
3132
&ClusterAutoscaler{},
3233
&ClusterAutoscalerList{},
3334
&Deployment{},

pkg/apis/metrics/register.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func init() {
2727

2828
// Adds the list of known types to api.Scheme.
2929
func addKnownTypes() {
30-
api.Scheme.AddKnownTypes("",
30+
// TODO this will get cleaned up with the scheme types are fixed
31+
api.Scheme.AddKnownTypes("metrics/",
3132
&RawNode{},
3233
&RawPod{},
3334
)

pkg/apiserver/api_installer.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,14 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
375375
// test/integration/auth_test.go is currently the most comprehensive status code test
376376

377377
reqScope := RequestScope{
378-
ContextFunc: ctxFn,
379-
Creater: a.group.Creater,
380-
Convertor: a.group.Convertor,
381-
Codec: mapping.Codec,
382-
APIVersion: a.group.GroupVersion.String(),
378+
ContextFunc: ctxFn,
379+
Creater: a.group.Creater,
380+
Convertor: a.group.Convertor,
381+
Codec: mapping.Codec,
382+
APIVersion: a.group.GroupVersion.String(),
383+
// TODO, this internal version needs to plumbed through from the caller
384+
// this works in all the cases we have now
385+
InternalVersion: unversioned.GroupVersion{Group: a.group.GroupVersion.Group},
383386
ServerAPIVersion: serverGroupVersion.String(),
384387
Resource: resource,
385388
Subresource: subresource,

pkg/apiserver/apiserver_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func convert(obj runtime.Object) (runtime.Object, error) {
5858

5959
// This creates fake API versions, similar to api/latest.go.
6060
var testAPIGroup = "test.group"
61+
var testInternalGroupVersion = unversioned.GroupVersion{Group: testAPIGroup, Version: ""}
6162
var testGroupVersion = unversioned.GroupVersion{Group: testAPIGroup, Version: "version"}
6263
var newGroupVersion = unversioned.GroupVersion{Group: testAPIGroup, Version: "version2"}
6364
var prefix = "apis"
@@ -136,8 +137,9 @@ func init() {
136137

137138
// "internal" version
138139
api.Scheme.AddKnownTypes(
139-
"", &apiservertesting.Simple{}, &apiservertesting.SimpleList{}, &unversioned.Status{},
140+
testInternalGroupVersion.String(), &apiservertesting.Simple{}, &apiservertesting.SimpleList{}, &unversioned.Status{},
140141
&unversioned.ListOptions{}, &apiservertesting.SimpleGetOptions{}, &apiservertesting.SimpleRoot{})
142+
api.Scheme.AddInternalGroupVersion(testInternalGroupVersion)
141143
addGrouplessTypes()
142144
addTestTypes()
143145
addNewTestTypes()
@@ -1630,7 +1632,7 @@ func TestConnectWithOptions(t *testing.T) {
16301632
}
16311633
opts, ok := connectStorage.receivedConnectOptions.(*apiservertesting.SimpleGetOptions)
16321634
if !ok {
1633-
t.Errorf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
1635+
t.Fatalf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
16341636
}
16351637
if opts.Param1 != "value1" && opts.Param2 != "value2" {
16361638
t.Errorf("Unexpected options value: %#v", opts)
@@ -1677,7 +1679,7 @@ func TestConnectWithOptionsAndPath(t *testing.T) {
16771679
}
16781680
opts, ok := connectStorage.receivedConnectOptions.(*apiservertesting.SimpleGetOptions)
16791681
if !ok {
1680-
t.Errorf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
1682+
t.Fatalf("Unexpected options type: %#v", connectStorage.receivedConnectOptions)
16811683
}
16821684
if opts.Param1 != "value1" && opts.Param2 != "value2" {
16831685
t.Errorf("Unexpected options value: %#v", opts)

pkg/apiserver/resthandler.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ type RequestScope struct {
7373
Creater runtime.ObjectCreater
7474
Convertor runtime.ObjectConvertor
7575

76-
Resource string
77-
Subresource string
78-
Kind string
79-
APIVersion string
76+
Resource string
77+
Subresource string
78+
Kind string
79+
APIVersion string
80+
InternalVersion unversioned.GroupVersion
8081

8182
// The version of apiserver resources to use
8283
ServerAPIVersion string
@@ -156,7 +157,7 @@ func getRequestOptions(req *restful.Request, scope RequestScope, kind string, su
156157
if err := scope.Codec.DecodeParametersInto(query, versioned); err != nil {
157158
return nil, errors.NewBadRequest(err.Error())
158159
}
159-
out, err := scope.Convertor.ConvertToVersion(versioned, "")
160+
out, err := scope.Convertor.ConvertToVersion(versioned, scope.InternalVersion.String())
160161
if err != nil {
161162
// programmer error
162163
return nil, err

pkg/conversion/decode.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,37 @@ import (
2626
"k8s.io/kubernetes/pkg/api/unversioned"
2727
)
2828

29-
func (s *Scheme) DecodeToVersionedObject(data []byte) (obj interface{}, version, kind string, err error) {
30-
version, kind, err = s.DataVersionAndKind(data)
29+
func (s *Scheme) DecodeToVersionedObject(data []byte) (interface{}, string, string, error) {
30+
version, kind, err := s.DataVersionAndKind(data)
3131
if err != nil {
32-
return
32+
return nil, "", "", err
33+
}
34+
35+
gv, err := unversioned.ParseGroupVersion(version)
36+
if err != nil {
37+
return nil, "", "", err
38+
}
39+
40+
internalGV, exists := s.InternalVersions[gv.Group]
41+
if !exists {
42+
return nil, "", "", fmt.Errorf("no internalVersion specified for %v", gv)
3343
}
34-
if version == "" && s.InternalVersion != "" {
44+
45+
if len(gv.Version) == 0 && len(internalGV.Version) != 0 {
3546
return nil, "", "", fmt.Errorf("version not set in '%s'", string(data))
3647
}
3748
if kind == "" {
3849
return nil, "", "", fmt.Errorf("kind not set in '%s'", string(data))
3950
}
40-
obj, err = s.NewObject(version, kind)
51+
obj, err := s.NewObject(version, kind)
4152
if err != nil {
4253
return nil, "", "", err
4354
}
4455

4556
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(obj); err != nil {
4657
return nil, "", "", err
4758
}
48-
return
59+
return obj, version, kind, nil
4960
}
5061

5162
// Decode converts a JSON string back into a pointer to an api object.
@@ -54,15 +65,16 @@ func (s *Scheme) DecodeToVersionedObject(data []byte) (obj interface{}, version,
5465
// s.InternalVersion type before being returned. Decode will not decode
5566
// objects without version set unless InternalVersion is also "".
5667
func (s *Scheme) Decode(data []byte) (interface{}, error) {
57-
// TODO this is cleaned up when internal types are fixed
58-
return s.DecodeToVersion(data, unversioned.ParseGroupVersionOrDie(s.InternalVersion))
68+
return s.DecodeToVersion(data, unversioned.GroupVersion{})
5969
}
6070

6171
// DecodeToVersion converts a JSON string back into a pointer to an api object.
6272
// Deduces the type based upon the fields added by the MetaInsertionFactory
6373
// technique. The object will be converted, if necessary, into the versioned
6474
// type before being returned. Decode will not decode objects without version
6575
// set unless version is also "".
76+
// a GroupVersion with .IsEmpty() == true is means "use the internal version for
77+
// the object's group"
6678
func (s *Scheme) DecodeToVersion(data []byte, gv unversioned.GroupVersion) (interface{}, error) {
6779
obj, sourceVersion, kind, err := s.DecodeToVersionedObject(data)
6880
if err != nil {
@@ -73,8 +85,23 @@ func (s *Scheme) DecodeToVersion(data []byte, gv unversioned.GroupVersion) (inte
7385
return nil, err
7486
}
7587

88+
sourceGV, err := unversioned.ParseGroupVersion(sourceVersion)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
// if the gv is empty, then we want the internal version, but the internal version varies by
94+
// group. We can lookup the group now because we have knowledge of the group
95+
if gv.IsEmpty() {
96+
exists := false
97+
gv, exists = s.InternalVersions[sourceGV.Group]
98+
if !exists {
99+
return nil, fmt.Errorf("no internalVersion specified for %v", gv)
100+
}
101+
}
102+
76103
// Convert if needed.
77-
if gv.String() != sourceVersion {
104+
if gv != sourceGV {
78105
objOut, err := s.NewObject(gv.String(), kind)
79106
if err != nil {
80107
return nil, err
@@ -118,7 +145,7 @@ func (s *Scheme) DecodeIntoWithSpecifiedVersionKind(data []byte, obj interface{}
118145
if dataKind == "" {
119146
dataKind = gvk.Kind
120147
}
121-
if (len(gvk.GroupVersion().Group) > 0 || len(gvk.GroupVersion().Version) > 0) && (dataVersion != gvk.GroupVersion().String()) {
148+
if (len(gvk.Group) > 0 || len(gvk.Version) > 0) && (dataVersion != gvk.GroupVersion().String()) {
122149
return errors.New(fmt.Sprintf("The apiVersion in the data (%s) does not match the specified apiVersion(%v)", dataVersion, gvk.GroupVersion()))
123150
}
124151
if len(gvk.Kind) > 0 && (dataKind != gvk.Kind) {

pkg/conversion/error.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ package conversion
1919
import (
2020
"fmt"
2121
"reflect"
22+
23+
"k8s.io/kubernetes/pkg/api/unversioned"
2224
)
2325

2426
type notRegisteredErr struct {
25-
kind string
26-
version string
27-
t reflect.Type
27+
gvk unversioned.GroupVersionKind
28+
t reflect.Type
2829
}
2930

3031
func (k *notRegisteredErr) Error() string {
3132
if k.t != nil {
3233
return fmt.Sprintf("no kind is registered for the type %v", k.t)
3334
}
34-
if len(k.kind) == 0 {
35-
return fmt.Sprintf("no version %q has been registered", k.version)
35+
if len(k.gvk.Kind) == 0 {
36+
return fmt.Sprintf("no version %q has been registered", k.gvk.GroupVersion())
3637
}
37-
if len(k.version) == 0 {
38-
return fmt.Sprintf("no kind %q is registered for the default version", k.kind)
38+
if len(k.gvk.Version) == 0 {
39+
return fmt.Sprintf("no kind %q is registered for the default version of group %q", k.gvk.Kind, k.gvk.Group)
3940
}
40-
return fmt.Sprintf("no kind %q is registered for version %q", k.kind, k.version)
41+
42+
return fmt.Sprintf("no kind %q is registered for version %q", k.gvk.Kind, k.gvk.GroupVersion())
4143
}
4244

4345
// IsNotRegisteredError returns true if the error indicates the provided

0 commit comments

Comments
 (0)