5

Kubernetes APIServer Storage 架构设计源码阅读 | 云原生社区

 3 years ago
source link: https://cloudnative.to/blog/kubernetes-apiserver-storage/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

大家好,我是杨鼎睿,这一次给大家带来的是 API Server 的源码阅读。包括之前的 etcd 源码阅读,整个 API Server 共 109 张源码及源码图,文章最后有 API Server 系列目录。欢迎大家的阅读。

本文研究了 Storage 部分的源码,配备源码进行进一步理解,可以加深理解,增强相关设计能力。

StorageFactory

StorageFactory 的作用是封装并简化对资源的操作。StorageFactory 最主要的作用是根据传入的 GroupResource 获取对应于该资源的存储配置 Config。

storage-storage-factory.svg

在 API Server 中,StorageFactory 由 StorageFactoryConfig 来生成,StorageFactoryConfig 又通过 EtcdOption 来生成,毕竟无论如何变化,etcd 存储才是最终的目的地。

image.png

DefaultStorageFactory

DefaultStorageFactory 是到 1.18 版本前,K8S 内部 StorageFactory 的唯一实现,下面我们来详细分析 DefaultStorageFactory 的模式。

Cohabitating Resources

storage-cohabitating-resources.svg

DefaultStorageFactory 将关联的 GroupResource 组织在一起,从上图可以看到,每一个传入的 GroupResource 是按顺序处理的,因此,关联的 GroupResource 间也是有优先级问题存在的。下图为创建 kube-apiserver 时,使用的 StorageFactory 中关联资源的配置情况

image.png

RESTOptionsGetter

Etcd 配置与 StorageFactory 最终都汇入 RESTOptionsGetter 中。RESTOptionsGetter 做为核心配置项,用于通过 GroupResource 找到最终的存储。

image.png

创建 storage.Interface 的过程如下图所示

rest-options-getter-landscape.svg

StorageFactoryRestOptionFactory

以 StorageFactoryRestOptionFactory 为例,GetRESTOptions 方法步骤如下

  • 使用 StorageFactory 生成 Storage Config
  • 创建 RESTOptions 结构体,并保存生成的 Storage Config
  • 默认使用 generic.UndecoratedStorage 方法作为 Decorator
  • 如果开启了 EnableWatchCache 选项,会修改 Decorator

image.png

UndecoratedStorage

UndecoratedStorage 只使用了传入的 storagebackend.Config 参数

image.png

直接调用 factory.Create 创建后端存储

image.png

[3] API Server


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK