Skip to content

Commit

Permalink
Don’t print logs while deploy is running.
Browse files Browse the repository at this point in the history
But once deploy is done, print the logs up the
just before deploy was started.

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Jun 28, 2019
1 parent 580078b commit f181475
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
9 changes: 6 additions & 3 deletions pkg/skaffold/kubernetes/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type LogAggregator struct {
colorPicker ColorPicker

muted int32
startTime time.Time
sinceTime time.Time
cancel context.CancelFunc
trackedContainers trackedContainers
}
Expand All @@ -64,12 +64,15 @@ func NewLogAggregator(out io.Writer, baseImageNames []string, podSelector PodSel
}
}

func (a *LogAggregator) SetSince(t time.Time) {
a.sinceTime = t
}

// Start starts a logger that listens to pods and tail their logs
// if they are matched by the `podSelector`.
func (a *LogAggregator) Start(ctx context.Context) error {
cancelCtx, cancel := context.WithCancel(ctx)
a.cancel = cancel
a.startTime = time.Now()

aggregate := make(chan watch.Event)
stopWatchers, err := AggregatePodWatcher(a.namespaces, aggregate)
Expand Down Expand Up @@ -141,7 +144,7 @@ func (a *LogAggregator) streamContainerLogs(ctx context.Context, pod *v1.Pod, co
// In theory, it's more precise to use --since-time='' but there can be a time
// difference between the user's machine and the server.
// So we use --since=Xs and round up to the nearest second to not lose any log.
sinceSeconds := fmt.Sprintf("--since=%ds", sinceSeconds(time.Since(a.startTime)))
sinceSeconds := fmt.Sprintf("--since=%ds", sinceSeconds(time.Since(a.sinceTime)))

tr, tw := io.Pipe()
cmd := exec.CommandContext(ctx, "kubectl", "logs", sinceSeconds, "-f", pod.Name, "-c", container.Name, "--namespace", pod.Namespace)
Expand Down
11 changes: 8 additions & 3 deletions pkg/skaffold/runner/build_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package runner
import (
"context"
"io"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -79,14 +80,18 @@ func (r *SkaffoldRunner) DeployAndLog(ctx context.Context, out io.Writer, artifa
logger := r.newLoggerForImages(out, imageNames)
defer logger.Stop()

if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}
// Logs should be retrieve up to just before the deploy
logger.SetSince(time.Now())

if err := r.Deploy(ctx, out, artifacts); err != nil {
return err
}

// Start printing the logs after deploy is finished
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}

<-ctx.Done()

return nil
Expand Down
16 changes: 10 additions & 6 deletions pkg/skaffold/runner/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package runner
import (
"context"
"io"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
Expand Down Expand Up @@ -133,18 +134,21 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
return errors.Wrap(err, "exiting dev mode because first build failed")
}

// Start logs
if r.runCtx.Opts.TailDev {
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}
}
// Logs should be retrieve up to just before the deploy
logger.SetSince(time.Now())

// First deploy
if err := r.Deploy(ctx, out, r.builds); err != nil {
return errors.Wrap(err, "exiting dev mode because first deploy failed")
}

// Start printing the logs after deploy is finished
if r.runCtx.Opts.TailDev {
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}
}

// Forward ports
if err := forwarderManager.Start(ctx); err != nil {
return errors.Wrap(err, "starting forwarder manager")
Expand Down

0 comments on commit f181475

Please sign in to comment.