kubernetes之store(一)

来源:互联网 发布:淘宝卖面膜名字店面 编辑:程序博客网 时间:2024/06/06 23:41

kubernetes的存储

用过kubernetes(以下简称k8s)的都知道,k8s的存储是用etcd,这个是肯定的。我现在要说的是k8s的内存的存储(用golang的map实现的),本来是想继上一篇接着讲list-wather的,但是,发现要讲的list-watcher都涉及到了内存存储,所以就打算先把k8s的内存存储讲完之后再接着讲list-wather,这样才能更好的理解之后的list-wather是如何实现的。

在来做个简短的说明下k8s是如何通过list-wather机制去实现对etcd中各种k8s的resource进行同步同步到内存存储,然后将各种resource的改变(事件)广播个各个wather的。首先是 etcd本身的list-wather 实现对etcd中的各种resource的list和watcher,然后将list和wather到的各种event(就是各种resource的事件,其中包括各种resource对象本身)存入到内存(也就是本文要说的store),然后各个组件就在启动后去内存中进行相应的resource的list和wather。得到相应的event(就是各种resource的事件,其中包括各种resource对象本身),然后进行各自的广播,让各个事件的监听者能收到相对的event(比如:podController在wather到对应的事件后,将event的runtime.Object转成对应的pod对象,然后进行相关的操作)。所以总的来说,数据的流向是:etcd—>store—>apiserver list-wather–>broadcast–>各个wather。

store详解

之前版本的k8s源码中,storage这个包就在源码下的pkg下,应该是1.6之后统一放到了k8s.io/client-go这个项目下去了,包路径:k8s.io/client-go/tools/cache

store.go

首先看下store接口,以下说的对象实质就是event对象(实质就是runtime.Object对象)

type Store interface {    //添加一个对象到内存中    Add(obj interface{}) error    //更新内存中的对象    Update(obj interface{}) error    //删除内存中的对象    Delete(obj interface{}) error    //获取全部内存中的对象    List() []interface{}    //获取全部对象的key    ListKeys() []string    //获取一个对象    Get(obj interface{}) (item interface{}, exists bool, err error)    //通过key获取对象    GetByKey(key string) (item interface{}, exists bool, err error)    //删除内存中的所有对象,添加新的一组对象    Replace([]interface{}, string) error    //从新同步etcd数据到内存    Resync() error}

接下来要说的就是store接口的实现

type cache struct {    // 线程安全的存储ThreadSafeStore的实现类    cacheStorage ThreadSafeStore    //     keyFunc KeyFunc}

ThreadSafeStore的定义以及实现的接口:

type threadSafeMap struct {    lock  sync.RWMutex    //内存存储的map,key就是event的key,val就是对应的event    items map[string]interface{}    // indexers maps a name to an IndexFunc    indexers Indexers    // indices maps a name to an Index    indices Indices}type ThreadSafeStore interface {    Add(key string, obj interface{})    Update(key string, obj interface{})    Delete(key string)    Get(key string) (item interface{}, exists bool)    List() []interface{}    ListKeys() []string    Replace(map[string]interface{}, string)    Index(indexName string, obj interface{}) ([]interface{}, error)    ListIndexFuncValues(name string) []string    ByIndex(indexName, indexKey string) ([]interface{}, error)    GetIndexers() Indexers    // AddIndexers adds more indexers to this store.  If you call this after you already have data    // in the store, the results are undefined.    AddIndexers(newIndexers Indexers) error    Resync() error}

ThreadSafeStore涉及到indexer,这个下一次再讲这个。

接下 就是 cache 的各种方法的实现,主要列出与index无关的方法,

//添加一个对象到cacheStorage的items的map中func (c *cache) Add(obj interface{}) error {    key, err := c.keyFunc(obj)    if err != nil {        return KeyError{obj, err}    }    c.cacheStorage.Add(key, obj)    return nil}//更新cacheStorage的items的map中一个对象func (c *cache) Update(obj interface{}) error {    key, err := c.keyFunc(obj)    if err != nil {        return KeyError{obj, err}    }    c.cacheStorage.Update(key, obj)    return nil}//从cacheStorage的items的map中删除一个对象func (c *cache) Delete(obj interface{}) error {    key, err := c.keyFunc(obj)    if err != nil {        return KeyError{obj, err}    }    c.cacheStorage.Delete(key)    return nil}//获取cacheStorage的items的map的全部对象func (c *cache) List() []interface{} {    return c.cacheStorage.List()}//获取cacheStorage的items的map的全部对象的keyfunc (c *cache) ListKeys() []string {    return c.cacheStorage.ListKeys()}//获取cacheStorage的items的map的一个对象func (c *cache) Get(obj interface{}) (item interface{}, exists bool, err error) {    key, err := c.keyFunc(obj)    if err != nil {        return nil, false, KeyError{obj, err}    }    return c.GetByKey(key)}//通过key获取cacheStorage的items的map的全一个对象func (c *cache) GetByKey(key string) (item interface{}, exists bool, err error) {    item, exists = c.cacheStorage.Get(key)    return item, exists, nil}//替换cacheStorage的items的mapfunc (c *cache) Replace(list []interface{}, resourceVersion string) error {    items := map[string]interface{}{}    for _, item := range list {        key, err := c.keyFunc(item)        if err != nil {            return KeyError{item, err}        }        items[key] = item    }    c.cacheStorage.Replace(items, resourceVersion)    return nil}//从新同步数据func (c *cache) Resync() error {    return c.cacheStorage.Resync()}

以上只是最底层的内存存储的实现,还有各种对内存存储的封装,之后,会一一讲解到

0 0
原创粉丝点击