2014-09-25 18:34:01 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2014-09-25 18:34:01 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package api
import (
2014-09-29 21:17:42 +00:00
stderrs "errors"
2015-10-09 14:49:01 +00:00
"time"
2014-09-29 21:17:42 +00:00
2014-11-05 23:48:29 +00:00
"golang.org/x/net/context"
2015-08-05 22:05:17 +00:00
"k8s.io/kubernetes/pkg/auth/user"
2016-05-07 13:23:15 +00:00
"k8s.io/kubernetes/pkg/types"
2014-09-25 18:34:01 +00:00
)
2014-09-26 15:46:04 +00:00
// Context carries values across API boundaries.
2015-10-09 14:49:01 +00:00
// This context matches the context.Context interface
// (https://blog.golang.org/context), for the purposes
// of passing the api.Context through to the storage tier.
// TODO: Determine the extent that this abstraction+interface
// is used by the api, and whether we can remove.
2014-09-26 15:46:04 +00:00
type Context interface {
2015-10-09 14:49:01 +00:00
// Value returns the value associated with key or nil if none.
2014-09-26 15:46:04 +00:00
Value ( key interface { } ) interface { }
2015-10-09 14:49:01 +00:00
// Deadline returns the time when this Context will be canceled, if any.
Deadline ( ) ( deadline time . Time , ok bool )
// Done returns a channel that is closed when this Context is canceled
// or times out.
Done ( ) <- chan struct { }
// Err indicates why this context was canceled, after the Done channel
// is closed.
Err ( ) error
2014-09-26 15:46:04 +00:00
}
2014-09-29 21:17:42 +00:00
// The key type is unexported to prevent collisions
type key int
2016-05-07 13:23:15 +00:00
const (
// namespaceKey is the context key for the request namespace.
namespaceKey key = iota
2014-09-29 21:17:42 +00:00
2016-05-07 13:23:15 +00:00
// userKey is the context key for the request user.
userKey
// uidKey is the context key for the uid to assign to an object on create.
uidKey
)
2015-02-11 22:06:08 +00:00
2014-09-29 21:17:42 +00:00
// NewContext instantiates a base context object for request flows.
2014-09-26 15:46:04 +00:00
func NewContext ( ) Context {
return context . TODO ( )
2014-09-25 18:34:01 +00:00
}
2014-09-29 21:17:42 +00:00
// NewDefaultContext instantiates a base context object for request flows in the default namespace
func NewDefaultContext ( ) Context {
return WithNamespace ( NewContext ( ) , NamespaceDefault )
}
// WithValue returns a copy of parent in which the value associated with key is val.
func WithValue ( parent Context , key interface { } , val interface { } ) Context {
internalCtx , ok := parent . ( context . Context )
if ! ok {
panic ( stderrs . New ( "Invalid context type" ) )
}
return context . WithValue ( internalCtx , key , val )
}
// WithNamespace returns a copy of parent in which the namespace value is set
func WithNamespace ( parent Context , namespace string ) Context {
return WithValue ( parent , namespaceKey , namespace )
}
// NamespaceFrom returns the value of the namespace key on the ctx
func NamespaceFrom ( ctx Context ) ( string , bool ) {
namespace , ok := ctx . Value ( namespaceKey ) . ( string )
return namespace , ok
}
2015-01-19 19:35:41 +00:00
// NamespaceValue returns the value of the namespace key on the ctx, or the empty string if none
func NamespaceValue ( ctx Context ) string {
2014-10-03 15:44:06 +00:00
namespace , _ := NamespaceFrom ( ctx )
return namespace
}
2014-10-01 14:42:07 +00:00
// ValidNamespace returns false if the namespace on the context differs from the resource. If the resource has no namespace, it is set to the value in the context.
2014-10-23 20:53:32 +00:00
func ValidNamespace ( ctx Context , resource * ObjectMeta ) bool {
2014-09-29 21:17:42 +00:00
ns , ok := NamespaceFrom ( ctx )
if len ( resource . Namespace ) == 0 {
resource . Namespace = ns
}
return ns == resource . Namespace && ok
}
2014-10-03 15:44:06 +00:00
// 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 {
namespace , ok := NamespaceFrom ( parent )
if ! ok || len ( namespace ) == 0 {
return WithNamespace ( parent , NamespaceDefault )
}
return parent
}
2015-02-11 22:06:08 +00:00
// 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
}
2016-05-07 13:23:15 +00:00
// WithUID returns a copy of parent in which the uid value is set
func WithUID ( parent Context , uid types . UID ) Context {
return WithValue ( parent , uidKey , uid )
}
// UIDFrom returns the value of the uid key on the ctx
func UIDFrom ( ctx Context ) ( types . UID , bool ) {
uid , ok := ctx . Value ( uidKey ) . ( types . UID )
return uid , ok
}