This is a creation in Article, where the information may have evolved or changed.
Objective
This article describes how Kube-apiserver accesses the ETCD back-end storage
Related source code mainly in Kubernetes/staging/src/k8s.io/apiserver/pkg/storage
Universal interface
Interface offers a common Interface for object marshaling/unmarshaling operations and hides all the storage-related Operat Ions behind it (original note)
// kubernetes/vendor/k8s.io/apiserver/storage/interfaces.gotype Interface interface { Versioner() Versioner Create(...) Delete(...) Watch(...) WatchList(...) Get(...) GetToList(...) List(...) GuaranteedUpdate(...)}
Interface defines a common interface for back-end storage, mainly a number of "add and subtract" methods, this interface-oriented programming, the implementation and design of the separation of design to improve the software scalability, reduce the coupling between modules, such as as long as we provide Interface specific implementation, then in addition to the use of Etcd as back-end storage, is it possible to use distributed KV storage such as Consul/zookeeper?
Specific implementation
Interface interface currently has two specific implementation classes, respectively, corresponding to ETCD v2 and ETCD v3 two different ETCD API versions
// kubernetes/vendor/k8s.io/apiserver/pkg/storage/etcd/etcd_helper.go type etcdHelper struct { ...}// kubernetes/vender/k8s.io/apiserver/pkg/storage/etcd3/store.gotype store struct { ...}
The names of the two implementation classes are too big ... The go language uses non-intrusive interfaces, so it is not visible from the class (struct) names that they are two and storage. Interface have a half-wool relationship, but as long as they achieve the storate. Interface interface declaration Method they are "is-a" storage. Interface
Create
The factory method in the factory package Create will be storagebackend according to the configuration. Config to create the appropriate storage. Interface
func Create(c storagebackend.Config) (storage.Interface, DestroyFunc, error) { swtich c.Type { case storagebackend.StorageTypeETCD2: return newETCD2Storage(c) case storagebackend.StorageTypeUnset, storageback.StorageTypeETCD3: return newETCD3Storage(c) default: return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type) }}
Use
Storage. Interface is the Store class (what is this?) ) is used to implement rest. Standardstorage (RESTful additions and deletions) interface
// kubernetes/vendor/k8s.io/apiserver/pkg/registry/generic/registry/store.gotype Store struct { ... Storage storage.Interface}// Create inserts a new item according to the unique key from the objectfunc (e *Store) Create(ctx genericapirequest.Context, obj runtime.Object, includeUninitialized bool)(runtime.Object, error) { ... key, err := e.KeyFunc(ctx, name) ... out := e.NewFunc() if err := e.Storage.Create(ctx, key, obj, out, ttl); err != nil { ... } ...}
Summarize
This article describes the classes (structures) and methods used in Kube-apiserver access to ETCD backend storage, the design patterns to use, and the architectural routines