Skip to content

Commit

Permalink
Merge pull request #924 from dgageot/extract-tail-logs
Browse files Browse the repository at this point in the history
Extract code to tail logs
  • Loading branch information
dgageot authored Aug 24, 2018
2 parents 80a04db + e4fdba6 commit 80d365c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ type BuildOutput struct {
}

func runBuild(out io.Writer) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
catchCtrlC(cancel)

runner, config, err := newRunner(opts)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/skaffold/app/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func NewCmdDelete(out io.Writer) *cobra.Command {
}

func delete(out io.Writer) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
catchCtrlC(cancel)

runner, _, err := newRunner(opts)
if err != nil {
Expand Down
23 changes: 4 additions & 19 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -50,7 +49,9 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
}

func runDeploy(out io.Writer) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
catchCtrlC(cancel)

r, config, err := newRunner(opts)
if err != nil {
Expand Down Expand Up @@ -78,21 +79,5 @@ func runDeploy(out io.Writer) error {
return err
}

if !opts.Tail {
return nil
}
// If tail is true, stream logs from deployed objects
imageList := kubernetes.NewImageList()
for _, b := range builds {
imageList.Add(b.Tag)
}
colorPicker := kubernetes.NewColorPicker(config.Build.Artifacts)
logger := kubernetes.NewLogAggregator(out, imageList, colorPicker)

if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}

<-ctx.Done()
return nil
return r.TailLogs(ctx, out, config.Build.Artifacts, builds)
}
3 changes: 1 addition & 2 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ func NewCmdDev(out io.Writer) *cobra.Command {
func dev(out io.Writer) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
catchCtrlC(cancel)

if opts.Cleanup {
catchCtrlC(cancel)

defer func() {
if err := delete(out); err != nil {
logrus.Warnln("cleanup:", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func NewCmdRun(out io.Writer) *cobra.Command {
}

func run(out io.Writer) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
catchCtrlC(cancel)

runner, config, err := newRunner(opts)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (opts *SkaffoldOptions) Labels() map[string]string {
if opts.Cleanup {
labels["cleanup"] = "true"
}
if opts.Tail {
labels["tail"] = "true"
}
if opts.Namespace != "" {
labels["namespace"] = opts.Namespace
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func getTagger(t v1alpha2.TagPolicy, customTag string) (tag.Tagger, error) {
}
}

// Run builds artifacts ad then deploys them.
// Run builds artifacts and then deploys them.
func (r *SkaffoldRunner) Run(ctx context.Context, out io.Writer, artifacts []*v1alpha2.Artifact) error {
bRes, err := r.Build(ctx, out, r.Tagger, artifacts)
if err != nil {
Expand All @@ -172,14 +172,20 @@ func (r *SkaffoldRunner) Run(ctx context.Context, out io.Writer, artifacts []*v1
return errors.Wrap(err, "deploy step")
}

return r.TailLogs(ctx, out, artifacts, bRes)
}

// TailLogs prints the logs for deployed artifacts.
func (r *SkaffoldRunner) TailLogs(ctx context.Context, out io.Writer, artifacts []*v1alpha2.Artifact, bRes []build.Artifact) error {
if !r.opts.Tail {
return nil
}
// If tail is true, stream logs from deployed objects

imageList := kubernetes.NewImageList()
for _, b := range bRes {
imageList.Add(b.Tag)
}

colorPicker := kubernetes.NewColorPicker(artifacts)
logger := kubernetes.NewLogAggregator(out, imageList, colorPicker)
if err := logger.Start(ctx); err != nil {
Expand Down

0 comments on commit 80d365c

Please sign in to comment.