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

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

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

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

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

代码语言:javascript
复制
//  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

代码语言:javascript
复制
// 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 的注册。

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

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

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

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

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