Merge pull request #3179 from Random-Liu/publish-error

Return event publish errors
This commit is contained in:
Phil Estes 2019-04-04 20:35:34 -04:00 committed by GitHub
commit eea500d122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -59,6 +59,12 @@ var (
criuFlag string
systemdCgroupFlag bool
containerdBinaryFlag string
bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
)
func init() {
@ -275,16 +281,20 @@ func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event
}
cmd := exec.CommandContext(ctx, containerdBinaryFlag, "--address", l.address, "publish", "--topic", topic, "--namespace", ns)
cmd.Stdin = bytes.NewReader(data)
b := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(b)
cmd.Stdout = b
cmd.Stderr = b
c, err := shim.Default.Start(cmd)
if err != nil {
return err
}
status, err := shim.Default.Wait(cmd, c)
if err != nil {
return err
return errors.Wrapf(err, "failed to publish event: %s", b.String())
}
if status != 0 {
return errors.New("failed to publish event")
return errors.Errorf("failed to publish event: %s", b.String())
}
return nil
}

View File

@ -26,6 +26,7 @@ import (
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
"github.com/containerd/containerd/events"
@ -37,6 +38,12 @@ import (
"golang.org/x/sys/unix"
)
var bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
// setupSignals creates a new signal handler for all signals and sets the shim as a
// sub-reaper so that the container processes are reparented
func setupSignals(config Config) (chan os.Signal, error) {
@ -106,12 +113,16 @@ func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event
}
cmd := exec.CommandContext(ctx, l.containerdBinaryPath, "--address", l.address, "publish", "--topic", topic, "--namespace", ns)
cmd.Stdin = bytes.NewReader(data)
b := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(b)
cmd.Stdout = b
cmd.Stderr = b
if l.noReaper {
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return errors.Wrap(err, "failed to publish event")
return errors.Wrapf(err, "failed to publish event: %s", b.String())
}
return nil
}
@ -121,10 +132,10 @@ func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event
}
status, err := Default.Wait(cmd, c)
if err != nil {
return err
return errors.Wrapf(err, "failed to publish event: %s", b.String())
}
if status != 0 {
return errors.New("failed to publish event")
return errors.Errorf("failed to publish event: %s", b.String())
}
return nil
}