Create volume with labels

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-11-26 14:53:16 +01:00
parent 646aca82d9
commit f65a0d3720
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
2 changed files with 15 additions and 6 deletions

View File

@ -73,7 +73,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, detach
volume.Name = fmt.Sprintf("%s_%s", project.Name, k) volume.Name = fmt.Sprintf("%s_%s", project.Name, k)
project.Volumes[k] = volume project.Volumes[k] = volume
} }
err := s.ensureVolume(ctx, volume) err := s.ensureVolume(ctx, project, volume)
if err != nil { if err != nil {
return err return err
} }
@ -182,8 +182,7 @@ func (s *composeService) Logs(ctx context.Context, projectName string, w io.Writ
return err return err
}) })
} }
eg.Wait() return eg.Wait()
return nil
} }
func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) { 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 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 // TODO could identify volume by label vs name
_, err := s.apiClient.VolumeInspect(ctx, volume.Name) _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
if err != nil { 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) { if errdefs.IsNotFound(err) {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.Event{ 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 // TODO we miss support for driver_opts and labels
_, err := s.apiClient.VolumeCreate(ctx, mobyvolume.VolumeCreateBody{ _, err := s.apiClient.VolumeCreate(ctx, mobyvolume.VolumeCreateBody{
Labels: nil, Labels: labels,
Name: volume.Name, Name: volume.Name,
Driver: volume.Driver,
DriverOpts: volume.DriverOpts,
}) })
w.Event(progress.Event{ w.Event(progress.Event{
ID: fmt.Sprintf("Volume %q", volume.Name), ID: fmt.Sprintf("Volume %q", volume.Name),

View File

@ -28,6 +28,7 @@ const (
containerNumberLabel = "com.docker.compose.container-number" containerNumberLabel = "com.docker.compose.container-number"
oneoffLabel = "com.docker.compose.oneoff" oneoffLabel = "com.docker.compose.oneoff"
projectLabel = "com.docker.compose.project" projectLabel = "com.docker.compose.project"
volumeLabel = "com.docker.compose.volume"
workingDirLabel = "com.docker.compose.project.working_dir" workingDirLabel = "com.docker.compose.project.working_dir"
configFilesLabel = "com.docker.compose.project.config_files" configFilesLabel = "com.docker.compose.project.config_files"
serviceLabel = "com.docker.compose.service" serviceLabel = "com.docker.compose.service"