参考
接口实现
1 | 资源的服务层接口实现(增删改查类)
上一篇文章中,我们主要介绍了 kubernetes 中资源的服务层接口定义,包括增删改查类接口,以及操作的策略类接口。在本篇文章里, 我们主要来介绍资源增删改查类接口的实现。
对于增删改查类接口实现,图解和源码如下:
- registry.store.Store 结构体实现了增删改查类型接口定义的函数。
- registry.store.Store 结构体中,以组合的方式封装了上一篇文章中介绍的对于资源操作策略类型的一系列相关属性,例如RESTCreateStrategy/RESTUpdateStrategy/RESTDeleteStrategy 等,用来完成不同操作的逻辑。
- registry.store.Store 结构体中,以组合的方式封装以前文章中介绍的数据访问层属性 DryRunnableStorage, 用来对后端存储的数据层访问。
// k8s.io/apiserver/pkg/registry/generic/registry/store.go type GenericStore interface { GetCreateStrategy() rest.RESTCreateStrategy GetUpdateStrategy() rest.RESTUpdateStrategy GetDeleteStrategy() rest.RESTDeleteStrategy } // 实现了增删改查类型接口定义的函数 type Store struct { NewFunc func() runtime.Object NewListFunc func() runtime.Object // 组合了一些操作策略 CreateStrategy rest.RESTCreateStrategy UpdateStrategy rest.RESTUpdateStrategy DeleteStrategy rest.RESTDeleteStrategy // 组合了 dry-run 属性 Storage DryRunnableStorage KeyRootFunc func(ctx context.Context) string KeyFunc func(ctx context.Context, name string) (string, error) ObjectNameFunc func(obj runtime.Object) (string, error) ...... } func (e *Store) New() runtime.Object{...} ==>Storage func (e *Store) NewList() runtime.Object{...}==>Lister func (e *Store) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {...} func (e *Store) NamespaceScoped() bool {...}==>Scoper func (e *Store) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error){...}==>Getter func (e *Store) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error)==>{...}Watcher func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error)==>{...}Creater func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error){...}==>Updater func (e *Store) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error){...} ==>GracefulDeleter func (e *Store) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error){...}==>CollectionDeleter func (e *Store) GetCreateStrategy() rest.RESTCreateStrategy{...} ==>GenericStore func (e *Store) GetUpdateStrategy() rest.RESTUpdateStrategy{...} func (e *Store) GetDeleteStrategy() rest.RESTDeleteStrategy{...}
2 | 资源的服务层策略接口实现
上一节主要介绍了 kubernetes 中资源增删改查类接口的实现。在本篇文章里, 我们继续来介绍服务类接口的实现,包括操作策略类接口以及其它的类型实现。
2.1 | 操作策略类(每种资源都要有自己的实现)
这里我们以常见的资源 daemonset 为例介绍操作策略类的具体实现。一般资源的操作策略类实现定义在${group}/${kind}/strategy.go
中
-
对于任何一种资源都有自己的操作策略类接口的具体实现,其一般会被定义在源码文件 ·、
${group}/${kind}/strategy
中。 -
在本例中由结构体 daemonSetStrategy 实现 daemonset 资源的操作策略类接口。
// kubernetes/blob/master/pkg/registry/apps/daemonset/strategy.go
type daemonSetStrategy struct {
runtime.ObjectTyper
names.NameGenerator
}
var Strategy = daemonSetStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
func (daemonSetStrategy) NamespaceScoped() bool{...}
func (daemonSetStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object){...}
func (daemonSetStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList{...}
func (daemonSetStrategy) Canonicalize(obj runtime.Object){...}
func (daemonSetStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList{...}
func (daemonSetStrategy) AllowCreateOnUpdate(){...}
func (daemonSetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object){...}
2.2 | 数据服务类(每种资源都要有自己的实现)
对于任何资源,同样会定义具体的数据服务类,还是以 daemonset 为例子,其数据服务类定义在${group}/${kind}/storage/storage.go
-
${group}.${kind}.storage.storage.REST
结构体中定义了资源的数据服务类(就是存储)。 -
app.daemonset.storage.storage.REST
这个结构体实现了 daemonset 资源的数据服务类。 -
一般会有 REST 结构体和 StatusREST 结构体来对应每种资源,分别用来提供资源的操作服务和状态服务。
-
在 REST 结构体和 StatusREST 结构体中会以组合的方式包含上一篇文章介绍的增删改查类接口的具体实例。
// pkg/registry/apps/daemonset/storage/storage.go
// 实现了 daemonset 资源的数据服务类
type REST struct {
*genericregistry.Store // 增删改查类接口
categories []string
}
type StatusREST struct {
store *genericregistry.Store // 增删改查类接口
}
func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &apps.DaemonSet{} },
NewListFunc: func() runtime.Object { return &apps.DaemonSetList{} },
DefaultQualifiedResource: apps.Resource("daemonsets"),
CreateStrategy: daemonset.Strategy,
UpdateStrategy: daemonset.Strategy,
DeleteStrategy: daemonset.Strategy,
ResetFieldsStrategy: daemonset.Strategy,
TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
}
options := &generic.StoreOptions{RESTOptions: optsGetter}
if err := store.CompleteWithOptions(options); err != nil {
return nil, nil, err
}
statusStore := *store
statusStore.UpdateStrategy = daemonset.StatusStrategy
statusStore.ResetFieldsStrategy = daemonset.StatusStrategy
return &REST{store, []string{"all"}}, &StatusREST{store: &statusStore}, nil
}