k8s :kube-apiserver RESTful API 實現 - Storage

前言

瞭解 k8s 的同學都知道,kube-apiserver 對外提供 RESTful API 接口提供 查詢,監聽集羣(資源)狀態的服務,kube-apiserver 主要就做一件事,就是如何將 RESTful API (CREATE, DELETE, UPDATE, GET .etc)接口調用映射到對後端存儲(比如 etcd)的(增刪改查)訪問,在設計的時候考慮到 k8s 是個快速迭代的開源項目,很多 API 接口(版本)可能在未來版本發生變化,因此如何設計一個擴展性強,耦合度低的架構應該是 Google 那幫貨當初主要考慮的問題,所以才導致 kube-apiserver 本來相比 kube-scheduler 和 kube-controller-manager 應該簡單的代碼設計的巨複雜(個人觀點)~

從 kube-apiserver 收到 RESTful API 請求到從 後端存儲中獲取(更新 .etc)到數據大概需要經過一下幾層(非官方命名),各層之間通過 《接口》 交互(解偶)

RESTful API
||
<REST Operation Interface>
||
Storage
||
<Storage Backend Interface>
||
Sotrage Backend(etcd2,etcd3)

比如 Storage 和 Storage Backend 之間通過 Storage Backend Interface(參考k8s :kube-apiserver 訪問 etcd 後端存儲)交互,Storage 和 RESTful API 之間通過 REST Operation Interface(增刪改查 方法的封裝)交互

Storage

Storage is a generic interface for RESTful storage services.
Resources which are exported to the RESTful API of apiserver need to implement this interface(原文註釋,下同)
It is expected that objects may implement any of the below interfaces
所有想通過 RESTful API 暴露出去的資源都必須實現 Storage 接口,Storage 接口是個最小接口(單一職責),資源類可以根據自身情況實現其它各種接口

// kubernetes/staging/src/k8s.io/apiserver/pkg/registry/rest/rest.go
type Storage interface {
 New() runtime.Object
}

REST Operation Interface

StandardStorage is an interface covering the common verbs. Provided for testing whether a resource satisfies the normal storage methods.
Use Storage when passing opaque storage objects
StandardStorage

type StandardStorage interface {
 Getter Lister GreaterUpdater GracefulDeleter CollectionDeleter Watcher
}

StandardStorage 聚合了可以對 Storage 施加的操作(或者叫 Verb,動作),RESTful API根據該(子)接口測試 Storage 是否支持相關操作,然後註冊相應的 API 接口,比如如果 Storage 支持 Delete 接口,就註冊一個 HTTP method 爲 DELETE 的方法到相應的資源路徑

Storage 實現類

kubernetes/pkg/registry/core 目錄下包含了各種 Storage 實現類,比如大家耳熟能詳的 pod, service, endpoint, configmap, node 等等,各個資源的目錄結構很相似,以 pod 爲例

kubernetes/pkg/registry/core/pod
 rest
 storage
 storage.go <- Storage 實現
 doc.go
 strategy.go
 strategy_test.go

PodStorage

我們以 pod storage 爲例來分析 storage 創建,首先是 pod storage 定義

type PodStorage struct {
 Pod *REST Binding *BindingREST Eviction *EvictionREST Status *StatusREST Log *podrest.LogREST Proxy *podrest.ProxyREST Exec *podrest.ExecREST Attach *podrest.AttachREST PortForward *podrest.PortForwardREST
}

這裏又冒出一些新的類型 REST,BindingREST .etc,這些 XXXREST 纔是"真正"的 Storage,對應具體的 RESTful endpoint

// REST implements a RESTStorage for pods type REST struct {
 *genericregistry.Store
 proxyTransport http.RoundTripper
}

// BindingREST implements the REST endpoint for binding pods to nodes when etcd is in use type BindingREST struct {
 store *genericregistry.Store
}

XXXREST 類類包含一個 genericregistry.Store 類型的字段,我們在k8s :kube-apiserver 訪問 etcd 後端存儲中分析過,它用於訪問後端存儲

PodStorage 通過 NewStorage 方法創建,各個 XXXREST 共享 Store

func NewStorage(optsGetter generic.RESTOptionsGetter, ...) {
 創建 genericregistry.Store
 store := &genericregistry.Store {
 ...
 }
 ...
 return PodStorage {
 Pod: &REST{store, proxyTransport},
 Binding: &BindingREST{store: store}
 ...
 }
}

Storage 註冊

Storage 是如何"綁定"到 api 接口呢?這中間還涉及到一些數據結構(類),這裏先列出綁定相關的代碼:

// kubernetes/pkg/registry/core/rest/storage_core.go func (c LegacyRESTStorageProvider) NewLegacyRESTStorage(...) {
 ...
 restStorageMap := map[string]rest.Storage {
 "pods": podStorage.Pod,
 "pods/attach": podStorage.Attach
 ...
 }
}

後續再詳細分析

總結

本文介紹了 kube-apiserver 中 Storage 相關的一些概念,希望對大家閱讀 k8s 源代碼有所幫助

本文轉自中文社區-k8s :kube-apiserver RESTful API 實現 - Storage

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章