Order containers returned from state and make container config public

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #63
Approved by: baude
This commit is contained in:
Matthew Heon 2017-11-22 12:21:53 -05:00 committed by Atomic Bot
parent bd4e106de3
commit 34ba0cb8a9
4 changed files with 28 additions and 8 deletions

View File

@ -46,7 +46,7 @@ const (
// Container is a single OCI container
type Container struct {
config *containerConfig
config *ContainerConfig
pod *Pod
runningSpec *spec.Spec
@ -59,7 +59,7 @@ type Container struct {
runtime *Runtime
}
// containerState contains the current state of the container
// containerRuntimeInfo contains the current state of the container
// It is stored on disk in a tmpfs and recreated on reboot
type containerRuntimeInfo struct {
// The current state of the running container
@ -88,10 +88,10 @@ type containerRuntimeInfo struct {
// TODO: Save information about image used in container if one is used
}
// containerConfig contains all information that was used to create the
// ContainerConfig contains all information that was used to create the
// container. It may not be changed once created.
// It is stored, read-only, on disk
type containerConfig struct {
type ContainerConfig struct {
Spec *spec.Spec `json:"spec"`
ID string `json:"id"`
Name string `json:"name"`
@ -153,6 +153,14 @@ func (c *Container) Labels() map[string]string {
return labels
}
// Config returns the configuration used to create the container
func (c *Container) Config() *ContainerConfig {
returnConfig := new(ContainerConfig)
deepcopier.Copy(c.config).To(returnConfig)
return returnConfig
}
// LogPath returns the path to the container's log file
// This file will only be present after Init() is called to create the container
// in runc
@ -235,7 +243,7 @@ func newContainer(rspec *spec.Spec) (*Container, error) {
}
ctr := new(Container)
ctr.config = new(containerConfig)
ctr.config = new(ContainerConfig)
ctr.state = new(containerRuntimeInfo)
ctr.config.ID = stringid.GenerateNonCryptoID()

View File

@ -544,7 +544,8 @@ func (s *SQLState) AllContainers() ([]*Container, error) {
containerState.Pid
FROM containers
INNER JOIN
containerState ON containers.Id = containerState.Id;`
containerState ON containers.Id = containerState.Id
ORDER BY containers.CreatedTime DESC;`
if !s.valid {
return nil, ErrDBClosed

View File

@ -186,7 +186,7 @@ func ctrFromScannable(row scannable, runtime *Runtime, specsDir string) (*Contai
}
ctr := new(Container)
ctr.config = new(containerConfig)
ctr.config = new(ContainerConfig)
ctr.state = new(containerRuntimeInfo)
ctr.config.ID = id

View File

@ -15,7 +15,7 @@ import (
func getTestContainer(id, name string) *Container {
ctr := &Container{
config: &containerConfig{
config: &ContainerConfig{
ID: id,
Name: name,
RootfsImageID: id,
@ -518,4 +518,15 @@ func TestGetAllContainersTwoContainers(t *testing.T) {
ctrs, err := state.AllContainers()
assert.NoError(t, err)
assert.Equal(t, 2, len(ctrs))
// Containers should be ordered by creation time
// Use assert.EqualValues if the test fails to pretty print diff
// between actual and expected
if !testContainersEqual(testCtr2, ctrs[0]) {
assert.EqualValues(t, testCtr2, ctrs[0])
}
if !testContainersEqual(testCtr1, ctrs[1]) {
assert.EqualValues(t, testCtr1, ctrs[1])
}
}