Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for humanize time in #5005 #5079

Merged
merged 12 commits into from
Jan 19, 2021
34 changes: 30 additions & 4 deletions integration/skaffold/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -186,7 +187,14 @@ func (b *RunBuilder) RunBackground(t *testing.T) io.ReadCloser {

go func() {
cmd.Wait()
logrus.Infoln("Ran in", time.Since(start))
//Create human readable time string
showsTime := humanize.Time(start)

//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Ran", showsTime)
}()

t.Cleanup(func() {
Expand Down Expand Up @@ -215,8 +223,14 @@ func (b *RunBuilder) Run(t *testing.T) error {
if err := cmd.Run(); err != nil {
return fmt.Errorf("skaffold %q: %w", b.command, err)
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Ran in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Ran", showsTime)
return nil
}

Expand All @@ -233,8 +247,14 @@ func (b *RunBuilder) RunWithCombinedOutput(t *testing.T) ([]byte, error) {
if err != nil {
return out, fmt.Errorf("skaffold %q: %w", b.command, err)
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Ran in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Ran", showsTime)
return out, nil
}

Expand All @@ -256,8 +276,14 @@ func (b *RunBuilder) RunOrFailOutput(t *testing.T) []byte {
}
t.Fatalf("skaffold %s: %v, %s", b.command, err, out)
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Ran in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Ran", showsTime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would make an excellent utility function so you don't have to repeat the code.

Suggested change
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Ran", showsTime)
logrus.Infoln("Ran", util.showHumanizeTime(start))

and

def showHumanizeTime(start time.Time) string {

showsTime := humanize.Time(start)
if time.Since(start).Seconds() < 1 {
	return  time.Since(start).String() + " ago"
}
return humanize.Time(start)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a function in util and reworked its usage in the files.

return out
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/skaffold/build/cache/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
Expand Down Expand Up @@ -110,8 +111,14 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
Tag: uniqueTag,
})
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Cache check complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Cache check completed", showsTime)

bRes, err := buildAndTest(ctx, out, tags, needToBuild)
if err != nil {
Expand Down
42 changes: 32 additions & 10 deletions pkg/skaffold/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io/ioutil"
"time"

"github.com/dustin/go-humanize"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
Expand Down Expand Up @@ -60,7 +62,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error {
}

fmt.Fprintln(out, " - Dependencies:", len(deps), "files")
fmt.Fprintf(out, " - Time to list dependencies: %v (2nd time: %v)\n", timeDeps1, timeDeps2)
fmt.Fprintf(out, " - Time to list dependencies: %s (2nd time: %s)\n", timeDeps1, timeDeps2)

timeSyncMap1, err := timeToConstructSyncMap(artifact, cfg)
if err != nil {
Expand All @@ -74,7 +76,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error {
return fmt.Errorf("construct artifact dependencies: %w", err)
}
} else {
fmt.Fprintf(out, " - Time to construct sync-map: %v (2nd time: %v)\n", timeSyncMap1, timeSyncMap2)
fmt.Fprintf(out, " - Time to construct sync-map: %s (2nd time: %s)\n", timeSyncMap1, timeSyncMap2)
}

timeMTimes1, err := timeToComputeMTimes(deps)
Expand All @@ -86,7 +88,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error {
return fmt.Errorf("computing modTimes: %w", err)
}

fmt.Fprintf(out, " - Time to compute mTimes on dependencies: %v (2nd time: %v)\n", timeMTimes1, timeMTimes2)
fmt.Fprintf(out, " - Time to compute mTimes on dependencies: %s (2nd time: %s)\n", timeMTimes1, timeMTimes2)
}

return nil
Expand All @@ -111,26 +113,46 @@ func typeOfArtifact(a *latest.Artifact) string {
}
}

func timeToListDependencies(ctx context.Context, a *latest.Artifact, cfg docker.Config) (time.Duration, []string, error) {
func timeToListDependencies(ctx context.Context, a *latest.Artifact, cfg docker.Config) (string, []string, error) {
start := time.Now()
paths, err := build.DependenciesForArtifact(ctx, a, cfg, nil)
return time.Since(start), paths, err
//Create human readable time string
showsTime := humanize.Time(start)

//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
return showsTime, paths, err
}

func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (time.Duration, error) {
func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (string, error) {
start := time.Now()
_, err := sync.SyncMap(a, cfg)
return time.Since(start), err
//Create human readable time string
showsTime := humanize.Time(start)

//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
return showsTime, err
}

func timeToComputeMTimes(deps []string) (time.Duration, error) {
func timeToComputeMTimes(deps []string) (string, error) {
start := time.Now()

if _, err := filemon.Stat(func() ([]string, error) { return deps, nil }); err != nil {
return 0, fmt.Errorf("computing modTimes: %w", err)
return "nil", fmt.Errorf("computing modTimes: %w", err)
}
//Create human readable time string
showsTime := humanize.Time(start)

return time.Since(start), nil
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
return showsTime, nil
}

func sizeOfDockerContext(ctx context.Context, a *latest.Artifact, cfg docker.Config) (int64, error) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/skaffold/runner/build_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
Expand Down Expand Up @@ -202,8 +203,14 @@ func (r *SkaffoldRunner) imageTags(ctx context.Context, out io.Writer, artifacts
if showWarning {
color.Yellow.Fprintln(out, "Some taggers failed. Rerun with -vdebug for errors.")
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Tags generated in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Tags generated", showsTime)
return imageTags, nil
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/skaffold/runner/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd/api"

Expand Down Expand Up @@ -157,6 +158,14 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer)
return err
}

color.Default.Fprintln(out, "Deployments stabilized in", time.Since(start))
//Create human readable time string
showsTime := humanize.Time(start)

//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}

color.Default.Fprintln(out, "Deployments stabilized ", showsTime)
return nil
}
9 changes: 8 additions & 1 deletion pkg/skaffold/runner/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
Expand Down Expand Up @@ -194,8 +195,14 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
event.DevLoopFailedWithErrorCode(r.devIteration, proto.StatusCode_DEVINIT_REGISTER_CONFIG_DEP, err)
return fmt.Errorf("watching skaffold configuration %q: %w", r.runCtx.ConfigurationFile(), err)
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("List generated in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("List generated ", showsTime)

// Init Sync State
if err := sync.Init(ctx, artifacts); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/skaffold/runner/load_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/docker/distribution/reference"
"github.com/dustin/go-humanize"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
Expand Down Expand Up @@ -85,8 +86,14 @@ func (r *SkaffoldRunner) loadImages(ctx context.Context, out io.Writer, artifact

color.Green.Fprintln(out, "Loaded")
}
//Create human readable time string
showsTime := humanize.Time(start)

color.Default.Fprintln(out, "Images loaded in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
color.Default.Fprintln(out, "Images loaded", showsTime)
return nil
}

Expand Down
41 changes: 36 additions & 5 deletions pkg/skaffold/runner/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"time"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
Expand Down Expand Up @@ -60,8 +61,14 @@ func (w withTimings) Build(ctx context.Context, out io.Writer, tags tag.ImageTag
if err != nil {
return nil, err
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Build complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Build completed", showsTime)
return bRes, nil
}

Expand All @@ -72,8 +79,14 @@ func (w withTimings) Test(ctx context.Context, out io.Writer, builds []build.Art
if err != nil {
return err
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Test complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Test completed", showsTime)
return nil
}

Expand All @@ -85,8 +98,14 @@ func (w withTimings) Deploy(ctx context.Context, out io.Writer, builds []build.A
if err != nil {
return nil, err
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Deploy complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Deploy completed", showsTime)
return ns, err
}

Expand All @@ -98,8 +117,14 @@ func (w withTimings) Cleanup(ctx context.Context, out io.Writer) error {
if err != nil {
return err
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Cleanup complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String() + " ago"
}
logrus.Infoln("Cleanup completed", showsTime)
return nil
}

Expand All @@ -111,7 +136,13 @@ func (w withTimings) Prune(ctx context.Context, out io.Writer) error {
if err != nil {
return err
}
//Create human readable time string
showsTime := humanize.Time(start)

logrus.Infoln("Image prune complete in", time.Since(start))
//Case for when it takes less than a second
if time.Since(start).Seconds() < 1 {
showsTime = time.Since(start).String()
}
logrus.Infoln("Image prune completed", showsTime)
return nil
}
Loading