From 07431a413d2bb03bc362f2a71c4e157569497ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=BE=D0=B2=D0=B0=D1=80=D0=B8=D1=89=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B8=D1=81=D1=82?= <2962928213@qq.com> Date: Mon, 23 Jan 2023 22:43:56 +0800 Subject: [PATCH] feat: Add kicbase download progress to JSON output --- pkg/minikube/download/image.go | 59 +++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/pkg/minikube/download/image.go b/pkg/minikube/download/image.go index 16dfa40bc472..d8a903d8cd86 100644 --- a/pkg/minikube/download/image.go +++ b/pkg/minikube/download/image.go @@ -24,6 +24,7 @@ import ( "path/filepath" "runtime" "strings" + "time" "github.com/cheggaaa/pb/v3" "github.com/google/go-containerregistry/pkg/name" @@ -37,6 +38,7 @@ import ( "k8s.io/minikube/pkg/minikube/image" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/out" + "k8s.io/minikube/pkg/minikube/out/register" ) var ( @@ -113,9 +115,6 @@ func ImageToCache(img string) error { return DownloadMock(img, f) } - // buffered channel - c := make(chan v1.Update, 200) - klog.Infof("Writing %s to local cache", img) ref, err := name.ParseReference(img) if err != nil { @@ -139,38 +138,54 @@ func ImageToCache(img string) error { return errors.Wrap(err, "getting remote image") } klog.V(3).Infof("Writing image %v", tag) + errchan := make(chan error) + // buffered channel + c := make(chan v1.Update, 200) + var p *pb.ProgressBar if out.JSON { - if err := tarball.WriteToFile(f, tag, i); err != nil { - return errors.Wrap(err, "writing tarball image") + register.PrintDownloadProgress(img, "0") + } else { + // if we need to print progress bar to stdout + p = pb.Full.Start64(0) + fn := image.Tag(ref.Name()) + // abbreviate filename for progress + maxwidth := 30 - len("...") + if len(fn) > maxwidth { + fn = fn[0:maxwidth] + "..." } - return nil - } - errchan := make(chan error) - p := pb.Full.Start64(0) - fn := image.Tag(ref.Name()) - // abbreviate filename for progress - maxwidth := 30 - len("...") - if len(fn) > maxwidth { - fn = fn[0:maxwidth] + "..." - } - p.Set("prefix", " > "+fn+": ") - p.Set(pb.Bytes, true) + p.Set("prefix", " > "+fn+": ") + p.Set(pb.Bytes, true) - // Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs - p.SetWidth(79) + // Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs + p.SetWidth(79) + } go func() { err = tarball.WriteToFile(f, tag, i, tarball.WithProgress(c)) errchan <- err }() var update v1.Update + previousTime := time.Now() for { select { case update = <-c: - p.SetCurrent(update.Complete) - p.SetTotal(update.Total) + if out.JSON { + now := time.Now() + if update.Complete == update.Total || now.Sub(previousTime) > time.Second*5 { + register.PrintDownloadProgress(img, fmt.Sprintf("%f", float64(update.Complete)/float64(update.Total))) + previousTime = now + } + } else { + p.SetCurrent(update.Complete) + p.SetTotal(update.Total) + } + case err = <-errchan: - p.Finish() + if out.JSON { + register.PrintDownloadProgress(img, "1") + } else { + p.Finish() + } if err != nil { return errors.Wrap(err, "writing tarball image") }