diff --git a/pkg/api/context.go b/pkg/api/context.go index 6206b7c6eda..169c938e69e 100644 --- a/pkg/api/context.go +++ b/pkg/api/context.go @@ -19,6 +19,7 @@ package api import ( stderrs "errors" + "github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user" "golang.org/x/net/context" ) @@ -33,6 +34,9 @@ type key int // namespaceKey is the context key for the request namespace. const namespaceKey key = 0 +// userKey is the context key for the request user. +const userKey key = 1 + // NewContext instantiates a base context object for request flows. func NewContext() Context { return context.TODO() @@ -86,3 +90,14 @@ func WithNamespaceDefaultIfNone(parent Context) Context { } return parent } + +// WithUser returns a copy of parent in which the user value is set +func WithUser(parent Context, user user.Info) Context { + return WithValue(parent, userKey, user) +} + +// UserFrom returns the value of the user key on the ctx +func UserFrom(ctx Context) (user.Info, bool) { + user, ok := ctx.Value(userKey).(user.Info) + return user, ok +}