Skip to content

Commit

Permalink
Wait for logs if pipeline is running
Browse files Browse the repository at this point in the history
pipeline logs command waits for the logs if pipeline is running
prints a message if there are no logs to show

Fixes : #117

Signed-off-by: Pradeep Kumar <[email protected]>
  • Loading branch information
pradeepitm12 authored and tekton-robot committed Aug 22, 2019
1 parent b5a87fd commit 0b23e04
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 5 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
64 changes: 63 additions & 1 deletion pkg/cmd/pipelinerun/log_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
"github.com/tektoncd/cli/pkg/helper/pipelinerun"
"github.com/tektoncd/cli/pkg/helper/pods/stream"
trh "github.com/tektoncd/cli/pkg/helper/taskrun"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
)

type LogReader struct {
Run string
Ns string
Clients *cli.Clients
Streamer stream.NewStreamerFunc
Stream *cli.Stream
AllSteps bool
Follow bool
Tasks []string
Expand Down Expand Up @@ -90,14 +94,21 @@ func (lr *LogReader) readLiveLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-chan
}

wg.Wait()

if pr.Status.Conditions[0].Status == corev1.ConditionFalse {
errC <- fmt.Errorf(pr.Status.Conditions[0].Message)
}
}()

return logC, errC, nil
}

func (lr *LogReader) readAvailableLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-chan error, error) {
tkn := lr.Clients.Tekton
if err := lr.waitUntilAvailable(10); err != nil {
return nil, nil, err
}

tkn := lr.Clients.Tekton
pl, err := tkn.TektonV1alpha1().Pipelines(lr.Ns).Get(pr.Spec.PipelineRef.Name, metav1.GetOptions{})
if err != nil {
return nil, nil, fmt.Errorf(err.Error())
Expand All @@ -121,11 +132,58 @@ func (lr *LogReader) readAvailableLogs(pr *v1alpha1.PipelineRun) (<-chan Log, <-

pipeLogs(logC, errC, tlr)
}
if pr.Status.Conditions[0].Status == corev1.ConditionFalse {
errC <- fmt.Errorf(pr.Status.Conditions[0].Message)
}
}()

return logC, errC, nil
}

// reading of logs should wait till the status of run is unknown
// only if run status is unknown, open a watch channel on run
// and keep checking the status until it changes to true|false
// or the reach timeout
func (lr *LogReader) waitUntilAvailable(timeout time.Duration) error {
var first = true
opts := metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", lr.Run).String(),
}
tkn := lr.Clients.Tekton
run, err := tkn.TektonV1alpha1().PipelineRuns(lr.Ns).Get(lr.Run, metav1.GetOptions{})
if err != nil {
return err
}
if empty(run.Status) {
return nil
}
if run.Status.Conditions[0].Status != corev1.ConditionUnknown {
return nil
}

watchRun, err := tkn.TektonV1alpha1().PipelineRuns(lr.Ns).Watch(opts)
if err != nil {
return err
}
for {
select {
case event := <-watchRun.ResultChan():
if event.Object.(*v1alpha1.PipelineRun).IsDone() {
watchRun.Stop()
return nil
}
if first {
first = false
fmt.Fprintln(lr.Stream.Out, "Pipeline still running ...")
}
case <-time.After(timeout * time.Second):
watchRun.Stop()
fmt.Fprintln(lr.Stream.Err, "No logs found")
return nil
}
}
}

func pipeLogs(logC chan<- Log, errC chan<- error, tlr *taskrun.LogReader) {
tlogC, terrC, err := tlr.Read()
if err != nil {
Expand All @@ -151,3 +209,7 @@ func pipeLogs(logC chan<- Log, errC chan<- error, tlr *taskrun.LogReader) {
}
}
}

func empty(status v1alpha1.PipelineRunStatus) bool {
return len(status.Conditions) == 0
}
Loading

0 comments on commit 0b23e04

Please sign in to comment.