@@ -182,14 +182,14 @@ func (ds *dockerService) CreateContainer(_ context.Context, r *runtimeapi.Create
182
182
if cleanupInfo != nil {
183
183
// we don't perform the clean up just yet at that could destroy information
184
184
// needed for the container to start (e.g. Windows credentials stored in
185
- // registry keys); instead, we'll clean up after the container successfully
186
- // starts or gets removed
185
+ // registry keys); instead, we'll clean up when the container gets removed
187
186
ds .containerCleanupInfos [containerID ] = cleanupInfo
188
187
}
189
188
return & runtimeapi.CreateContainerResponse {ContainerId : containerID }, nil
190
189
}
191
190
192
- // the creation failed, let's clean up right away
191
+ // the creation failed, let's clean up right away - we ignore any errors though,
192
+ // this is best effort
193
193
ds .performPlatformSpecificContainerCleanupAndLogErrors (containerName , cleanupInfo )
194
194
195
195
return nil , createErr
@@ -278,8 +278,6 @@ func (ds *dockerService) StartContainer(_ context.Context, r *runtimeapi.StartCo
278
278
return nil , fmt .Errorf ("failed to start container %q: %v" , r .ContainerId , err )
279
279
}
280
280
281
- ds .performPlatformSpecificContainerForContainer (r .ContainerId )
282
-
283
281
return & runtimeapi.StartContainerResponse {}, nil
284
282
}
285
283
@@ -294,19 +292,22 @@ func (ds *dockerService) StopContainer(_ context.Context, r *runtimeapi.StopCont
294
292
295
293
// RemoveContainer removes the container.
296
294
func (ds * dockerService ) RemoveContainer (_ context.Context , r * runtimeapi.RemoveContainerRequest ) (* runtimeapi.RemoveContainerResponse , error ) {
297
- ds .performPlatformSpecificContainerForContainer (r .ContainerId )
298
-
299
295
// Ideally, log lifecycle should be independent of container lifecycle.
300
296
// However, docker will remove container log after container is removed,
301
297
// we can't prevent that now, so we also clean up the symlink here.
302
298
err := ds .removeContainerLogSymlink (r .ContainerId )
303
299
if err != nil {
304
300
return nil , err
305
301
}
302
+ errors := ds .performPlatformSpecificContainerForContainer (r .ContainerId )
303
+ if len (errors ) != 0 {
304
+ return nil , fmt .Errorf ("failed to run platform-specific clean ups for container %q: %v" , r .ContainerId , errors )
305
+ }
306
306
err = ds .client .RemoveContainer (r .ContainerId , dockertypes.ContainerRemoveOptions {RemoveVolumes : true , Force : true })
307
307
if err != nil {
308
308
return nil , fmt .Errorf ("failed to remove container %q: %v" , r .ContainerId , err )
309
309
}
310
+
310
311
return & runtimeapi.RemoveContainerResponse {}, nil
311
312
}
312
313
@@ -454,19 +455,27 @@ func (ds *dockerService) UpdateContainerResources(_ context.Context, r *runtimea
454
455
return & runtimeapi.UpdateContainerResourcesResponse {}, nil
455
456
}
456
457
457
- func (ds * dockerService ) performPlatformSpecificContainerForContainer (containerID string ) {
458
+ func (ds * dockerService ) performPlatformSpecificContainerForContainer (containerID string ) ( errors [] error ) {
458
459
if cleanupInfo , present := ds .containerCleanupInfos [containerID ]; present {
459
- ds .performPlatformSpecificContainerCleanupAndLogErrors (containerID , cleanupInfo )
460
- delete (ds .containerCleanupInfos , containerID )
460
+ errors = ds .performPlatformSpecificContainerCleanupAndLogErrors (containerID , cleanupInfo )
461
+
462
+ if len (errors ) == 0 {
463
+ delete (ds .containerCleanupInfos , containerID )
464
+ }
461
465
}
466
+
467
+ return
462
468
}
463
469
464
- func (ds * dockerService ) performPlatformSpecificContainerCleanupAndLogErrors (containerNameOrID string , cleanupInfo * containerCleanupInfo ) {
470
+ func (ds * dockerService ) performPlatformSpecificContainerCleanupAndLogErrors (containerNameOrID string , cleanupInfo * containerCleanupInfo ) [] error {
465
471
if cleanupInfo == nil {
466
- return
472
+ return nil
467
473
}
468
474
469
- for _ , err := range ds .performPlatformSpecificContainerCleanup (cleanupInfo ) {
475
+ errors := ds .performPlatformSpecificContainerCleanup (cleanupInfo )
476
+ for _ , err := range errors {
470
477
klog .Warningf ("error when cleaning up after container %q: %v" , containerNameOrID , err )
471
478
}
479
+
480
+ return errors
472
481
}
0 commit comments