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