Address code review comments`

pull/9808/head
Priya Wadhwa 2020-11-30 19:03:30 -08:00
parent a2bc2fe64c
commit ae4fa9a2ee
2 changed files with 65 additions and 28 deletions

View File

@ -19,6 +19,7 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -97,8 +98,20 @@ func execute() error {
}
defer sd.StopMetricsExporter()
ctx := context.Background()
// create a tmp file to download minikube to
tmp, err := ioutil.TempFile("", binary())
if err != nil {
return errors.Wrap(err, "creating tmp file")
}
if err := tmp.Close(); err != nil {
return errors.Wrap(err, "closing file")
}
defer os.Remove(tmp.Name())
// start loop where we will track minikube start time
for {
st, err := minikubeStartTime(ctx, projectID)
st, err := minikubeStartTime(ctx, projectID, tmp.Name())
if err != nil {
log.Printf("error collecting start time: %v", err)
continue
@ -109,12 +122,10 @@ func execute() error {
}
}
func minikubeStartTime(ctx context.Context, projectID string) (float64, error) {
minikubePath, err := downloadMinikube()
if err != nil {
func minikubeStartTime(ctx context.Context, projectID, minikubePath string) (float64, error) {
if err := downloadMinikube(ctx, minikubePath); err != nil {
return 0, errors.Wrap(err, "downloading minikube")
}
defer os.Remove(minikubePath)
defer deleteMinikube(ctx, minikubePath)
cmd := exec.CommandContext(ctx, minikubePath, "start", "--driver=docker", "-p", profile, "--memory=2000", "--trace=gcp")
@ -123,11 +134,12 @@ func minikubeStartTime(ctx context.Context, projectID string) (float64, error) {
cmd.Stderr = os.Stderr
t := time.Now()
log.Print("Running minikube start....")
log.Printf("Running [%v]....", cmd.Args)
if err := cmd.Run(); err != nil {
return 0, errors.Wrapf(err, "running %v", cmd.Args)
}
return time.Since(t).Seconds(), nil
totalTime := time.Since(t).Seconds()
return totalTime, nil
}
func deleteMinikube(ctx context.Context, minikubePath string) {

View File

@ -21,7 +21,9 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"log"
@ -33,39 +35,62 @@ const (
bucketName = "minikube/latest"
)
// download minikube latest to a tmp file
func downloadMinikube() (string, error) {
b := binary()
tmp, err := ioutil.TempFile("", b)
// download minikube latest to file
func downloadMinikube(ctx context.Context, minikubePath string) error {
client, err := storage.NewClient(ctx)
if err != nil {
return "", errors.Wrap(err, "creating tmp file")
return errors.Wrap(err, "creating client")
}
if err := tmp.Close(); err != nil {
return "", errors.Wrap(err, "closing tmp file")
if localMinikubeIsLatest(ctx, minikubePath, client) {
log.Print("local minikube is latest, skipping download...")
return nil
}
client, err := storage.NewClient(context.Background())
os.Remove(minikubePath)
// download minikube binary from GCS
obj := client.Bucket("minikube").Object(fmt.Sprintf("latest/%s", binary()))
rc, err := obj.NewReader(ctx)
if err != nil {
return "", errors.Wrap(err, "creating client")
}
ctx := context.Background()
rc, err := client.Bucket(bucketName).Object(b).NewReader(ctx)
if err != nil {
return "", errors.Wrap(err, "gcs new reader")
return errors.Wrap(err, "gcs new reader")
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
return "", errors.Wrap(err, "ioutil read all")
return errors.Wrap(err, "ioutil read all")
}
log.Printf("downloading gs://%s/%s to %v", bucketName, b, tmp.Name())
if err := ioutil.WriteFile(tmp.Name(), data, 0777); err != nil {
return "", errors.Wrap(err, "writing file")
log.Printf("downloading gs://%s/%s to %v", bucketName, binary(), minikubePath)
if err := ioutil.WriteFile(minikubePath, data, 0777); err != nil {
return errors.Wrap(err, "writing minikubePath")
}
if err := os.Chmod(tmp.Name(), 0700); err != nil {
return "", errors.Wrap(err, "chmod")
if err := os.Chmod(minikubePath, 0700); err != nil {
return errors.Wrap(err, "chmod")
}
return tmp.Name(), nil
return nil
}
// localMinikubeIsLatest returns true if the local version of minikube
// matches the latest version in GCS
func localMinikubeIsLatest(ctx context.Context, minikubePath string, client *storage.Client) bool {
log.Print("checking if local minikube is latest...")
obj := client.Bucket("minikube").Object(fmt.Sprintf("latest/%s", binary()))
attrs, err := obj.Attrs(ctx)
if err != nil {
log.Printf("error getting %s object attrs: %v", obj.ObjectName(), err)
return false
}
gcsMinikubeVersion, ok := attrs.Metadata["commit"]
if !ok {
log.Printf("there is no commit: %v", attrs.Metadata)
return false
}
currentMinikubeVersion, err := exec.Command(minikubePath, "version", "--output=json").Output()
if err != nil {
log.Printf("error running [%s version]: %v", minikubePath, err)
return false
}
return strings.Contains(string(currentMinikubeVersion), gcsMinikubeVersion)
}
func binary() string {