From f65a0d372024426896a4034eb741508e7722de8c Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 26 Nov 2020 14:53:16 +0100 Subject: [PATCH] Create volume with labels Signed-off-by: Nicolas De Loof --- local/compose.go | 20 ++++++++++++++------ local/labels.go | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/local/compose.go b/local/compose.go index 1688db1c0..aa7c7b658 100644 --- a/local/compose.go +++ b/local/compose.go @@ -73,7 +73,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, detach volume.Name = fmt.Sprintf("%s_%s", project.Name, k) project.Volumes[k] = volume } - err := s.ensureVolume(ctx, volume) + err := s.ensureVolume(ctx, project, volume) if err != nil { return err } @@ -182,8 +182,7 @@ func (s *composeService) Logs(ctx context.Context, projectName string, w io.Writ return err }) } - eg.Wait() - return nil + return eg.Wait() } func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) { @@ -592,10 +591,17 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi return nil } -func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error { +func (s *composeService) ensureVolume(ctx context.Context, project *types.Project, volume types.VolumeConfig) error { // TODO could identify volume by label vs name _, err := s.apiClient.VolumeInspect(ctx, volume.Name) if err != nil { + labels := volume.Labels + if labels == nil { + labels = map[string]string{} + } + labels[projectLabel] = project.Name + labels[volumeLabel] = volume.Name + if errdefs.IsNotFound(err) { w := progress.ContextWriter(ctx) w.Event(progress.Event{ @@ -605,8 +611,10 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo }) // TODO we miss support for driver_opts and labels _, err := s.apiClient.VolumeCreate(ctx, mobyvolume.VolumeCreateBody{ - Labels: nil, - Name: volume.Name, + Labels: labels, + Name: volume.Name, + Driver: volume.Driver, + DriverOpts: volume.DriverOpts, }) w.Event(progress.Event{ ID: fmt.Sprintf("Volume %q", volume.Name), diff --git a/local/labels.go b/local/labels.go index 318ac633e..e026b15c2 100644 --- a/local/labels.go +++ b/local/labels.go @@ -28,6 +28,7 @@ const ( containerNumberLabel = "com.docker.compose.container-number" oneoffLabel = "com.docker.compose.oneoff" projectLabel = "com.docker.compose.project" + volumeLabel = "com.docker.compose.volume" workingDirLabel = "com.docker.compose.project.working_dir" configFilesLabel = "com.docker.compose.project.config_files" serviceLabel = "com.docker.compose.service"