-
Notifications
You must be signed in to change notification settings - Fork 1k
/
image_remove.go
64 lines (56 loc) · 1.71 KB
/
image_remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package server
import (
"context"
"errors"
"github.com/cri-o/cri-o/internal/log"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
// RemoveImage removes the image.
func (s *Server) RemoveImage(ctx context.Context, req *types.RemoveImageRequest) (*types.RemoveImageResponse, error) {
ctx, span := log.StartSpan(ctx)
defer span.End()
imageRef := ""
img := req.Image
if img != nil {
imageRef = img.Image
}
if imageRef == "" {
return nil, errors.New("no image specified")
}
if err := s.removeImage(ctx, imageRef); err != nil {
return nil, err
}
return &types.RemoveImageResponse{}, nil
}
func (s *Server) removeImage(ctx context.Context, imageRef string) error {
var deleted bool
ctx, span := log.StartSpan(ctx)
defer span.End()
// FIXME: The CRI API definition says
// This call is idempotent, and must not return an error if the image has
// already been removed.
// and this code doesn’t seem to conform to that.
// Actually Kubelet is only ever calling this with full image IDs.
// So we don't really need to accept ID prefixes nor short names;
// or is there another user?!
if id := s.StorageImageServer().HeuristicallyTryResolvingStringAsIDPrefix(imageRef); id != nil {
return s.StorageImageServer().DeleteImage(s.config.SystemContext, *id)
}
potentialMatches, err := s.StorageImageServer().CandidatesForPotentiallyShortImageName(s.config.SystemContext, imageRef)
if err != nil {
return err
}
for _, name := range potentialMatches {
err = s.StorageImageServer().UntagImage(s.config.SystemContext, name)
if err != nil {
log.Debugf(ctx, "Error deleting image %s: %v", name, err)
continue
}
deleted = true
break
}
if !deleted && err != nil {
return err
}
return nil
}