config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 config
  14. import (
  15. "sync"
  16. "k8s.io/kubernetes/pkg/util/wait"
  17. )
  18. type Merger interface {
  19. // Invoked when a change from a source is received. May also function as an incremental
  20. // merger if you wish to consume changes incrementally. Must be reentrant when more than
  21. // one source is defined.
  22. Merge(source string, update interface{}) error
  23. }
  24. // MergeFunc implements the Merger interface
  25. type MergeFunc func(source string, update interface{}) error
  26. func (f MergeFunc) Merge(source string, update interface{}) error {
  27. return f(source, update)
  28. }
  29. // Mux is a class for merging configuration from multiple sources. Changes are
  30. // pushed via channels and sent to the merge function.
  31. type Mux struct {
  32. // Invoked when an update is sent to a source.
  33. merger Merger
  34. // Sources and their lock.
  35. sourceLock sync.RWMutex
  36. // Maps source names to channels
  37. sources map[string]chan interface{}
  38. }
  39. // NewMux creates a new mux that can merge changes from multiple sources.
  40. func NewMux(merger Merger) *Mux {
  41. mux := &Mux{
  42. sources: make(map[string]chan interface{}),
  43. merger: merger,
  44. }
  45. return mux
  46. }
  47. // Channel returns a channel where a configuration source
  48. // can send updates of new configurations. Multiple calls with the same
  49. // source will return the same channel. This allows change and state based sources
  50. // to use the same channel. Different source names however will be treated as a
  51. // union.
  52. func (m *Mux) Channel(source string) chan interface{} {
  53. if len(source) == 0 {
  54. panic("Channel given an empty name")
  55. }
  56. m.sourceLock.Lock()
  57. defer m.sourceLock.Unlock()
  58. channel, exists := m.sources[source]
  59. if exists {
  60. return channel
  61. }
  62. newChannel := make(chan interface{})
  63. m.sources[source] = newChannel
  64. go wait.Until(func() { m.listen(source, newChannel) }, 0, wait.NeverStop)
  65. return newChannel
  66. }
  67. func (m *Mux) listen(source string, listenChannel <-chan interface{}) {
  68. for update := range listenChannel {
  69. m.merger.Merge(source, update)
  70. }
  71. }
  72. // Accessor is an interface for retrieving the current merge state.
  73. type Accessor interface {
  74. // MergedState returns a representation of the current merge state.
  75. // Must be reentrant when more than one source is defined.
  76. MergedState() interface{}
  77. }
  78. // AccessorFunc implements the Accessor interface.
  79. type AccessorFunc func() interface{}
  80. func (f AccessorFunc) MergedState() interface{} {
  81. return f()
  82. }
  83. type Listener interface {
  84. // OnUpdate is invoked when a change is made to an object.
  85. OnUpdate(instance interface{})
  86. }
  87. // ListenerFunc receives a representation of the change or object.
  88. type ListenerFunc func(instance interface{})
  89. func (f ListenerFunc) OnUpdate(instance interface{}) {
  90. f(instance)
  91. }
  92. type Broadcaster struct {
  93. // Listeners for changes and their lock.
  94. listenerLock sync.RWMutex
  95. listeners []Listener
  96. }
  97. // NewBroadcaster registers a set of listeners that support the Listener interface
  98. // and notifies them all on changes.
  99. func NewBroadcaster() *Broadcaster {
  100. return &Broadcaster{}
  101. }
  102. // Add registers listener to receive updates of changes.
  103. func (b *Broadcaster) Add(listener Listener) {
  104. b.listenerLock.Lock()
  105. defer b.listenerLock.Unlock()
  106. b.listeners = append(b.listeners, listener)
  107. }
  108. // Notify notifies all listeners.
  109. func (b *Broadcaster) Notify(instance interface{}) {
  110. b.listenerLock.RLock()
  111. listeners := b.listeners
  112. b.listenerLock.RUnlock()
  113. for _, listener := range listeners {
  114. listener.OnUpdate(instance)
  115. }
  116. }