K8s:kube-apiserver Legacy API Initialization

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Objective

See K8s source code for a period of time, the total feeling in the maze, sometimes feel that finally found the exit, and suddenly went to the wall, summed down or their internal strength is not deep enough, this article is Kube-apiserver Legacy (legacy, will be discarded) API The initialization process (as well as the data structure) of a comb, is to make a "road sign", so that later in the "Maze" can also find the way back

Kube-apiserver main function is to provide API interface to the client access back-end ETCD storage, of course, this is not only a simple key/value storage, in order to facilitate the expansion, Kube-apiserver designed a set of code framework to map "resource objects" to Restfu L API

This article has combed through the API initialization processes and related data structures that legacy API is/API.

K8s code update is faster, this article is based on k8s release-1.9.x code

Data

Kube-apiserver initiating process-related (primary) data structures

    • Xxxoptions classes related to command line arguments
    • Xxxconfig configuration-related classes
    • Xxxserver Kube-apiserver Service Object
    • Xxxstorage resource objects, such as Posstorage, operate back-end storage via Xxxstorage (ETCD)

Options,config (approximate) relationship of the Server object:
Create a config object from the Options object, and then create the Server object from the Config object

Serverrunoptions

The Serverrunoptions class encapsulates the kube-apiserver command-line arguments, which are grouped by different categories

// kubernetes/cmd/kube-apiserver/app/options/options.goimport (    ...    genericoptions "k8s.io/apiserver/pkg/server/options"    ...)type ServerRunOptions struct {    GenericServerRunOptions *genericoptions.ServerRunOptions    Etcd                    *genericoptions.EtcdOptions    SecureServing           *genericoptions.SecureServingOptions    InsecureServing         *kubeoptions.InsecureServingOptions    ...}

Let's focus on the ETCDOPTIONS,ETCD storage-related configuration, which is closely related to how kube-apiserver accesses back-end ETCD storage

Etcdoptions

Storageconfig defines ETCD detailed configuration, such as ETCD version, key public prefix, etc.

// kubernetes/vendor/k8s.io/apiserver/pkg/server/options/etcd.gotype EtcdOptions struct {    StorageConfig storagebackend.Config    EncryptionProviderConfigFilepath string    ...}// kubernetes/vender/k8s.io/apiserver/storage/storagebackend/config.otype Config struct {    // Type defines the type of storage backend, e.g. "etcd2", "etcd3"    Type string    // Prefix is the prefix to all keys passed to storage.Interface methods.    Prefix string    // ServerList is the list of storage servers to connect with    ...}

Create

Factory method Newetcdoptions used to create etcdoptions

// kubernetes/cmd/kube-apiserver/app/options/options.gofunc NewServerRunOptions() *ServerRunOptions {    s := ServerRunOptions {        ...        Etcd: genericoptions.NewEtcdOptions(            storagebackend.NewDefaultConfig(kubeoptions.DefaultEtcdPathPrefix, nil))        ...    }}

Applywithstoragefactoryto

Etcdoptions has an important method Applywithstoragefactoryto used to initialize the server. The Restoptionsgetter property of the Config (Service configuration class, see below), which is used to create the ETCD back-end storage (see below)

Kubernetes/vendor/k8s.io/apiserver/pkg/server/options/etcd.go
Func (S *etcdoptions) Applywithstoragefactoryto (

factory server storage.StorageFactory, c *server.Config) error {s.RESTOptionsGetter = &storageFactoryRestOptionsFactory{    Options: *s,    StorageFactory: factory}

}

// kubernetes/pkg/kubeapiserver/options/storage_versions.goconst (    DefaultEtcdPathPrefix = "/registry")

Genericapiserver

Genericapiserver contains state for a Kubernetes Cluster API server

type GenericAPIServer struct {    ...}

Master

Master contains state for a Kubernetes cluster MASTER/API server

type Master struct {    GenericAPIServer *genericapiserver.GenericAPIServer    ClientCARegistrationHook ClientCARegistrationHook}

Process

Each k8s service or utility has a corresponding folder in the Kubernetes/cmd directory with the entry of these services or utilities, which, by the way, can be built separately by a command kube-apiserver

# export GOPATH=/opt/kubernetes-src/# cd $GOPATH/src/k8s.io/kubernetes# make WHAT=cmd/kube-apiserver all

Kube-apiserver's entrance (main function, main package)

// kubernetes/cmd/kube-apiserver/apiserver.gofunc main() {    rand.Seed(time.Now().UTC().UnixNano())    // 解析命令行参数    s := options.NewServerRunOptions()    s.AddFlags(pflag.CommandLine)    flag.InitFlags()    // 初始化 log    logs.InitLogs()    defer logs.FlushLogs()    ...    stopCh := server.SetupSignalHandler()    if err := app.Run(s, stopCh); err != nil {        fmt.FPrintf(os.Stderr, "%v\n", err)        os.Exit(1)    }}

The main function is just a shell, and the run function in the app package is called after parsing the command-line arguments

// kubernetes/cmd/kube-apiserver/app.server.gofunc Run(runOptions *options.ServerRunOptions, stopCh <-chan struct{}) error {    ...    server, err := CreateServerChain(runOptions, stopCh)    if err != nil {        return err    }    return server.PrepareRun().Run(stopCh)}

The Createserverchain function uses the responsibility chain design pattern, and multiple servers make up a List to handle client requests, and the run function finally calls the Server's Preparerun and run methods to start the Kube-apiserver service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.