前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >K8s源码分析(13)-资源的服务层接口定义

K8s源码分析(13)-资源的服务层接口定义

作者头像
TA码字
发布2022-02-25 09:32:12
4270
发布2022-02-25 09:32:12
举报
文章被收录于专栏:TA码字TA码字

上一篇文章中,我们主要介绍了 kubernetes 资源的数据访问层, 包括接口以及相关的 etcd3 数据访问层实现,支持 dry run 的数据访问层实现。在本篇文章里, 我们主要来介绍资源的服务层接口。

在数据服务层的设计上,也是秉承接口和实现的原则,定义接口功能,由相关的具体实现类来实现功能。kubernetes 在服务层上定义分为两大类,一类是增删改查类接口,定义增删改查 watch 等操作。另一类是操作的策略类型接口,用来定义资源在增删改查等不同操作中的逻辑。

对于增删改查类接口定义,图解和源码如下:

代码语言:javascript
复制
// staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go
type Storage interface {
    New() runtime.Object
}

type Scoper interface {
  NamespaceScoped() bool
}
type Getter interface {
  Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error)
}
type Lister interface {
  NewList() runtime.Object
  List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error)
  TableConvertor
}
type Watcher interface {
  Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error)
}
type Creater interface {
  New() runtime.Object
  Create(ctx context.Context, obj runtime.Object, createValidation ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error)
}
type Updater interface {
  New() runtime.Object
  Update(ctx context.Context, name string, objInfo UpdatedObjectInfo, createValidation ValidateObjectFunc, updateValidation ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error)
}
type CreaterUpdater interface {
  Creater
  Update(ctx context.Context, name string, objInfo UpdatedObjectInfo, createValidation ValidateObjectFunc, updateValidation ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error)
}
type GracefulDeleter interface {
  Delete(ctx context.Context, name string, deleteValidation ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error)
}
type CollectionDeleter interface {
  DeleteCollection(ctx context.Context, deleteValidation ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error)
}
type StandardStorage interface {
  Getter
  Lister
  CreaterUpdater
  GracefulDeleter
  CollectionDeleter
  Watcher
}
  • 对于不同的增删改查以及 watch 操作均有不同类型的独立接口与之对应定义,例如 Storage, Getter, Lister, Creater, Updater, Watcher 等等。
  • 有聚合类型接口 StandardStorage 封装了一些独立接口功能。

对于操作类策略接口定义,图解和源码如下:

代码语言:javascript
复制
// k8s.io/apiserver/pkg/registry/rest/create.go
type RESTCreateStrategy interface {
  runtime.ObjectTyper
  names.NameGenerator
  NamespaceScoped() bool
  PrepareForCreate(ctx context.Context, obj runtime.Object)
  Validate(ctx context.Context, obj runtime.Object) field.ErrorList
  Canonicalize(obj runtime.Object)
}
// k8s.io/apiserver/pkg/registry/rest/update.go
type RESTUpdateStrategy interface {
  runtime.ObjectTyper
  NamespaceScoped() bool
  AllowCreateOnUpdate() bool
  PrepareForUpdate(ctx context.Context, obj, old runtime.Object)
  ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList
  Canonicalize(obj runtime.Object)
  AllowUnconditionalUpdate() bool
}
// k8s.io/apiserver/pkg/registry/rest/create_update.go
type RESTCreateUpdateStrategy interface {
  RESTCreateStrategy
  AllowCreateOnUpdate() bool
  PrepareForUpdate(ctx context.Context, obj, old runtime.Object)
  ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList
  AllowUnconditionalUpdate() bool
}
// k8s.io/apiserver/pkg/registry/rest/delete.go
type RESTDeleteStrategy interface {
  runtime.ObjectTyper
}
  • 操作策略类会定义创建操作策略,更新操作策略,创建或更新操作策略,删除操作策略等独立接口,这些定义都涉及到了资源的持久化操作。
  • 策略类接口被定义在不同文件中, 例如 create.go/update.go/create_update.go/delete.go 等等。

目前先我们写到这里,在下一篇文章中我们继续来介绍资源数据服务层接口的实现。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TA码字 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档