Commit 91038c92 authored by Alessio Caiazza's avatar Alessio Caiazza 💡
Browse files

Merge branch 'add-more-debug-logs-to-k8s-executor' into 'master'

Extend debug logging for k8s executor

See merge request !949
parents bb617165 cddaf301
Pipeline #29382792 failed with stages
in 23 minutes and 19 seconds
......@@ -151,6 +151,8 @@ func (b *Build) StartBuild(rootDir, cacheDir string, sharedDir bool) {
func (b *Build) executeStage(ctx context.Context, buildStage BuildStage, executor Executor) error {
b.CurrentStage = buildStage
b.Log().WithField("build_stage", buildStage).Debug("Executing build stage")
shell := executor.Shell()
if shell == nil {
return errors.New("No shell defined")
......
......@@ -23,9 +23,10 @@ import (
"io"
"net/url"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
......@@ -45,6 +46,7 @@ func (*DefaultRemoteExecutor) Execute(method string, url *url.URL, config *restc
if err != nil {
return err
}
return exec.Stream(remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
......@@ -84,7 +86,7 @@ func (p *ExecOptions) Run() error {
containerName := p.ContainerName
if len(containerName) == 0 {
log.Infof("defaulting container name to '%s'", pod.Spec.Containers[0].Name)
logrus.Infof("defaulting container name to '%s'", pod.Spec.Containers[0].Name)
containerName = pod.Spec.Containers[0].Name
}
......@@ -111,3 +113,13 @@ func (p *ExecOptions) Run() error {
return p.Executor.Execute("POST", req.URL(), p.Config, stdin, p.Out, p.Err, false)
}
func init() {
runtime.ErrorHandlers = append(runtime.ErrorHandlers, func(err error) {
logrus.WithError(err).Error("K8S stream error")
})
runtime.PanicHandlers = append(runtime.PanicHandlers, func(r interface{}) {
logrus.Errorf("K8S stream panic: %v", r)
})
}
......@@ -150,8 +150,16 @@ func (s *executor) Run(cmd common.ExecutorCommand) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Debugln(fmt.Sprintf(
"Starting in container %q the command %q with script: %s",
containerName,
containerCommand,
cmd.Script,
))
select {
case err := <-s.runInContainer(ctx, containerName, containerCommand, cmd.Script):
s.Debugln(fmt.Sprintf("Container %q exited with error: %v", containerName, err))
if err != nil && strings.Contains(err.Error(), "command terminated with exit code") {
return &common.BuildError{Inner: err}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment