Skip to content

Commit

Permalink
Merge pull request #10887 from afbjorklund/kicbase-progress
Browse files Browse the repository at this point in the history
UI: Add progressbar when downloading kic base image
  • Loading branch information
medyagh authored Mar 21, 2021
2 parents dea7aff + 1c5d35e commit 185b4a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20210321165649-761f6f9626b1
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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ 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/afbjorklund/go-containerregistry v0.4.1-0.20210321165649-761f6f9626b1 h1:AI8EIk8occ3pruhaTpkaQxQGlC1dHx3J9hAtg7t+FLI=
github.com/afbjorklund/go-containerregistry v0.4.1-0.20210321165649-761f6f9626b1/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=
Expand Down
39 changes: 35 additions & 4 deletions pkg/minikube/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -86,6 +87,7 @@ func DigestByGoLib(imgName string) string {
// ExistsImageInDaemon if img exist in local docker daemon
func ExistsImageInDaemon(img string) bool {
// Check if image exists locally
klog.Infof("Checking for %s in local docker daemon", img)
cmd := exec.Command("docker", "images", "--format", "{{.Repository}}:{{.Tag}}@{{.Digest}}")
if output, err := cmd.Output(); err == nil {
if strings.Contains(string(output), img) {
Expand Down Expand Up @@ -137,6 +139,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 {
Expand All @@ -156,12 +161,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) {
Expand Down

0 comments on commit 185b4a7

Please sign in to comment.