k8s :kube-apiserver RESTful API 實現 - Storage

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

前言

瞭解 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.gotype 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 podstype REST struct {    *genericregistry.Store    proxyTransport http.RoundTripper}// BindingREST implements the REST endpoint for binding pods to nodes when etcd is in usetype 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.gofunc (c LegacyRESTStorageProvider) NewLegacyRESTStorage(...) {    ...    restStorageMap := map[string]rest.Storage {        "pods": podStorage.Pod,        "pods/attach": podStorage.Attach        ...    }}

後續再詳細分析

總結

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.