Use multierror for cleanup error
Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
parent
34314717b0
commit
b0e97c0f9b
@ -25,6 +25,7 @@ import (
|
||||
v1 "github.com/containerd/nri/types/v1"
|
||||
"github.com/containerd/typeurl"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/opencontainers/selinux/go-selinux"
|
||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
||||
|
||||
@ -46,6 +47,10 @@ func init() {
|
||||
"github.com/containerd/cri/pkg/store/sandbox", "Metadata")
|
||||
}
|
||||
|
||||
type CleanupErr struct {
|
||||
error
|
||||
}
|
||||
|
||||
// Start creates resources required for the sandbox and starts the sandbox. If an error occurs, Start attempts to tear
|
||||
// down the created resources. If an error occurs while tearing down resources, a zero-valued response is returned
|
||||
// alongside the error. If the teardown was successful, a nil response is returned with the error.
|
||||
@ -55,6 +60,7 @@ func (c *Controller) Start(ctx context.Context, id string) (cin sandbox.Controll
|
||||
defer func() {
|
||||
if retErr != nil && cleanupErr != nil {
|
||||
log.G(ctx).WithField("id", id).WithError(cleanupErr).Errorf("failed to fully teardown sandbox resources after earlier error: %s", retErr)
|
||||
retErr = multierror.Append(retErr, CleanupErr{cleanupErr})
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -28,6 +28,7 @@ import (
|
||||
|
||||
"github.com/containerd/go-cni"
|
||||
"github.com/containerd/typeurl"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/sirupsen/logrus"
|
||||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
|
||||
|
||||
@ -232,9 +233,16 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
|
||||
ctrl, err := controller.Start(ctx, id)
|
||||
if err != nil {
|
||||
sandbox.Container, _ = c.client.LoadContainer(ctx, id)
|
||||
if ctrl.SandboxID == "" && ctrl.Pid == 0 && ctrl.CreatedAt.IsZero() && len(ctrl.Labels) == 0 {
|
||||
// if resp is a non-nil zero-value, an error occurred during cleanup
|
||||
cleanupErr = fmt.Errorf("failed to cleanup sandbox")
|
||||
var cerr podsandbox.CleanupErr
|
||||
if errors.As(err, &cerr) {
|
||||
cleanupErr = fmt.Errorf("failed to cleanup sandbox: %w", cerr)
|
||||
|
||||
// Strip last error as cleanup error to handle separately
|
||||
if merr, ok := err.(*multierror.Error); ok {
|
||||
if errs := merr.WrappedErrors(); len(errs) > 0 {
|
||||
err = errs[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("failed to start sandbox %q: %w", id, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user