Use objGV instead of gvk.GroupVersion

This commit is contained in:
njuptlzf 2021-06-02 14:27:30 +08:00
parent 58d7bf67d4
commit 15c4d579f0
3 changed files with 9 additions and 8 deletions

View File

@ -111,7 +111,7 @@ func LogImpersonatedUser(ae *auditinternal.Event, user user.Info) {
// LogRequestObject fills in the request object into an audit event. The passed runtime.Object // LogRequestObject fills in the request object into an audit event. The passed runtime.Object
// will be converted to the given gv. // will be converted to the given gv.
func LogRequestObject(ae *auditinternal.Event, obj runtime.Object, objectGV schema.GroupVersion, gvr schema.GroupVersionResource, subresource string, s runtime.NegotiatedSerializer) { func LogRequestObject(ae *auditinternal.Event, obj runtime.Object, objGV schema.GroupVersion, gvr schema.GroupVersionResource, subresource string, s runtime.NegotiatedSerializer) {
if ae == nil || ae.Level.Less(auditinternal.LevelMetadata) { if ae == nil || ae.Level.Less(auditinternal.LevelMetadata) {
return return
} }
@ -153,7 +153,7 @@ func LogRequestObject(ae *auditinternal.Event, obj runtime.Object, objectGV sche
// TODO(audit): hook into the serializer to avoid double conversion // TODO(audit): hook into the serializer to avoid double conversion
var err error var err error
ae.RequestObject, err = encodeObject(obj, objectGV, s) ae.RequestObject, err = encodeObject(obj, objGV, s)
if err != nil { if err != nil {
// TODO(audit): add error slice to audit event struct // TODO(audit): add error slice to audit event struct
klog.Warningf("Auditing failed of %v request: %v", reflect.TypeOf(obj).Name(), err) klog.Warningf("Auditing failed of %v request: %v", reflect.TypeOf(obj).Name(), err)

View File

@ -123,8 +123,10 @@ func createHandler(r rest.NamedCreater, scope *RequestScope, admit admission.Int
scope.err(err, w, req) scope.err(err, w, req)
return return
} }
if !scope.AcceptsGroupVersion(gvk.GroupVersion()) {
err = errors.NewBadRequest(fmt.Sprintf("the API version in the data (%s) does not match the expected API version (%v)", gvk.GroupVersion().String(), gv.String())) objGV := gvk.GroupVersion()
if !scope.AcceptsGroupVersion(objGV) {
err = errors.NewBadRequest(fmt.Sprintf("the API version in the data (%s) does not match the expected API version (%v)", objGV.String(), gv.String()))
scope.err(err, w, req) scope.err(err, w, req)
return return
} }
@ -141,7 +143,6 @@ func createHandler(r rest.NamedCreater, scope *RequestScope, admit admission.Int
ae := request.AuditEventFrom(ctx) ae := request.AuditEventFrom(ctx)
admit = admission.WithAudit(admit, ae) admit = admission.WithAudit(admit, ae)
objGV := gvk.GroupVersion()
audit.LogRequestObject(ae, obj, objGV, scope.Resource, scope.Subresource, scope.Serializer) audit.LogRequestObject(ae, obj, objGV, scope.Resource, scope.Subresource, scope.Serializer)
userInfo, _ := request.UserFrom(ctx) userInfo, _ := request.UserFrom(ctx)

View File

@ -110,15 +110,15 @@ func UpdateResource(r rest.Updater, scope *RequestScope, admit admission.Interfa
scope.err(err, w, req) scope.err(err, w, req)
return return
} }
if !scope.AcceptsGroupVersion(gvk.GroupVersion()) { objGV := gvk.GroupVersion()
err = errors.NewBadRequest(fmt.Sprintf("the API version in the data (%s) does not match the expected API version (%s)", gvk.GroupVersion(), defaultGVK.GroupVersion())) if !scope.AcceptsGroupVersion(objGV) {
err = errors.NewBadRequest(fmt.Sprintf("the API version in the data (%s) does not match the expected API version (%s)", objGV, defaultGVK.GroupVersion()))
scope.err(err, w, req) scope.err(err, w, req)
return return
} }
trace.Step("Conversion done") trace.Step("Conversion done")
ae := request.AuditEventFrom(ctx) ae := request.AuditEventFrom(ctx)
objGV := gvk.GroupVersion()
audit.LogRequestObject(ae, obj, objGV, scope.Resource, scope.Subresource, scope.Serializer) audit.LogRequestObject(ae, obj, objGV, scope.Resource, scope.Subresource, scope.Serializer)
admit = admission.WithAudit(admit, ae) admit = admission.WithAudit(admit, ae)