cri-o pod 创建源码分析
1、 server/sandbox.go
// RunPodSandbox creates and runs a pod-level sandbox
func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest) (*pb.RunPodSandboxResponse, error)
name := req.GetConfig().GetMetadata().GetName()
namespace := req.GetConfig().GetMetadata().GetNamespace() //在test中,该字段为空
attempt := req.GetConfig().GetMetadata().GetAttempt() //在test中,该字段为空
id, name, err := s.generatePodIDandName(name, namespace, attempt)
podSandboxDir := filepath.Join(s.sandbox, id)
os.MkdirAll(podSandboxDir, 0755)
... // defer函数,用于创建pod失败,移除podSandboxDir
// creates a spec Generator with the default spec
g := generate.New() // 返回一个Generator结构,其中包含了默认的spec
podInfraRootfs := filepath.Join(s.root, "graph/vfs/pause")
g.SetRootPath(filepath.Join(podInfraRootfs, "rootfs")) //对默认的spec进行修改,针对的字段为Root和Process.Args
g.SetRootReadonly(true)
g.SetProcessArgs([]string{"/pause"})
... // 设置g.spec的hostname,如果req.config中的hostname 不为空的话
// set log directory
logDir := req.GetConfig().GetLogDirectory() // test的config文件默认为"."
if logDir == "" {
logDir = fmt.Sprintf("/var/log/ocid/pods/%s", id)
}
// set DNS options
... // 从req.Config中获取dnsServers和dnsSearches
resolvPat := fmt.Sprintf("%s/resolv.conf", podSandboxDir)
parseDNSOptions(dnsServers, dnsSearches, resolvPath)
// add labels
labels := req.GetConfig().GetLabels()
labelsJSON, err := json.Marshal(labels)
// add annotations
annotations := req.GetConfig().GetAnnotations()
annotationsJSON, err := json.Marshal(annotations)
// Don't use SELinux separation with Host Pid or IPC Namespace
if !req.GetConfig.GetLinux().GetNamespaceOptions().GetHostPid() && !req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() {
processLabel, mountLabel, err = getSELinuxLabels(nil)
g.SetProcessSelinuxLabel(processLabel)
}
containerID, containerName, err := s.generateContainerIDandName(name, "infra", 0)
g.AddAnnotation("ocid/labels", string(labelsJSON))
... // add annotation "ocid/annotations", "ocid/log_path", "ocid/name", "ocid/container_name", "ocid/container_id"
s.addSandbox(&sandbox{
id: id,
....
containers: oci.NewMemoryStore(),
...
metadata: req.GetConfig().GetMetadata(),
})
for k, v := range annotations {
g.AddAnnotation(k, v)
}
... // setup cgroup settings, setup namespaces
err = g.SaveToFile(filepath.Join(podSandboxDir, "config.json"))
if _, err = os.stat(podInfraRootfs); err != nil {
if os.IsNotExist(err) {
utils.CreateInfraRootfs(podInfraRootfs, s.pausePath) // podInfraRootfs is /var/lib/ocid/graph/vfs/pause
// copying infra rootfs binary: /usr/libexec/ocid/pause -> /var/lib/ocid/graph/vfs/pause/rootfs/pause
}
}
container, err := oci.NewContainer(containerID, containerName, podSandboxDir, podSanboxDir, labels, id, false) // bundlePath 也是podSandboxDir
s.runtime.CreateContainer(container)
s.runtime.UpdateStatus(container)
// setup the network
podNamespace := ""
netnsPath, err := container.NetNsPath()
s.netPlugin.SetUpPod(netnsPath, podNamespace, id, containerName)
s.runtime.StartContainer(container)
s.addContainer(container)
s.podIDIndex.Add(id)
s.runtime.UpdateStatus(container)
return &pb.RunSandboxResponse{PodSandboxId: &id}, nil
2、 oci/oci.go
// 该函数主要用于创建容器,并且同步等待返回容器的pid
func (r *Runtime) CreateContainer(c *Container) error
parentPipe, childPipe, err := newPipe()
defer parentPipe.Close()
args := []string{"-c", c.name}
args = append(args, "-r", r.path)
if c.terminal { args = append(args, "-t")}
cmd := exec.Command(r.conmonPath, args...)
cms.Dir = c.bundlePath
cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, }
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = append(cmd.ExtraFiles, childPipe)
cmd.Env = append(cmd.Env, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3))
err = cmd.Start()
childPipe.Close()
// Wait to get container pid from conmon
var si *syncInfo
json.NewDecoder(parentPipe).Decode(&si)
logrus.Infof("Received container pid: %v", si.Pid)
return nil
3、 oci/oci.go
func (r *Runtime) UpdateStatus(c *Container) error
...
out, err := exec.Command(r.path, "state", c.name).Output()
stateReader := bytes.NewReader(out)
json.NewDecoder(stateReader).Decode(&c.state)
if c.state.Status == ContainerStateStopped {
exitFilePath := filepath.Join(c.bundlePath, "exit")
fi, err := os.Stat(exitFilePath)
st := fi.Sys().(*syscall.Stat_t)
c.state.Finished = time.Unix(st.Ctim.Sec, st.Ctim.Nsec)
statusCodeStr, err := ioutil.ReadFile(exitFilePath)
statusCode, err := strconv.Atoi(string(statusCodeStr))
c.state.ExitCode = int32(utils.StatusToExitCode(statusCode))
}
cri-o pod 创建源码分析的更多相关文章
- Netty中NioEventLoopGroup的创建源码分析
NioEventLoopGroup的无参构造: public NioEventLoopGroup() { this(0); } 调用了单参的构造: public NioEventLoopGroup(i ...
- 【Java】NIO中Selector的创建源码分析
在使用Selector时首先需要通过静态方法open创建Selector对象 public static Selector open() throws IOException { return Sel ...
- kubelet源码分析——关闭Pod
上一篇说到kublet如何启动一个pod,本篇讲述如何关闭一个Pod,引用一段来自官方文档介绍pod的生命周期的话 你使用 kubectl 工具手动删除某个特定的 Pod,而该 Pod 的体面终止限期 ...
- kubelet源码分析——监控Pod变更
前言 前文介绍Pod无论是启动时还是关闭时,处理是由kubelet的主循环syncLoop开始执行逻辑,而syncLoop的入参是一条传递变更Pod的通道,显然syncLoop往后的逻辑属于消费者一方 ...
- scheduler源码分析——调度流程
前言 当api-server处理完一个pod的创建请求后,此时可以通过kubectl把pod get出来,但是pod的状态是Pending.在这个Pod能运行在节点上之前,它还需要经过schedule ...
- apiserver源码分析——启动流程
前言 apiserver是k8s控制面的一个组件,在众多组件中唯一一个对接etcd,对外暴露http服务的形式为k8s中各种资源提供增删改查等服务.它是RESTful风格,每个资源的URI都会形如 / ...
- apiserver源码分析——处理请求
前言 上一篇说道k8s-apiserver如何启动,本篇则介绍apiserver启动后,接收到客户端请求的处理流程.如下图所示 认证与授权一般系统都会使用到,认证是鉴别访问apiserver的请求方是 ...
- scheduler源码分析——preempt抢占
前言 之前探讨scheduler的调度流程时,提及过preempt抢占机制,它发生在预选调度失败的时候,当时由于篇幅限制就没有展开细说. 回顾一下抢占流程的主要逻辑在DefaultPreemption ...
- 【Java】NIO中Selector的select方法源码分析
该篇博客的有些内容和在之前介绍过了,在这里再次涉及到的就不详细说了,如果有不理解请看[Java]NIO中Channel的注册源码分析, [Java]NIO中Selector的创建源码分析 Select ...
随机推荐
- Windows nexus 启动失败
现象: nexus Windows系统服务安装成功,但启动失败 D:\nexus-2.10.0-02-bundle\nexus-2.10.0-02\bin>nexus.bat Usage: ne ...
- mybatis generator with oracle
1.generator.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generat ...
- Tomcat服务器与MyEclipse绑定
myeclipse中eclipse添加了很多非常实用的插件,几乎包含了常用的所有应用服务器插件,其中自然包括支持各个版本的Tomcat插件. 先来看看Tomcat处理浏览器请求的过程图: 1.Tomc ...
- 【Android】Android SDK Manager更新慢/失败的问题
前言:更新下载Intel x86 Atom_64 System Image的时候总是失败,速度只有几KB,我这是10M的网啊. 最后找到一篇日志,解决了这个问题.非常感谢!其参考地址:http://w ...
- spring mvc各种常见类型参数绑定方式以及json字符串绑定对象
在使用spring mvc作为框架的时候,为了规范,我们通常希望客户端的请求参数符合规范直接通过DTO的方式从客户端提交到服务端,以便保持规范的一致性,除了很简单的情况使用RequestParam映射 ...
- Exchange 2013 、Lync 2013、SharePoint 2013 二
上一篇简单介绍了安装过程,本篇主要集成 上一篇文章有关于头像的显示问题,engineer 给出了一个连接,介绍了Exchange和Lync的集成过程,根据介绍都配制了一遍. 一.Exchange 和 ...
- Bootstrap 我的学习记录4 轮播图的使用和理解
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- EntityFramework嵌套查询的五种方法
这样的双where的语句应该怎么写呢: var test=MyList.Where(a => a.Flows.Where(b => b.CurrentUser == “”) 下面我就说说这 ...
- CSS3中的calc()
什么是calc()? calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能; MDN的解释为可以用在任何长度,数值,时间,角度,频率等处; /* property: calc ...
- GridView自带的分页功能实现
要实现GrdView分页的功能操作如下:1.更改GrdView控件的AllowPaging属性为true.2.更改GrdView控件的PageSize属性为 任意数值(默认为10)3.更改GrdVie ...