guan000 2020-02-21
Pod事件生成主要是根据对应Pod前后的状态对比来实现,首先通过runtime来获取当前节点的所有Pod的列表,并将对应的状态进行保存,这样在下一个轮训周期就可以通过前后状态的对比去发现状态发生改变的Pod的容器,并且产生对应的事件
Pod事件生成之后会通过管道将对应的事件同步给状态同步线程,状态同步线程感知到Pod的变更事件后,会与Pod的目标状态进行对比同步,并调用Runtime来进行最终校证操作的执行,同时在下个轮询周期中又会重新从Runtime获取状态,从而不断校证Pod的状态,直至目标状态
Pod记录其实就是一个map,并通过podRecord来保存前后轮询周期Runtime返回的Pod的信息
type podRecord struct { old *kubecontainer.Pod current *kubecontainer.Pod } type podRecords map[types.UID]*podRecord
首先通过runtime来获取当前节点的所有pod的状态
// Get all the pods. podList, err := g.runtime.GetPods(true) if err != nil { klog.Errorf("GenericPLEG: Unable to retrieve pods: %v", err) return }
eventsByPodID := map[types.UID][]*PodLifecycleEvent{} for pid := range g.podRecords { // 获取之前的Pod信息 oldPod := g.podRecords.getOld(pid) pod := g.podRecords.getCurrent(pid) // 获取当前Pod的所有容器集合 allContainers := getContainersFromPods(oldPod, pod) for _, container := range allContainers { // events := computeEvents(oldPod, pod, &container.ID) for _, e := range events { // 更新pod的events事件 updateEvents(eventsByPodID, e) } } }
Pod的事件主要是通过底层容器的状态来生成的,会最终对比每个容器的前后状态,从而获取变更事件
func generateEvents(podID types.UID, cid string, oldState, newState plegContainerState) []*PodLifecycleEvent { if newState == oldState { return nil } klog.V(4).Infof("GenericPLEG: %v/%v: %v -> %v", podID, cid, oldState, newState) switch newState { case plegContainerRunning: return []*PodLifecycleEvent{{ID: podID, Type: ContainerStarted, Data: cid}} case plegContainerExited: return []*PodLifecycleEvent{{ID: podID, Type: ContainerDied, Data: cid}} case plegContainerUnknown: return []*PodLifecycleEvent{{ID: podID, Type: ContainerChanged, Data: cid}} case plegContainerNonExistent: switch oldState { case plegContainerExited: // We already reported that the container died before. return []*PodLifecycleEvent{{ID: podID, Type: ContainerRemoved, Data: cid}} default: return []*PodLifecycleEvent{{ID: podID, Type: ContainerDied, Data: cid}, {ID: podID, Type: ContainerRemoved, Data: cid}} } default: panic(fmt.Sprintf("unrecognized container state: %v", newState)) } }
在k8s中有很多类似PLEG的设计,总的设计目标都是为了通过事件的变更和实际期望状态,不断的进行调整,从而达到最终的期望状态, 以后我尽量只给出组件最核心的一点代码,梳理清除整个流程中核心的数据结构与算法
k8s源码阅读电子书地址: https://www.yuque.com/baxiaoshi/tyado3