server: store containers state on disk

Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
Antonio Murdaca 2017-05-11 12:03:59 +02:00
parent 2ddc062bbe
commit da0b8a6157
No known key found for this signature in database
GPG key ID: B2BEAD150DE936B9
7 changed files with 30 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package oci
import ( import (
"fmt" "fmt"
"path/filepath"
"sync" "sync"
"time" "time"
@ -64,6 +65,11 @@ func NewContainer(id string, name string, bundlePath string, logPath string, net
return c, nil return c, nil
} }
// StatePath returns the containers state.json path
func (c *Container) StatePath() string {
return filepath.Join(c.dir, "state.json")
}
// CreatedAt returns the container creation time // CreatedAt returns the container creation time
func (c *Container) CreatedAt() time.Time { func (c *Container) CreatedAt() time.Time {
return c.state.Created return c.state.Created

View file

@ -310,6 +310,8 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
return nil, err return nil, err
} }
s.containerStateToDisk(container)
resp := &pb.CreateContainerResponse{ resp := &pb.CreateContainerResponse{
ContainerId: containerID, ContainerId: containerID,
} }

View file

@ -20,6 +20,8 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque
return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err) return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err)
} }
s.containerStateToDisk(c)
resp := &pb.StartContainerResponse{} resp := &pb.StartContainerResponse{}
logrus.Debugf("StartContainerResponse %+v", resp) logrus.Debugf("StartContainerResponse %+v", resp)
return resp, nil return resp, nil

View file

@ -27,6 +27,8 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest
} }
} }
s.containerStateToDisk(c)
resp := &pb.StopContainerResponse{} resp := &pb.StopContainerResponse{}
logrus.Debugf("StopContainerResponse: %+v", resp) logrus.Debugf("StopContainerResponse: %+v", resp)
return resp, nil return resp, nil

View file

@ -425,6 +425,8 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
return nil, err return nil, err
} }
s.containerStateToDisk(container)
resp = &pb.RunPodSandboxResponse{PodSandboxId: id} resp = &pb.RunPodSandboxResponse{PodSandboxId: id}
logrus.Debugf("RunPodSandboxResponse: %+v", resp) logrus.Debugf("RunPodSandboxResponse: %+v", resp)
return resp, nil return resp, nil

View file

@ -52,6 +52,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
return nil, fmt.Errorf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.id, err) return nil, fmt.Errorf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.id, err)
} }
} }
s.containerStateToDisk(c)
} }
resp := &pb.StopPodSandboxResponse{} resp := &pb.StopPodSandboxResponse{}

View file

@ -19,6 +19,7 @@ import (
"github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/kubernetes-incubator/cri-o/pkg/storage"
"github.com/kubernetes-incubator/cri-o/server/apparmor" "github.com/kubernetes-incubator/cri-o/server/apparmor"
"github.com/kubernetes-incubator/cri-o/server/seccomp" "github.com/kubernetes-incubator/cri-o/server/seccomp"
"github.com/moby/moby/pkg/ioutils"
rspec "github.com/opencontainers/runtime-spec/specs-go" rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
@ -155,6 +156,20 @@ func (s *Server) loadContainer(id string) error {
return s.ctrIDIndex.Add(id) return s.ctrIDIndex.Add(id)
} }
func (s *Server) containerStateToDisk(c *oci.Container) error {
// ignore errors, this is a best effort to have up-to-date info about
// a given container before its state gets stored
s.runtime.UpdateStatus(c)
jsonSource, err := ioutils.NewAtomicFileWriter(c.StatePath(), 0644)
if err != nil {
return err
}
defer jsonSource.Close()
enc := json.NewEncoder(jsonSource)
return enc.Encode(s.runtime.ContainerStatus(c))
}
func configNetNsPath(spec rspec.Spec) (string, error) { func configNetNsPath(spec rspec.Spec) (string, error) {
for _, ns := range spec.Linux.Namespaces { for _, ns := range spec.Linux.Namespaces {
if ns.Type != rspec.NetworkNamespace { if ns.Type != rspec.NetworkNamespace {