diff --git a/oci/container.go b/oci/container.go index ea8856c0..cd020fcb 100644 --- a/oci/container.go +++ b/oci/container.go @@ -42,7 +42,9 @@ type ContainerState struct { } // NewContainer creates a container object. -func NewContainer(id string, name string, bundlePath string, logPath string, netns ns.NetNS, labels map[string]string, annotations map[string]string, image *pb.ImageSpec, metadata *pb.ContainerMetadata, sandbox string, terminal bool, privileged bool, dir string) (*Container, error) { +func NewContainer(id string, name string, bundlePath string, logPath string, netns ns.NetNS, labels map[string]string, annotations map[string]string, image *pb.ImageSpec, metadata *pb.ContainerMetadata, sandbox string, terminal bool, privileged bool, dir string, created time.Time) (*Container, error) { + state := &ContainerState{} + state.Created = created c := &Container{ id: id, name: name, @@ -57,10 +59,16 @@ func NewContainer(id string, name string, bundlePath string, logPath string, net annotations: annotations, image: image, dir: dir, + state: state, } return c, nil } +// CreatedAt returns the container creation time +func (c *Container) CreatedAt() time.Time { + return c.state.Created +} + // Name returns the name of the container. func (c *Container) Name() string { return c.name diff --git a/server/container_create.go b/server/container_create.go index 6babb63b..6c5c3679 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/stringid" @@ -519,6 +520,9 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, specgen.AddAnnotation("ocid/tty", fmt.Sprintf("%v", containerConfig.Tty)) specgen.AddAnnotation("ocid/image", image) + created := time.Now() + specgen.AddAnnotation("ocid/created", created.Format(time.RFC3339Nano)) + metadataJSON, err := json.Marshal(metadata) if err != nil { return nil, err @@ -627,7 +631,7 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, return nil, err } - container, err := oci.NewContainer(containerID, containerName, containerInfo.RunDir, logPath, sb.netNs(), labels, annotations, imageSpec, metadata, sb.id, containerConfig.Tty, sb.privileged, containerInfo.Dir) + container, err := oci.NewContainer(containerID, containerName, containerInfo.RunDir, logPath, sb.netNs(), labels, annotations, imageSpec, metadata, sb.id, containerConfig.Tty, sb.privileged, containerInfo.Dir, created) if err != nil { return nil, err } diff --git a/server/sandbox.go b/server/sandbox.go index ffcef13f..0f57f557 100644 --- a/server/sandbox.go +++ b/server/sandbox.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "sync" - "time" "github.com/Sirupsen/logrus" "github.com/containernetworking/cni/pkg/ns" @@ -146,7 +145,6 @@ type sandbox struct { privileged bool resolvPath string hostname string - created time.Time } const ( diff --git a/server/sandbox_run.go b/server/sandbox_run.go index 68a404b1..72bc8ee7 100644 --- a/server/sandbox_run.go +++ b/server/sandbox_run.go @@ -287,7 +287,6 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest privileged: privileged, resolvPath: resolvPath, hostname: hostname, - created: created, } defer func() { @@ -404,7 +403,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest return nil, fmt.Errorf("failed to write runtime configuration for pod sandbox %s(%s): %v", sb.name, id, err) } - container, err := oci.NewContainer(id, containerName, podContainer.RunDir, logPath, sb.netNs(), labels, annotations, nil, nil, id, false, sb.privileged, podContainer.Dir) + container, err := oci.NewContainer(id, containerName, podContainer.RunDir, logPath, sb.netNs(), labels, annotations, nil, nil, id, false, sb.privileged, podContainer.Dir, created) if err != nil { return nil, err } diff --git a/server/sandbox_status.go b/server/sandbox_status.go index 6be2ebb0..9a8c864f 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -41,7 +41,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR resp := &pb.PodSandboxStatusResponse{ Status: &pb.PodSandboxStatus{ Id: sandboxID, - CreatedAt: sb.created.UnixNano(), + CreatedAt: podInfraContainer.CreatedAt().UnixNano(), Linux: &pb.LinuxPodSandboxStatus{ Namespaces: &pb.Namespace{ Network: netNsPath, diff --git a/server/server.go b/server/server.go index d6e9ce9d..1e51ae1d 100644 --- a/server/server.go +++ b/server/server.go @@ -139,7 +139,12 @@ func (s *Server) loadContainer(id string) error { return err } - ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], sb.netNs(), labels, annotations, img, &metadata, sb.id, tty, sb.privileged, containerDir) + created, err := time.Parse(time.RFC3339Nano, m.Annotations["ocid/created"]) + if err != nil { + return err + } + + ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], sb.netNs(), labels, annotations, img, &metadata, sb.id, tty, sb.privileged, containerDir, created) if err != nil { return err } @@ -205,10 +210,6 @@ func (s *Server) loadSandbox(id string) error { } privileged := m.Annotations["ocid/privileged_runtime"] == "true" - created, err := time.Parse(time.RFC3339Nano, m.Annotations["ocid/created"]) - if err != nil { - return err - } sb := &sandbox{ id: id, @@ -224,7 +225,6 @@ func (s *Server) loadSandbox(id string) error { shmPath: m.Annotations["ocid/shm_path"], privileged: privileged, resolvPath: m.Annotations["ocid/resolv_path"], - created: created, } // We add a netNS only if we can load a permanent one. @@ -270,7 +270,12 @@ func (s *Server) loadSandbox(id string) error { } }() - scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, m.Annotations["ocid/log_path"], sb.netNs(), labels, annotations, nil, nil, id, false, privileged, sandboxDir) + created, err := time.Parse(time.RFC3339Nano, m.Annotations["ocid/created"]) + if err != nil { + return err + } + + scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, m.Annotations["ocid/log_path"], sb.netNs(), labels, annotations, nil, nil, id, false, privileged, sandboxDir, created) if err != nil { return err }