github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/context.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package api
    18  
    19  import (
    20  	stderrs "errors"
    21  	"time"
    22  
    23  	"golang.org/x/net/context"
    24  	"k8s.io/kubernetes/pkg/auth/user"
    25  )
    26  
    27  // Context carries values across API boundaries.
    28  // This context matches the context.Context interface
    29  // (https://blog.golang.org/context), for the purposes
    30  // of passing the api.Context through to the storage tier.
    31  // TODO: Determine the extent that this abstraction+interface
    32  // is used by the api, and whether we can remove.
    33  type Context interface {
    34  	// Value returns the value associated with key or nil if none.
    35  	Value(key interface{}) interface{}
    36  
    37  	// Deadline returns the time when this Context will be canceled, if any.
    38  	Deadline() (deadline time.Time, ok bool)
    39  
    40  	// Done returns a channel that is closed when this Context is canceled
    41  	// or times out.
    42  	Done() <-chan struct{}
    43  
    44  	// Err indicates why this context was canceled, after the Done channel
    45  	// is closed.
    46  	Err() error
    47  }
    48  
    49  // The key type is unexported to prevent collisions
    50  type key int
    51  
    52  // namespaceKey is the context key for the request namespace.
    53  const namespaceKey key = 0
    54  
    55  // userKey is the context key for the request user.
    56  const userKey key = 1
    57  
    58  // NewContext instantiates a base context object for request flows.
    59  func NewContext() Context {
    60  	return context.TODO()
    61  }
    62  
    63  // NewDefaultContext instantiates a base context object for request flows in the default namespace
    64  func NewDefaultContext() Context {
    65  	return WithNamespace(NewContext(), NamespaceDefault)
    66  }
    67  
    68  // WithValue returns a copy of parent in which the value associated with key is val.
    69  func WithValue(parent Context, key interface{}, val interface{}) Context {
    70  	internalCtx, ok := parent.(context.Context)
    71  	if !ok {
    72  		panic(stderrs.New("Invalid context type"))
    73  	}
    74  	return context.WithValue(internalCtx, key, val)
    75  }
    76  
    77  // WithNamespace returns a copy of parent in which the namespace value is set
    78  func WithNamespace(parent Context, namespace string) Context {
    79  	return WithValue(parent, namespaceKey, namespace)
    80  }
    81  
    82  // NamespaceFrom returns the value of the namespace key on the ctx
    83  func NamespaceFrom(ctx Context) (string, bool) {
    84  	namespace, ok := ctx.Value(namespaceKey).(string)
    85  	return namespace, ok
    86  }
    87  
    88  // NamespaceValue returns the value of the namespace key on the ctx, or the empty string if none
    89  func NamespaceValue(ctx Context) string {
    90  	namespace, _ := NamespaceFrom(ctx)
    91  	return namespace
    92  }
    93  
    94  // 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.
    95  func ValidNamespace(ctx Context, resource *ObjectMeta) bool {
    96  	ns, ok := NamespaceFrom(ctx)
    97  	if len(resource.Namespace) == 0 {
    98  		resource.Namespace = ns
    99  	}
   100  	return ns == resource.Namespace && ok
   101  }
   102  
   103  // WithNamespaceDefaultIfNone returns a context whose namespace is the default if and only if the parent context has no namespace value
   104  func WithNamespaceDefaultIfNone(parent Context) Context {
   105  	namespace, ok := NamespaceFrom(parent)
   106  	if !ok || len(namespace) == 0 {
   107  		return WithNamespace(parent, NamespaceDefault)
   108  	}
   109  	return parent
   110  }
   111  
   112  // WithUser returns a copy of parent in which the user value is set
   113  func WithUser(parent Context, user user.Info) Context {
   114  	return WithValue(parent, userKey, user)
   115  }
   116  
   117  // UserFrom returns the value of the user key on the ctx
   118  func UserFrom(ctx Context) (user.Info, bool) {
   119  	user, ok := ctx.Value(userKey).(user.Info)
   120  	return user, ok
   121  }