From d2c04f844b8258d712da4b8feac25df7590b037c Mon Sep 17 00:00:00 2001 From: Donald Huang Date: Fri, 30 Oct 2015 22:04:21 +0000 Subject: [PATCH] fix pre-1.22 docker stats This fixes a bug introduced in #15786: * if a pre-v1.20 client requested docker stats, the daemon would return both an API-compatible JSON blob *and* an API-incompatible JSON blob: see https://gist.github.com/donhcd/338a5b3681cd6a071629 Signed-off-by: Donald Huang --- daemon/stats.go | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/daemon/stats.go b/daemon/stats.go index d3837e14c6..05e03709a3 100644 --- a/daemon/stats.go +++ b/daemon/stats.go @@ -71,7 +71,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats return nil } - statsJSON := getStatJSON(v) + var statsJSON interface{} + statsJSONPost120 := getStatJSON(v) if config.Version.LessThan("1.21") { var ( rxBytes uint64 @@ -83,7 +84,7 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats txErrors uint64 txDropped uint64 ) - for _, v := range statsJSON.Networks { + for _, v := range statsJSONPost120.Networks { rxBytes += v.RxBytes rxPackets += v.RxPackets rxErrors += v.RxErrors @@ -93,8 +94,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats txErrors += v.TxErrors txDropped += v.TxDropped } - statsJSONPre121 := &v1p20.StatsJSON{ - Stats: statsJSON.Stats, + statsJSON = &v1p20.StatsJSON{ + Stats: statsJSONPost120.Stats, Network: types.NetworkStats{ RxBytes: rxBytes, RxPackets: rxPackets, @@ -106,20 +107,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats TxDropped: txDropped, }, } - - if !config.Stream && noStreamFirstFrame { - // prime the cpu stats so they aren't 0 in the final output - noStreamFirstFrame = false - continue - } - - if err := enc.Encode(statsJSONPre121); err != nil { - return err - } - - if !config.Stream { - return nil - } + } else { + statsJSON = statsJSONPost120 } if !config.Stream && noStreamFirstFrame {