From d29a9dca67cc4644e00f1e62a571aaff0fe2342e Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Fri, 10 Jul 2020 11:58:23 -0400 Subject: [PATCH 1/4] Add Download and DownloadProgress types implementations --- pkg/minikube/download/json_output.go | 24 +++++++++++++++++++++ pkg/minikube/out/register/json.go | 12 +++++++++++ pkg/minikube/out/register/log.go | 32 +++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 pkg/minikube/download/json_output.go diff --git a/pkg/minikube/download/json_output.go b/pkg/minikube/download/json_output.go new file mode 100644 index 0000000000..b7710d7724 --- /dev/null +++ b/pkg/minikube/download/json_output.go @@ -0,0 +1,24 @@ +/* +Copyright 2020 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This file implements a go-getter wrapper for cheggaaa progress bar +// based on: +// https://github.com/hashicorp/go-getter/blob/master/cmd/go-getter/progress_tracking.go + +package download + +type jsonOutput struct { +} diff --git a/pkg/minikube/out/register/json.go b/pkg/minikube/out/register/json.go index 4521796bd2..13b1760bc9 100644 --- a/pkg/minikube/out/register/json.go +++ b/pkg/minikube/out/register/json.go @@ -21,3 +21,15 @@ func PrintStep(message string) { s := NewStep(message) printAsCloudEvent(s, s.data) } + +// PrintDownload prints a Download type in JSON format +func PrintDownload(artifact string) { + s := NewDownload(artifact) + printAsCloudEvent(s, s.data) +} + +// PrintDownloadProgress prints a DownloadProgress type in JSON format +func PrintDownloadProgress(artifact, progress string) { + s := NewDownloadProgress(artifact, progress) + printAsCloudEvent(s, s.data) +} diff --git a/pkg/minikube/out/register/log.go b/pkg/minikube/out/register/log.go index 27b571c2f1..f1e7a9c032 100644 --- a/pkg/minikube/out/register/log.go +++ b/pkg/minikube/out/register/log.go @@ -27,10 +27,12 @@ type Step struct { data map[string]string } +// Type returns the cloud events compatible type of this struct func (s *Step) Type() string { return "io.k8s.sigs.minikube.step" } +// NewStep returns a new step type func NewStep(message string) *Step { return &Step{data: map[string]string{ "totalsteps": Reg.totalSteps(), @@ -40,23 +42,47 @@ func NewStep(message string) *Step { }} } -// TODO (priyawadhwa@): implement all types below this comment // Download will be used to notify the user that a download has begun type Download struct { + data map[string]string } +// Type returns the cloud events compatible type of this struct func (s *Download) Type() string { return "io.k8s.sigs.minikube.download" } -// DownloadProgress will be used to notify the user around the progress of a download -type DownloadProgress struct { +// NewDownload returns a new download type +func NewDownload(artifact string) *Download { + return &Download{data: map[string]string{ + "totalsteps": Reg.totalSteps(), + "currentstep": Reg.currentStep(), + "artifact": artifact, + }} } +// DownloadProgress will be used to notify the user around the progress of a download +type DownloadProgress struct { + data map[string]string +} + +// Type returns the cloud events compatible type of this struct func (s *DownloadProgress) Type() string { return "io.k8s.sigs.minikube.download.progress" } +// NewDownloadProgress returns a new download progress type +func NewDownloadProgress(artifact, progress string) *DownloadProgress { + return &DownloadProgress{data: map[string]string{ + "totalsteps": Reg.totalSteps(), + "currentstep": Reg.currentStep(), + "progress": progress, + "artifact": artifact, + }} +} + +// TODO (priyawadhwa@): implement all types below this comment + // Warning will be used to notify the user of warnings type Warning struct { } From 3d105ef98fb284a7a6245264970f81daee1e2fcd Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Fri, 10 Jul 2020 12:31:08 -0400 Subject: [PATCH 2/4] Download and DownloadProgress implemented --- pkg/minikube/download/download.go | 7 +++- pkg/minikube/download/json_output.go | 52 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/pkg/minikube/download/download.go b/pkg/minikube/download/download.go index 45d7932d0e..f630803037 100644 --- a/pkg/minikube/download/download.go +++ b/pkg/minikube/download/download.go @@ -26,6 +26,7 @@ import ( "github.com/golang/glog" "github.com/hashicorp/go-getter" "github.com/pkg/errors" + "k8s.io/minikube/pkg/minikube/out" ) var ( @@ -39,13 +40,17 @@ func EnableMock(b bool) { // download is a well-configured atomic download function func download(src string, dst string) error { + progress := getter.WithProgress(DefaultProgressBar) + if out.JSON { + progress = getter.WithProgress(DefaultJSONOutput) + } tmpDst := dst + ".download" client := &getter.Client{ Src: src, Dst: tmpDst, Dir: false, Mode: getter.ClientModeFile, - Options: []getter.ClientOption{getter.WithProgress(DefaultProgressBar)}, + Options: []getter.ClientOption{progress}, Getters: map[string]getter.Getter{ "file": &getter.FileGetter{Copy: false}, "http": &getter.HttpGetter{Netrc: false}, diff --git a/pkg/minikube/download/json_output.go b/pkg/minikube/download/json_output.go index b7710d7724..fccee44730 100644 --- a/pkg/minikube/download/json_output.go +++ b/pkg/minikube/download/json_output.go @@ -20,5 +20,57 @@ limitations under the License. package download +import ( + "fmt" + "io" + "sync" + + "github.com/cheggaaa/pb/v3" + "github.com/hashicorp/go-getter" + "k8s.io/minikube/pkg/minikube/out/register" +) + +var DefaultJSONOutput getter.ProgressTracker = &jsonOutput{} + type jsonOutput struct { + lock sync.Mutex + progress *pb.ProgressBar +} + +// TrackProgress prints out progress of the stream in JSON format until closed +func (cpb *jsonOutput) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { + cpb.lock.Lock() + defer cpb.lock.Unlock() + + register.PrintDownload(src) + + return &readCloser{ + Reader: &jsonReader{ + Reader: stream, + artifact: src, + current: currentSize, + total: totalSize, + }, + close: func() error { + cpb.lock.Lock() + defer cpb.lock.Unlock() + return nil + }, + } +} + +// jsonReader is a wrapper for printing with JSON output +type jsonReader struct { + artifact string + current int64 + total int64 + io.Reader +} + +func (r *jsonReader) Read(p []byte) (n int, err error) { + n, err = r.Reader.Read(p) + r.current += int64(n) + progress := float64(r.current) / float64(r.total) + register.PrintDownloadProgress(r.artifact, fmt.Sprintf("%v", progress)) + return } From e771b72a829f7f229fd9e7d27ec62682b1c84294 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 13 Jul 2020 10:05:06 -0400 Subject: [PATCH 3/4] fix lint --- pkg/minikube/download/json_output.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pkg/minikube/download/json_output.go b/pkg/minikube/download/json_output.go index fccee44730..7a19838883 100644 --- a/pkg/minikube/download/json_output.go +++ b/pkg/minikube/download/json_output.go @@ -14,10 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// This file implements a go-getter wrapper for cheggaaa progress bar -// based on: -// https://github.com/hashicorp/go-getter/blob/master/cmd/go-getter/progress_tracking.go - package download import ( @@ -25,7 +21,6 @@ import ( "io" "sync" - "github.com/cheggaaa/pb/v3" "github.com/hashicorp/go-getter" "k8s.io/minikube/pkg/minikube/out/register" ) @@ -33,8 +28,7 @@ import ( var DefaultJSONOutput getter.ProgressTracker = &jsonOutput{} type jsonOutput struct { - lock sync.Mutex - progress *pb.ProgressBar + lock sync.Mutex } // TrackProgress prints out progress of the stream in JSON format until closed From cb03f56b1b4a31ebe50a429f6b73878139f04e9b Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Tue, 14 Jul 2020 13:39:33 -0400 Subject: [PATCH 4/4] remove todo --- pkg/minikube/out/register/log.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/minikube/out/register/log.go b/pkg/minikube/out/register/log.go index 8226e0c6cc..90d45948f9 100644 --- a/pkg/minikube/out/register/log.go +++ b/pkg/minikube/out/register/log.go @@ -81,8 +81,6 @@ func NewDownloadProgress(artifact, progress string) *DownloadProgress { }} } -// TODO (priyawadhwa@): implement all types below this comment - // Warning will be used to notify the user of warnings type Warning struct { }