frakti && RunPodSandbox 源码分析
listen = flag.String("listen", "/var/run/frakti.sock", "...")
hyperEndpoint = flag.String("hyper-endpoint", "127.0.0.1:8080", "...")
streamingServerPort = flag.String("streaming-server-port", "22521", "...")
streamingServerAddress = flag.String("streaming-server-addr", "0.0.0.0", "...")
// cmd/frakti/frakti.go
1、func main()
(0)、调用streamingConfig := getStreamingConfig() --->用streaming.DefaultConfig的内容填充获得一个streaming.Config结构
(1)、调用hyperRuntime, streamingServer, err := hyper.NewHyperRuntime(*hyperendpoint, streamingConfig),相当于生成一个hyper client
(2)、调用server, err := manager.NewFraktiManager(hyperRuntime, hyperRuntime, streamingServer)
(3)、最后调用fmt.Println(server.Server(*listen))
// Runtime is the HyperContainer implementation of kubelet runtime API type Runtime struct { client *Client }
// pkg/hyper/hyper.go
// NewHyperRuntime creates a new Runtime
2、func NewHyperRuntime(hyperEndpoint string) (*Runtime, error)
(1)、调用hyperClient, err := NewClient(hyperEndpoint, hyperConnectionTimeout)
// Client is the gRPC client for hyperd type Client struct { client api.PublicAPIClient timeout time.Duration }
(2)、调用streamingRuntime := &streamingRuntime{client: hyperClient}
(3)、如果streamingConfig不为nil,调用streamingServer, err = streaming.NewServer(*streamingConfig, streamingRuntime)
(4)、最后return &Runtime{client: hyperClient, streamingServer: streamingServer}, streamingServer, nil
// pkg/hyper/client.go
// NewClient creates a new hyper client
3、func NewClient(server string, timeout time.Duration) (*Client, error)
调用conn, err := grpc.Dial(server, grpc.WithInsecure()),再返回&Client{client: api.NewPublicAPIClient(conn), timeout: timeout}
// FraktiManager serves the kubelet runtime gRPC api which will be consumed by kubelet type FraktiManager struct { // The grpc server server *grpc.Server
// The streaming server
streamingServer streaming.Server
runtimeService runtime.RuntimeService imageService runtime.ImageService }
// pkg/manager/manager.go
// NewFraktiManager creates a new FraktiManager
func NewFraktiManager(runtimeService runtime.RuntimeService, imageService runtime.ImageService) (*FraktiManager, error)
创建 s := &FraktiManager{server: grpc.NewServer(), runtimeService: runtimeService, imageService: imageService},再调用s.registerServer(),最后return s, nil
// pkg/manager/manager.go
func (s *FraktiManager) registerServer()
调用kubeapi.RegisterRuntimeServiceServer(s.server, s)和kubeapi.RegisterImageServiceServer(s.server, s)
// pkg/manager/manager.go
// Server starts gRPC server at unix://addr
func (s *FraktiManager) Serve(addr string) error
(1)、调用syscall.Unlink(addr)
(2)、如果s.streamingServer不为nil,调用s.streamingServer.Start(true)
(3)、调用lis, err := net.Listen("unix", addr),最后return s.server.Serve(lis)
--------------------------------------------------- RunPodSandbox 源码分析 -----------------------------------------
// RunPodSandbox creates and starts a pod-level sandbox.
func (h *Runtime) RunPodSandbox(config *kubeapi.PodSandboxConfig) (string, error)
(1)、调用userpod, err := buildUserPod(config)
(2)、调用podID, err := h.client.CreatePod(userpod)
(3)、调用err = h.client.StartPod(podID)
(4)、最后return podID, nil
// buildUserPod builds hyperd's UserPod based kubelet PodSandboxConfig.
// TODO: support pod-level portmapping (depends on hyperd)
func buildUserPod(config *kubeapi.PodSandboxConfig) (*types.UserPod, error)
(1)、调用cgroupParent := config.Linux.GetCgroupParent(),根据cgroupParent计算得到cpuNumber和memoryinMegabytes
(2)、创建spec := &types.UserPod{
Id: buildSandboxName(config),
Hostname: config.GetHostname(),
Labels: buildLabelsWithAnnotations(config.Labels, config.Annotations),
Resource: &types.UserResource{
Vcpu: cpuNumber,
Memory: memoryinMegabytes,
}
}
(3)、如果config.DnsConfig不为nil,则调用spec.Dns = config.DnsConfig.Servers
(4)、最后return spec, nil