Merge pull request #57867 from CaoShuFeng/patch_trace

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

trace patch operations

Just like `update`, `create`, `get` and `delete` operations.

**Release note**:
```release-note
NONE
```

Kubernetes-commit: a5e33195e4418eac8fde3a9db33f4fbf0f73d87f
This commit is contained in:
Kubernetes Publisher 2018-01-22 23:53:48 -08:00
commit d48bded43d
3 changed files with 15 additions and 2 deletions

View File

@ -32,6 +32,7 @@ go_test(
"//vendor/k8s.io/apiserver/pkg/apis/example/v1:go_default_library",
"//vendor/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
"//vendor/k8s.io/apiserver/pkg/registry/rest:go_default_library",
"//vendor/k8s.io/apiserver/pkg/util/trace:go_default_library",
],
)

View File

@ -39,12 +39,17 @@ import (
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
utiltrace "k8s.io/apiserver/pkg/util/trace"
)
// PatchResource returns a function that will handle a resource patch
// TODO: Eventually PatchResource should just use GuaranteedUpdate and this routine should be a bit cleaner
func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface, converter runtime.ObjectConvertor, patchTypes []string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
// For performance tracking purposes.
trace := utiltrace.New("Patch " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)
// Do this first, otherwise name extraction can fail for unrecognized content types
// TODO: handle this in negotiation
contentType := req.Header.Get("Content-Type")
@ -88,6 +93,7 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
ae := request.AuditEventFrom(ctx)
audit.LogRequestPatch(ae, patchJS)
trace.Step("Recorded the audit event")
s, ok := runtime.SerializerInfoForMediaType(scope.Serializer.SupportedMediaTypes(), runtime.ContentTypeJSON)
if !ok {
@ -119,11 +125,12 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
name,
patchType,
patchJS,
scope.Namer, scope.Creater, scope.Defaulter, scope.UnsafeConvertor, scope.Kind, scope.Resource, codec)
scope.Namer, scope.Creater, scope.Defaulter, scope.UnsafeConvertor, scope.Kind, scope.Resource, codec, trace)
if err != nil {
scope.err(err, w, req)
return
}
trace.Step("Object stored in database")
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
@ -134,6 +141,7 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
scope.err(err, w, req)
return
}
trace.Step("Self-link added")
transformResponseObject(ctx, scope, req, w, http.StatusOK, result)
}
@ -160,6 +168,7 @@ func patchResource(
kind schema.GroupVersionKind,
resource schema.GroupVersionResource,
codec runtime.Codec,
trace *utiltrace.Trace,
) (runtime.Object, error) {
namespace := request.NamespaceValue(ctx)
@ -177,6 +186,7 @@ func patchResource(
// and is given the currently persisted object as input.
applyPatch := func(_ request.Context, _, currentObject runtime.Object) (runtime.Object, error) {
// Make sure we actually have a persisted currentObject
trace.Step("About to apply patch")
if hasUID, err := hasUID(currentObject); err != nil {
return nil, err
} else if !hasUID {
@ -360,6 +370,7 @@ func patchResource(
// applyAdmission is called every time GuaranteedUpdate asks for the updated object,
// and is given the currently persisted object and the patched object as input.
applyAdmission := func(ctx request.Context, patchedObject runtime.Object, currentObject runtime.Object) (runtime.Object, error) {
trace.Step("About to check admission control")
return patchedObject, updateMutation(patchedObject, currentObject)
}
updatedObjectInfo := rest.DefaultUpdatedObjectInfo(nil, applyPatch, applyAdmission)

View File

@ -40,6 +40,7 @@ import (
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
utiltrace "k8s.io/apiserver/pkg/util/trace"
)
var (
@ -347,7 +348,7 @@ func (tc *patchTestCase) Run(t *testing.T) {
name,
patchType,
patch,
namer, creater, defaulter, convertor, kind, resource, codec)
namer, creater, defaulter, convertor, kind, resource, codec, utiltrace.New("Patch"+name))
if len(tc.expectedError) != 0 {
if err == nil || err.Error() != tc.expectedError {
t.Errorf("%s: expected error %v, but got %v", tc.name, tc.expectedError, err)