| 1 |
package imageprogress |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"time" |
| 12 |
|
| 13 |
"go.podman.io/storage/pkg/regexp" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
defaultProgressTimeThreshhold = 30 * time.Second |
| 18 |
defaultStableThreshhold = 10 |
| 19 |
) |
| 20 |
|
| 21 |
// progressLine is a structure representation of a Docker pull progress line |
| 22 |
type progressLine struct { |
| 23 |
ID string `json:"id"` |
| 24 |
Status string `json:"status"` |
| 25 |
Detail *progressDetail `json:"progressDetail"` |
| 26 |
Error string `json:"error"` |
| 27 |
} |
| 28 |
|
| 29 |
// progressDetail is the progressDetail structure in a Docker pull progress line |
| 30 |
type progressDetail struct { |
| 31 |
Current int64 `json:"current"` |
| 32 |
Total int64 `json:"total"` |
| 33 |
} |
| 34 |
|
| 35 |
// layerDetail is layer information associated with a specific layerStatus |
| 36 |
type layerDetail struct { |
| 37 |
Count int |
| 38 |
Current int64 |
| 39 |
Total int64 |
| 40 |
} |
| 41 |
|
| 42 |
// layerStatus is one of different possible status for layers detected by |
| 43 |
// the ProgressWriter |
| 44 |
type layerStatus int |
| 45 |
|
| 46 |
const ( |
| 47 |
statusPending layerStatus = iota |
| 48 |
statusDownloading |
| 49 |
statusExtracting |
| 50 |
statusComplete |
| 51 |
statusPushing |
| 52 |
) |
| 53 |
|
| 54 |
// layerStatusFromDockerString translates a string in a Docker status |
| 55 |
// line to a layerStatus |
| 56 |
func layerStatusFromDockerString(dockerStatus string) layerStatus { |
| 57 |
switch dockerStatus { |
| 58 |
case "Pushing": |
| 59 |
return statusPushing |
| 60 |
case "Downloading": |
| 61 |
return statusDownloading |
| 62 |
case "Extracting", "Verifying Checksum", "Download complete": |
| 63 |
return statusExtracting |
| 64 |
case "Pull complete", "Already exists", "Pushed", "Layer already exists": |
| 65 |
return statusComplete |
| 66 |
default: |
| 67 |
return statusPending |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
type report map[layerStatus]*layerDetail |
| 72 |
|
| 73 |
func (r report) count(status layerStatus) int { |
| 74 |
detail, ok := r[status] |
| 75 |
if !ok { |
| 76 |
return 0 |
| 77 |
} |
| 78 |
return detail.Count |
| 79 |
} |
| 80 |
|
| 81 |
func (r report) percentProgress(status layerStatus) float32 { |
| 82 |
detail, ok := r[status] |
| 83 |
if !ok { |
| 84 |
return 0 |
| 85 |
} |
| 86 |
if detail.Total == 0 { |
| 87 |
return 0 |
| 88 |
} |
| 89 |
pct := float32(detail.Current) / float32(detail.Total) * 100.0 |
| 90 |
if pct > 100.0 { |
| 91 |
pct = 100.0 |
| 92 |
} |
| 93 |
if pct < 0.0 { |
| 94 |
pct = 0.0 |
| 95 |
} |
| 96 |
return pct |
| 97 |
} |
| 98 |
|
| 99 |
func (r report) totalCount() int { |
| 100 |
cnt := 0 |
| 101 |
for _, detail := range r { |
| 102 |
cnt += detail.Count |
| 103 |
} |
| 104 |
return cnt |
| 105 |
} |
| 106 |
|
| 107 |
// String is used for test output |
| 108 |
func (r report) String() string { |
| 109 |
result := &bytes.Buffer{} |
| 110 |
fmt.Fprintf(result, "{") |
| 111 |
for k := range r { |
| 112 |
var status string |
| 113 |
switch k { |
| 114 |
case statusPending: |
| 115 |
status = "pending" |
| 116 |
case statusDownloading: |
| 117 |
status = "downloading" |
| 118 |
case statusExtracting: |
| 119 |
status = "extracting" |
| 120 |
case statusComplete: |
| 121 |
status = "complete" |
| 122 |
} |
| 123 |
fmt.Fprintf(result, "%s:{Count: %d, Current: %d, Total: %d}, ", status, r[k].Count, r[k].Current, r[k].Total) |
| 124 |
} |
| 125 |
fmt.Fprintf(result, "}") |
| 126 |
return result.String() |
| 127 |
} |
| 128 |
|
| 129 |
// newWriter creates a writer that periodically reports |
| 130 |
// on pull/push progress of a Docker image. It only reports when the state of the |
| 131 |
// different layers has changed and uses time thresholds to limit the |
| 132 |
// rate of the reports. |
| 133 |
func newWriter(reportFn func(report), layersChangedFn func(report, report) bool) io.WriteCloser { |
| 134 |
writer := &imageProgressWriter{ |
| 135 |
mutex: &sync.Mutex{}, |
| 136 |
layerStatus: map[string]*progressLine{}, |
| 137 |
reportFn: reportFn, |
| 138 |
layersChangedFn: layersChangedFn, |
| 139 |
progressTimeThreshhold: defaultProgressTimeThreshhold, |
| 140 |
stableThreshhold: defaultStableThreshhold, |
| 141 |
} |
| 142 |
return writer |
| 143 |
} |
| 144 |
|
| 145 |
type imageProgressWriter struct { |
| 146 |
mutex *sync.Mutex |
| 147 |
internalWriter io.WriteCloser |
| 148 |
readingGoroutineStatus <-chan error // Exists if internalWriter != nil |
| 149 |
layerStatus map[string]*progressLine |
| 150 |
lastLayerCount int |
| 151 |
stableLines int |
| 152 |
stableThreshhold int |
| 153 |
progressTimeThreshhold time.Duration |
| 154 |
lastReport report |
| 155 |
lastReportTime time.Time |
| 156 |
reportFn func(report) |
| 157 |
layersChangedFn func(report, report) bool |
| 158 |
} |
| 159 |
|
| 160 |
func (w *imageProgressWriter) Write(data []byte) (int, error) { |
| 161 |
w.mutex.Lock() |
| 162 |
defer w.mutex.Unlock() |
| 163 |
if w.internalWriter == nil { |
| 164 |
var pipeIn *io.PipeReader |
| 165 |
statusChannel := make(chan error, 1) // Buffered, so that sending a value after this or our caller has failed and exited does not block. |
| 166 |
pipeIn, w.internalWriter = io.Pipe() |
| 167 |
w.readingGoroutineStatus = statusChannel |
| 168 |
go w.readingGoroutine(statusChannel, pipeIn) |
| 169 |
} |
| 170 |
return w.internalWriter.Write(data) |
| 171 |
} |
| 172 |
|
| 173 |
func (w *imageProgressWriter) Close() error { |
| 174 |
w.mutex.Lock() |
| 175 |
defer w.mutex.Unlock() |
| 176 |
|
| 177 |
if w.internalWriter == nil { |
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
err := w.internalWriter.Close() // As of Go 1.9 and 1.10, PipeWriter.Close() always returns nil |
| 182 |
readingErr := <-w.readingGoroutineStatus |
| 183 |
if err == nil && readingErr != nil { |
| 184 |
err = readingErr |
| 185 |
} |
| 186 |
return err |
| 187 |
} |
| 188 |
|
| 189 |
func (w *imageProgressWriter) readingGoroutine(statusChannel chan<- error, pipeIn *io.PipeReader) { |
| 190 |
err := errors.New("Internal error: unexpected panic in imageProgressWriter.readingGoroutine") |
| 191 |
defer func() { statusChannel <- err }() |
| 192 |
defer func() { |
| 193 |
if err != nil { |
| 194 |
pipeIn.CloseWithError(err) |
| 195 |
} else { |
| 196 |
pipeIn.Close() |
| 197 |
} |
| 198 |
}() |
| 199 |
|
| 200 |
err = w.readProgress(pipeIn) |
| 201 |
// err is nil on reaching EOF |
| 202 |
} |
| 203 |
|
| 204 |
func (w *imageProgressWriter) readProgress(pipeIn *io.PipeReader) error { |
| 205 |
decoder := json.NewDecoder(pipeIn) |
| 206 |
for { |
| 207 |
line := &progressLine{} |
| 208 |
err := decoder.Decode(line) |
| 209 |
if err == io.EOF { |
| 210 |
break |
| 211 |
} |
| 212 |
if err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
err = w.processLine(line) |
| 216 |
if err != nil { |
| 217 |
return err |
| 218 |
} |
| 219 |
} |
| 220 |
return nil |
| 221 |
} |
| 222 |
|
| 223 |
func (w *imageProgressWriter) processLine(line *progressLine) error { |
| 224 |
|
| 225 |
if err := getError(line); err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
|
| 229 |
// determine if it's a line we want to process |
| 230 |
if !islayerStatus(line) { |
| 231 |
return nil |
| 232 |
} |
| 233 |
|
| 234 |
w.layerStatus[line.ID] = line |
| 235 |
|
| 236 |
// if the number of layers has not stabilized yet, return and wait for more |
| 237 |
// progress |
| 238 |
if !w.isStableLayerCount() { |
| 239 |
return nil |
| 240 |
} |
| 241 |
|
| 242 |
r := createReport(w.layerStatus) |
| 243 |
|
| 244 |
// check if the count of layers in each state has changed |
| 245 |
if w.layersChangedFn(w.lastReport, r) { |
| 246 |
w.lastReport = r |
| 247 |
w.lastReportTime = time.Now() |
| 248 |
w.reportFn(r) |
| 249 |
return nil |
| 250 |
} |
| 251 |
// If layer counts haven't changed, but enough time has passed (30 sec by default), |
| 252 |
// at least report on download/push progress |
| 253 |
if time.Since(w.lastReportTime) > w.progressTimeThreshhold { |
| 254 |
w.lastReport = r |
| 255 |
w.lastReportTime = time.Now() |
| 256 |
w.reportFn(r) |
| 257 |
} |
| 258 |
return nil |
| 259 |
} |
| 260 |
|
| 261 |
func (w *imageProgressWriter) isStableLayerCount() bool { |
| 262 |
// If the number of layers has changed since last status, we're not stable |
| 263 |
if w.lastLayerCount != len(w.layerStatus) { |
| 264 |
w.lastLayerCount = len(w.layerStatus) |
| 265 |
w.stableLines = 0 |
| 266 |
return false |
| 267 |
} |
| 268 |
// Only proceed after we've received status for the same number |
| 269 |
// of layers at least stableThreshhold times. If not, they're still increasing |
| 270 |
w.stableLines++ |
| 271 |
return w.stableLines >= w.stableThreshhold |
| 272 |
} |
| 273 |
|
| 274 |
var layerIDRegexp = regexp.Delayed("^[a-f0-9]*$") |
| 275 |
|
| 276 |
func islayerStatus(line *progressLine) bool { |
| 277 |
// ignore status lines with no layer id |
| 278 |
if len(line.ID) == 0 { |
| 279 |
return false |
| 280 |
} |
| 281 |
// ignore layer ids that are not hex string |
| 282 |
if !layerIDRegexp.MatchString(line.ID) { |
| 283 |
return false |
| 284 |
} |
| 285 |
// ignore retrying status |
| 286 |
if strings.HasPrefix(line.Status, "Retrying") { |
| 287 |
return false |
| 288 |
} |
| 289 |
return true |
| 290 |
} |
| 291 |
|
| 292 |
func getError(line *progressLine) error { |
| 293 |
if len(line.Error) > 0 { |
| 294 |
return errors.New(line.Error) |
| 295 |
} |
| 296 |
return nil |
| 297 |
} |
| 298 |
|
| 299 |
func createReport(dockerProgress map[string]*progressLine) report { |
| 300 |
r := report{} |
| 301 |
for _, line := range dockerProgress { |
| 302 |
layerStatus := layerStatusFromDockerString(line.Status) |
| 303 |
detail, exists := r[layerStatus] |
| 304 |
if !exists { |
| 305 |
detail = &layerDetail{} |
| 306 |
r[layerStatus] = detail |
| 307 |
} |
| 308 |
detail.Count++ |
| 309 |
if line.Detail != nil { |
| 310 |
detail.Current += line.Detail.Current |
| 311 |
detail.Total += line.Detail.Total |
| 312 |
} |
| 313 |
} |
| 314 |
return r |
| 315 |
} |