Use multierror for cleanup error

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2023-02-07 09:57:22 -08:00
parent 34314717b0
commit b0e97c0f9b
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
2 changed files with 17 additions and 3 deletions

View File

@ -25,6 +25,7 @@ import (
v1 "github.com/containerd/nri/types/v1" v1 "github.com/containerd/nri/types/v1"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/hashicorp/go-multierror"
"github.com/opencontainers/selinux/go-selinux" "github.com/opencontainers/selinux/go-selinux"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@ -46,6 +47,10 @@ func init() {
"github.com/containerd/cri/pkg/store/sandbox", "Metadata") "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 // 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 // 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. // 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() { defer func() {
if retErr != nil && cleanupErr != nil { 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) 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})
} }
}() }()

View File

@ -28,6 +28,7 @@ import (
"github.com/containerd/go-cni" "github.com/containerd/go-cni"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" 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) ctrl, err := controller.Start(ctx, id)
if err != nil { if err != nil {
sandbox.Container, _ = c.client.LoadContainer(ctx, id) sandbox.Container, _ = c.client.LoadContainer(ctx, id)
if ctrl.SandboxID == "" && ctrl.Pid == 0 && ctrl.CreatedAt.IsZero() && len(ctrl.Labels) == 0 { var cerr podsandbox.CleanupErr
// if resp is a non-nil zero-value, an error occurred during cleanup if errors.As(err, &cerr) {
cleanupErr = fmt.Errorf("failed to cleanup sandbox") 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) return nil, fmt.Errorf("failed to start sandbox %q: %w", id, err)
} }