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

Add --tail flag to stream logs with skaffold run #914

Merged
merged 3 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func AddDevFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&opts.Cleanup, "cleanup", true, "Delete deployments after dev mode is interrupted")
}

func AddRunDeployFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&opts.Tail, "tail", false, "Stream logs from deployed objects")
}

func AddRunDevFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&opts.Notification, "toot", false, "Emit a terminal beep after the deploy is complete")
Expand Down
27 changes: 24 additions & 3 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 @@ -42,6 +43,7 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
},
}
AddRunDevFlags(cmd)
AddRunDeployFlags(cmd)
cmd.Flags().StringSliceVar(&images, "images", nil, "A list of images to deploy")
cmd.Flags().BoolVarP(&quietFlag, "quiet", "q", false, "Suppress the deploy output")
return cmd
Expand All @@ -50,7 +52,7 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
func runDeploy(out io.Writer) error {
ctx := context.Background()

r, _, err := newRunner(opts)
r, config, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand All @@ -72,6 +74,25 @@ func runDeploy(out io.Writer) error {
})
}

_, err = r.Deploy(ctx, deployOut, builds)
return err
if _, err := r.Deploy(ctx, deployOut, builds); err != nil {
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
}
1 change: 1 addition & 0 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewCmdRun(out io.Writer) *cobra.Command {
},
}
AddRunDevFlags(cmd)
AddRunDeployFlags(cmd)

cmd.Flags().StringVarP(&opts.CustomTag, "tag", "t", "", "The optional custom tag to use for images which overrides the current Tagger configuration")
return cmd
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SkaffoldOptions struct {
ConfigurationFile string
Cleanup bool
Notification bool
Tail bool
Profiles []string
CustomTag string
Namespace string
Expand Down
15 changes: 15 additions & 0 deletions pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ func (r *SkaffoldRunner) Run(ctx context.Context, out io.Writer, artifacts []*v1
return errors.Wrap(err, "deploy step")
}

if !r.opts.Tail {
Copy link
Contributor

Choose a reason for hiding this comment

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

@priyawadhwa do you think the duplication could be removed?

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 thought about moving it into a separate function, but I think four things would have to be passed in, so it would look like:

func StreamLogsFromDeployedObjects(ctx context.Context, out io.Writer, builds []build.Artifact, artifacts []*v1alpha2.Artifact)

and I personally didn't think that was much cleaner, but I don't have a strong opinion so can definitely move it into a function if you prefer that!

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's merge this one any think about how to remove the duplication afterwards!

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 {
return errors.Wrap(err, "starting logger")
}

<-ctx.Done()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func TestRun(t *testing.T) {
Builder: test.builder,
Deployer: test.deployer,
Tagger: &tag.ChecksumTagger{},
opts: &config.SkaffoldOptions{},
}
err := runner.Run(context.Background(), ioutil.Discard, test.config.Build.Artifacts)

Expand Down