mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Add user helper methods to context
This commit is contained in:
parent
4d141025da
commit
ec66e5147e
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user