栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

上一篇文章中,我们主要介绍了 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){...}

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

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

对于任何资源,同样会定义具体的数据服务类,还是以 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
}

${group}.${kind}.storage.storage.REST 结构体中定义了资源的数据服务类。 

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

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/729419.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号