context.go 4.3 KB

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