In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
What is the example analysis of Kubernetes Informer? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Friends who are familiar with Kubernetes must have the motivation to take a peek at the source code and appreciate the design and implementation of the master. It should be a good choice to cut into Kubernetes from the official open go-sdk. In the client-go toolkit, informer is a major tool, the following is a brief introduction to the main functions of inform components and part of the code, if there are any mistakes, welcome to point out that the editor really offered an iron head for everyone to tap.
Main functions:
List and Watch methods, from the APIServer synchronization / decoding object to the local cache, and provide access to the local cache to reduce the pressure of access to the APIServer.
The AddEventHandler method, the entry for handler registration. After the user changes the object, the corresponding function of handler is triggered, and the object status information is written to workqueue for worker consumption.
Logical diagram (citing the sharing of Xu Chao):
Analysis of code structure:
SharedInformerFactory
Infomers members: maintain the map of the Type interface to SharedIndexInformer, which maps to the sharedIndexInformer struct pointer variable, factory design pattern.
StartedInformers members: maintain the mapping of the Type interface to the Bool value, and judge here before starting the informer to avoid starting infromer repeatedly.
Start method: iterate over the informers member and call the informer.run method to start the informer.
InformerFor method: continuously add entry for informers members.
...
DeploymentInformer
Factory member: a structure variable that implements the SharedInformerFactory interface, here assigned as a sharedInformerFactory struct pointer variable.
Informer method: calls the InformerFor method of factory and returns the sharedIndexInformer pointer object.
Lister method: returns the indexer member of informer to read the local cache.
...
SharedIndexInformer
Indexer member: implements the Indexer interface and maintains the local cache (map [string] interface {}), where the value is assigned to the cache struct pointer variable.
Controller member: implements the Controller interface, maintains the synchronization mechanism from APIServer to the local cache, while maintaining communication with processor, which is assigned to the controller struct pointer object.
Processor member: maintains communication with controller and triggers the method corresponding to handler, where the value is assigned to the sharedProcessor struct pointer object.
The AddEventHandler method: create a processorListener struct value object, listeners and syncinglisteners for append to processor members.
HandleDelta method: iterate controller.config.Queue.items [key] (Delta slice), call indexer.Add/Delete/Update to update the local cache, and call the processor.distribute method to iterate the listeners and syncinglisteners of processor as needed, and send messages to the message queue of listener.
...
Indexer
CacheStorage member: implements the ThreadSafeStore interface to maintain the local thread-safe cache map [string] interface {}, where the value is assigned to the threadSafeMap struct pointer variable.
KeyFunc member: a function that maintains a local cache key.
The Add/Delete/Update/List method: supports common operations on local caches.
...
Controller
Reflector member: use the resourceVersion mechanism to call the listerWatcher.List/Watch method, from the APIServer synchronization / decoding object to the config.Queue.items (map [string] Deltas), where the value is assigned to the Reflect struct pointer object.
Config member: maintains the chain queue and object operation mapping (Deltas [string]), which is assigned to the Config struct value object.
Processloop method: pass in the config.Process member and call the config.Queue.Pop method in a loop.
...
Processor
Listeners member: a slice of the processorListener struct pointer variable.
Run method: iterate through listeners, call the processorListener.run/pop method, and trigger the processorListener.handler.OnAdd/OnUpdate/OnDelete method.
...
Reflector
Store member: implements the Store interface, which is assigned to the DeltaFIFO struct pointer object.
ListerWatcher member: implements the List/Watch method of ListerWatcher, synchronizing / decoding objects from APIServer, where the value is assigned to the ListWatch struct pointer variable.
ListAndWatch method: call the List/Watch method of the listerWatcher member to complete the synchronization / decoding of the object from the APIServer, and then call the syncWith and watchHandler methods to synchronize the event and object to the storage member of the store.
SyncWith method: call the Replace method of the Store interface to synchronize event and object information.
WatchHandler method: call the Add/Update/Delete method of the Store interface to synchronize event and object information.
...
Config
Queue member: implements the Queue interface, which is assigned to the DeltaFIFO struct pointer object.
Process member: the value is the sharedIndexInformer.HandleDelta method.
...
DeltaFIFO
Items member: map [string] Deltas, which stores object and operation information.
Queue member: [] string, simple chain queue.
KnownObjects member: implements the KeyListerGetter interface, which is assigned to the cache struct pointer object.
Pop method: call the config.Process method from the queue member pop.
Add/Delete/Update/Replace method: call the queueActionLocked method.
QueueActionLocked method: maintains items and queue members.
...
ProcessorListener
NextCh member: the message is produced from local pendingNotifications or addCh, and the run method consumes the message.
AddCh member: produces a message from a call to the add method, writes it to the local pendingNotifications or passes it to nextCh.
PendingNotifications member: maintains a local buffer message, which is assigned to the RingGrowing struct value object.
Pop method: maintains messaging for nextCh and addCh.
Run method: consume nextCh messages and call the processorListener.OnAdd/OnUpdate/OnDelete method as needed.
...
After being familiar with the main strategies and mechanisms of Informer implementation, it will bring a lot of confidence and benefits to the subsequent development of controller, making it convenient for everyone to continue to work on the Kubernetes platform. Briefly describe the main structure of informer, there are some areas that need to be studied, such as cache compression, protobuf decode/encode mechanism, processListener message caching mechanism, lock notification mechanism and so on. Thank you.
The answer to the question about the sample analysis of Kubernetes Informer is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.