Only print container size JSON if --size was requested

To do this, move it into a separate struct, and embed that in
the JSON we return.

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon 2018-07-13 11:51:06 -04:00
parent f08fffa306
commit 5b43a6a7ee
3 changed files with 59 additions and 43 deletions

View File

@ -35,13 +35,13 @@ type PsOptions struct {
// BatchContainerStruct is the return obkect from BatchContainer and contains
// container related information
type BatchContainerStruct struct {
ConConfig *libpod.ContainerConfig
ConState libpod.ContainerStatus
ExitCode int32
Exited bool
Pid int
RootFsSize, RwSize int64
StartedTime time.Time
ConConfig *libpod.ContainerConfig
ConState libpod.ContainerStatus
ExitCode int32
Exited bool
Pid int
StartedTime time.Time
Size *ContainerSize
}
// Namespace describes output for ps namespace
@ -56,18 +56,25 @@ type Namespace struct {
UTS string `json:"uts,omitempty"`
}
// ContainerSize holds the size of the container's root filesystem and top
// read-write layer
type ContainerSize struct {
RootFsSize int64 `json:"rootFsSize"`
RwSize int64 `json:"rwSize"`
}
// BatchContainer is used in ps to reduce performance hits by "batching"
// locks.
func BatchContainerOp(ctr *libpod.Container, opts PsOptions) (BatchContainerStruct, error) {
var (
conConfig *libpod.ContainerConfig
conState libpod.ContainerStatus
err error
exitCode int32
exited bool
pid int
rootFsSize, rwSize int64
startedTime time.Time
conConfig *libpod.ContainerConfig
conState libpod.ContainerStatus
err error
exitCode int32
exited bool
pid int
size *ContainerSize
startedTime time.Time
)
batchErr := ctr.Batch(func(c *libpod.Container) error {
@ -97,16 +104,20 @@ func BatchContainerOp(ctr *libpod.Container, opts PsOptions) (BatchContainerStru
}
}
if opts.Size {
rootFsSize, err = c.RootFsSize()
size = new(ContainerSize)
rootFsSize, err := c.RootFsSize()
if err != nil {
logrus.Errorf("error getting root fs size for %q: %v", c.ID(), err)
}
rwSize, err = c.RWSize()
rwSize, err := c.RWSize()
if err != nil {
logrus.Errorf("error getting rw size for %q: %v", c.ID(), err)
}
size.RootFsSize = rootFsSize
size.RwSize = rwSize
}
return nil
})
@ -119,9 +130,8 @@ func BatchContainerOp(ctr *libpod.Container, opts PsOptions) (BatchContainerStru
ExitCode: exitCode,
Exited: exited,
Pid: pid,
RootFsSize: rootFsSize,
RwSize: rwSize,
StartedTime: startedTime,
Size: size,
}, nil
}

View File

@ -52,24 +52,23 @@ type psTemplateParams struct {
// psJSONParams will be populated by data from libpod.Container,
// the members of the struct are the sama data types as their sources.
type psJSONParams struct {
ID string `json:"id"`
Image string `json:"image"`
ImageID string `json:"image_id"`
Command []string `json:"command"`
CreatedAt time.Time `json:"createdAt"`
ExitCode int32 `json:"exitCode"`
Exited bool `json:"exited"`
RunningFor time.Duration `json:"runningFor"`
Status string `json:"status"`
PID int `json:"PID"`
Ports []ocicni.PortMapping `json:"ports"`
RootFsSize int64 `json:"rootFsSize,omitempty"`
RWSize int64 `json:"rwSize,omitempty"`
Names string `json:"names"`
Labels fields.Set `json:"labels"`
Mounts []string `json:"mounts"`
ContainerRunning bool `json:"ctrRunning"`
Namespaces *batchcontainer.Namespace `json:"namespace,omitempty"`
ID string `json:"id"`
Image string `json:"image"`
ImageID string `json:"image_id"`
Command []string `json:"command"`
CreatedAt time.Time `json:"createdAt"`
ExitCode int32 `json:"exitCode"`
Exited bool `json:"exited"`
RunningFor time.Duration `json:"runningFor"`
Status string `json:"status"`
PID int `json:"PID"`
Ports []ocicni.PortMapping `json:"ports"`
Size *batchcontainer.ContainerSize `json:"size,omitempty"`
Names string `json:"names"`
Labels fields.Set `json:"labels"`
Mounts []string `json:"mounts"`
ContainerRunning bool `json:"ctrRunning"`
Namespaces *batchcontainer.Namespace `json:"namespace,omitempty"`
}
// Type declaration and functions for sorting the PS output
@ -115,7 +114,10 @@ func (a psSortedStatus) Less(i, j int) bool { return a.psSorted[i].Status < a.ps
type psSortedSize struct{ psSorted }
func (a psSortedSize) Less(i, j int) bool {
return a.psSorted[i].RootFsSize < a.psSorted[j].RootFsSize
if a.psSorted[i].Size == nil || a.psSorted[j].Size == nil {
return false
}
return a.psSorted[i].Size.RootFsSize < a.psSorted[j].Size.RootFsSize
}
var (
@ -497,7 +499,10 @@ func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) (
ns = psParam.Namespaces
}
if opts.Size {
size = units.HumanSizeWithPrecision(float64(psParam.RWSize), 3) + " (virtual " + units.HumanSizeWithPrecision(float64(psParam.RootFsSize), 3) + ")"
if psParam.Size == nil {
return nil, errors.Errorf("Container %s does not have a size struct", psParam.ID)
}
size = units.HumanSizeWithPrecision(float64(psParam.Size.RwSize), 3) + " (virtual " + units.HumanSizeWithPrecision(float64(psParam.Size.RootFsSize), 3) + ")"
}
runningFor := units.HumanDuration(psParam.RunningFor)
@ -587,8 +592,7 @@ func getAndSortJSONParams(containers []*libpod.Container, opts batchcontainer.Ps
Status: batchInfo.ConState.String(),
PID: batchInfo.Pid,
Ports: batchInfo.ConConfig.PortMappings,
RootFsSize: batchInfo.RootFsSize,
RWSize: batchInfo.RwSize,
Size: batchInfo.Size,
Names: batchInfo.ConConfig.Name,
Labels: batchInfo.ConConfig.Labels,
Mounts: batchInfo.ConConfig.UserVolumes,

View File

@ -65,13 +65,15 @@ func makeListContainer(containerID string, batchInfo batchcontainer.BatchContain
Runningfor: time.Since(batchInfo.ConConfig.CreatedTime).String(),
Status: batchInfo.ConState.String(),
Ports: ports,
Rootfssize: batchInfo.RootFsSize,
Rwsize: batchInfo.RwSize,
Names: batchInfo.ConConfig.Name,
Labels: batchInfo.ConConfig.Labels,
Mounts: mounts,
Containerrunning: batchInfo.ConState == libpod.ContainerStateRunning,
Namespaces: namespace,
}
if batchInfo.Size != nil {
lc.Rootfssize = batchInfo.Size.RootFsSize
lc.Rwsize = batchInfo.Size.RwSize
}
return lc
}