From b3b6f15075e1ecd07ca49d667e6dedd94bf4145a Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 30 Jul 2024 10:40:14 +0200 Subject: [PATCH] Make `StopPodSandbox` RPC idempotent Similar to sandbox removal, the stop of a sandbox should be a noop if the sandbox has not been found. Found during: https://github.com/kubernetes-sigs/cri-tools/pull/1535 Signed-off-by: Sascha Grunert (cherry picked from commit c6cea95d95a66afe14c465adc7c43f03565bf7ea) Signed-off-by: Sascha Grunert --- pkg/cri/sbserver/sandbox_stop.go | 12 ++++++++++-- pkg/cri/server/sandbox_stop.go | 11 +++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/cri/sbserver/sandbox_stop.go b/pkg/cri/sbserver/sandbox_stop.go index 3346364cf0d8..930eb31652a9 100644 --- a/pkg/cri/sbserver/sandbox_stop.go +++ b/pkg/cri/sbserver/sandbox_stop.go @@ -22,6 +22,7 @@ import ( "fmt" "time" + "github.com/containerd/errdefs" "github.com/containerd/log" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" @@ -33,8 +34,15 @@ import ( func (c *criService) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandboxRequest) (*runtime.StopPodSandboxResponse, error) { sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId()) if err != nil { - return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %w", - r.GetPodSandboxId(), err) + if !errdefs.IsNotFound(err) { + return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %w", + r.GetPodSandboxId(), err) + } + + // The StopPodSandbox RPC is idempotent, and must not return an error + // if all relevant resources have already been reclaimed. Ref: + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L45-L46 + return &runtime.StopPodSandboxResponse{}, nil } if err := c.stopPodSandbox(ctx, sandbox); err != nil { diff --git a/pkg/cri/server/sandbox_stop.go b/pkg/cri/server/sandbox_stop.go index 9a753edc54a0..893ce90c5f5b 100644 --- a/pkg/cri/server/sandbox_stop.go +++ b/pkg/cri/server/sandbox_stop.go @@ -38,8 +38,15 @@ import ( func (c *criService) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandboxRequest) (*runtime.StopPodSandboxResponse, error) { sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId()) if err != nil { - return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %w", - r.GetPodSandboxId(), err) + if !errdefs.IsNotFound(err) { + return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %w", + r.GetPodSandboxId(), err) + } + + // The StopPodSandbox RPC is idempotent, and must not return an error + // if all relevant resources have already been reclaimed. Ref: + // https://github.com/kubernetes/cri-api/blob/c20fa40/pkg/apis/runtime/v1/api.proto#L45-L46 + return &runtime.StopPodSandboxResponse{}, nil } if err := c.stopPodSandbox(ctx, sandbox); err != nil {