mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
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 ```
This commit is contained in:
commit
a5e33195e4
@ -32,6 +32,7 @@ go_test(
|
|||||||
"//vendor/k8s.io/apiserver/pkg/apis/example/v1:go_default_library",
|
"//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/endpoints/request:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/registry/rest:go_default_library",
|
"//vendor/k8s.io/apiserver/pkg/registry/rest:go_default_library",
|
||||||
|
"//vendor/k8s.io/apiserver/pkg/util/trace:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -39,12 +39,17 @@ import (
|
|||||||
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
|
"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"
|
||||||
|
utiltrace "k8s.io/apiserver/pkg/util/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PatchResource returns a function that will handle a resource patch
|
// 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
|
// 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 {
|
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) {
|
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
|
// Do this first, otherwise name extraction can fail for unrecognized content types
|
||||||
// TODO: handle this in negotiation
|
// TODO: handle this in negotiation
|
||||||
contentType := req.Header.Get("Content-Type")
|
contentType := req.Header.Get("Content-Type")
|
||||||
@ -88,6 +93,7 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
|
|||||||
|
|
||||||
ae := request.AuditEventFrom(ctx)
|
ae := request.AuditEventFrom(ctx)
|
||||||
audit.LogRequestPatch(ae, patchJS)
|
audit.LogRequestPatch(ae, patchJS)
|
||||||
|
trace.Step("Recorded the audit event")
|
||||||
|
|
||||||
s, ok := runtime.SerializerInfoForMediaType(scope.Serializer.SupportedMediaTypes(), runtime.ContentTypeJSON)
|
s, ok := runtime.SerializerInfoForMediaType(scope.Serializer.SupportedMediaTypes(), runtime.ContentTypeJSON)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -119,11 +125,12 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
|
|||||||
name,
|
name,
|
||||||
patchType,
|
patchType,
|
||||||
patchJS,
|
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 {
|
if err != nil {
|
||||||
scope.err(err, w, req)
|
scope.err(err, w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
trace.Step("Object stored in database")
|
||||||
|
|
||||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -134,6 +141,7 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
|
|||||||
scope.err(err, w, req)
|
scope.err(err, w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
trace.Step("Self-link added")
|
||||||
|
|
||||||
transformResponseObject(ctx, scope, req, w, http.StatusOK, result)
|
transformResponseObject(ctx, scope, req, w, http.StatusOK, result)
|
||||||
}
|
}
|
||||||
@ -160,6 +168,7 @@ func patchResource(
|
|||||||
kind schema.GroupVersionKind,
|
kind schema.GroupVersionKind,
|
||||||
resource schema.GroupVersionResource,
|
resource schema.GroupVersionResource,
|
||||||
codec runtime.Codec,
|
codec runtime.Codec,
|
||||||
|
trace *utiltrace.Trace,
|
||||||
) (runtime.Object, error) {
|
) (runtime.Object, error) {
|
||||||
|
|
||||||
namespace := request.NamespaceValue(ctx)
|
namespace := request.NamespaceValue(ctx)
|
||||||
@ -177,6 +186,7 @@ func patchResource(
|
|||||||
// and is given the currently persisted object as input.
|
// and is given the currently persisted object as input.
|
||||||
applyPatch := func(_ request.Context, _, currentObject runtime.Object) (runtime.Object, error) {
|
applyPatch := func(_ request.Context, _, currentObject runtime.Object) (runtime.Object, error) {
|
||||||
// Make sure we actually have a persisted currentObject
|
// Make sure we actually have a persisted currentObject
|
||||||
|
trace.Step("About to apply patch")
|
||||||
if hasUID, err := hasUID(currentObject); err != nil {
|
if hasUID, err := hasUID(currentObject); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !hasUID {
|
} else if !hasUID {
|
||||||
@ -360,6 +370,7 @@ func patchResource(
|
|||||||
// applyAdmission is called every time GuaranteedUpdate asks for the updated object,
|
// applyAdmission is called every time GuaranteedUpdate asks for the updated object,
|
||||||
// and is given the currently persisted object and the patched object as input.
|
// 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) {
|
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)
|
return patchedObject, updateMutation(patchedObject, currentObject)
|
||||||
}
|
}
|
||||||
updatedObjectInfo := rest.DefaultUpdatedObjectInfo(nil, applyPatch, applyAdmission)
|
updatedObjectInfo := rest.DefaultUpdatedObjectInfo(nil, applyPatch, applyAdmission)
|
||||||
|
@ -40,6 +40,7 @@ import (
|
|||||||
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
|
utiltrace "k8s.io/apiserver/pkg/util/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -347,7 +348,7 @@ func (tc *patchTestCase) Run(t *testing.T) {
|
|||||||
name,
|
name,
|
||||||
patchType,
|
patchType,
|
||||||
patch,
|
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 len(tc.expectedError) != 0 {
|
||||||
if err == nil || err.Error() != tc.expectedError {
|
if err == nil || err.Error() != tc.expectedError {
|
||||||
t.Errorf("%s: expected error %v, but got %v", tc.name, tc.expectedError, err)
|
t.Errorf("%s: expected error %v, but got %v", tc.name, tc.expectedError, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user