前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【K8s源码品读】007:Phase 1 - kube-apiserver - Pod数据的保存

【K8s源码品读】007:Phase 1 - kube-apiserver - Pod数据的保存

作者头像
junedayday
发布2021-08-05 11:50:03
5190
发布2021-08-05 11:50:03
举报
文章被收录于专栏:Go编程点滴

聚焦目标

理解Pod发送到kube-apiserver后是怎么保存的

目录

  1. RESTCreateStrategy创建的预处理
  2. REST Pod数据的存储
  3. 存储的底层实现
  4. kube-apiserver第一阶段源码阅读总结

RESTCreateStrategy

代码语言:javascript
复制
// podStrategy 是封装了 Pod 的各类动作,这里我们先关注create这个操作
type podStrategy struct {
 runtime.ObjectTyper
 names.NameGenerator
}

// podStrategy 的接口
type RESTCreateStrategy interface {
 runtime.ObjectTyper
 names.NameGenerator
  // 是否属于当前的 namespace
 NamespaceScoped() bool
  // 准备创建前的检查
 PrepareForCreate(ctx context.Context, obj runtime.Object)
  // 验证资源对象
 Validate(ctx context.Context, obj runtime.Object) field.ErrorList
  // 规范化
 Canonicalize(obj runtime.Object)
}

// 完成了检查,我们就要保存数据了

Storage

代码语言:javascript
复制
// PodStorage 是 Pod 存储的实现,里面包含了多个存储的定义
type PodStorage struct {
  // REST implements a RESTStorage for pods
 Pod                 *REST
  // BindingREST implements the REST endpoint for binding pods to nodes when etcd is in use.
 Binding             *BindingREST
  // LegacyBindingREST implements the REST endpoint for binding pods to nodes when etcd is in use.
 LegacyBinding       *LegacyBindingREST
 Eviction            *EvictionREST
  // StatusREST implements the REST endpoint for changing the status of a pod.
 Status              *StatusREST
  // EphemeralContainersREST implements the REST endpoint for adding EphemeralContainers
 EphemeralContainers *EphemeralContainersREST
 Log                 *podrest.LogREST
 Proxy               *podrest.ProxyREST
 Exec                *podrest.ExecREST
 Attach              *podrest.AttachREST
 PortForward         *podrest.PortForwardREST
}

/*
从上一节的map关系中,保存在REST中
restStorageMap := map[string]rest.Storage{
  "pods":             podStorage.Pod,
}
*/
type REST struct {
 *genericregistry.Store
 proxyTransport http.RoundTripper
}

// Store是一个通用的数据结构
type Store struct {
 // Storage定义
 Storage DryRunnableStorage
}

// DryRunnableStorage中的Storage是一个Interface
type DryRunnableStorage struct {
 Storage storage.Interface
 Codec   runtime.Codec
}

func (s *DryRunnableStorage) Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64, dryRun bool) error {
 if dryRun {
  if err := s.Storage.Get(ctx, key, storage.GetOptions{}, out); err == nil {
   return storage.NewKeyExistsError(key, 0)
  }
  return s.copyInto(obj, out)
 }
  // 这里,就是Create的真正调用
 return s.Storage.Create(ctx, key, obj, out, ttl)
}

Storage Implement

代码语言:javascript
复制
// Storage Interface 的定义,包括基本的增删改查,以及watch等等进阶操作
type Interface interface {
 Versioner() Versioner
 Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error
 Delete(ctx context.Context, key string, out runtime.Object, preconditions *Preconditions, validateDeletion ValidateObjectFunc) error
 Watch(ctx context.Context, key string, opts ListOptions) (watch.Interface, error)
 WatchList(ctx context.Context, key string, opts ListOptions) (watch.Interface, error)
 Get(ctx context.Context, key string, opts GetOptions, objPtr runtime.Object) error
 GetToList(ctx context.Context, key string, opts ListOptions, listObj runtime.Object) error
 List(ctx context.Context, key string, opts ListOptions, listObj runtime.Object) error
 GuaranteedUpdate(
  ctx context.Context, key string, ptrToType runtime.Object, ignoreNotFound bool,
  precondtions *Preconditions, tryUpdate UpdateFunc, suggestion ...runtime.Object) error
 Count(key string) (int64, error)
}

func NewRawStorage(config *storagebackend.Config) (storage.Interface, factory.DestroyFunc, error) {
 return factory.Create(*config)
}

func Create(c storagebackend.Config) (storage.Interface, DestroyFunc, error) {
 switch c.Type {
  // 已经不支持etcd2
 case "etcd2":
  return nil, nil, fmt.Errorf("%v is no longer a supported storage backend", c.Type)
  // 默认为etcd3版本
 case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3:
  return newETCD3Storage(c)
 default:
  return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type)
 }
}

Summary

我们对第一阶段学习kube-apiserver的知识点进行总结:

  1. kube-apiserver 包含三个apiserverAPIExtensionsServerKubeAPIServerAggregatorServer
    1. 三个APIServer底层均依赖通用的GenericServer,使用go-restful对外提供RESTful风格的API服务
  2. kube-apiserver 对请求进行 AuthenticationAuthorizationAdmission三层验证
  3. 完成验证后,请求会根据路由规则,触发到对应资源的handler,主要包括数据的预处理保存
  4. kube-apiserver 的底层存储为etcd v3,它被抽象为一种RESTStorage,使请求和存储操作一一对应
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Go编程点滴 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 聚焦目标
    • 目录
      • RESTCreateStrategy
        • Storage
          • Storage Implement
            • Summary
            相关产品与服务
            对象存储
            对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档