make audit metadata work for custom resources

Kubernetes-commit: 9d41c0099adc9a9328209075421eb42681bd4b09
This commit is contained in:
David Eads 2019-02-26 14:37:58 -05:00 committed by Kubernetes Publisher
parent 823443c66e
commit 22712db708
1 changed files with 5 additions and 4 deletions

View File

@ -20,13 +20,13 @@ import (
"bytes"
"fmt"
"net/http"
"reflect"
"time"
"github.com/pborman/uuid"
"k8s.io/klog"
"reflect"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@ -117,8 +117,9 @@ func LogRequestObject(ae *auditinternal.Event, obj runtime.Object, gvr schema.Gr
if ae.ObjectRef == nil {
ae.ObjectRef = &auditinternal.ObjectReference{}
}
if acc, ok := obj.(metav1.ObjectMetaAccessor); ok {
meta := acc.GetObjectMeta()
// meta.Accessor is more general than ObjectMetaAccessor, but if it fails, we can just skip setting these bits
if meta, err := meta.Accessor(obj); err == nil {
if len(ae.ObjectRef.Namespace) == 0 {
ae.ObjectRef.Namespace = meta.GetNamespace()
}