Skip to content

Commit

Permalink
Add user helper methods to context
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Feb 13, 2015
1 parent 4d14102 commit ec66e51
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package api
import (
stderrs "errors"

"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user"
"golang.org/x/net/context"
)

Expand All @@ -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()
Expand Down Expand Up @@ -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
}

0 comments on commit ec66e51

Please sign in to comment.