Add progressbar when downloading kic base image

pull/10887/head
Anders F Björklund 2021-03-21 10:09:26 +01:00
parent dea7aff19f
commit 2cfc608bbf
3 changed files with 37 additions and 5 deletions

2
go.mod
View File

@ -108,7 +108,7 @@ replace (
git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
github.com/briandowns/spinner => github.com/alonyb/spinner v1.12.6
github.com/docker/machine => github.com/machine-drivers/machine v0.7.1-0.20200824110434-7da9b61f0a42
github.com/google/go-containerregistry => github.com/afbjorklund/go-containerregistry v0.1.2-0.20210306075852-e67a8ff8ae6f
github.com/google/go-containerregistry => github.com/afbjorklund/go-containerregistry v0.4.1-0.20210321082428-ecd05871b469
github.com/samalba/dockerclient => github.com/sayboras/dockerclient v1.0.0
k8s.io/api => k8s.io/api v0.17.3
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.17.3

2
go.sum
View File

@ -94,6 +94,8 @@ github.com/VividCortex/godaemon v0.0.0-20201030160542-15e3f4925a21 h1:Pgxfz/g+Xy
github.com/VividCortex/godaemon v0.0.0-20201030160542-15e3f4925a21/go.mod h1:Y8CJ3IwPIAkMhv/rRUWIlczaeqd9ty9yrl+nc2AbaL4=
github.com/afbjorklund/go-containerregistry v0.1.2-0.20210306075852-e67a8ff8ae6f h1:TqV4eXL41kacdMcZz2wVkodIKMh4Ruc3rGLP5T/bRgQ=
github.com/afbjorklund/go-containerregistry v0.1.2-0.20210306075852-e67a8ff8ae6f/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/afbjorklund/go-containerregistry v0.4.1-0.20210321082428-ecd05871b469 h1:Idy/v8PWwrQimq2QOwDyW4KOLJ25hgn+smqsXjRKJHM=
github.com/afbjorklund/go-containerregistry v0.4.1-0.20210321082428-ecd05871b469/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

View File

@ -27,6 +27,7 @@ import (
"strings"
"time"
"github.com/cheggaaa/pb/v3"
"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
@ -137,6 +138,9 @@ func Tag(img string) string {
// WriteImageToDaemon write img to the local docker daemon
func WriteImageToDaemon(img string) error {
// buffered channel
c := make(chan v1.Update, 200)
klog.Infof("Writing %s to local daemon", img)
ref, err := name.ParseReference(img)
if err != nil {
@ -156,12 +160,38 @@ func WriteImageToDaemon(img string) error {
return errors.Wrap(err, "getting remote image")
}
klog.V(3).Infof("Writing image %v", ref)
_, err = daemon.Write(ref, i)
if err != nil {
return errors.Wrap(err, "writing daemon image")
errchan := make(chan error)
p := pb.Full.Start64(0)
fn := strings.Split(ref.Name(), "@")[0]
// abbreviate filename for progress
maxwidth := 30 - len("...")
if len(fn) > maxwidth {
fn = fn[0:maxwidth] + "..."
}
p.Set("prefix", " > "+fn+": ")
p.Set(pb.Bytes, true)
return nil
// Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs
p.SetWidth(79)
go func() {
_, err = daemon.Write(ref, i, tarball.WithProgress(c))
errchan <- err
}()
var update v1.Update
for {
select {
case update = <-c:
p.SetCurrent(update.Complete)
p.SetTotal(update.Total)
case err = <-errchan:
p.Finish()
if err != nil {
return errors.Wrap(err, "writing daemon image")
}
return nil
}
}
}
func retrieveImage(ref name.Reference) (v1.Image, error) {