mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
remove request context.WithUID
This commit is contained in:
parent
e59ae29fbc
commit
7ea612db16
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
|
||||||
"k8s.io/apiserver/pkg/apis/audit"
|
"k8s.io/apiserver/pkg/apis/audit"
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
"k8s.io/apiserver/pkg/authentication/user"
|
||||||
)
|
)
|
||||||
@ -35,12 +34,6 @@ const (
|
|||||||
// userKey is the context key for the request user.
|
// userKey is the context key for the request user.
|
||||||
userKey
|
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 is the context key for the audit event.
|
||||||
auditKey
|
auditKey
|
||||||
)
|
)
|
||||||
@ -77,15 +70,6 @@ func NamespaceValue(ctx context.Context) string {
|
|||||||
return namespace
|
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
|
// WithUser returns a copy of parent in which the user value is set
|
||||||
func WithUser(parent context.Context, user user.Info) context.Context {
|
func WithUser(parent context.Context, user user.Info) context.Context {
|
||||||
return WithValue(parent, userKey, user)
|
return WithValue(parent, userKey, user)
|
||||||
@ -97,17 +81,6 @@ func UserFrom(ctx context.Context) (user.Info, bool) {
|
|||||||
return user, ok
|
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.
|
// WithAuditEvent returns set audit event struct.
|
||||||
func WithAuditEvent(parent context.Context, ev *audit.Event) context.Context {
|
func WithAuditEvent(parent context.Context, ev *audit.Event) context.Context {
|
||||||
return WithValue(parent, auditKey, ev)
|
return WithValue(parent, auditKey, ev)
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
"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.SetDeletionTimestamp(nil)
|
||||||
objectMeta.SetDeletionGracePeriodSeconds(nil)
|
objectMeta.SetDeletionGracePeriodSeconds(nil)
|
||||||
strategy.PrepareForCreate(ctx, obj)
|
strategy.PrepareForCreate(ctx, obj)
|
||||||
FillObjectMetaSystemFields(ctx, objectMeta)
|
FillObjectMetaSystemFields(objectMeta)
|
||||||
if len(objectMeta.GetGenerateName()) > 0 && len(objectMeta.GetName()) == 0 {
|
if len(objectMeta.GetGenerateName()) > 0 && len(objectMeta.GetName()) == 0 {
|
||||||
objectMeta.SetName(strategy.GenerateName(objectMeta.GetGenerateName()))
|
objectMeta.SetName(strategy.GenerateName(objectMeta.GetGenerateName()))
|
||||||
}
|
}
|
||||||
|
@ -25,15 +25,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FillObjectMetaSystemFields populates fields that are managed by the system on ObjectMeta.
|
// 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())
|
meta.SetCreationTimestamp(metav1.Now())
|
||||||
// allows admission controllers to assign a UID earlier in the request processing
|
meta.SetUID(uuid.NewUUID())
|
||||||
// to support tracking resources pending creation.
|
|
||||||
uid, found := genericapirequest.UIDFrom(ctx)
|
|
||||||
if !found {
|
|
||||||
uid = uuid.NewUUID()
|
|
||||||
}
|
|
||||||
meta.SetUID(uid)
|
|
||||||
meta.SetSelfLink("")
|
meta.SetSelfLink("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,34 +21,26 @@ import (
|
|||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/uuid"
|
|
||||||
"k8s.io/apiserver/pkg/apis/example"
|
"k8s.io/apiserver/pkg/apis/example"
|
||||||
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestFillObjectMetaSystemFields validates that system populated fields are set on an object
|
// TestFillObjectMetaSystemFields validates that system populated fields are set on an object
|
||||||
func TestFillObjectMetaSystemFields(t *testing.T) {
|
func TestFillObjectMetaSystemFields(t *testing.T) {
|
||||||
ctx := genericapirequest.NewDefaultContext()
|
|
||||||
resource := metav1.ObjectMeta{}
|
resource := metav1.ObjectMeta{}
|
||||||
FillObjectMetaSystemFields(ctx, &resource)
|
FillObjectMetaSystemFields(&resource)
|
||||||
if resource.CreationTimestamp.Time.IsZero() {
|
if resource.CreationTimestamp.Time.IsZero() {
|
||||||
t.Errorf("resource.CreationTimestamp is zero")
|
t.Errorf("resource.CreationTimestamp is zero")
|
||||||
} else if len(resource.UID) == 0 {
|
} else if len(resource.UID) == 0 {
|
||||||
t.Errorf("resource.UID missing")
|
t.Errorf("resource.UID missing")
|
||||||
}
|
}
|
||||||
// verify we can inject a UID
|
if len(resource.UID) == 0 {
|
||||||
uid := uuid.NewUUID()
|
t.Errorf("resource.UID missing")
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated
|
// TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated
|
||||||
func TestHasObjectMetaSystemFieldValues(t *testing.T) {
|
func TestHasObjectMetaSystemFieldValues(t *testing.T) {
|
||||||
ctx := genericapirequest.NewDefaultContext()
|
|
||||||
resource := metav1.ObjectMeta{}
|
resource := metav1.ObjectMeta{}
|
||||||
objMeta, err := meta.Accessor(&resource)
|
objMeta, err := meta.Accessor(&resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,7 +49,7 @@ func TestHasObjectMetaSystemFieldValues(t *testing.T) {
|
|||||||
if metav1.HasObjectMetaSystemFieldValues(objMeta) {
|
if metav1.HasObjectMetaSystemFieldValues(objMeta) {
|
||||||
t.Errorf("the resource does not have all fields yet populated, but incorrectly reports it does")
|
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) {
|
if !metav1.HasObjectMetaSystemFieldValues(objMeta) {
|
||||||
t.Errorf("the resource does have all fields populated, but incorrectly reports it does not")
|
t.Errorf("the resource does have all fields populated, but incorrectly reports it does not")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user