Merge pull request #8709 from priyawadhwa/json-output-download

Implement Download and DownloadProgress types for JSON output
pull/8737/head
priyawadhwa 2020-07-14 15:56:56 -04:00 committed by GitHub
commit c2e63844db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 4 deletions

View File

@ -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},

View File

@ -0,0 +1,70 @@
/*
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.
*/
package download
import (
"fmt"
"io"
"sync"
"github.com/hashicorp/go-getter"
"k8s.io/minikube/pkg/minikube/out/register"
)
var DefaultJSONOutput getter.ProgressTracker = &jsonOutput{}
type jsonOutput struct {
lock sync.Mutex
}
// 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
}

View File

@ -27,3 +27,15 @@ func PrintInfo(message string) {
s := NewInfo(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)
}

View File

@ -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,45 @@ 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,
}}
}
// Warning will be used to notify the user of warnings
type Warning struct {
}