This article QQ space link: http://user.qzone.qq.com/29185807/blog/1461036130
This article links to csdn blog: http://blog.csdn.net/screscent/article/details/51188790
Source code for K8s v1.1.1
1. Reason
First, let's talk about why, we're going to explain factory.
Code in K8S.IO\KUBERNETES\CMD\KUBECTL
First, from the main function portal,
The main function is simple, in which a CMD is built directly, and then the Execute is invoked
and cmd inside the parameters in addition to several standard input and output, there is only one newfactory
Below we continue to look newkubectlcommand, in the function, constructs a lot of CMD's, each cmd incoming parameter is also factory
Code in K8s.io\kubernetes\pkg\kubectl\cmd
Let's simply look at a newcmdcreate
Obviously, the most important function in Run is runcreate.
And in Runcreate, all the important parameters are factory output.
So before we analyze the Kubectl command, we'll analyze the factory.
2, Build
Code in K8s.io\kubernetes\pkg\kubectl\cmd\util
Here are the comments in the source code
Factory provides abstractions that allow "Kubectl command to" extended "across multiple types of resources and Differe NT API sets.
The main implication is that factory provides a number of abstract actions that are intended to be used for KUBECTL command to extend many different types and different APIs (English translation is not good, roughly this meaning)
In factory, a lot of function objects are provided
We see in the main function, which calls the newfactory to build the factory
function is longer, we analyze it slowly.
The code is too long, not all posted. Here are some important things to do for detailed analysis
2.1 Mapper
Mapper: = Kubectl. Shortcutexpander{restmapper:api. Restmapper}
Let's see.
Code in K8s.io\kubernetes\pkg\kubectl\ kubectl.go
Here the Shortcutexpander is actually meta.restmapper to do a layer of encapsulation, and we last article ("original" k8s source code Analysis-----KUBECTL (1) API. Restmapper) has already explained to restmapper in detail, will not elaborate
2.2 Generator
5 types of generator were built
Code in K8s.io\kubernetes\pkg\kubectl\generator.go
Generator is to construct the relative API object based on the input parameters.
2.2.1 Basicreplicationcontroller
"Run/v1" corresponds to Basicreplicationcontroller, so let's take a look at this.
An empty struct, but the function Paramnames provides the parameters required for the Replicationcontroller
Now let's look at building
Generate function is relatively long, here pasted several pictures, just pasted up. From the above function flow, we look for the API from the input parameters. Replicationcontroller the required parameters and finally built into an API. Replicationcontroller object, returning it to the
2.2.2 Basicpod
"RUN-POD/V1": the corresponding is Basicpod
Above is the parameters needed to build the pod
Here we look at the generate, the function is too long, not all screenshots. Key part screenshots out to see
Func (basicpod) Generate (GenericParams map[string]interface{}) (Runtime. Object, error) {
Find the relevant input parameters, and then build an API. Pod
2.2.3 Servicegenerator
"SERVICE/V1": the corresponding is ServiceGeneratorV1