diff options
author | Valentin Rothberg <rothberg@redhat.com> | 2020-01-15 13:14:56 +0100 |
---|---|---|
committer | Valentin Rothberg <rothberg@redhat.com> | 2020-01-15 13:14:56 +0100 |
commit | fb5e5973027afa7659831a7988208ade4e667c84 (patch) | |
tree | d75eb870e29f7865199c8d2ba70143f02ee05f09 /pkg | |
parent | 0f6955c30e26b5d8bad4eb2360993e7e3cf708d6 (diff) | |
download | podman-fb5e5973027afa7659831a7988208ade4e667c84.tar.gz podman-fb5e5973027afa7659831a7988208ade4e667c84.tar.bz2 podman-fb5e5973027afa7659831a7988208ade4e667c84.zip |
v2: stats: rigorous error checks
Also expect the container to be running.
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/api/handlers/generic/containers_stats.go | 43 |
1 files changed, 13 insertions, 30 deletions
diff --git a/pkg/api/handlers/generic/containers_stats.go b/pkg/api/handlers/generic/containers_stats.go index b8021378b..4fb856bdb 100644 --- a/pkg/api/handlers/generic/containers_stats.go +++ b/pkg/api/handlers/generic/containers_stats.go @@ -51,10 +51,7 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) { return } if state != define.ContainerStateRunning && !query.Stream { - utils.WriteJSON(w, http.StatusOK, &handlers.Stats{StatsJSON: docker.StatsJSON{ - Name: ctnr.Name(), - ID: ctnr.ID(), - }}) + utils.InternalServerError(w, define.ErrCtrStateInvalid) return } @@ -82,44 +79,30 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) { time.Sleep(DefaultStatsPeriod) } - // Anonymous function to get the cgroup on demand. - getCgroup := func() *cgroups.CgroupControl { - cgroupPath, err := ctnr.CGroupPath() - if err != nil { - return &cgroups.CgroupControl{} - } - - cgroup, err := cgroups.Load(cgroupPath) - if err != nil { - return &cgroups.CgroupControl{} - } - - return cgroup - } - for ok := true; ok; ok = query.Stream { - state, err := ctnr.State() + // Container stats + stats, err := ctnr.GetContainerStats(stats) if err != nil { utils.InternalServerError(w, err) return } - if state != define.ContainerStateRunning { - time.Sleep(10 * time.Second) - continue + inspect, err := ctnr.Inspect(false) + if err != nil { + utils.InternalServerError(w, err) + return } - - cgroup := getCgroup() - cgroupStat, err := cgroup.Stat() + // Cgroup stats + cgroupPath, err := ctnr.CGroupPath() if err != nil { - cgroupStat = &cgroups.Metrics{} + utils.InternalServerError(w, err) + return } - - stats, err := ctnr.GetContainerStats(stats) + cgroup, err := cgroups.Load(cgroupPath) if err != nil { utils.InternalServerError(w, err) return } - inspect, err := ctnr.Inspect(false) + cgroupStat, err := cgroup.Stat() if err != nil { utils.InternalServerError(w, err) return |