K8s源码分析(15)-资源的服务层策略接口实现

上一篇文章中,我们主要介绍了 kubernetes 中资源增删改查类接口的实现。在本篇文章里, 我们继续来介绍服务类接口的实现,包括操作策略类接口以及其它的类型实现。

这里我们以常见的资源 daemonset 为例介绍操作策略类的具体实现。一般资源的操作策略类实现定义在 ${group}/${kind}/strategy.go 中

e4b117498d2b0961210d2dc4f890ae2e.png

303528a5245624259d745ad9ccd28a5b.png

//  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){...}
  • 对于任何一种资源都有自己的操作策略类接口的具体实现,其一般会被定义在源码文件 ${group}/${kind}/strategy 中。

  • 在本例中由结构体 daemonSetStrategy 实现 daemonset 资源的操作策略类接口。

对于任何资源,同样会定义具体的数据服务类,还是以 daemonset 为例子,其数据服务类定义在 ${group}/${kind}/storage/storage.go

56ea58e8913e3636ff72c5506ae4d90b.png

13a5571620facd21dbbcd6daa2c5185a.png

// 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
}
  • ${group}.${kind}.storage.storage.REST 结构体中定义了资源的数据服务类。 

  • app.daemonset.storage.storage.REST 这个结构体实现了 daemonset 资源的数据服务类。

  • 一般会有 REST 结构体和 StatusREST 结构体来对应每种资源,分别用来提供资源的操作服务和状态服务。

  • 在 REST 结构体和 StatusREST 结构体中会以组合的方式包含上一篇文章介绍的增删改查类接口的具体实例。

目前先我们写到这里,在下一篇文章中我们继续来介绍 kubernetes API 的注册。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值