Skip to content

Commit

Permalink
only remove volumes set by compose file
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Jan 4, 2022
1 parent 9f06a02 commit e25ae54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
46 changes: 28 additions & 18 deletions pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
}

if options.Project == nil {
options.Project = s.projectFromContainerLabels(containers.filter(isNotOneOff), projectName)
options.Project, err = s.projectFromLabels(ctx, containers.filter(isNotOneOff), projectName)
if err != nil {
return err
}
}

if len(containers) > 0 {
Expand Down Expand Up @@ -85,11 +88,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
}

if options.Volumes {
rm, err := s.ensureVolumesDown(ctx, projectName, w)
if err != nil {
return err
}
ops = append(ops, rm...)
ops = append(ops, s.ensureVolumesDown(ctx, options.Project, w)...)
}

if !resourceToRemove && len(ops) == 0 {
Expand All @@ -103,19 +102,14 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
return eg.Wait()
}

func (s *composeService) ensureVolumesDown(ctx context.Context, projectName string, w progress.Writer) ([]downOp, error) {
func (s *composeService) ensureVolumesDown(ctx context.Context, project *types.Project, w progress.Writer) []downOp {
var ops []downOp
volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
if err != nil {
return ops, err
}
for _, vol := range volumes.Volumes {
id := vol.Name
for _, vol := range project.Volumes {
ops = append(ops, func() error {
return s.removeVolume(ctx, id, w)
return s.removeVolume(ctx, vol.Name, w)
})
}
return ops, nil
return ops
}

func (s *composeService) ensureImagesDown(ctx context.Context, projectName string, options api.DownOptions, w progress.Writer) []downOp {
Expand Down Expand Up @@ -237,12 +231,13 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
return eg.Wait()
}

func (s *composeService) projectFromContainerLabels(containers Containers, projectName string) *types.Project {
// projectFromLabels builds a types.Project based on actual resources with compose labels set
func (s *composeService) projectFromLabels(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
project := &types.Project{
Name: projectName,
}
if len(containers) == 0 {
return project
return project, nil
}
set := map[string]moby.Container{}
for _, c := range containers {
Expand All @@ -263,5 +258,20 @@ func (s *composeService) projectFromContainerLabels(containers Containers, proje
}
project.Services = append(project.Services, service)
}
return project

volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
if err != nil {
return nil, err
}

project.Volumes = types.Volumes{}
for _, vol := range volumes.Volumes {
project.Volumes[vol.Labels[api.VolumeLabel]] = types.VolumeConfig{
Name: vol.Name,
Driver: vol.Driver,
Labels: vol.Labels,
}
}

return project, nil
}
3 changes: 2 additions & 1 deletion pkg/compose/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestDownRemoveOrphans(t *testing.T) {
testContainer("service2", "789", false),
testContainer("service_orphan", "321", true),
}, nil)
api.EXPECT().VolumeList(gomock.Any(), projectFilterListOpt()).Return(nil)

api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
api.EXPECT().ContainerStop(gomock.Any(), "789", nil).Return(nil)
Expand All @@ -100,13 +101,13 @@ func TestDownRemoveVolumes(t *testing.T) {

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
[]moby.Container{testContainer("service1", "123", false)}, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).Return(volume.VolumeListOKBody{Volumes: []*moby.Volume{{Name: "myProject_volume"}}}, nil)

api.EXPECT().ContainerStop(gomock.Any(), "123", nil).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "123", moby.ContainerRemoveOptions{Force: true, RemoveVolumes: true}).Return(nil)

api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).Return(nil, nil)

api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).Return(volume.VolumeListOKBody{Volumes: []*moby.Volume{{Name: "myProject_volume"}}}, nil)
api.EXPECT().VolumeRemove(gomock.Any(), "myProject_volume", true).Return(nil)

err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{Volumes: true})
Expand Down

0 comments on commit e25ae54

Please sign in to comment.