K8s Source Analysis------Third Party library ETCD client Analysis

Source: Internet
Author: User
Tags etcd k8s
This blog from my space reproduced over, http://user.qzone.qq.com/29185807/blog/1458542207

Code is GITHUB.COM/COREOS/GO-ETCD/ETCD

Note: This version is the ETCD client used in k8s v1.1.1.

First I look at how the next k8s is used. Location in K8s.io\kubernetes\cmd\kube-apiserver\app\server.go


Code directory, from this point of view, the code is very clear



So we're going to go in here, see Client.go.

First look at the structural body


Inside the structure, the more important things

1, cluster, cluster management

2, HttpClient, this will be the final entrance.

We continue to



This is mainly the initialization of the Cluster,httpclient

Which cluster back in detail, here we continue to see HttpClient



It's clear that this is where the client in a standard library net/http is constructed.

The approximate initialization is simple. There is actually an initialization of TLS. For Newtlsclient, the corresponding httpclient is initialized to Inithttpsclient

Let's take a look at what the ETCD client has provided those operations.

Get operation

Let's see Get.go.



Obviously, we saw that get called Rawget, and Rawget called the Get

We're going to track get

In the Request.go



Keep Getcancelable.

or in a request.go.



Mainly have newrawrequest. In Request.go

In fact, some of the packaging of request, including some parameters



We continue to watch SendRequest. This is the core of the entire code.



Here we see that when the cancel in Rawrequest is not nil, a coprocessor is initiated to receive the cancel message. But the current analysis of get is not set



We continue to have a for loop, attempt for the number of attempts, and, if successful, Exit For loop, and the failure will continue. which has a variable of httppath is a key



We're going to go in there and see Requet.go.



We've seen cluster, and we're going to go in and see.



From the above cluster code to see in fact pick is to return to the cluster in a host, and pick machine is a random host, in the initialization of a random, in the failure time, will be again random

We go back to the sendrequest.



The main place here is to construct a request in a standard library net/http

and continue



We see the httpclient we mentioned earlier, receive the request just built, and do. is to initiate an HTTP request.



Check the head and, if it is normal, exit the For loop

After exiting the For loop, a rawresponse structure is returned

Finally we return to



We saw the results.

is actually a decoding process.


The get operation is resolved

There are also some operations such as:

Addchilddir

In fact, tracking in, and finally to the SendRequest function


There are other operations that are not introduced, and the process of its code is basically consistent.

Hao Haohua

QQ Crescent moon Lonely Road length 29185807

March 21, 2016 (Copyright: This article is original for the author, if you need to reprint, please notify me, and indicate the source and author. Reproduced without authorization, reserve the right to pursue its infringement. )

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.