Skip to content

Commit

Permalink
feat(cmd/log): log follow
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <[email protected]>
  • Loading branch information
fntlnz committed Dec 29, 2018
1 parent c9df760 commit f673a26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
38 changes: 22 additions & 16 deletions pkg/cmd/log.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package cmd

import (
"context"
"fmt"

"github.com/fntlnz/kubectl-trace/pkg/factory"
"github.com/fntlnz/kubectl-trace/pkg/logs"
"github.com/fntlnz/kubectl-trace/pkg/meta"
"github.com/fntlnz/kubectl-trace/pkg/signals"
"github.com/fntlnz/kubectl-trace/pkg/tracejob"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -18,17 +16,21 @@ import (
)

var (
logShort = `` // Wrap with i18n.T()
logLong = logShort + `
logShort = `Print the logs for a specific trace execution` // Wrap with i18n.T()
logLong = logShort
logExamples = `
# Logs from a trace using its name
%[1]s trace logs kubectl-trace-d5842929-0b78-11e9-a9fa-40a3cc632df1
...`
# Logs from a trace using its id
%[1]s trace log 5594d7e1-0b78-11e9-b7f1-40a3cc632df1
logExamples = `
# ...
%[1]s trace log -h
# Follow logs
%[1]s trace logs kubectl-trace-d5842929-0b78-11e9-a9fa-40a3cc632df1 -f
# ...
%[1]s trace log`
# Add timestamp to logs
%[1]s trace logs kubectl-trace-d5842929-0b78-11e9-a9fa-40a3cc632df1 --timestamp
`
)

// LogOptions ...
Expand All @@ -38,12 +40,16 @@ type LogOptions struct {
traceName *string
namespace string
clientConfig *rest.Config
follow bool
timestamps bool
}

// NewLogOptions provides an instance of LogOptions with default values.
func NewLogOptions(streams genericclioptions.IOStreams) *LogOptions {
return &LogOptions{
IOStreams: streams,
IOStreams: streams,
follow: false,
timestamps: false,
}
}

Expand All @@ -52,8 +58,9 @@ func NewLogCommand(factory factory.Factory, streams genericclioptions.IOStreams)
o := NewLogOptions(streams)

cmd := &cobra.Command{
Use: "log (TRACE_ID | TRACE_NAME)",
Use: "logs (TRACE_ID | TRACE_NAME) [-f]",
DisableFlagsInUseLine: true,
Aliases: []string{"log"},
Short: logShort,
Long: logLong, // Wrap with templates.LongDesc()
Example: fmt.Sprintf(logExamples, "kubectl"), // Wrap with templates.Examples()
Expand All @@ -73,6 +80,8 @@ func NewLogCommand(factory factory.Factory, streams genericclioptions.IOStreams)
},
}

cmd.Flags().BoolVarP(&o.follow, "follow", "f", o.follow, "Specify if the logs should be streamed")
cmd.Flags().BoolVar(&o.timestamps, "timestamps", o.timestamps, "Include timestamps on each line in the log output")
return cmd
}

Expand Down Expand Up @@ -136,10 +145,7 @@ func (o *LogOptions) Run() error {

job := jobs[0]

ctx := context.Background()
ctx = signals.WithStandardSignals(ctx)
nl := logs.NewLogs(client, o.IOStreams)
nl.WithContext(ctx)
nl.Run(job.ID, job.Namespace)
nl.Run(job.ID, job.Namespace, o.follow, o.timestamps)
return nil
}
21 changes: 4 additions & 17 deletions pkg/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ import (
"fmt"
"io"

"context"

"k8s.io/cli-runtime/pkg/genericclioptions"
)

type Logs struct {
genericclioptions.IOStreams
coreV1Client tcorev1.CoreV1Interface
ctx context.Context
}

func NewLogs(client tcorev1.CoreV1Interface, streams genericclioptions.IOStreams) *Logs {
return &Logs{
coreV1Client: client,
IOStreams: streams,
ctx: context.TODO(),
}
}

Expand All @@ -37,11 +33,7 @@ const (
invalidPodContainersSizeError = "unexpected number of containers in trace job pod"
)

func (l *Logs) WithContext(c context.Context) {
l.ctx = c
}

func (l *Logs) Run(jobID types.UID, namespace string) error {
func (l *Logs) Run(jobID types.UID, namespace string, follow bool, timestamps bool) error {
pl, err := l.coreV1Client.Pods(namespace).List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", meta.TraceIDLabelKey, jobID),
})
Expand All @@ -65,20 +57,16 @@ func (l *Logs) Run(jobID types.UID, namespace string) error {

containerName := pod.Spec.Containers[0].Name

// TODO(fntlnz): let the user choose to follow or not
logOptions := &corev1.PodLogOptions{
Container: containerName,
Follow: true,
Follow: follow,
Previous: false,
Timestamps: false,
Timestamps: timestamps,
}

logsRequest := l.coreV1Client.Pods(namespace).GetLogs(pod.Name, logOptions)

go consumeRequest(logsRequest, l.IOStreams.Out)
<-l.ctx.Done()

return nil
return consumeRequest(logsRequest, l.IOStreams.Out)
}

func consumeRequest(request *rest.Request, out io.Writer) error {
Expand All @@ -91,4 +79,3 @@ func consumeRequest(request *rest.Request, out io.Writer) error {
_, err = io.Copy(out, readCloser)
return err
}

0 comments on commit f673a26

Please sign in to comment.