mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 18:54:06 +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
|
||||
// the client's desired form, as well as ensuring any API level fields like self-link
|
||||
// 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 mediaType.Convert != nil {
|
||||
if target != nil {
|
||||
// Non-nil mediaType.Convert means that some conversion of the object
|
||||
// has to happen. Currently conversion may potentially modify the
|
||||
// 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
|
||||
// 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.
|
||||
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 {
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
switch target := mediaType.Convert; {
|
||||
switch {
|
||||
case target == nil:
|
||||
return obj, nil
|
||||
|
||||
@ -140,7 +140,7 @@ func transformResponseObject(ctx context.Context, scope *RequestScope, req *http
|
||||
|
||||
var obj runtime.Object
|
||||
do := func() {
|
||||
obj, err = transformObject(ctx, result, options, mediaType, scope, req)
|
||||
obj, err = transformObject(ctx, result, options, mediaType.Convert, scope)
|
||||
}
|
||||
endpointsrequest.TrackTransformResponseObjectLatency(ctx, do)
|
||||
|
||||
|
@ -30,7 +30,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
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/registry/rest"
|
||||
)
|
||||
@ -107,10 +106,10 @@ func TestCacheableObject(t *testing.T) {
|
||||
tableConvertor := rest.NewDefaultTableConvertor(examplev1.Resource("Pod"))
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
object runtime.Object
|
||||
opts *metav1beta1.TableOptions
|
||||
mediaType negotiation.MediaTypeOptions
|
||||
desc string
|
||||
object runtime.Object
|
||||
opts *metav1beta1.TableOptions
|
||||
target *schema.GroupVersionKind
|
||||
|
||||
expectedUnwrap bool
|
||||
expectedObj runtime.Object
|
||||
@ -125,14 +124,14 @@ func TestCacheableObject(t *testing.T) {
|
||||
{
|
||||
desc: "cacheableObject nil convert",
|
||||
object: &mockCacheableObject{obj: pod},
|
||||
mediaType: negotiation.MediaTypeOptions{},
|
||||
target: nil,
|
||||
expectedObj: &mockCacheableObject{obj: pod},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
desc: "cacheableObject as PartialObjectMeta",
|
||||
object: &mockCacheableObject{obj: pod},
|
||||
mediaType: negotiation.MediaTypeOptions{Convert: &pomGVK},
|
||||
target: &pomGVK,
|
||||
expectedObj: podMeta,
|
||||
expectedErr: nil,
|
||||
},
|
||||
@ -140,7 +139,7 @@ func TestCacheableObject(t *testing.T) {
|
||||
desc: "cacheableObject as Table",
|
||||
object: &mockCacheableObject{obj: pod},
|
||||
opts: &metav1beta1.TableOptions{NoHeaders: true, IncludeObject: metav1.IncludeNone},
|
||||
mediaType: negotiation.MediaTypeOptions{Convert: &tableGVK},
|
||||
target: &tableGVK,
|
||||
expectedObj: podTable,
|
||||
expectedErr: nil,
|
||||
},
|
||||
@ -150,12 +149,12 @@ func TestCacheableObject(t *testing.T) {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
result, err := transformObject(
|
||||
request.WithRequestInfo(context.TODO(), &request.RequestInfo{}),
|
||||
test.object, test.opts, test.mediaType,
|
||||
test.object, test.opts, test.target,
|
||||
&RequestScope{
|
||||
Namer: &mockNamer{},
|
||||
TableConvertor: tableConvertor,
|
||||
},
|
||||
nil)
|
||||
)
|
||||
|
||||
if 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,
|
||||
|
||||
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 {
|
||||
utilruntime.HandleError(fmt.Errorf("failed to transform object %v: %v", reflect.TypeOf(obj), err))
|
||||
return obj
|
||||
|
Loading…
Reference in New Issue
Block a user