k8s
中用到一个hyperkube
模块,其功能如下:
// Package hyperkube is a framework for kubernetes server components. It
// allows us to combine all of the kubernetes server components into a single
// binary where the user selects which components to run in any individual
// process.
//
// Currently, only one server component can be run at once. As such there is
// no need to harmonize flags or identify logs across the various servers. In
// the future we will support launching and running many servers — either by
// managing processes or running in-proc.
//
// This package is inspired by https://github.com/spf13/cobra. However, as
// the eventual goal is to run multiple servers from one call, a new package
// was needed.
通俗地讲,hyperkube
模块就是把各种功能集成到一个可执行文件,然后在运行时指定模块。比如km
程序就用到了hyperkube
:
$ km --help
This is an all-in-one binary that can run any of the various Kubernetes-Mesos
servers.
Usage
km <server> [flags]
Servers
apiserver
The main API entrypoint and interface to the storage system. The API server
is also the focal point for all authorization decisions.
......
hyperkube
结构体定义:
type HyperKube struct {
Name string // The executable name, used for help and soft-link invocation
Long string // A long description of the binary. It will be world wrapped before output.
servers []Server
baseFlags *pflag.FlagSet
out io.Writer
helpFlagVal bool
}
其中用到的Server
结构体的定义:
// Server describes a server that this binary can morph into.
type Server struct {
SimpleUsage string // One line description of the server.
Long string // Longer free form description of the server
Run serverRunFunc // Run the server. This is not expected to return.
flags *pflag.FlagSet // Flags for the command (and all dependents)
name string
hk *HyperKube
}
参看km
程序的main
函数:
func main() {
hk := HyperKube{
Name: "km",
Long: "This is an all-in-one binary that can run any of the various Kubernetes-Mesos servers.",
}
hk.AddServer(NewKubeAPIServer())
hk.AddServer(NewControllerManager())
hk.AddServer(NewScheduler())
hk.AddServer(NewKubeletExecutor())
hk.AddServer(NewKubeProxy())
hk.AddServer(NewMinion())
hk.RunToExit(os.Args)
}
main
函数用到了hyperkube
的一个重要方法AddServer
:
// AddServer adds a server to the HyperKube object.
func (hk *HyperKube) AddServer(s *Server) {
hk.servers = append(hk.servers, *s)
hk.servers[len(hk.servers)-1].hk = hk
}
可以看到,在这个方法中,hk.servers[len(hk.servers)-1].hk = hk
可以让Server
结构体的hk
字段指向同一个binary
中的hyperkube
,这样就把这些功能集成到一起。 接下来调用hyperkube
的RunToExit
运行相应的功能。