Add create/start times. Add helpers for locating common files.

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon 2017-10-23 14:21:15 -04:00
parent 3262565d61
commit 88e2acdc4f
2 changed files with 27 additions and 7 deletions

View file

@ -5,9 +5,11 @@ import (
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sync" "sync"
"time"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
crioAnnotations "github.com/kubernetes-incubator/cri-o/pkg/annotations"
spec "github.com/opencontainers/runtime-spec/specs-go" spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -56,17 +58,21 @@ type Container struct {
type containerRuntimeInfo struct { type containerRuntimeInfo struct {
// The current state of the running container // The current state of the running container
State ContainerState `json:"state"` State ContainerState `json:"state"`
// The path to the JSON OCI runtime spec for this container // The path to the JSON OCI runtime spec for this container
ConfigPath string `json:"configPath,omitempty"` ConfigPath string `json:"configPath,omitempty"`
// RunDir is a per-boot directory for container content // RunDir is a per-boot directory for container content
RunDir string `json:"runDir,omitempty"` RunDir string `json:"runDir,omitempty"`
// Mounted indicates whether the container's storage has been mounted // Mounted indicates whether the container's storage has been mounted
// for use // for use
Mounted bool `json:"-"` Mounted bool `json:"-"`
// MountPoint contains the path to the container's mounted storage // MountPoint contains the path to the container's mounted storage
Mountpoint string `json:"mountPoint,omitempty"` Mountpoint string `json:"mountPoint,omitempty"`
// StartedTime is the time the container was started
StartedTime time.Time `json:"startedTime,omitempty"`
// FinishedTime is the time the container finished executing
FinishedTime time.Time `json:"finishedTime,omitempty"`
// ExitCode is the exit code returned when the container stopped
ExitCode int32 `json:"exitCode,omitempty"`
// TODO: Save information about image used in container if one is used // TODO: Save information about image used in container if one is used
// TODO save start time, create time (create time in the containerConfig?) // TODO save start time, create time (create time in the containerConfig?)
@ -98,8 +104,9 @@ type containerConfig struct {
// Shared namespaces with container // Shared namespaces with container
SharedNamespaceCtr *string `json:"shareNamespacesWith,omitempty"` SharedNamespaceCtr *string `json:"shareNamespacesWith,omitempty"`
SharedNamespaceMap map[string]string `json:"sharedNamespaces"` SharedNamespaceMap map[string]string `json:"sharedNamespaces"`
// Time container was created
CreatedTime time.Time `json:"createdTime"`
// TODO save create time
// TODO save log location here and pass into OCI code // TODO save log location here and pass into OCI code
// TODO allow overriding of log path // TODO allow overriding of log path
} }
@ -137,6 +144,17 @@ func (c *Container) State() (ContainerState, error) {
return c.state.State, nil return c.state.State, nil
} }
// The path to the container's root filesystem - where the OCI spec will be
// placed, amongst other things
func (c *Container) bundlePath() string {
return c.state.RunDir
}
// Retrieves the path of the container's attach socket
func (c *Container) attachSocketPath() string {
return filepath.Join(c.runtime.ociRuntime.socketsDir, c.ID(), "attach")
}
// Make a new container // Make a new container
func newContainer(rspec *spec.Spec) (*Container, error) { func newContainer(rspec *spec.Spec) (*Container, error) {
if rspec == nil { if rspec == nil {
@ -153,6 +171,8 @@ func newContainer(rspec *spec.Spec) (*Container, error) {
ctr.config.Spec = new(spec.Spec) ctr.config.Spec = new(spec.Spec)
deepcopier.Copy(rspec).To(ctr.config.Spec) deepcopier.Copy(rspec).To(ctr.config.Spec)
ctr.config.CreatedTime = time.Now()
return ctr, nil return ctr, nil
} }
@ -239,11 +259,10 @@ func (c *Container) Create() (err error) {
c.runningSpec = new(spec.Spec) c.runningSpec = new(spec.Spec)
deepcopier.Copy(c.config.Spec).To(c.runningSpec) deepcopier.Copy(c.config.Spec).To(c.runningSpec)
c.runningSpec.Root.Path = c.state.Mountpoint c.runningSpec.Root.Path = c.state.Mountpoint
c.runningSpec.Annotations[crioAnnotations.Created] = c.config.CreatedTime.Format(time.RFC3339Nano)
// TODO Add annotation for start time to spec
// Save the OCI spec to disk // Save the OCI spec to disk
jsonPath := filepath.Join(c.state.RunDir, "config.json") jsonPath := filepath.Join(c.bundlePath(), "config.json")
fileJSON, err := json.Marshal(c.runningSpec) fileJSON, err := json.Marshal(c.runningSpec)
if err != nil { if err != nil {
return errors.Wrapf(err, "error exporting runtime spec for container %s to JSON", c.ID()) return errors.Wrapf(err, "error exporting runtime spec for container %s to JSON", c.ID())
@ -284,6 +303,7 @@ func (c *Container) Start() error {
} }
// TODO should flush state to disk here // TODO should flush state to disk here
c.state.StartedTime = time.Now()
c.state.State = ContainerStateRunning c.state.State = ContainerStateRunning
return nil return nil

View file

@ -132,7 +132,7 @@ func (r *OCIRuntime) createContainer(ctr *Container, cgroupParent string) error
args = append(args, "-c", ctr.ID()) args = append(args, "-c", ctr.ID())
args = append(args, "-u", ctr.ID()) args = append(args, "-u", ctr.ID())
args = append(args, "-r", r.path) args = append(args, "-r", r.path)
args = append(args, "-b", ctr.state.RunDir) args = append(args, "-b", ctr.bundlePath())
args = append(args, "-p", filepath.Join(ctr.state.RunDir, "pidfile")) args = append(args, "-p", filepath.Join(ctr.state.RunDir, "pidfile"))
// TODO container log location should be configurable // TODO container log location should be configurable
// The default also likely shouldn't be this // The default also likely shouldn't be this