上一篇文章中,我们主要介绍了 kubernetes 中资源增删改查类接口的实现。在本篇文章里, 我们继续来介绍服务类接口的实现,包括操作策略类接口以及其它的类型实现。
这里我们以常见的资源 daemonset 为例介绍操作策略类的具体实现。一般资源的操作策略类实现定义在 {group}/{kind}/strategy.go 中
// 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){...}
对于任何资源,同样会定义具体的数据服务类,还是以 daemonset 为例子,其数据服务类定义在 {group}/{kind}/storage/storage.go
// pkg/registry/apps/daemonset/storage/storage.go
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
}
目前先我们写到这里,在下一篇文章中我们继续来介绍 kubernetes API 的注册。