This is a creation in Article, where the information may have evolved or changed.
Objective
Know K8s's classmates know, Kube-apiserver provide RESTful API interface to provide query, monitor the status of cluster (resource) services, Kube-apiserver mainly do one thing, is how to put the RESTful API (CREATE, DELE TE, UPDATE, GET. etc) interface calls are mapped to back-end storage (such as ETCD) access, when designed to consider that k8s is a fast iteration of open source projects, many API interfaces (versions) may change in future versions, so how to design a strong extensibility, Low coupling architecture should be the first major consideration of Google's goods, so that led to Kube-apiserver compared to Kube-scheduler and Kube-controller-manager should be simple code design of the huge complex (personal view) ~
Receive a RESTful API request from Kube-apiserver to get (update. etc) from the back-end store to the data it probably takes a few layers (unofficially named) to interact between the layers via interface (Zhei)
RESTful API
||
<rest Operation Interface>
||
Storage
||
<storage Backend Interface>
||
Sotrage Backend (ETCD2,ETCD3)
For example, between Storage and Storage backend through Storage backend Interface (refer to K8s:kube-apiserver access ETCD back-end storage), Storage and RESTful APIs Interaction via REST Operation Interface (encapsulation of the add-and-scan method)
Storage
Storage is a generic interface for RESTful Storage services.
Resources which is exported to the RESTful API of Apiserver need to implement this interface (original note, hereinafter)
It is expected this objects may implement any of the below interfaces
All resources that want to be exposed through the RESTful API must implement the Storage interface, the Storage interface is a minimal interface (single duty), and the resource class can implement various other interfaces according to its own situation.
// 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 aggregates the actions that can be imposed on Storage (or called Verb, actions), and the RESTful API tests whether the Storage supports related operations based on the (sub) interface, and then registers the appropriate API interfaces, such as if Storage supports the De Lete interface, register an HTTP method for DELETE to the appropriate resource path
Storage Implementation Class
Kubernetes/pkg/registry/core directory contains a variety of Storage implementation classes, such as people familiar with the pod, service, endpoint, CONFIGMAP, node, etc., the directory structure of the various resources are very similar to Pod as an example
kubernetes/pkg/registry/core/pod rest storage storage.go <- Storage 实现 doc.go strategy.go strategy_test.go
Podstorage
We use pod storage as an example to analyze storage creation, first the pod storage definition
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}
There are some new types of rest,bindingrest. etc, these xxxrest are "real" Storage, corresponding to the specific 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}
The Xxxrest class class contains a genericregistry. Store Type field, which we have analyzed in k8s:kube-apiserver access ETCD back-end storage, which is used to access back-end storage
Podstorage created by Newstorage method, each xxxrest shared Store
func NewStorage(optsGetter generic.RESTOptionsGetter, ...) { 创建 genericregistry.Store store := &genericregistry.Store { ... } ... return PodStorage { Pod: &REST{store, proxyTransport}, Binding: &BindingREST{store: store} ... }}
Storage Registration
How does Storage "bind" to API interfaces? It also involves some data structures (classes), where the binding-related code is listed first:
// kubernetes/pkg/registry/core/rest/storage_core.gofunc (c LegacyRESTStorageProvider) NewLegacyRESTStorage(...) { ... restStorageMap := map[string]rest.Storage { "pods": podStorage.Pod, "pods/attach": podStorage.Attach ... }}
Follow-up and detailed analysis
Summarize
This paper introduces some concepts related to Storage in Kube-apiserver, hoping to help you read k8s source code.