RemoveImage removes the image.
( _ context.Context, r *runtimeapi.RemoveImageRequest, )
| 165 | |
| 166 | // RemoveImage removes the image. |
| 167 | func (ds *dockerService) RemoveImage( |
| 168 | _ context.Context, |
| 169 | r *runtimeapi.RemoveImageRequest, |
| 170 | ) (*runtimeapi.RemoveImageResponse, error) { |
| 171 | image := r.GetImage() |
| 172 | // If the image has multiple tags, we need to remove all the tags |
| 173 | // of kubelet, but we should still clarify this in CRI. |
| 174 | imageInspect, err := ds.client.InspectImageByID(image.Image) |
| 175 | |
| 176 | // dockerclient.InspectImageByID doesn't work with digest and repoTags, |
| 177 | // it is safe to continue removing it since there is another check below. |
| 178 | if err != nil && !libdocker.IsImageNotFoundError(err) { |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | if imageInspect == nil { |
| 183 | // image is nil, assuming it doesn't exist. |
| 184 | return &runtimeapi.RemoveImageResponse{}, nil |
| 185 | } |
| 186 | |
| 187 | // An image can have different numbers of RepoTags and RepoDigests. |
| 188 | // Iterating over both of them plus the image ID ensures the image really got removed. |
| 189 | // It also prevents images from being deleted, which actually are deletable using this approach. |
| 190 | var images []string |
| 191 | images = append(images, imageInspect.RepoTags...) |
| 192 | images = append(images, imageInspect.RepoDigests...) |
| 193 | images = append(images, image.Image) |
| 194 | |
| 195 | for _, image := range images { |
| 196 | if _, err := ds.client.RemoveImage(image, dockertypes.ImageRemoveOptions{PruneChildren: true}); err != nil && |
| 197 | !libdocker.IsImageNotFoundError(err) { |
| 198 | return nil, err |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return &runtimeapi.RemoveImageResponse{}, nil |
| 203 | } |
| 204 | |
| 205 | // getImageRef returns the image digest if exists, or else returns the image ID. |
| 206 | func getImageRef(client libdocker.DockerClientInterface, image string) (string, error) { |
nothing calls this directly
no test coverage detected