remove request context.WithUID
Kubernetes-commit: 7ea612db162c65eca16a00b6b5cd83a93bd05b1d
This commit is contained in:
parent
70ad7616f1
commit
d6a18bbf53
|
@ -20,7 +20,6 @@ import (
|
|||
"context"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apiserver/pkg/apis/audit"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
)
|
||||
|
@ -35,12 +34,6 @@ const (
|
|||
// userKey is the context key for the request user.
|
||||
userKey
|
||||
|
||||
// uidKey is the context key for the uid to assign to an object on create.
|
||||
uidKey
|
||||
|
||||
// userAgentKey is the context key for the request user agent.
|
||||
userAgentKey
|
||||
|
||||
// auditKey is the context key for the audit event.
|
||||
auditKey
|
||||
)
|
||||
|
@ -77,15 +70,6 @@ func NamespaceValue(ctx context.Context) string {
|
|||
return namespace
|
||||
}
|
||||
|
||||
// WithNamespaceDefaultIfNone returns a context whose namespace is the default if and only if the parent context has no namespace value
|
||||
func WithNamespaceDefaultIfNone(parent context.Context) context.Context {
|
||||
namespace, ok := NamespaceFrom(parent)
|
||||
if !ok || len(namespace) == 0 {
|
||||
return WithNamespace(parent, metav1.NamespaceDefault)
|
||||
}
|
||||
return parent
|
||||
}
|
||||
|
||||
// WithUser returns a copy of parent in which the user value is set
|
||||
func WithUser(parent context.Context, user user.Info) context.Context {
|
||||
return WithValue(parent, userKey, user)
|
||||
|
@ -97,17 +81,6 @@ func UserFrom(ctx context.Context) (user.Info, bool) {
|
|||
return user, ok
|
||||
}
|
||||
|
||||
// WithUID returns a copy of parent in which the uid value is set
|
||||
func WithUID(parent context.Context, uid types.UID) context.Context {
|
||||
return WithValue(parent, uidKey, uid)
|
||||
}
|
||||
|
||||
// UIDFrom returns the value of the uid key on the ctx
|
||||
func UIDFrom(ctx context.Context) (types.UID, bool) {
|
||||
uid, ok := ctx.Value(uidKey).(types.UID)
|
||||
return uid, ok
|
||||
}
|
||||
|
||||
// WithAuditEvent returns set audit event struct.
|
||||
func WithAuditEvent(parent context.Context, ev *audit.Event) context.Context {
|
||||
return WithValue(parent, auditKey, ev)
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
)
|
||||
|
||||
|
@ -92,20 +91,3 @@ func TestUserContext(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
//TestUIDContext validates that a UID can be get/set on a context object
|
||||
func TestUIDContext(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
_, ok := UIDFrom(ctx)
|
||||
if ok {
|
||||
t.Fatalf("Should not be ok because there is no UID on the context")
|
||||
}
|
||||
ctx = WithUID(
|
||||
ctx,
|
||||
types.UID("testUID"),
|
||||
)
|
||||
_, ok = UIDFrom(ctx)
|
||||
if !ok {
|
||||
t.Fatalf("Error getting UID")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime.
|
|||
objectMeta.SetDeletionTimestamp(nil)
|
||||
objectMeta.SetDeletionGracePeriodSeconds(nil)
|
||||
strategy.PrepareForCreate(ctx, obj)
|
||||
FillObjectMetaSystemFields(ctx, objectMeta)
|
||||
FillObjectMetaSystemFields(objectMeta)
|
||||
if len(objectMeta.GetGenerateName()) > 0 && len(objectMeta.GetName()) == 0 {
|
||||
objectMeta.SetName(strategy.GenerateName(objectMeta.GetGenerateName()))
|
||||
}
|
||||
|
|
|
@ -25,15 +25,9 @@ import (
|
|||
)
|
||||
|
||||
// FillObjectMetaSystemFields populates fields that are managed by the system on ObjectMeta.
|
||||
func FillObjectMetaSystemFields(ctx context.Context, meta metav1.Object) {
|
||||
func FillObjectMetaSystemFields(meta metav1.Object) {
|
||||
meta.SetCreationTimestamp(metav1.Now())
|
||||
// allows admission controllers to assign a UID earlier in the request processing
|
||||
// to support tracking resources pending creation.
|
||||
uid, found := genericapirequest.UIDFrom(ctx)
|
||||
if !found {
|
||||
uid = uuid.NewUUID()
|
||||
}
|
||||
meta.SetUID(uid)
|
||||
meta.SetUID(uuid.NewUUID())
|
||||
meta.SetSelfLink("")
|
||||
}
|
||||
|
||||
|
|
|
@ -21,34 +21,26 @@ import (
|
|||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/uuid"
|
||||
"k8s.io/apiserver/pkg/apis/example"
|
||||
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||
)
|
||||
|
||||
// TestFillObjectMetaSystemFields validates that system populated fields are set on an object
|
||||
func TestFillObjectMetaSystemFields(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
resource := metav1.ObjectMeta{}
|
||||
FillObjectMetaSystemFields(ctx, &resource)
|
||||
FillObjectMetaSystemFields(&resource)
|
||||
if resource.CreationTimestamp.Time.IsZero() {
|
||||
t.Errorf("resource.CreationTimestamp is zero")
|
||||
} else if len(resource.UID) == 0 {
|
||||
t.Errorf("resource.UID missing")
|
||||
}
|
||||
// verify we can inject a UID
|
||||
uid := uuid.NewUUID()
|
||||
ctx = genericapirequest.WithUID(ctx, uid)
|
||||
resource = metav1.ObjectMeta{}
|
||||
FillObjectMetaSystemFields(ctx, &resource)
|
||||
if resource.UID != uid {
|
||||
t.Errorf("resource.UID expected: %v, actual: %v", uid, resource.UID)
|
||||
if len(resource.UID) == 0 {
|
||||
t.Errorf("resource.UID missing")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated
|
||||
func TestHasObjectMetaSystemFieldValues(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
resource := metav1.ObjectMeta{}
|
||||
objMeta, err := meta.Accessor(&resource)
|
||||
if err != nil {
|
||||
|
@ -57,7 +49,7 @@ func TestHasObjectMetaSystemFieldValues(t *testing.T) {
|
|||
if metav1.HasObjectMetaSystemFieldValues(objMeta) {
|
||||
t.Errorf("the resource does not have all fields yet populated, but incorrectly reports it does")
|
||||
}
|
||||
FillObjectMetaSystemFields(ctx, &resource)
|
||||
FillObjectMetaSystemFields(&resource)
|
||||
if !metav1.HasObjectMetaSystemFieldValues(objMeta) {
|
||||
t.Errorf("the resource does have all fields populated, but incorrectly reports it does not")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue