Merge pull request #8414 from kiashok/deleteCtrFromCtrStore
Remove entry for container from container store on error
This commit is contained in:
		| @@ -33,20 +33,30 @@ import ( | |||||||
| // RemoveContainer removes the container. | // RemoveContainer removes the container. | ||||||
| func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) { | func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) { | ||||||
| 	start := time.Now() | 	start := time.Now() | ||||||
| 	container, err := c.containerStore.Get(r.GetContainerId()) | 	ctrID := r.GetContainerId() | ||||||
|  | 	container, err := c.containerStore.Get(ctrID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		if !errdefs.IsNotFound(err) { | 		if !errdefs.IsNotFound(err) { | ||||||
| 			return nil, fmt.Errorf("an error occurred when try to find container %q: %w", r.GetContainerId(), err) | 			return nil, fmt.Errorf("an error occurred when try to find container %q: %w", ctrID, err) | ||||||
| 		} | 		} | ||||||
| 		// Do not return error if container metadata doesn't exist. | 		// Do not return error if container metadata doesn't exist. | ||||||
| 		log.G(ctx).Tracef("RemoveContainer called for container %q that does not exist", r.GetContainerId()) | 		log.G(ctx).Tracef("RemoveContainer called for container %q that does not exist", ctrID) | ||||||
| 		return &runtime.RemoveContainerResponse{}, nil | 		return &runtime.RemoveContainerResponse{}, nil | ||||||
| 	} | 	} | ||||||
| 	id := container.ID | 	id := container.ID | ||||||
| 	i, err := container.Container.Info(ctx) | 	i, err := container.Container.Info(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		if !errdefs.IsNotFound(err) { | ||||||
| 			return nil, fmt.Errorf("get container info: %w", err) | 			return nil, fmt.Errorf("get container info: %w", err) | ||||||
| 		} | 		} | ||||||
|  | 		// Since containerd doesn't see the container and criservice's content store does, | ||||||
|  | 		// we should try to recover from this state by removing entry for this container | ||||||
|  | 		// from the container store as well and return successfully. | ||||||
|  | 		log.G(ctx).WithError(err).Warn("get container info failed") | ||||||
|  | 		c.containerStore.Delete(ctrID) | ||||||
|  | 		c.containerNameIndex.ReleaseByKey(ctrID) | ||||||
|  | 		return &runtime.RemoveContainerResponse{}, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// Forcibly stop the containers if they are in running or unknown state | 	// Forcibly stop the containers if they are in running or unknown state | ||||||
| 	state := container.Status.Get().State() | 	state := container.Status.Get().State() | ||||||
|   | |||||||
| @@ -33,20 +33,30 @@ import ( | |||||||
| // RemoveContainer removes the container. | // RemoveContainer removes the container. | ||||||
| func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) { | func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) { | ||||||
| 	start := time.Now() | 	start := time.Now() | ||||||
| 	container, err := c.containerStore.Get(r.GetContainerId()) | 	ctrID := r.GetContainerId() | ||||||
|  | 	container, err := c.containerStore.Get(ctrID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		if !errdefs.IsNotFound(err) { | 		if !errdefs.IsNotFound(err) { | ||||||
| 			return nil, fmt.Errorf("an error occurred when try to find container %q: %w", r.GetContainerId(), err) | 			return nil, fmt.Errorf("an error occurred when try to find container %q: %w", ctrID, err) | ||||||
| 		} | 		} | ||||||
| 		// Do not return error if container metadata doesn't exist. | 		// Do not return error if container metadata doesn't exist. | ||||||
| 		log.G(ctx).Tracef("RemoveContainer called for container %q that does not exist", r.GetContainerId()) | 		log.G(ctx).Tracef("RemoveContainer called for container %q that does not exist", ctrID) | ||||||
| 		return &runtime.RemoveContainerResponse{}, nil | 		return &runtime.RemoveContainerResponse{}, nil | ||||||
| 	} | 	} | ||||||
| 	id := container.ID | 	id := container.ID | ||||||
| 	i, err := container.Container.Info(ctx) | 	i, err := container.Container.Info(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		if !errdefs.IsNotFound(err) { | ||||||
| 			return nil, fmt.Errorf("get container info: %w", err) | 			return nil, fmt.Errorf("get container info: %w", err) | ||||||
| 		} | 		} | ||||||
|  | 		// Since containerd doesn't see the container and criservice's content store does, | ||||||
|  | 		// we should try to recover from this state by removing entry for this container | ||||||
|  | 		// from the container store as well and return successfully. | ||||||
|  | 		log.G(ctx).WithError(err).Warn("get container info failed") | ||||||
|  | 		c.containerStore.Delete(ctrID) | ||||||
|  | 		c.containerNameIndex.ReleaseByKey(ctrID) | ||||||
|  | 		return &runtime.RemoveContainerResponse{}, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// Forcibly stop the containers if they are in running or unknown state | 	// Forcibly stop the containers if they are in running or unknown state | ||||||
| 	state := container.Status.Get().State() | 	state := container.Status.Get().State() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Fu Wei
					Fu Wei