From ca152e7aa2848d80a395ff25ffaf67e1128e979e Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Mon, 29 May 2023 17:26:30 +0200 Subject: [PATCH 1/3] fix: implement volume deletion when removing a devcontainer Signed-off-by: Luca Di Maio --- pkg/docker/helper.go | 9 +++++++++ pkg/driver/docker/docker.go | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/docker/helper.go b/pkg/docker/helper.go index ae39f8e5f..9439f18e5 100644 --- a/pkg/docker/helper.go +++ b/pkg/docker/helper.go @@ -55,6 +55,15 @@ func (r *DockerHelper) FindDevContainer(labels []string) (*config.ContainerDetai return nil, nil } +func (r *DockerHelper) DeleteVolume(ctx context.Context, volume string) error { + out, err := r.buildCmd(ctx, "volume", "rm", volume).CombinedOutput() + if err != nil { + return perrors.Wrapf(err, "%s", string(out)) + } + + return nil +} + func (r *DockerHelper) Stop(ctx context.Context, id string) error { out, err := r.buildCmd(ctx, "stop", id).CombinedOutput() if err != nil { diff --git a/pkg/driver/docker/docker.go b/pkg/driver/docker/docker.go index 00406fc8c..0723af574 100644 --- a/pkg/driver/docker/docker.go +++ b/pkg/driver/docker/docker.go @@ -90,9 +90,28 @@ func (d *dockerDriver) StartDevContainer(ctx context.Context, id string, labels } func (d *dockerDriver) DeleteDevContainer(ctx context.Context, id string, deleteVolumes bool) error { - // TODO: implement deleteVolumes + var volume string - return d.Docker.Remove(ctx, id) + if deleteVolumes { + // inspect container deleteVolumes + container, err := d.Docker.InspectContainers([]string{id}) + if err != nil { + return err + } + volume = container[0].Config.Labels["dev.containers.id"] + } + + + err := d.Docker.Remove(ctx, id) + if err != nil { + return err + } + + if deleteVolumes { + return d.Docker.DeleteVolume(ctx, volume) + } + + return nil } func (d *dockerDriver) StopDevContainer(ctx context.Context, id string) error { From fc26764daf6fc4eab99e87ec9084fe2e3c198d39 Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Tue, 30 May 2023 14:16:31 +0200 Subject: [PATCH 2/3] fix: delete volume only if it exists Signed-off-by: Luca Di Maio --- pkg/docker/helper.go | 15 ++++++++++++++- pkg/driver/docker/docker.go | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/docker/helper.go b/pkg/docker/helper.go index 9439f18e5..29a287c60 100644 --- a/pkg/docker/helper.go +++ b/pkg/docker/helper.go @@ -56,7 +56,20 @@ func (r *DockerHelper) FindDevContainer(labels []string) (*config.ContainerDetai } func (r *DockerHelper) DeleteVolume(ctx context.Context, volume string) error { - out, err := r.buildCmd(ctx, "volume", "rm", volume).CombinedOutput() + if volume == "" { + return nil + } + + // If volume does not exist, just exit + out, err := r.buildCmd(ctx, "volume", "list", "-q", "--filter", "name="+volume).CombinedOutput() + if err != nil { + return nil + } + if len(out) == 0 { + return nil + } + + out, err = r.buildCmd(ctx, "volume", "rm", volume).CombinedOutput() if err != nil { return perrors.Wrapf(err, "%s", string(out)) } diff --git a/pkg/driver/docker/docker.go b/pkg/driver/docker/docker.go index 0723af574..077784993 100644 --- a/pkg/driver/docker/docker.go +++ b/pkg/driver/docker/docker.go @@ -101,7 +101,6 @@ func (d *dockerDriver) DeleteDevContainer(ctx context.Context, id string, delete volume = container[0].Config.Labels["dev.containers.id"] } - err := d.Docker.Remove(ctx, id) if err != nil { return err From bbf7258b3b3d5e1a3472e13d8fb326bb6621415c Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Wed, 31 May 2023 09:36:04 +0200 Subject: [PATCH 3/3] fix: add additional check for container existence Signed-off-by: Luca Di Maio --- pkg/driver/docker/docker.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/driver/docker/docker.go b/pkg/driver/docker/docker.go index 077784993..7e416002e 100644 --- a/pkg/driver/docker/docker.go +++ b/pkg/driver/docker/docker.go @@ -98,6 +98,9 @@ func (d *dockerDriver) DeleteDevContainer(ctx context.Context, id string, delete if err != nil { return err } + if len(container) == 0 { + return errors.Errorf("cannot find container") + } volume = container[0].Config.Labels["dev.containers.id"] }