Merge pull request #176 from gao-feng/hidden-infra-container
store infra container in sandbox
This commit is contained in:
commit
c89157e45c
3 changed files with 49 additions and 41 deletions
|
@ -291,9 +291,7 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Join the namespace paths for the pod sandbox container.
|
// Join the namespace paths for the pod sandbox container.
|
||||||
podContainerName := sb.name + "-infra"
|
podInfraState := s.runtime.ContainerStatus(sb.infraContainer)
|
||||||
podInfraContainer := sb.getContainer(podContainerName)
|
|
||||||
podInfraState := s.runtime.ContainerStatus(podInfraContainer)
|
|
||||||
|
|
||||||
logrus.Infof("pod container state %v", podInfraState)
|
logrus.Infof("pod container state %v", podInfraState)
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ type sandbox struct {
|
||||||
logDir string
|
logDir string
|
||||||
labels fields.Set
|
labels fields.Set
|
||||||
annotations map[string]string
|
annotations map[string]string
|
||||||
|
infraContainer *oci.Container
|
||||||
containers oci.Store
|
containers oci.Store
|
||||||
processLabel string
|
processLabel string
|
||||||
mountLabel string
|
mountLabel string
|
||||||
|
@ -226,7 +227,8 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
g.AddAnnotation("ocid/name", name)
|
g.AddAnnotation("ocid/name", name)
|
||||||
g.AddAnnotation("ocid/container_name", containerName)
|
g.AddAnnotation("ocid/container_name", containerName)
|
||||||
g.AddAnnotation("ocid/container_id", containerID)
|
g.AddAnnotation("ocid/container_id", containerID)
|
||||||
s.addSandbox(&sandbox{
|
|
||||||
|
sb := &sandbox{
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
logDir: logDir,
|
logDir: logDir,
|
||||||
|
@ -236,7 +238,9 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
processLabel: processLabel,
|
processLabel: processLabel,
|
||||||
mountLabel: mountLabel,
|
mountLabel: mountLabel,
|
||||||
metadata: req.GetConfig().GetMetadata(),
|
metadata: req.GetConfig().GetMetadata(),
|
||||||
})
|
}
|
||||||
|
|
||||||
|
s.addSandbox(sb)
|
||||||
|
|
||||||
for k, v := range annotations {
|
for k, v := range annotations {
|
||||||
g.AddAnnotation(k, v)
|
g.AddAnnotation(k, v)
|
||||||
|
@ -291,6 +295,8 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sb.infraContainer = container
|
||||||
|
|
||||||
if err = s.runtime.CreateContainer(container); err != nil {
|
if err = s.runtime.CreateContainer(container); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -313,8 +319,6 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.addContainer(container)
|
|
||||||
|
|
||||||
if err = s.runtime.UpdateStatus(container); err != nil {
|
if err = s.runtime.UpdateStatus(container); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -331,18 +335,22 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainer := sb.name + "-infra"
|
|
||||||
for _, c := range sb.containers.List() {
|
|
||||||
if podInfraContainer == c.Name() {
|
|
||||||
podNamespace := ""
|
podNamespace := ""
|
||||||
netnsPath, err := c.NetNsPath()
|
podInfraContainer := sb.infraContainer
|
||||||
|
netnsPath, err := podInfraContainer.NetNsPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, sb.id, podInfraContainer); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, sb.id, podInfraContainer.Name()); err != nil {
|
||||||
}
|
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v",
|
||||||
|
podInfraContainer.Name(), sb.id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
containers := sb.containers.List()
|
||||||
|
containers = append(containers, podInfraContainer)
|
||||||
|
|
||||||
|
for _, c := range containers {
|
||||||
cStatus := s.runtime.ContainerStatus(c)
|
cStatus := s.runtime.ContainerStatus(c)
|
||||||
if cStatus.Status != oci.ContainerStateStopped {
|
if cStatus.Status != oci.ContainerStateStopped {
|
||||||
if err := s.runtime.StopContainer(c); err != nil {
|
if err := s.runtime.StopContainer(c); err != nil {
|
||||||
|
@ -363,11 +371,12 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainerName := sb.name + "-infra"
|
podInfraContainer := sb.infraContainer
|
||||||
var podInfraContainer *oci.Container
|
containers := sb.containers.List()
|
||||||
|
containers = append(containers, podInfraContainer)
|
||||||
|
|
||||||
// Delete all the containers in the sandbox
|
// Delete all the containers in the sandbox
|
||||||
for _, c := range sb.containers.List() {
|
for _, c := range containers {
|
||||||
if err := s.runtime.UpdateStatus(c); err != nil {
|
if err := s.runtime.UpdateStatus(c); err != nil {
|
||||||
return nil, fmt.Errorf("failed to update container state: %v", err)
|
return nil, fmt.Errorf("failed to update container state: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -383,8 +392,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if podInfraContainerName == c.Name() {
|
if c == podInfraContainer {
|
||||||
podInfraContainer = c
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,6 +416,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
}
|
}
|
||||||
s.releaseContainerName(podInfraContainer.Name())
|
s.releaseContainerName(podInfraContainer.Name())
|
||||||
s.removeContainer(podInfraContainer)
|
s.removeContainer(podInfraContainer)
|
||||||
|
sb.infraContainer = nil
|
||||||
|
|
||||||
s.releasePodName(sb.name)
|
s.releasePodName(sb.name)
|
||||||
s.removeSandbox(sb.id)
|
s.removeSandbox(sb.id)
|
||||||
|
@ -423,8 +432,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainerName := sb.name + "-infra"
|
podInfraContainer := sb.infraContainer
|
||||||
podInfraContainer := sb.getContainer(podInfraContainerName)
|
|
||||||
if err = s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
if err = s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -437,7 +445,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
podNamespace := ""
|
podNamespace := ""
|
||||||
ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, sb.id, podInfraContainerName)
|
ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, sb.id, podInfraContainer.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// ignore the error on network status
|
// ignore the error on network status
|
||||||
ip = ""
|
ip = ""
|
||||||
|
@ -508,8 +516,7 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxReque
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sb := range podList {
|
for _, sb := range podList {
|
||||||
podInfraContainerName := sb.name + "-infra"
|
podInfraContainer := sb.infraContainer
|
||||||
podInfraContainer := sb.getContainer(podInfraContainerName)
|
|
||||||
if podInfraContainer == nil {
|
if podInfraContainer == nil {
|
||||||
// this can't really happen, but if it does because of a bug
|
// this can't really happen, but if it does because of a bug
|
||||||
// it's better not to panic
|
// it's better not to panic
|
||||||
|
|
|
@ -102,7 +102,8 @@ func (s *Server) loadSandbox(id string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.addSandbox(&sandbox{
|
|
||||||
|
sb := &sandbox{
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
logDir: m.Annotations["ocid/log_path"],
|
logDir: m.Annotations["ocid/log_path"],
|
||||||
|
@ -110,7 +111,9 @@ func (s *Server) loadSandbox(id string) error {
|
||||||
containers: oci.NewMemoryStore(),
|
containers: oci.NewMemoryStore(),
|
||||||
processLabel: processLabel,
|
processLabel: processLabel,
|
||||||
mountLabel: mountLabel,
|
mountLabel: mountLabel,
|
||||||
})
|
}
|
||||||
|
s.addSandbox(sb)
|
||||||
|
|
||||||
sandboxPath := filepath.Join(s.config.SandboxDir, id)
|
sandboxPath := filepath.Join(s.config.SandboxDir, id)
|
||||||
|
|
||||||
if err := label.ReserveLabel(processLabel); err != nil {
|
if err := label.ReserveLabel(processLabel); err != nil {
|
||||||
|
@ -125,7 +128,7 @@ func (s *Server) loadSandbox(id string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.addContainer(scontainer)
|
sb.infraContainer = scontainer
|
||||||
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
||||||
logrus.Warnf("error updating status for container %s: %v", scontainer.ID(), err)
|
logrus.Warnf("error updating status for container %s: %v", scontainer.ID(), err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue