mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 20:17:41 +00:00
Minor cleanup tranformers interface
This commit is contained in:
parent
172a41192c
commit
3fcc045bce
@ -37,9 +37,9 @@ import (
|
|||||||
// transformObject takes the object as returned by storage and ensures it is in
|
// transformObject takes the object as returned by storage and ensures it is in
|
||||||
// the client's desired form, as well as ensuring any API level fields like self-link
|
// the client's desired form, as well as ensuring any API level fields like self-link
|
||||||
// are properly set.
|
// are properly set.
|
||||||
func transformObject(ctx context.Context, obj runtime.Object, opts interface{}, mediaType negotiation.MediaTypeOptions, scope *RequestScope, req *http.Request) (runtime.Object, error) {
|
func transformObject(ctx context.Context, obj runtime.Object, opts interface{}, target *schema.GroupVersionKind, scope *RequestScope) (runtime.Object, error) {
|
||||||
if co, ok := obj.(runtime.CacheableObject); ok {
|
if co, ok := obj.(runtime.CacheableObject); ok {
|
||||||
if mediaType.Convert != nil {
|
if target != nil {
|
||||||
// Non-nil mediaType.Convert means that some conversion of the object
|
// Non-nil mediaType.Convert means that some conversion of the object
|
||||||
// has to happen. Currently conversion may potentially modify the
|
// has to happen. Currently conversion may potentially modify the
|
||||||
// object or assume something about it (e.g. asTable operates on
|
// object or assume something about it (e.g. asTable operates on
|
||||||
@ -49,19 +49,19 @@ func transformObject(ctx context.Context, obj runtime.Object, opts interface{},
|
|||||||
//
|
//
|
||||||
// TODO: Long-term, transformObject should be changed so that it
|
// TODO: Long-term, transformObject should be changed so that it
|
||||||
// implements runtime.Encoder interface.
|
// implements runtime.Encoder interface.
|
||||||
return doTransformObject(ctx, co.GetObject(), opts, mediaType, scope, req)
|
return doTransformObject(ctx, co.GetObject(), opts, target, scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return doTransformObject(ctx, obj, opts, mediaType, scope, req)
|
return doTransformObject(ctx, obj, opts, target, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
// doTransformResponseObject is used for handling all requests, including watch.
|
// doTransformResponseObject is used for handling all requests, including watch.
|
||||||
func doTransformObject(ctx context.Context, obj runtime.Object, opts interface{}, mediaType negotiation.MediaTypeOptions, scope *RequestScope, req *http.Request) (runtime.Object, error) {
|
func doTransformObject(ctx context.Context, obj runtime.Object, opts interface{}, target *schema.GroupVersionKind, scope *RequestScope) (runtime.Object, error) {
|
||||||
if _, ok := obj.(*metav1.Status); ok {
|
if _, ok := obj.(*metav1.Status); ok {
|
||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch target := mediaType.Convert; {
|
switch {
|
||||||
case target == nil:
|
case target == nil:
|
||||||
return obj, nil
|
return obj, nil
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ func transformResponseObject(ctx context.Context, scope *RequestScope, req *http
|
|||||||
|
|
||||||
var obj runtime.Object
|
var obj runtime.Object
|
||||||
do := func() {
|
do := func() {
|
||||||
obj, err = transformObject(ctx, result, options, mediaType, scope, req)
|
obj, err = transformObject(ctx, result, options, mediaType.Convert, scope)
|
||||||
}
|
}
|
||||||
endpointsrequest.TrackTransformResponseObjectLatency(ctx, do)
|
endpointsrequest.TrackTransformResponseObjectLatency(ctx, do)
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
||||||
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
|
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
)
|
)
|
||||||
@ -107,10 +106,10 @@ func TestCacheableObject(t *testing.T) {
|
|||||||
tableConvertor := rest.NewDefaultTableConvertor(examplev1.Resource("Pod"))
|
tableConvertor := rest.NewDefaultTableConvertor(examplev1.Resource("Pod"))
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
object runtime.Object
|
object runtime.Object
|
||||||
opts *metav1beta1.TableOptions
|
opts *metav1beta1.TableOptions
|
||||||
mediaType negotiation.MediaTypeOptions
|
target *schema.GroupVersionKind
|
||||||
|
|
||||||
expectedUnwrap bool
|
expectedUnwrap bool
|
||||||
expectedObj runtime.Object
|
expectedObj runtime.Object
|
||||||
@ -125,14 +124,14 @@ func TestCacheableObject(t *testing.T) {
|
|||||||
{
|
{
|
||||||
desc: "cacheableObject nil convert",
|
desc: "cacheableObject nil convert",
|
||||||
object: &mockCacheableObject{obj: pod},
|
object: &mockCacheableObject{obj: pod},
|
||||||
mediaType: negotiation.MediaTypeOptions{},
|
target: nil,
|
||||||
expectedObj: &mockCacheableObject{obj: pod},
|
expectedObj: &mockCacheableObject{obj: pod},
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "cacheableObject as PartialObjectMeta",
|
desc: "cacheableObject as PartialObjectMeta",
|
||||||
object: &mockCacheableObject{obj: pod},
|
object: &mockCacheableObject{obj: pod},
|
||||||
mediaType: negotiation.MediaTypeOptions{Convert: &pomGVK},
|
target: &pomGVK,
|
||||||
expectedObj: podMeta,
|
expectedObj: podMeta,
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
},
|
},
|
||||||
@ -140,7 +139,7 @@ func TestCacheableObject(t *testing.T) {
|
|||||||
desc: "cacheableObject as Table",
|
desc: "cacheableObject as Table",
|
||||||
object: &mockCacheableObject{obj: pod},
|
object: &mockCacheableObject{obj: pod},
|
||||||
opts: &metav1beta1.TableOptions{NoHeaders: true, IncludeObject: metav1.IncludeNone},
|
opts: &metav1beta1.TableOptions{NoHeaders: true, IncludeObject: metav1.IncludeNone},
|
||||||
mediaType: negotiation.MediaTypeOptions{Convert: &tableGVK},
|
target: &tableGVK,
|
||||||
expectedObj: podTable,
|
expectedObj: podTable,
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
},
|
},
|
||||||
@ -150,12 +149,12 @@ func TestCacheableObject(t *testing.T) {
|
|||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
result, err := transformObject(
|
result, err := transformObject(
|
||||||
request.WithRequestInfo(context.TODO(), &request.RequestInfo{}),
|
request.WithRequestInfo(context.TODO(), &request.RequestInfo{}),
|
||||||
test.object, test.opts, test.mediaType,
|
test.object, test.opts, test.target,
|
||||||
&RequestScope{
|
&RequestScope{
|
||||||
Namer: &mockNamer{},
|
Namer: &mockNamer{},
|
||||||
TableConvertor: tableConvertor,
|
TableConvertor: tableConvertor,
|
||||||
},
|
},
|
||||||
nil)
|
)
|
||||||
|
|
||||||
if err != test.expectedErr {
|
if err != test.expectedErr {
|
||||||
t.Errorf("unexpected error: %v, expected: %v", err, test.expectedErr)
|
t.Errorf("unexpected error: %v, expected: %v", err, test.expectedErr)
|
||||||
|
@ -143,7 +143,7 @@ func serveWatch(watcher watch.Interface, scope *RequestScope, mediaTypeOptions n
|
|||||||
EmbeddedEncoder: embeddedEncoder,
|
EmbeddedEncoder: embeddedEncoder,
|
||||||
|
|
||||||
Fixup: func(obj runtime.Object) runtime.Object {
|
Fixup: func(obj runtime.Object) runtime.Object {
|
||||||
result, err := transformObject(ctx, obj, options, mediaTypeOptions, scope, req)
|
result, err := transformObject(ctx, obj, options, mediaTypeOptions.Convert, scope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utilruntime.HandleError(fmt.Errorf("failed to transform object %v: %v", reflect.TypeOf(obj), err))
|
utilruntime.HandleError(fmt.Errorf("failed to transform object %v: %v", reflect.TypeOf(obj), err))
|
||||||
return obj
|
return obj
|
||||||
|
Loading…
Reference in New Issue
Block a user