-
Notifications
You must be signed in to change notification settings - Fork 619
/
Copy pathdocker_image_manager.go
380 lines (340 loc) · 14.3 KB
/
docker_image_manager.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package engine
import (
"fmt"
"sort"
"sync"
"time"
"context"
"github.com/aws/amazon-ecs-agent/agent/api"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/engine/image"
"github.com/aws/amazon-ecs-agent/agent/statemanager"
"github.com/cihub/seelog"
)
const (
imageNotFoundForDeletionError = "no such image"
)
// ImageManager is responsible for saving the Image states,
// adding and removing container references to ImageStates
type ImageManager interface {
RecordContainerReference(container *api.Container) error
RemoveContainerReferenceFromImageState(container *api.Container) error
AddAllImageStates(imageStates []*image.ImageState)
GetImageStateFromImageName(containerImageName string) *image.ImageState
StartImageCleanupProcess(ctx context.Context)
SetSaver(stateManager statemanager.Saver)
}
// dockerImageManager accounts all the images and their states in the instance.
// It also has the cleanup policy configuration.
type dockerImageManager struct {
imageStates []*image.ImageState
client DockerClient
updateLock sync.RWMutex
imageCleanupTicker *time.Ticker
state dockerstate.TaskEngineState
saver statemanager.Saver
imageStatesConsideredForDeletion map[string]*image.ImageState
minimumAgeBeforeDeletion time.Duration
numImagesToDelete int
imageCleanupTimeInterval time.Duration
}
// ImageStatesForDeletion is used for implementing the sort interface
type ImageStatesForDeletion []*image.ImageState
// NewImageManager returns a new ImageManager
func NewImageManager(cfg *config.Config, client DockerClient, state dockerstate.TaskEngineState) ImageManager {
return &dockerImageManager{
client: client,
state: state,
minimumAgeBeforeDeletion: cfg.MinimumImageDeletionAge,
numImagesToDelete: cfg.NumImagesToDeletePerCycle,
imageCleanupTimeInterval: cfg.ImageCleanupInterval,
}
}
func (imageManager *dockerImageManager) SetSaver(stateManager statemanager.Saver) {
imageManager.saver = stateManager
}
func (imageManager *dockerImageManager) AddAllImageStates(imageStates []*image.ImageState) {
imageManager.updateLock.Lock()
defer imageManager.updateLock.Unlock()
for _, imageState := range imageStates {
imageManager.addImageState(imageState)
}
}
func (imageManager *dockerImageManager) GetImageStatesCount() int {
imageManager.updateLock.RLock()
defer imageManager.updateLock.RUnlock()
return len(imageManager.imageStates)
}
// RecordContainerReference adds container reference to the corresponding imageState object
func (imageManager *dockerImageManager) RecordContainerReference(container *api.Container) error {
// the image state has been updated, save the new state
defer imageManager.saver.ForceSave()
// On agent restart, container ID was retrieved from agent state file
// TODO add setter and getter for modifying this
if container.ImageID != "" {
if !imageManager.addContainerReferenceToExistingImageState(container) {
return fmt.Errorf("Failed to add container to existing image state")
}
return nil
}
if container.Image == "" {
return fmt.Errorf("Invalid container reference: Empty image name")
}
// Inspect image for obtaining Container's Image ID
imageInspected, err := imageManager.client.InspectImage(container.Image)
if err != nil {
seelog.Errorf("Error inspecting image %v: %v", container.Image, err)
return err
}
container.ImageID = imageInspected.ID
added := imageManager.addContainerReferenceToExistingImageState(container)
if !added {
imageManager.addContainerReferenceToNewImageState(container, imageInspected.Size)
}
return nil
}
func (imageManager *dockerImageManager) addContainerReferenceToExistingImageState(container *api.Container) bool {
// this lock is used for reading the image states in the image manager
imageManager.updateLock.RLock()
defer imageManager.updateLock.RUnlock()
imageManager.removeExistingImageNameOfDifferentID(container.Image, container.ImageID)
imageState, ok := imageManager.getImageState(container.ImageID)
if ok {
imageState.UpdateImageState(container)
}
return ok
}
func (imageManager *dockerImageManager) addContainerReferenceToNewImageState(container *api.Container, imageSize int64) {
// this lock is used while creating and adding new image state to image manager
imageManager.updateLock.Lock()
defer imageManager.updateLock.Unlock()
imageManager.removeExistingImageNameOfDifferentID(container.Image, container.ImageID)
// check to see if a different thread added image state for same image ID
imageState, ok := imageManager.getImageState(container.ImageID)
if ok {
imageState.UpdateImageState(container)
} else {
sourceImage := &image.Image{
ImageID: container.ImageID,
Size: imageSize,
}
sourceImageState := &image.ImageState{
Image: sourceImage,
PulledAt: time.Now(),
LastUsedAt: time.Now(),
}
sourceImageState.UpdateImageState(container)
imageManager.addImageState(sourceImageState)
}
}
// RemoveContainerReferenceFromImageState removes container reference from the corresponding imageState object
func (imageManager *dockerImageManager) RemoveContainerReferenceFromImageState(container *api.Container) error {
// the image state has been updated, save the new state
defer imageManager.saver.ForceSave()
// this lock is for reading image states and finding the one that the container belongs to
imageManager.updateLock.RLock()
defer imageManager.updateLock.RUnlock()
if container.ImageID == "" {
return fmt.Errorf("Invalid container reference: Empty image id")
}
// Find image state that this container is part of, and remove the reference
imageState, ok := imageManager.getImageState(container.ImageID)
if !ok {
return fmt.Errorf("Cannot find image state for the container to be removed")
}
// Found matching ImageState
return imageState.RemoveContainerReference(container)
}
func (imageManager *dockerImageManager) addImageState(imageState *image.ImageState) {
imageManager.imageStates = append(imageManager.imageStates, imageState)
}
// getAllImageStates returns the list of imageStates in the instance
func (imageManager *dockerImageManager) getAllImageStates() []*image.ImageState {
return imageManager.imageStates
}
// getImageState returns the ImageState object that the container is referenced at
func (imageManager *dockerImageManager) getImageState(containerImageID string) (*image.ImageState, bool) {
for _, imageState := range imageManager.getAllImageStates() {
if imageState.Image.ImageID == containerImageID {
return imageState, true
}
}
return nil, false
}
// removeImageState removes the imageState from the list of imageState objects in ImageManager
func (imageManager *dockerImageManager) removeImageState(imageStateToBeRemoved *image.ImageState) {
for i, imageState := range imageManager.imageStates {
if imageState.Image.ImageID == imageStateToBeRemoved.Image.ImageID {
// Image State found; hence remove it
seelog.Infof("Removing Image State: [%s] from Image Manager", imageState.String())
imageManager.imageStates = append(imageManager.imageStates[:i], imageManager.imageStates[i+1:]...)
return
}
}
}
func (imageManager *dockerImageManager) getCandidateImagesForDeletion() []*image.ImageState {
if len(imageManager.imageStatesConsideredForDeletion) < 1 {
seelog.Debugf("Image Manager: Empty state!")
// no image states present in image manager
return nil
}
var imagesForDeletion []*image.ImageState
for _, imageState := range imageManager.imageStatesConsideredForDeletion {
if imageManager.isImageOldEnough(imageState) && imageState.HasNoAssociatedContainers() {
seelog.Infof("Candidate image for deletion: [%s]", imageState.String())
imagesForDeletion = append(imagesForDeletion, imageState)
}
}
return imagesForDeletion
}
func (imageManager *dockerImageManager) isImageOldEnough(imageState *image.ImageState) bool {
ageOfImage := time.Now().Sub(imageState.PulledAt)
return ageOfImage > imageManager.minimumAgeBeforeDeletion
}
// Implementing sort interface based on last used times of the images
func (imageStates ImageStatesForDeletion) Len() int {
return len(imageStates)
}
func (imageStates ImageStatesForDeletion) Less(i, j int) bool {
return imageStates[i].LastUsedAt.Before(imageStates[j].LastUsedAt)
}
func (imageStates ImageStatesForDeletion) Swap(i, j int) {
imageStates[i], imageStates[j] = imageStates[j], imageStates[i]
}
func (imageManager *dockerImageManager) getLeastRecentlyUsedImage(imagesForDeletion []*image.ImageState) *image.ImageState {
var candidateImages ImageStatesForDeletion
for _, imageState := range imagesForDeletion {
candidateImages = append(candidateImages, imageState)
}
// sort images in the order of last used times
sort.Sort(candidateImages)
// return only the top LRU image for deletion
return candidateImages[0]
}
func (imageManager *dockerImageManager) removeExistingImageNameOfDifferentID(containerImageName string, inspectedImageID string) {
for _, imageState := range imageManager.getAllImageStates() {
// image with same name pulled in the instance. Untag the already existing image name
if imageState.Image.ImageID != inspectedImageID {
imageState.RemoveImageName(containerImageName)
}
}
}
func (imageManager *dockerImageManager) StartImageCleanupProcess(ctx context.Context) {
// passing the cleanup interval as argument which would help during testing
imageManager.performPeriodicImageCleanup(ctx, imageManager.imageCleanupTimeInterval)
}
func (imageManager *dockerImageManager) performPeriodicImageCleanup(ctx context.Context, imageCleanupInterval time.Duration) {
imageManager.imageCleanupTicker = time.NewTicker(imageCleanupInterval)
for {
select {
case <-imageManager.imageCleanupTicker.C:
go imageManager.removeUnusedImages()
case <-ctx.Done():
imageManager.imageCleanupTicker.Stop()
return
}
}
}
func (imageManager *dockerImageManager) removeUnusedImages() {
seelog.Debug("Attempting to obtain ImagePullDeleteLock for removing images")
ImagePullDeleteLock.Lock()
seelog.Debug("Obtained ImagePullDeleteLock for removing images")
defer seelog.Debug("Released ImagePullDeleteLock after removing images")
defer ImagePullDeleteLock.Unlock()
imageManager.updateLock.Lock()
defer imageManager.updateLock.Unlock()
imageManager.imageStatesConsideredForDeletion = make(map[string]*image.ImageState)
seelog.Info("Begin building map of eligible unused images for deletion")
for _, imageState := range imageManager.getAllImageStates() {
imageManager.imageStatesConsideredForDeletion[imageState.Image.ImageID] = imageState
}
for i := 0; i < imageManager.numImagesToDelete; i++ {
err := imageManager.removeLeastRecentlyUsedImage()
if err != nil {
seelog.Infof("End of eligible images for deletion: %v; Still have %d image states being managed", err, len(imageManager.getAllImageStates()))
break
}
}
}
func (imageManager *dockerImageManager) removeLeastRecentlyUsedImage() error {
leastRecentlyUsedImage := imageManager.getUnusedImageForDeletion()
if leastRecentlyUsedImage == nil {
return fmt.Errorf("No more eligible images for deletion")
}
imageManager.removeImage(leastRecentlyUsedImage)
return nil
}
func (imageManager *dockerImageManager) getUnusedImageForDeletion() *image.ImageState {
candidateImageStatesForDeletion := imageManager.getCandidateImagesForDeletion()
if len(candidateImageStatesForDeletion) < 1 {
seelog.Infof("No eligible images for deletion for this cleanup cycle")
return nil
}
seelog.Infof("Found %d eligible images for deletion", len(candidateImageStatesForDeletion))
return imageManager.getLeastRecentlyUsedImage(candidateImageStatesForDeletion)
}
func (imageManager *dockerImageManager) removeImage(leastRecentlyUsedImage *image.ImageState) {
// Handling deleting while traversing a slice
imageNames := make([]string, len(leastRecentlyUsedImage.Image.Names))
copy(imageNames, leastRecentlyUsedImage.Image.Names)
if len(imageNames) == 0 {
// potentially untagged image of format <none>:<none>; remove by ID
imageManager.deleteImage(leastRecentlyUsedImage.Image.ImageID, leastRecentlyUsedImage)
} else {
// Image has multiple tags/repos. Untag each name and delete the final reference to image
for _, imageName := range imageNames {
imageManager.deleteImage(imageName, leastRecentlyUsedImage)
}
}
}
func (imageManager *dockerImageManager) deleteImage(imageID string, imageState *image.ImageState) {
if imageID == "" {
seelog.Errorf("Image ID to be deleted is null")
return
}
seelog.Infof("Removing Image: %s", imageID)
err := imageManager.client.RemoveImage(imageID, removeImageTimeout)
if err != nil {
if err.Error() == imageNotFoundForDeletionError {
seelog.Errorf("Image already removed from the instance: %v", err)
} else {
seelog.Errorf("Error removing Image %v - %v", imageID, err)
delete(imageManager.imageStatesConsideredForDeletion, imageState.Image.ImageID)
return
}
}
seelog.Infof("Image removed: %v", imageID)
imageState.RemoveImageName(imageID)
if len(imageState.Image.Names) == 0 {
seelog.Infof("Cleaning up all tracking information for image %s as it has zero references", imageID)
delete(imageManager.imageStatesConsideredForDeletion, imageState.Image.ImageID)
imageManager.removeImageState(imageState)
imageManager.state.RemoveImageState(imageState)
imageManager.saver.Save()
}
}
func (imageManager *dockerImageManager) GetImageStateFromImageName(containerImageName string) *image.ImageState {
imageManager.updateLock.Lock()
defer imageManager.updateLock.Unlock()
for _, imageState := range imageManager.getAllImageStates() {
for _, imageName := range imageState.Image.Names {
if imageName == containerImageName {
return imageState
}
}
}
return nil
}