detect volume we didn't created and ask user to explicitely mark them as external

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-11-19 08:30:33 +01:00 committed by Nicolas De loof
parent 8f9dc2e7f8
commit 10cd7e130f

View File

@ -210,7 +210,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
volume.Labels = volume.Labels.Add(api.VolumeLabel, k) volume.Labels = volume.Labels.Add(api.VolumeLabel, k)
volume.Labels = volume.Labels.Add(api.ProjectLabel, project.Name) volume.Labels = volume.Labels.Add(api.ProjectLabel, project.Name)
volume.Labels = volume.Labels.Add(api.VersionLabel, api.ComposeVersion) volume.Labels = volume.Labels.Add(api.VersionLabel, api.ComposeVersion)
err := s.ensureVolume(ctx, volume) err := s.ensureVolume(ctx, volume, project.Name)
if err != nil { if err != nil {
return err return err
} }
@ -1086,27 +1086,42 @@ func (s *composeService) removeNetwork(ctx context.Context, networkID string, ne
return nil return nil
} }
func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error { func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig, project string) error {
// TODO could identify volume by label vs name inspected, err := s.apiClient.VolumeInspect(ctx, volume.Name)
_, err := s.apiClient.VolumeInspect(ctx, volume.Name)
if err != nil { if err != nil {
if !errdefs.IsNotFound(err) { if !errdefs.IsNotFound(err) {
return err return err
} }
eventName := fmt.Sprintf("Volume %q", volume.Name) err := s.createVolume(ctx, volume)
w := progress.ContextWriter(ctx) return err
w.Event(progress.CreatingEvent(eventName)) }
_, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
Labels: volume.Labels, // Volume exists with name, but let's double check this is the expected one
Name: volume.Name, // (better safe than sorry when it comes to user's data)
Driver: volume.Driver, p, ok := inspected.Labels[api.ProjectLabel]
DriverOpts: volume.DriverOpts, if !ok {
}) return fmt.Errorf("volume %q already exists but was not created by Docker Compose. Use `external: true` to use an existing volume", volume.Name)
if err != nil { }
w.Event(progress.ErrorEvent(eventName)) if p != project {
return err return fmt.Errorf("volume %q already exists but was not created for project %q. Use `external: true` to use an existing volume", volume.Name, p)
}
w.Event(progress.CreatedEvent(eventName))
} }
return nil return nil
} }
func (s *composeService) createVolume(ctx context.Context, volume types.VolumeConfig) error {
eventName := fmt.Sprintf("Volume %q", volume.Name)
w := progress.ContextWriter(ctx)
w.Event(progress.CreatingEvent(eventName))
_, err := s.apiClient.VolumeCreate(ctx, volume_api.VolumeCreateBody{
Labels: volume.Labels,
Name: volume.Name,
Driver: volume.Driver,
DriverOpts: volume.DriverOpts,
})
if err != nil {
w.Event(progress.ErrorEvent(eventName))
return err
}
w.Event(progress.CreatedEvent(eventName))
return nil
}