From 211a5de2943b70a4b4c65eda26270c22ed7c3496 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Mon, 7 Oct 2024 23:05:36 +0800 Subject: [PATCH] fix(host): ignore NotFound error when doing some operation to container (#21354) --- pkg/hostman/guestman/pod.go | 12 ++++++++---- pkg/hostman/guestman/pod_helper.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/hostman/guestman/pod.go b/pkg/hostman/guestman/pod.go index 5f4ce5a5c0..5d1d09bf20 100644 --- a/pkg/hostman/guestman/pod.go +++ b/pkg/hostman/guestman/pod.go @@ -991,7 +991,7 @@ func (s *sPodGuestInstance) StartContainer(ctx context.Context, userCred mcclien if hasCtr { status, _, err := s.getContainerStatus(ctx, ctrId) if err != nil { - if errors.Cause(err) == errors.ErrNotFound || strings.Contains(err.Error(), "not found") { + if IsContainerNotFoundError(err) { needRecreate = true } else { return nil, errors.Wrap(err, "get container status") @@ -1128,7 +1128,11 @@ func (s *sPodGuestInstance) StopContainer(ctx context.Context, userCred mcclient } } if err := s.getCRI().StopContainer(ctx, criId, timeout); err != nil { - return nil, errors.Wrap(err, "CRI.StopContainer") + if !IsContainerNotFoundError(err) { + return nil, errors.Wrap(err, "CRI.StopContainer") + } else { + log.Warningf("CRI.StopContainer %s not found", criId) + } } if err := s.startStat.RemoveContainerFile(ctrId); err != nil { return nil, errors.Wrap(err, "startStat.RemoveContainerFile") @@ -1745,7 +1749,7 @@ func (s *sPodGuestInstance) DeleteContainer(ctx context.Context, userCred mcclie if criId != "" { s.expectedStatus.RemoveContainer(criId) - if err := s.getCRI().RemoveContainer(ctx, criId); err != nil && !strings.Contains(err.Error(), "not found") { + if err := s.getCRI().RemoveContainer(ctx, criId); err != nil && !IsContainerNotFoundError(err) { return nil, errors.Wrap(err, "cri.RemoveContainer") } } @@ -1771,7 +1775,7 @@ func (s *sPodGuestInstance) getContainerStatus(ctx context.Context, ctrId string } resp, err := s.getCRI().ContainerStatus(ctx, criId) if err != nil { - if strings.Contains(err.Error(), "NotFound") { + if IsContainerNotFoundError(err) { return computeapi.CONTAINER_STATUS_EXITED, nil, nil } return "", nil, errors.Wrap(err, "cri.ContainerStatus") diff --git a/pkg/hostman/guestman/pod_helper.go b/pkg/hostman/guestman/pod_helper.go index 1836861dea..8580b394ef 100644 --- a/pkg/hostman/guestman/pod_helper.go +++ b/pkg/hostman/guestman/pod_helper.go @@ -33,6 +33,21 @@ import ( "yunion.io/x/onecloud/pkg/util/pod/nerdctl" ) +func IsContainerNotFoundError(err error) bool { + if errors.Cause(err) == errors.ErrNotFound { + return true + } + for _, errMsg := range []string{ + "NotFound", + "not found", + } { + if strings.Contains(err.Error(), errMsg) { + return true + } + } + return false +} + func GetContainerdConnectionInfo() (string, string) { addr := options.HostOptions.ContainerRuntimeEndpoint addr = strings.TrimPrefix(addr, "unix://")