Add user helper methods to context

This commit is contained in:
Jordan Liggitt 2015-02-11 17:06:08 -05:00
parent 4d141025da
commit ec66e5147e

View File

@ -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
}