Create non-existing named volumes at container create

Replaces old functionality we used for handling image volumes.

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
Matthew Heon 2019-03-22 14:23:18 -04:00
parent d245c6df29
commit ee770ad5b5
2 changed files with 52 additions and 54 deletions

View File

@ -1265,14 +1265,11 @@ func WithNamedVolumes(volumes []*ContainerNamedVolume) CtrCreateOption {
destinations := make(map[string]bool) destinations := make(map[string]bool)
for _, vol := range volumes { for _, vol := range volumes {
// First check if libpod has the volumes // Don't check if they already exist.
_, err := ctr.runtime.GetVolume(vol.Name) // If they don't we will automatically create them.
if err != nil {
return errors.Wrapf(err, "error retrieving volume %s to add to container", vol.Name)
}
if _, ok := destinations[vol.Dest]; ok { if _, ok := destinations[vol.Dest]; ok {
return errors.Wrapf(err, "two volumes found with destination %s", vol.Dest) return errors.Wrapf(ErrInvalidArg, "two volumes found with destination %s", vol.Dest)
} }
destinations[vol.Dest] = true destinations[vol.Dest] = true
@ -1306,28 +1303,6 @@ func WithVolumeName(name string) VolumeCreateOption {
} }
} }
// WithVolumeUID sets the uid of the owner.
func WithVolumeUID(uid int) VolumeCreateOption {
return func(volume *Volume) error {
if volume.valid {
return ErrVolumeFinalized
}
volume.config.UID = uid
return nil
}
}
// WithVolumeGID sets the gid of the owner.
func WithVolumeGID(gid int) VolumeCreateOption {
return func(volume *Volume) error {
if volume.valid {
return ErrVolumeFinalized
}
volume.config.GID = gid
return nil
}
}
// WithVolumeLabels sets the labels of the volume. // WithVolumeLabels sets the labels of the volume.
func WithVolumeLabels(labels map[string]string) VolumeCreateOption { func WithVolumeLabels(labels map[string]string) VolumeCreateOption {
return func(volume *Volume) error { return func(volume *Volume) error {
@ -1373,6 +1348,32 @@ func WithVolumeOptions(options map[string]string) VolumeCreateOption {
} }
} }
// WithVolumeUID sets the UID that the volume will be created as.
func WithVolumeUID(uid int) VolumeCreateOption {
return func(volume *Volume) error {
if volume.valid {
return ErrVolumeFinalized
}
volume.config.UID = uid
return nil
}
}
// WithVolumeGID sets the GID that the volume will be created as.
func WithVolumeGID(gid int) VolumeCreateOption {
return func(volume *Volume) error {
if volume.valid {
return ErrVolumeFinalized
}
volume.config.GID = gid
return nil
}
}
// withSetCtrSpecific sets a bool notifying libpod that a volume was created // withSetCtrSpecific sets a bool notifying libpod that a volume was created
// specifically for a container. // specifically for a container.
// These volumes will be removed when the container is removed and volumes are // These volumes will be removed when the container is removed and volumes are

View File

@ -99,9 +99,6 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options ..
ctr.state.State = ContainerStateConfigured ctr.state.State = ContainerStateConfigured
ctr.runtime = r ctr.runtime = r
ctr.valid = true
ctr.state.State = ContainerStateConfigured
var pod *Pod var pod *Pod
if ctr.config.Pod != "" { if ctr.config.Pod != "" {
// Get the pod from state // Get the pod from state
@ -173,29 +170,29 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options ..
ctr.config.ConmonPidFile = filepath.Join(ctr.config.StaticDir, "conmon.pid") ctr.config.ConmonPidFile = filepath.Join(ctr.config.StaticDir, "conmon.pid")
} }
// // Go through the volume mounts and check for named volumes // Go through named volumes and add them.
// // If the named volme already exists continue, otherwise create // If they don't exist they will be created using basic options.
// // the storage for the named volume. for _, vol := range ctr.config.NamedVolumes {
// for i, vol := range ctr.config.Spec.Mounts { // Check if it exists already
// if vol.Source[0] != '/' && isNamedVolume(vol.Source) { _, err := r.state.Volume(vol.Name)
// volInfo, err := r.state.Volume(vol.Source) if err == nil {
// if err != nil { // The volume exists, we're good
// newVol, err := r.newVolume(ctx, WithVolumeName(vol.Source), withSetCtrSpecific()) continue
// if err != nil { } else if errors.Cause(err) != ErrNoSuchVolume {
// return nil, errors.Wrapf(err, "error creating named volume %q", vol.Source) return nil, errors.Wrapf(err, "error retrieving named volume %s for new container", vol.Name)
// } }
// ctr.config.Spec.Mounts[i].Source = newVol.MountPoint()
// if err := os.Chown(ctr.config.Spec.Mounts[i].Source, ctr.RootUID(), ctr.RootGID()); err != nil { // The volume does not exist, so we need to create it.
// return nil, errors.Wrapf(err, "cannot chown %q to %d:%d", ctr.config.Spec.Mounts[i].Source, ctr.RootUID(), ctr.RootGID()) newVol, err := r.newVolume(ctx, WithVolumeName(vol.Name), withSetCtrSpecific(),
// } WithVolumeUID(ctr.RootUID()), WithVolumeGID(ctr.RootGID()))
// if err := ctr.copyWithTarFromImage(ctr.config.Spec.Mounts[i].Destination, ctr.config.Spec.Mounts[i].Source); err != nil && !os.IsNotExist(err) { if err != nil {
// return nil, errors.Wrapf(err, "Failed to copy content into new volume mount %q", vol.Source) return nil, errors.Wrapf(err, "error creating named volume %q", vol.Name)
// } }
// continue
// } if err := ctr.copyWithTarFromImage(vol.Dest, newVol.MountPoint()); err != nil && !os.IsNotExist(err) {
// ctr.config.Spec.Mounts[i].Source = volInfo.MountPoint() return nil, errors.Wrapf(err, "Failed to copy content into new volume mount %q", vol.Name)
// } }
// } }
if ctr.config.LogPath == "" { if ctr.config.LogPath == "" {
ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log") ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log")