store infra container in sandbox

infra container is used to implement the pod
sandbox, it should not be exported to user.

this patch stores infra container in sandbox
immediately, only the containers created by user
are stored into container store, this prevents user
from removing/stopping infra container incorrectly.

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng 2016-10-24 20:01:01 +08:00
parent e14e6c7cfc
commit 78528d9bd1
3 changed files with 49 additions and 41 deletions

View file

@ -290,9 +290,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)

View file

@ -18,15 +18,16 @@ import (
) )
type sandbox struct { type sandbox struct {
id string id string
name string name string
logDir string logDir string
labels fields.Set labels fields.Set
annotations map[string]string annotations map[string]string
containers oci.Store infraContainer *oci.Container
processLabel string containers oci.Store
mountLabel string processLabel string
metadata *pb.PodSandboxMetadata mountLabel string
metadata *pb.PodSandboxMetadata
} }
const ( const (
@ -225,7 +226,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,
@ -235,7 +237,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)
@ -290,6 +294,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
} }
@ -312,8 +318,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
} }
@ -329,18 +333,22 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
return nil, err return nil, err
} }
podInfraContainer := sb.name + "-infra" podNamespace := ""
for _, c := range sb.containers.List() { podInfraContainer := sb.infraContainer
if podInfraContainer == c.Name() { netnsPath, err := podInfraContainer.NetNsPath()
podNamespace := "" if err != nil {
netnsPath, err := c.NetNsPath() return nil, err
if err != nil { }
return nil, err
} if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, sb.id, podInfraContainer.Name()); err != nil {
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",
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), sb.id, err) 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 {
@ -360,11 +368,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)
} }
@ -380,8 +389,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
} }
@ -405,6 +413,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)
@ -419,8 +428,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
} }
@ -433,7 +441,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 = ""
@ -503,8 +511,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

View file

@ -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)
} }