From cc83e3820bbd38e0edd2d0328d481996bd2da6f6 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Fri, 1 Dec 2017 13:51:00 +0000 Subject: [PATCH] Feed the stdout and stderr of the Pilot subprocess into glog Fixes: #166 --- hack/e2e.sh | 1 + pkg/pilot/elasticsearch/v5/process.go | 2 - pkg/pilot/genericpilot/process/process.go | 48 ++++++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/hack/e2e.sh b/hack/e2e.sh index a4b98b28b..30fafd90e 100755 --- a/hack/e2e.sh +++ b/hack/e2e.sh @@ -128,6 +128,7 @@ function test_elasticsearchcluster() { then fail_test "Elasticsearch pilot did not update the document count" fi + kubectl cluster-info dump --namespaces "${USER_NAMESPACE}" || true if ! kubectl delete \ --namespace "${USER_NAMESPACE}" \ ElasticSearchClusters \ diff --git a/pkg/pilot/elasticsearch/v5/process.go b/pkg/pilot/elasticsearch/v5/process.go index b37548b33..acc249b0a 100644 --- a/pkg/pilot/elasticsearch/v5/process.go +++ b/pkg/pilot/elasticsearch/v5/process.go @@ -14,8 +14,6 @@ func (p *Pilot) CmdFunc(pilot *v1alpha1.Pilot) (*exec.Cmd, error) { } cmd := exec.Command("elasticsearch") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr cmd.Env = p.env().Strings() return cmd, nil diff --git a/pkg/pilot/genericpilot/process/process.go b/pkg/pilot/genericpilot/process/process.go index fb37997e1..9170f80fd 100644 --- a/pkg/pilot/genericpilot/process/process.go +++ b/pkg/pilot/genericpilot/process/process.go @@ -1,9 +1,11 @@ package process import ( + "bufio" "fmt" "os" "os/exec" + "sync" "github.com/golang/glog" ) @@ -33,20 +35,64 @@ type Interface interface { type Adapter struct { Signals Signals Cmd *exec.Cmd + wg sync.WaitGroup } var _ Interface = &Adapter{} +func (p *Adapter) startCommandOutputLoggers() error { + stdout, err := p.Cmd.StdoutPipe() + if err != nil { + return err + } + p.wg.Add(1) + go func() { + defer p.wg.Done() + in := bufio.NewScanner(stdout) + for in.Scan() { + glog.Infoln(in.Text()) + } + err := in.Err() + if err != nil { + glog.Error(err) + } + }() + + stderr, err := p.Cmd.StderrPipe() + if err != nil { + return err + } + p.wg.Add(1) + go func() { + defer p.wg.Done() + in := bufio.NewScanner(stderr) + for in.Scan() { + glog.Infoln(in.Text()) + } + err := in.Err() + if err != nil { + glog.Error(err) + } + }() + return nil +} + func (p *Adapter) Start() error { glog.V(2).Infof("Starting process: %v", p.Cmd.Args) - if err := p.Cmd.Start(); err != nil { + err := p.startCommandOutputLoggers() + if err != nil { + return err + } + + if err = p.Cmd.Start(); err != nil { return fmt.Errorf("error starting process: %s", err.Error()) } return nil } func (p *Adapter) Wait() error { + defer p.wg.Wait() return p.Cmd.Wait() }