Browse code

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 <don.hcd@gmail.com>

Donald Huang authored on 2015/10/31 07:04:21
Showing 1 changed files
... ...
@@ -71,7 +71,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats
71 71
 				return nil
72 72
 			}
73 73
 
74
-			statsJSON := getStatJSON(v)
74
+			var statsJSON interface{}
75
+			statsJSONPost120 := getStatJSON(v)
75 76
 			if config.Version.LessThan("1.21") {
76 77
 				var (
77 78
 					rxBytes   uint64
... ...
@@ -83,7 +84,7 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats
83 83
 					txErrors  uint64
84 84
 					txDropped uint64
85 85
 				)
86
-				for _, v := range statsJSON.Networks {
86
+				for _, v := range statsJSONPost120.Networks {
87 87
 					rxBytes += v.RxBytes
88 88
 					rxPackets += v.RxPackets
89 89
 					rxErrors += v.RxErrors
... ...
@@ -93,8 +94,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats
93 93
 					txErrors += v.TxErrors
94 94
 					txDropped += v.TxDropped
95 95
 				}
96
-				statsJSONPre121 := &v1p20.StatsJSON{
97
-					Stats: statsJSON.Stats,
96
+				statsJSON = &v1p20.StatsJSON{
97
+					Stats: statsJSONPost120.Stats,
98 98
 					Network: types.NetworkStats{
99 99
 						RxBytes:   rxBytes,
100 100
 						RxPackets: rxPackets,
... ...
@@ -106,20 +107,8 @@ func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStats
106 106
 						TxDropped: txDropped,
107 107
 					},
108 108
 				}
109
-
110
-				if !config.Stream && noStreamFirstFrame {
111
-					// prime the cpu stats so they aren't 0 in the final output
112
-					noStreamFirstFrame = false
113
-					continue
114
-				}
115
-
116
-				if err := enc.Encode(statsJSONPre121); err != nil {
117
-					return err
118
-				}
119
-
120
-				if !config.Stream {
121
-					return nil
122
-				}
109
+			} else {
110
+				statsJSON = statsJSONPost120
123 111
 			}
124 112
 
125 113
 			if !config.Stream && noStreamFirstFrame {