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,13 +1086,29 @@ 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
} }
err := s.createVolume(ctx, volume)
return err
}
// Volume exists with name, but let's double check this is the expected one
// (better safe than sorry when it comes to user's data)
p, ok := inspected.Labels[api.ProjectLabel]
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 p != project {
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)
}
return nil
}
func (s *composeService) createVolume(ctx context.Context, volume types.VolumeConfig) error {
eventName := fmt.Sprintf("Volume %q", volume.Name) eventName := fmt.Sprintf("Volume %q", volume.Name)
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.CreatingEvent(eventName)) w.Event(progress.CreatingEvent(eventName))
@ -1107,6 +1123,5 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo
return err return err
} }
w.Event(progress.CreatedEvent(eventName)) w.Event(progress.CreatedEvent(eventName))
}
return nil return nil
} }