context.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2019 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 v1alpha1
  14. import (
  15. "errors"
  16. "sync"
  17. )
  18. const (
  19. // NotFound is the not found error message.
  20. NotFound = "not found"
  21. )
  22. // ContextData is a generic type for arbitrary data stored in PluginContext.
  23. type ContextData interface{}
  24. // ContextKey is the type of keys stored in PluginContext.
  25. type ContextKey string
  26. // PluginContext provides a mechanism for plugins to store and retrieve arbitrary data.
  27. // ContextData stored by one plugin can be read, altered, or deleted by another plugin.
  28. // PluginContext does not provide any data protection, as all plugins are assumed to be
  29. // trusted.
  30. type PluginContext struct {
  31. mx sync.RWMutex
  32. storage map[ContextKey]ContextData
  33. }
  34. // NewPluginContext initializes a new PluginContext and returns its pointer.
  35. func NewPluginContext() *PluginContext {
  36. return &PluginContext{
  37. storage: make(map[ContextKey]ContextData),
  38. }
  39. }
  40. // Read retrieves data with the given "key" from PluginContext. If the key is not
  41. // present an error is returned.
  42. // This function is not thread safe. In multi-threaded code, lock should be
  43. // acquired first.
  44. func (c *PluginContext) Read(key ContextKey) (ContextData, error) {
  45. if v, ok := c.storage[key]; ok {
  46. return v, nil
  47. }
  48. return nil, errors.New(NotFound)
  49. }
  50. // Write stores the given "val" in PluginContext with the given "key".
  51. // This function is not thread safe. In multi-threaded code, lock should be
  52. // acquired first.
  53. func (c *PluginContext) Write(key ContextKey, val ContextData) {
  54. c.storage[key] = val
  55. }
  56. // Delete deletes data with the given key from PluginContext.
  57. // This function is not thread safe. In multi-threaded code, lock should be
  58. // acquired first.
  59. func (c *PluginContext) Delete(key ContextKey) {
  60. delete(c.storage, key)
  61. }
  62. // Lock acquires PluginContext lock.
  63. func (c *PluginContext) Lock() {
  64. c.mx.Lock()
  65. }
  66. // Unlock releases PluginContext lock.
  67. func (c *PluginContext) Unlock() {
  68. c.mx.Unlock()
  69. }
  70. // RLock acquires PluginContext read lock.
  71. func (c *PluginContext) RLock() {
  72. c.mx.RLock()
  73. }
  74. // RUnlock releases PluginContext read lock.
  75. func (c *PluginContext) RUnlock() {
  76. c.mx.RUnlock()
  77. }