diff options
Diffstat (limited to 'pkg/domain')
-rw-r--r-- | pkg/domain/entities/containers.go | 5 | ||||
-rw-r--r-- | pkg/domain/entities/engine_container.go | 7 | ||||
-rw-r--r-- | pkg/domain/entities/system.go | 6 | ||||
-rw-r--r-- | pkg/domain/infra/abi/archive.go | 172 | ||||
-rw-r--r-- | pkg/domain/infra/abi/containers_stat.go | 251 | ||||
-rw-r--r-- | pkg/domain/infra/abi/cp.go | 70 | ||||
-rw-r--r-- | pkg/domain/infra/abi/system.go | 12 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/containers.go | 225 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/healthcheck.go | 2 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/helpers.go | 9 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/pods.go | 63 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/system.go | 2 |
12 files changed, 609 insertions, 215 deletions
diff --git a/pkg/domain/entities/containers.go b/pkg/domain/entities/containers.go index 01086a2b3..4442c0030 100644 --- a/pkg/domain/entities/containers.go +++ b/pkg/domain/entities/containers.go @@ -8,6 +8,7 @@ import ( "github.com/containers/image/v5/types" "github.com/containers/podman/v2/libpod/define" + "github.com/containers/podman/v2/pkg/copy" "github.com/containers/podman/v2/pkg/specgen" "github.com/cri-o/ocicni/pkg/ocicni" ) @@ -143,6 +144,10 @@ type ContainerInspectReport struct { *define.InspectContainerData } +type ContainerStatReport struct { + copy.FileInfo +} + type CommitOptions struct { Author string Changes []string diff --git a/pkg/domain/entities/engine_container.go b/pkg/domain/entities/engine_container.go index ac9073402..80127ea45 100644 --- a/pkg/domain/entities/engine_container.go +++ b/pkg/domain/entities/engine_container.go @@ -2,6 +2,7 @@ package entities import ( "context" + "io" "github.com/containers/common/pkg/config" "github.com/containers/podman/v2/libpod/define" @@ -9,6 +10,8 @@ import ( "github.com/spf13/cobra" ) +type ContainerCopyFunc func() error + type ContainerEngine interface { AutoUpdate(ctx context.Context, options AutoUpdateOptions) (*AutoUpdateReport, []error) Config(ctx context.Context) (*config.Config, error) @@ -16,7 +19,8 @@ type ContainerEngine interface { ContainerCheckpoint(ctx context.Context, namesOrIds []string, options CheckpointOptions) ([]*CheckpointReport, error) ContainerCleanup(ctx context.Context, namesOrIds []string, options ContainerCleanupOptions) ([]*ContainerCleanupReport, error) ContainerCommit(ctx context.Context, nameOrID string, options CommitOptions) (*CommitReport, error) - ContainerCp(ctx context.Context, source, dest string, options ContainerCpOptions) error + ContainerCopyFromArchive(ctx context.Context, nameOrID string, path string, reader io.Reader) (ContainerCopyFunc, error) + ContainerCopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (ContainerCopyFunc, error) ContainerCreate(ctx context.Context, s *specgen.SpecGenerator) (*ContainerCreateReport, error) ContainerDiff(ctx context.Context, nameOrID string, options DiffOptions) (*DiffReport, error) ContainerExec(ctx context.Context, nameOrID string, options ExecOptions, streams define.AttachStreams) (int, error) @@ -38,6 +42,7 @@ type ContainerEngine interface { ContainerRun(ctx context.Context, opts ContainerRunOptions) (*ContainerRunReport, error) ContainerRunlabel(ctx context.Context, label string, image string, args []string, opts ContainerRunlabelOptions) error ContainerStart(ctx context.Context, namesOrIds []string, options ContainerStartOptions) ([]*ContainerStartReport, error) + ContainerStat(ctx context.Context, nameOrDir string, path string) (*ContainerStatReport, error) ContainerStats(ctx context.Context, namesOrIds []string, options ContainerStatsOptions) (chan ContainerStatsReport, error) ContainerStop(ctx context.Context, namesOrIds []string, options StopOptions) ([]*StopReport, error) ContainerTop(ctx context.Context, options TopOptions) (*StringSliceReport, error) diff --git a/pkg/domain/entities/system.go b/pkg/domain/entities/system.go index 5266dc4cf..d5118f6a8 100644 --- a/pkg/domain/entities/system.go +++ b/pkg/domain/entities/system.go @@ -17,9 +17,9 @@ type ServiceOptions struct { // SystemPruneOptions provides options to prune system. type SystemPruneOptions struct { - All bool - Volume bool - ContainerPruneOptions + All bool + Volume bool + Filters map[string][]string `json:"filters" schema:"filters"` } // SystemPruneReport provides report after system prune is executed. diff --git a/pkg/domain/infra/abi/archive.go b/pkg/domain/infra/abi/archive.go new file mode 100644 index 000000000..809813756 --- /dev/null +++ b/pkg/domain/infra/abi/archive.go @@ -0,0 +1,172 @@ +package abi + +import ( + "context" + "io" + "strings" + + buildahCopiah "github.com/containers/buildah/copier" + "github.com/containers/buildah/pkg/chrootuser" + "github.com/containers/buildah/util" + "github.com/containers/podman/v2/libpod" + "github.com/containers/podman/v2/pkg/domain/entities" + "github.com/containers/storage" + "github.com/containers/storage/pkg/archive" + "github.com/containers/storage/pkg/idtools" + "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +// NOTE: Only the parent directory of the container path must exist. The path +// itself may be created while copying. +func (ic *ContainerEngine) ContainerCopyFromArchive(ctx context.Context, nameOrID string, containerPath string, reader io.Reader) (entities.ContainerCopyFunc, error) { + container, err := ic.Libpod.LookupContainer(nameOrID) + if err != nil { + return nil, err + } + + unmount := func() { + if err := container.Unmount(false); err != nil { + logrus.Errorf("Error unmounting container: %v", err) + } + } + + _, resolvedRoot, resolvedContainerPath, err := ic.containerStat(container, containerPath) + if err != nil { + unmount() + return nil, err + } + + decompressed, err := archive.DecompressStream(reader) + if err != nil { + unmount() + return nil, err + } + + idMappings, idPair, err := getIDMappingsAndPair(container, resolvedRoot) + if err != nil { + unmount() + return nil, err + } + + logrus.Debugf("Container copy *to* %q (resolved: %q) on container %q (ID: %s)", containerPath, resolvedContainerPath, container.Name(), container.ID()) + + return func() error { + defer unmount() + defer decompressed.Close() + putOptions := buildahCopiah.PutOptions{ + UIDMap: idMappings.UIDMap, + GIDMap: idMappings.GIDMap, + ChownDirs: idPair, + ChownFiles: idPair, + } + return buildahCopiah.Put(resolvedRoot, resolvedContainerPath, putOptions, decompressed) + }, nil +} + +func (ic *ContainerEngine) ContainerCopyToArchive(ctx context.Context, nameOrID string, containerPath string, writer io.Writer) (entities.ContainerCopyFunc, error) { + container, err := ic.Libpod.LookupContainer(nameOrID) + if err != nil { + return nil, err + } + + unmount := func() { + if err := container.Unmount(false); err != nil { + logrus.Errorf("Error unmounting container: %v", err) + } + } + + // Make sure that "/" copies the *contents* of the mount point and not + // the directory. + if containerPath == "/" { + containerPath = "/." + } + + _, resolvedRoot, resolvedContainerPath, err := ic.containerStat(container, containerPath) + if err != nil { + unmount() + return nil, err + } + + idMappings, idPair, err := getIDMappingsAndPair(container, resolvedRoot) + if err != nil { + unmount() + return nil, err + } + + logrus.Debugf("Container copy *from* %q (resolved: %q) on container %q (ID: %s)", containerPath, resolvedContainerPath, container.Name(), container.ID()) + + return func() error { + defer container.Unmount(false) + getOptions := buildahCopiah.GetOptions{ + // Unless the specified path ends with ".", we want to copy the base directory. + KeepDirectoryNames: !strings.HasSuffix(resolvedContainerPath, "."), + UIDMap: idMappings.UIDMap, + GIDMap: idMappings.GIDMap, + ChownDirs: idPair, + ChownFiles: idPair, + } + return buildahCopiah.Get(resolvedRoot, "", getOptions, []string{resolvedContainerPath}, writer) + }, nil +} + +// getIDMappingsAndPair returns the ID mappings for the container and the host +// ID pair. +func getIDMappingsAndPair(container *libpod.Container, containerMount string) (*storage.IDMappingOptions, *idtools.IDPair, error) { + user, err := getContainerUser(container, containerMount) + if err != nil { + return nil, nil, err + } + + idMappingOpts, err := container.IDMappings() + if err != nil { + return nil, nil, err + } + + hostUID, hostGID, err := util.GetHostIDs(idtoolsToRuntimeSpec(idMappingOpts.UIDMap), idtoolsToRuntimeSpec(idMappingOpts.GIDMap), user.UID, user.GID) + if err != nil { + return nil, nil, err + } + + idPair := idtools.IDPair{UID: int(hostUID), GID: int(hostGID)} + return &idMappingOpts, &idPair, nil +} + +// getContainerUser returns the specs.User of the container. +func getContainerUser(container *libpod.Container, mountPoint string) (specs.User, error) { + userspec := container.Config().User + + uid, gid, _, err := chrootuser.GetUser(mountPoint, userspec) + u := specs.User{ + UID: uid, + GID: gid, + Username: userspec, + } + + if !strings.Contains(userspec, ":") { + groups, err2 := chrootuser.GetAdditionalGroupsForUser(mountPoint, uint64(u.UID)) + if err2 != nil { + if errors.Cause(err2) != chrootuser.ErrNoSuchUser && err == nil { + err = err2 + } + } else { + u.AdditionalGids = groups + } + } + + return u, err +} + +// idtoolsToRuntimeSpec converts idtools ID mapping to the one of the runtime spec. +func idtoolsToRuntimeSpec(idMaps []idtools.IDMap) (convertedIDMap []specs.LinuxIDMapping) { + for _, idmap := range idMaps { + tempIDMap := specs.LinuxIDMapping{ + ContainerID: uint32(idmap.ContainerID), + HostID: uint32(idmap.HostID), + Size: uint32(idmap.Size), + } + convertedIDMap = append(convertedIDMap, tempIDMap) + } + return convertedIDMap +} diff --git a/pkg/domain/infra/abi/containers_stat.go b/pkg/domain/infra/abi/containers_stat.go new file mode 100644 index 000000000..c9610d1b9 --- /dev/null +++ b/pkg/domain/infra/abi/containers_stat.go @@ -0,0 +1,251 @@ +package abi + +import ( + "context" + "os" + "path/filepath" + "strings" + + buildahCopiah "github.com/containers/buildah/copier" + "github.com/containers/podman/v2/libpod" + "github.com/containers/podman/v2/pkg/copy" + "github.com/containers/podman/v2/pkg/domain/entities" + securejoin "github.com/cyphar/filepath-securejoin" + "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func (ic *ContainerEngine) containerStat(container *libpod.Container, containerPath string) (*entities.ContainerStatReport, string, string, error) { + containerMountPoint, err := container.Mount() + if err != nil { + return nil, "", "", err + } + + // Make sure that "/" copies the *contents* of the mount point and not + // the directory. + if containerPath == "/" { + containerPath += "/." + } + + // Now resolve the container's path. It may hit a volume, it may hit a + // bind mount, it may be relative. + resolvedRoot, resolvedContainerPath, err := resolveContainerPaths(container, containerMountPoint, containerPath) + if err != nil { + return nil, "", "", err + } + + statInfo, statInfoErr := secureStat(resolvedRoot, resolvedContainerPath) + if statInfoErr != nil { + // Not all errors from secureStat map to ErrNotExist, so we + // have to look into the error string. Turning it into an + // ENOENT let's the API handlers return the correct status code + // which is crucuial for the remote client. + if os.IsNotExist(err) || strings.Contains(statInfoErr.Error(), "o such file or directory") { + statInfoErr = copy.ENOENT + } + // If statInfo is nil, there's nothing we can do anymore. A + // non-nil statInfo may indicate a symlink where we must have + // a closer look. + if statInfo == nil { + return nil, "", "", statInfoErr + } + } + + // Now make sure that the info's LinkTarget is relative to the + // container's mount. + var absContainerPath string + + if statInfo.IsSymlink { + // Evaluated symlinks are always relative to the container's mount point. + absContainerPath = statInfo.ImmediateTarget + } else if strings.HasPrefix(resolvedContainerPath, containerMountPoint) { + // If the path is on the container's mount point, strip it off. + absContainerPath = strings.TrimPrefix(resolvedContainerPath, containerMountPoint) + absContainerPath = filepath.Join("/", absContainerPath) + } else { + // No symlink and not on the container's mount point, so let's + // move it back to the original input. It must have evaluated + // to a volume or bind mount but we cannot return host paths. + absContainerPath = containerPath + } + + // Now we need to make sure to preseve the base path as specified by + // the user. The `filepath` packages likes to remove trailing slashes + // and dots that are crucial to the copy logic. + absContainerPath = copy.PreserveBasePath(containerPath, absContainerPath) + resolvedContainerPath = copy.PreserveBasePath(containerPath, resolvedContainerPath) + + info := copy.FileInfo{ + IsDir: statInfo.IsDir, + Name: filepath.Base(absContainerPath), + Size: statInfo.Size, + Mode: statInfo.Mode, + ModTime: statInfo.ModTime, + LinkTarget: absContainerPath, + } + + return &entities.ContainerStatReport{FileInfo: info}, resolvedRoot, resolvedContainerPath, statInfoErr +} + +func (ic *ContainerEngine) ContainerStat(ctx context.Context, nameOrID string, containerPath string) (*entities.ContainerStatReport, error) { + container, err := ic.Libpod.LookupContainer(nameOrID) + if err != nil { + return nil, err + } + + defer func() { + if err := container.Unmount(false); err != nil { + logrus.Errorf("Error unmounting container: %v", err) + } + }() + + statReport, _, _, err := ic.containerStat(container, containerPath) + return statReport, err +} + +// resolveContainerPaths resolves the container's mount point and the container +// path as specified by the user. Both may resolve to paths outside of the +// container's mount point when the container path hits a volume or bind mount. +// +// NOTE: We must take volumes and bind mounts into account as, regrettably, we +// can copy to/from stopped containers. In that case, the volumes and bind +// mounts are not present. For running containers, the runtime (e.g., runc or +// crun) takes care of these mounts. For stopped ones, we need to do quite +// some dance, as done below. +func resolveContainerPaths(container *libpod.Container, mountPoint string, containerPath string) (string, string, error) { + // Let's first make sure we have a path relative to the mount point. + pathRelativeToContainerMountPoint := containerPath + if !filepath.IsAbs(containerPath) { + // If the containerPath is not absolute, it's relative to the + // container's working dir. To be extra careful, let's first + // join the working dir with "/", and the add the containerPath + // to it. + pathRelativeToContainerMountPoint = filepath.Join(filepath.Join("/", container.WorkingDir()), containerPath) + } + resolvedPathOnTheContainerMountPoint := filepath.Join(mountPoint, pathRelativeToContainerMountPoint) + pathRelativeToContainerMountPoint = strings.TrimPrefix(pathRelativeToContainerMountPoint, mountPoint) + pathRelativeToContainerMountPoint = filepath.Join("/", pathRelativeToContainerMountPoint) + + // Now we have an "absolute container Path" but not yet resolved on the + // host (e.g., "/foo/bar/file.txt"). As mentioned above, we need to + // check if "/foo/bar/file.txt" is on a volume or bind mount. To do + // that, we need to walk *down* the paths to the root. Assuming + // volume-1 is mounted to "/foo" and volume-2 is mounted to "/foo/bar", + // we must select "/foo/bar". Once selected, we need to rebase the + // remainder (i.e, "/file.txt") on the volume's mount point on the + // host. Same applies to bind mounts. + + searchPath := pathRelativeToContainerMountPoint + for { + volume, err := findVolume(container, searchPath) + if err != nil { + return "", "", err + } + if volume != nil { + logrus.Debugf("Container path %q resolved to volume %q on path %q", containerPath, volume.Name(), searchPath) + // We found a matching volume for searchPath. We now + // need to first find the relative path of our input + // path to the searchPath, and then join it with the + // volume's mount point. + pathRelativeToVolume := strings.TrimPrefix(pathRelativeToContainerMountPoint, searchPath) + absolutePathOnTheVolumeMount, err := securejoin.SecureJoin(volume.MountPoint(), pathRelativeToVolume) + if err != nil { + return "", "", err + } + return volume.MountPoint(), absolutePathOnTheVolumeMount, nil + } + + if mount := findBindMount(container, searchPath); mount != nil { + logrus.Debugf("Container path %q resolved to bind mount %q:%q on path %q", containerPath, mount.Source, mount.Destination, searchPath) + // We found a matching bind mount for searchPath. We + // now need to first find the relative path of our + // input path to the searchPath, and then join it with + // the source of the bind mount. + pathRelativeToBindMount := strings.TrimPrefix(pathRelativeToContainerMountPoint, searchPath) + absolutePathOnTheBindMount, err := securejoin.SecureJoin(mount.Source, pathRelativeToBindMount) + if err != nil { + return "", "", err + } + return mount.Source, absolutePathOnTheBindMount, nil + + } + + if searchPath == "/" { + // Cannot go beyond "/", so we're done. + break + } + + // Walk *down* the path (e.g., "/foo/bar/x" -> "/foo/bar"). + searchPath = filepath.Dir(searchPath) + } + + // No volume, no bind mount but just a normal path on the container. + return mountPoint, resolvedPathOnTheContainerMountPoint, nil +} + +// findVolume checks if the specified container path matches a volume inside +// the container. It returns a matching volume or nil. +func findVolume(c *libpod.Container, containerPath string) (*libpod.Volume, error) { + runtime := c.Runtime() + cleanedContainerPath := filepath.Clean(containerPath) + for _, vol := range c.Config().NamedVolumes { + if cleanedContainerPath == filepath.Clean(vol.Dest) { + return runtime.GetVolume(vol.Name) + } + } + return nil, nil +} + +// findBindMount checks if the specified container path matches a bind mount +// inside the container. It returns a matching mount or nil. +func findBindMount(c *libpod.Container, containerPath string) *specs.Mount { + cleanedPath := filepath.Clean(containerPath) + for _, m := range c.Config().Spec.Mounts { + if m.Type != "bind" { + continue + } + if cleanedPath == filepath.Clean(m.Destination) { + mount := m + return &mount + } + } + return nil +} + +// secureStat extracts file info for path in a chroot'ed environment in root. +func secureStat(root string, path string) (*buildahCopiah.StatForItem, error) { + var glob string + var err error + + // If root and path are equal, then dir must be empty and the glob must + // be ".". + if filepath.Clean(root) == filepath.Clean(path) { + glob = "." + } else { + glob, err = filepath.Rel(root, path) + if err != nil { + return nil, err + } + } + + globStats, err := buildahCopiah.Stat(root, "", buildahCopiah.StatOptions{}, []string{glob}) + if err != nil { + return nil, err + } + + if len(globStats) != 1 { + return nil, errors.Errorf("internal error: secureStat: expected 1 item but got %d", len(globStats)) + } + + stat, exists := globStats[0].Results[glob] // only one glob passed, so that's okay + if !exists { + return nil, copy.ENOENT + } + + var statErr error + if stat.Error != "" { + statErr = errors.New(stat.Error) + } + return stat, statErr +} diff --git a/pkg/domain/infra/abi/cp.go b/pkg/domain/infra/abi/cp.go deleted file mode 100644 index 362053cce..000000000 --- a/pkg/domain/infra/abi/cp.go +++ /dev/null @@ -1,70 +0,0 @@ -package abi - -import ( - "context" - - "github.com/containers/podman/v2/libpod" - "github.com/containers/podman/v2/pkg/copy" - "github.com/containers/podman/v2/pkg/domain/entities" -) - -func (ic *ContainerEngine) ContainerCp(ctx context.Context, source, dest string, options entities.ContainerCpOptions) error { - // Parse user input. - sourceContainerStr, sourcePath, destContainerStr, destPath, err := copy.ParseSourceAndDestination(source, dest) - if err != nil { - return err - } - - // Look up containers. - var sourceContainer, destContainer *libpod.Container - if len(sourceContainerStr) > 0 { - sourceContainer, err = ic.Libpod.LookupContainer(sourceContainerStr) - if err != nil { - return err - } - } - if len(destContainerStr) > 0 { - destContainer, err = ic.Libpod.LookupContainer(destContainerStr) - if err != nil { - return err - } - } - - var sourceItem, destinationItem copy.CopyItem - - // Source ... container OR host. - if sourceContainer != nil { - sourceItem, err = copy.CopyItemForContainer(sourceContainer, sourcePath, options.Pause, true) - defer sourceItem.CleanUp() - if err != nil { - return err - } - } else { - sourceItem, err = copy.CopyItemForHost(sourcePath, true) - if err != nil { - return err - } - } - - // Destination ... container OR host. - if destContainer != nil { - destinationItem, err = copy.CopyItemForContainer(destContainer, destPath, options.Pause, false) - defer destinationItem.CleanUp() - if err != nil { - return err - } - } else { - destinationItem, err = copy.CopyItemForHost(destPath, false) - defer destinationItem.CleanUp() - if err != nil { - return err - } - } - - // Copy from the host to the container. - copier, err := copy.GetCopier(&sourceItem, &destinationItem, options.Extract) - if err != nil { - return err - } - return copier.Copy() -} diff --git a/pkg/domain/infra/abi/system.go b/pkg/domain/infra/abi/system.go index 17faa7fff..5f6c95d4f 100644 --- a/pkg/domain/infra/abi/system.go +++ b/pkg/domain/infra/abi/system.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io/ioutil" + "net/url" "os" "os/exec" "path/filepath" @@ -180,7 +181,12 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys found = true } systemPruneReport.PodPruneReport = append(systemPruneReport.PodPruneReport, podPruneReport...) - containerPruneReport, err := ic.ContainerPrune(ctx, options.ContainerPruneOptions) + + // TODO: Figure out cleaner way to handle all of the different PruneOptions + containerPruneOptions := entities.ContainerPruneOptions{} + containerPruneOptions.Filters = (url.Values)(options.Filters) + + containerPruneReport, err := ic.ContainerPrune(ctx, containerPruneOptions) if err != nil { return nil, err } @@ -217,7 +223,9 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys systemPruneReport.ImagePruneReport.Report.Id = append(systemPruneReport.ImagePruneReport.Report.Id, results...) } if options.Volume { - volumePruneReport, err := ic.pruneVolumesHelper(ctx, nil) + volumePruneOptions := entities.VolumePruneOptions{} + volumePruneOptions.Filters = (url.Values)(options.Filters) + volumePruneReport, err := ic.VolumePrune(ctx, volumePruneOptions) if err != nil { return nil, err } diff --git a/pkg/domain/infra/tunnel/containers.go b/pkg/domain/infra/tunnel/containers.go index ad7688f62..ad8394675 100644 --- a/pkg/domain/infra/tunnel/containers.go +++ b/pkg/domain/infra/tunnel/containers.go @@ -16,7 +16,6 @@ import ( "github.com/containers/podman/v2/libpod/define" "github.com/containers/podman/v2/libpod/events" "github.com/containers/podman/v2/pkg/api/handlers" - "github.com/containers/podman/v2/pkg/bindings" "github.com/containers/podman/v2/pkg/bindings/containers" "github.com/containers/podman/v2/pkg/domain/entities" "github.com/containers/podman/v2/pkg/errorhandling" @@ -35,15 +34,16 @@ func (ic *ContainerEngine) ContainerExists(ctx context.Context, nameOrID string, return &entities.BoolReport{Value: exists}, err } -func (ic *ContainerEngine) ContainerWait(ctx context.Context, namesOrIds []string, options entities.WaitOptions) ([]entities.WaitReport, error) { +func (ic *ContainerEngine) ContainerWait(ctx context.Context, namesOrIds []string, opts entities.WaitOptions) ([]entities.WaitReport, error) { cons, err := getContainersByContext(ic.ClientCxt, false, false, namesOrIds) if err != nil { return nil, err } responses := make([]entities.WaitReport, 0, len(cons)) + options := new(containers.WaitOptions).WithCondition(opts.Condition) for _, c := range cons { response := entities.WaitReport{Id: c.ID} - exitCode, err := containers.Wait(ic.ClientCxt, c.ID, &options.Condition) + exitCode, err := containers.Wait(ic.ClientCxt, c.ID, options) if err != nil { response.Error = err } else { @@ -61,7 +61,7 @@ func (ic *ContainerEngine) ContainerPause(ctx context.Context, namesOrIds []stri } reports := make([]*entities.PauseUnpauseReport, 0, len(ctrs)) for _, c := range ctrs { - err := containers.Pause(ic.ClientCxt, c.ID) + err := containers.Pause(ic.ClientCxt, c.ID, nil) reports = append(reports, &entities.PauseUnpauseReport{Id: c.ID, Err: err}) } return reports, nil @@ -74,15 +74,15 @@ func (ic *ContainerEngine) ContainerUnpause(ctx context.Context, namesOrIds []st } reports := make([]*entities.PauseUnpauseReport, 0, len(ctrs)) for _, c := range ctrs { - err := containers.Unpause(ic.ClientCxt, c.ID) + err := containers.Unpause(ic.ClientCxt, c.ID, nil) reports = append(reports, &entities.PauseUnpauseReport{Id: c.ID, Err: err}) } return reports, nil } -func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []string, options entities.StopOptions) ([]*entities.StopReport, error) { +func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []string, opts entities.StopOptions) ([]*entities.StopReport, error) { reports := []*entities.StopReport{} - for _, cidFile := range options.CIDFiles { + for _, cidFile := range opts.CIDFiles { content, err := ioutil.ReadFile(cidFile) if err != nil { return nil, errors.Wrap(err, "error reading CIDFile") @@ -90,19 +90,23 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin id := strings.Split(string(content), "\n")[0] namesOrIds = append(namesOrIds, id) } - ctrs, err := getContainersByContext(ic.ClientCxt, options.All, options.Ignore, namesOrIds) + ctrs, err := getContainersByContext(ic.ClientCxt, opts.All, opts.Ignore, namesOrIds) if err != nil { return nil, err } + options := new(containers.StopOptions) + if to := opts.Timeout; to != nil { + options.WithTimeout(*to) + } for _, c := range ctrs { report := entities.StopReport{Id: c.ID} - if err = containers.Stop(ic.ClientCxt, c.ID, options.Timeout); err != nil { + if err = containers.Stop(ic.ClientCxt, c.ID, options); err != nil { // These first two are considered non-fatal under the right conditions if errors.Cause(err).Error() == define.ErrCtrStopped.Error() { logrus.Debugf("Container %s is already stopped", c.ID) reports = append(reports, &report) continue - } else if options.All && errors.Cause(err).Error() == define.ErrCtrStateInvalid.Error() { + } else if opts.All && errors.Cause(err).Error() == define.ErrCtrStateInvalid.Error() { logrus.Debugf("Container %s is not running, could not stop", c.ID) reports = append(reports, &report) continue @@ -120,8 +124,8 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin return reports, nil } -func (ic *ContainerEngine) ContainerKill(ctx context.Context, namesOrIds []string, options entities.KillOptions) ([]*entities.KillReport, error) { - ctrs, err := getContainersByContext(ic.ClientCxt, options.All, false, namesOrIds) +func (ic *ContainerEngine) ContainerKill(ctx context.Context, namesOrIds []string, opts entities.KillOptions) ([]*entities.KillReport, error) { + ctrs, err := getContainersByContext(ic.ClientCxt, opts.All, false, namesOrIds) if err != nil { return nil, err } @@ -129,40 +133,38 @@ func (ic *ContainerEngine) ContainerKill(ctx context.Context, namesOrIds []strin for _, c := range ctrs { reports = append(reports, &entities.KillReport{ Id: c.ID, - Err: containers.Kill(ic.ClientCxt, c.ID, options.Signal), + Err: containers.Kill(ic.ClientCxt, c.ID, opts.Signal, nil), }) } return reports, nil } -func (ic *ContainerEngine) ContainerRestart(ctx context.Context, namesOrIds []string, options entities.RestartOptions) ([]*entities.RestartReport, error) { +func (ic *ContainerEngine) ContainerRestart(ctx context.Context, namesOrIds []string, opts entities.RestartOptions) ([]*entities.RestartReport, error) { var ( reports = []*entities.RestartReport{} - timeout *int ) - if options.Timeout != nil { - t := int(*options.Timeout) - timeout = &t + options := new(containers.RestartOptions) + if to := opts.Timeout; to != nil { + options.WithTimeout(int(*to)) } - - ctrs, err := getContainersByContext(ic.ClientCxt, options.All, false, namesOrIds) + ctrs, err := getContainersByContext(ic.ClientCxt, opts.All, false, namesOrIds) if err != nil { return nil, err } for _, c := range ctrs { - if options.Running && c.State != define.ContainerStateRunning.String() { + if opts.Running && c.State != define.ContainerStateRunning.String() { continue } reports = append(reports, &entities.RestartReport{ Id: c.ID, - Err: containers.Restart(ic.ClientCxt, c.ID, timeout), + Err: containers.Restart(ic.ClientCxt, c.ID, options), }) } return reports, nil } -func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, options entities.RmOptions) ([]*entities.RmReport, error) { - for _, cidFile := range options.CIDFiles { +func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, opts entities.RmOptions) ([]*entities.RmReport, error) { + for _, cidFile := range opts.CIDFiles { content, err := ioutil.ReadFile(cidFile) if err != nil { return nil, errors.Wrap(err, "error reading CIDFile") @@ -170,32 +172,35 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, id := strings.Split(string(content), "\n")[0] namesOrIds = append(namesOrIds, id) } - ctrs, err := getContainersByContext(ic.ClientCxt, options.All, options.Ignore, namesOrIds) + ctrs, err := getContainersByContext(ic.ClientCxt, opts.All, opts.Ignore, namesOrIds) if err != nil { return nil, err } // TODO there is no endpoint for container eviction. Need to discuss reports := make([]*entities.RmReport, 0, len(ctrs)) + options := new(containers.RemoveOptions).WithForce(opts.Force).WithVolumes(opts.Volumes) for _, c := range ctrs { reports = append(reports, &entities.RmReport{ Id: c.ID, - Err: containers.Remove(ic.ClientCxt, c.ID, &options.Force, &options.Volumes), + Err: containers.Remove(ic.ClientCxt, c.ID, options), }) } return reports, nil } -func (ic *ContainerEngine) ContainerPrune(ctx context.Context, options entities.ContainerPruneOptions) (*entities.ContainerPruneReport, error) { - return containers.Prune(ic.ClientCxt, options.Filters) +func (ic *ContainerEngine) ContainerPrune(ctx context.Context, opts entities.ContainerPruneOptions) (*entities.ContainerPruneReport, error) { + options := new(containers.PruneOptions).WithFilters(opts.Filters) + return containers.Prune(ic.ClientCxt, options) } -func (ic *ContainerEngine) ContainerInspect(ctx context.Context, namesOrIds []string, options entities.InspectOptions) ([]*entities.ContainerInspectReport, []error, error) { +func (ic *ContainerEngine) ContainerInspect(ctx context.Context, namesOrIds []string, opts entities.InspectOptions) ([]*entities.ContainerInspectReport, []error, error) { var ( reports = make([]*entities.ContainerInspectReport, 0, len(namesOrIds)) errs = []error{} ) + options := new(containers.InspectOptions).WithSize(opts.Size) for _, name := range namesOrIds { - inspect, err := containers.Inspect(ic.ClientCxt, name, &options.Size) + inspect, err := containers.Inspect(ic.ClientCxt, name, options) if err != nil { errModel, ok := err.(entities.ErrorModel) if !ok { @@ -212,30 +217,30 @@ func (ic *ContainerEngine) ContainerInspect(ctx context.Context, namesOrIds []st return reports, errs, nil } -func (ic *ContainerEngine) ContainerTop(ctx context.Context, options entities.TopOptions) (*entities.StringSliceReport, error) { +func (ic *ContainerEngine) ContainerTop(ctx context.Context, opts entities.TopOptions) (*entities.StringSliceReport, error) { switch { - case options.Latest: + case opts.Latest: return nil, errors.New("latest is not supported") - case options.NameOrID == "": + case opts.NameOrID == "": return nil, errors.New("NameOrID must be specified") } - - topOutput, err := containers.Top(ic.ClientCxt, options.NameOrID, options.Descriptors) + options := new(containers.TopOptions).WithDescriptors(opts.Descriptors) + topOutput, err := containers.Top(ic.ClientCxt, opts.NameOrID, options) if err != nil { return nil, err } return &entities.StringSliceReport{Value: topOutput}, nil } -func (ic *ContainerEngine) ContainerCommit(ctx context.Context, nameOrID string, options entities.CommitOptions) (*entities.CommitReport, error) { +func (ic *ContainerEngine) ContainerCommit(ctx context.Context, nameOrID string, opts entities.CommitOptions) (*entities.CommitReport, error) { var ( repo string tag = "latest" ) - if len(options.ImageName) > 0 { - ref, err := reference.Parse(options.ImageName) + if len(opts.ImageName) > 0 { + ref, err := reference.Parse(opts.ImageName) if err != nil { - return nil, errors.Wrapf(err, "error parsing reference %q", options.ImageName) + return nil, errors.Wrapf(err, "error parsing reference %q", opts.ImageName) } if t, ok := ref.(reference.Tagged); ok { tag = t.Tag() @@ -244,19 +249,12 @@ func (ic *ContainerEngine) ContainerCommit(ctx context.Context, nameOrID string, repo = r.Name() } if len(repo) < 1 { - return nil, errors.Errorf("invalid image name %q", options.ImageName) + return nil, errors.Errorf("invalid image name %q", opts.ImageName) } } - commitOpts := containers.CommitOptions{ - Author: &options.Author, - Changes: options.Changes, - Comment: &options.Message, - Format: &options.Format, - Pause: &options.Pause, - Repo: &repo, - Tag: &tag, - } - response, err := containers.Commit(ic.ClientCxt, nameOrID, commitOpts) + options := new(containers.CommitOptions).WithAuthor(opts.Author).WithChanges(opts.Changes).WithComment(opts.Message) + options.WithFormat(opts.Format).WithPause(opts.Pause).WithRepo(repo).WithTag(tag) + response, err := containers.Commit(ic.ClientCxt, nameOrID, options) if err != nil { return nil, err } @@ -274,16 +272,16 @@ func (ic *ContainerEngine) ContainerExport(ctx context.Context, nameOrID string, return err } } - return containers.Export(ic.ClientCxt, nameOrID, w) + return containers.Export(ic.ClientCxt, nameOrID, w, nil) } -func (ic *ContainerEngine) ContainerCheckpoint(ctx context.Context, namesOrIds []string, options entities.CheckpointOptions) ([]*entities.CheckpointReport, error) { +func (ic *ContainerEngine) ContainerCheckpoint(ctx context.Context, namesOrIds []string, opts entities.CheckpointOptions) ([]*entities.CheckpointReport, error) { var ( err error ctrs = []entities.ListContainer{} ) - if options.All { + if opts.All { allCtrs, err := getContainersByContext(ic.ClientCxt, true, false, []string{}) if err != nil { return nil, err @@ -302,8 +300,10 @@ func (ic *ContainerEngine) ContainerCheckpoint(ctx context.Context, namesOrIds [ } } reports := make([]*entities.CheckpointReport, 0, len(ctrs)) + options := new(containers.CheckpointOptions).WithExport(opts.Export).WithIgnoreRootfs(opts.IgnoreRootFS).WithKeep(opts.Keep) + options.WithLeaveRunning(opts.LeaveRunning).WithTCPEstablished(opts.TCPEstablished) for _, c := range ctrs { - report, err := containers.Checkpoint(ic.ClientCxt, c.ID, &options.Keep, &options.LeaveRunning, &options.TCPEstablished, &options.IgnoreRootFS, &options.Export) + report, err := containers.Checkpoint(ic.ClientCxt, c.ID, options) if err != nil { reports = append(reports, &entities.CheckpointReport{Id: c.ID, Err: err}) } @@ -312,12 +312,12 @@ func (ic *ContainerEngine) ContainerCheckpoint(ctx context.Context, namesOrIds [ return reports, nil } -func (ic *ContainerEngine) ContainerRestore(ctx context.Context, namesOrIds []string, options entities.RestoreOptions) ([]*entities.RestoreReport, error) { +func (ic *ContainerEngine) ContainerRestore(ctx context.Context, namesOrIds []string, opts entities.RestoreOptions) ([]*entities.RestoreReport, error) { var ( err error ctrs = []entities.ListContainer{} ) - if options.All { + if opts.All { allCtrs, err := getContainersByContext(ic.ClientCxt, true, false, []string{}) if err != nil { return nil, err @@ -336,8 +336,9 @@ func (ic *ContainerEngine) ContainerRestore(ctx context.Context, namesOrIds []st } } reports := make([]*entities.RestoreReport, 0, len(ctrs)) + options := new(containers.RestoreOptions) for _, c := range ctrs { - report, err := containers.Restore(ic.ClientCxt, c.ID, &options.Keep, &options.TCPEstablished, &options.IgnoreRootFS, &options.IgnoreStaticIP, &options.IgnoreStaticMAC, &options.Name, &options.Import) + report, err := containers.Restore(ic.ClientCxt, c.ID, options) if err != nil { reports = append(reports, &entities.RestoreReport{Id: c.ID, Err: err}) } @@ -347,7 +348,7 @@ func (ic *ContainerEngine) ContainerRestore(ctx context.Context, namesOrIds []st } func (ic *ContainerEngine) ContainerCreate(ctx context.Context, s *specgen.SpecGenerator) (*entities.ContainerCreateReport, error) { - response, err := containers.CreateWithSpec(ic.ClientCxt, s) + response, err := containers.CreateWithSpec(ic.ClientCxt, s, nil) if err != nil { return nil, err } @@ -357,27 +358,20 @@ func (ic *ContainerEngine) ContainerCreate(ctx context.Context, s *specgen.SpecG return &entities.ContainerCreateReport{Id: response.ID}, nil } -func (ic *ContainerEngine) ContainerLogs(_ context.Context, nameOrIDs []string, options entities.ContainerLogsOptions) error { - since := options.Since.Format(time.RFC3339) - tail := strconv.FormatInt(options.Tail, 10) - stdout := options.StdoutWriter != nil - stderr := options.StderrWriter != nil - opts := containers.LogOptions{ - Follow: &options.Follow, - Since: &since, - Stderr: &stderr, - Stdout: &stdout, - Tail: &tail, - Timestamps: &options.Timestamps, - Until: nil, - } +func (ic *ContainerEngine) ContainerLogs(_ context.Context, nameOrIDs []string, opts entities.ContainerLogsOptions) error { + since := opts.Since.Format(time.RFC3339) + tail := strconv.FormatInt(opts.Tail, 10) + stdout := opts.StdoutWriter != nil + stderr := opts.StderrWriter != nil + options := new(containers.LogOptions).WithFollow(opts.Follow).WithSince(since).WithStderr(stderr) + options.WithStdout(stdout).WithTail(tail) var err error stdoutCh := make(chan string) stderrCh := make(chan string) ctx, cancel := context.WithCancel(context.Background()) go func() { - err = containers.Logs(ic.ClientCxt, nameOrIDs[0], opts, stdoutCh, stderrCh) + err = containers.Logs(ic.ClientCxt, nameOrIDs[0], options, stdoutCh, stderrCh) cancel() }() @@ -386,18 +380,18 @@ func (ic *ContainerEngine) ContainerLogs(_ context.Context, nameOrIDs []string, case <-ctx.Done(): return err case line := <-stdoutCh: - if options.StdoutWriter != nil { - _, _ = io.WriteString(options.StdoutWriter, line+"\n") + if opts.StdoutWriter != nil { + _, _ = io.WriteString(opts.StdoutWriter, line+"\n") } case line := <-stderrCh: - if options.StderrWriter != nil { - _, _ = io.WriteString(options.StderrWriter, line+"\n") + if opts.StderrWriter != nil { + _, _ = io.WriteString(opts.StderrWriter, line+"\n") } } } } -func (ic *ContainerEngine) ContainerAttach(ctx context.Context, nameOrID string, options entities.AttachOptions) error { +func (ic *ContainerEngine) ContainerAttach(ctx context.Context, nameOrID string, opts entities.AttachOptions) error { ctrs, err := getContainersByContext(ic.ClientCxt, false, false, []string{nameOrID}) if err != nil { return err @@ -406,8 +400,8 @@ func (ic *ContainerEngine) ContainerAttach(ctx context.Context, nameOrID string, if ctr.State != define.ContainerStateRunning.String() { return errors.Errorf("you can only attach to running containers") } - - return containers.Attach(ic.ClientCxt, nameOrID, &options.DetachKeys, nil, bindings.PTrue, options.Stdin, options.Stdout, options.Stderr, nil) + options := new(containers.AttachOptions).WithStream(true).WithDetachKeys(opts.DetachKeys) + return containers.Attach(ic.ClientCxt, nameOrID, opts.Stdin, opts.Stdout, opts.Stderr, nil, options) } func makeExecConfig(options entities.ExecOptions) *handlers.ExecCreateConfig { @@ -439,12 +433,17 @@ func (ic *ContainerEngine) ContainerExec(ctx context.Context, nameOrID string, o if err != nil { return 125, err } - - if err := containers.ExecStartAndAttach(ic.ClientCxt, sessionID, &streams); err != nil { + startAndAttachOptions := new(containers.ExecStartAndAttachOptions) + startAndAttachOptions.WithOutputStream(streams.OutputStream).WithErrorStream(streams.ErrorStream) + if streams.InputStream != nil { + startAndAttachOptions.WithInputStream(*streams.InputStream) + } + startAndAttachOptions.WithAttachError(streams.AttachError).WithAttachOutput(streams.AttachOutput).WithAttachInput(streams.AttachInput) + if err := containers.ExecStartAndAttach(ic.ClientCxt, sessionID, startAndAttachOptions); err != nil { return 125, err } - inspectOut, err := containers.ExecInspect(ic.ClientCxt, sessionID) + inspectOut, err := containers.ExecInspect(ic.ClientCxt, sessionID, nil) if err != nil { return 125, err } @@ -460,7 +459,7 @@ func (ic *ContainerEngine) ContainerExecDetached(ctx context.Context, nameOrID s return "", err } - if err := containers.ExecStart(ic.ClientCxt, sessionID); err != nil { + if err := containers.ExecStart(ic.ClientCxt, sessionID, nil); err != nil { return "", err } @@ -470,15 +469,23 @@ func (ic *ContainerEngine) ContainerExecDetached(ctx context.Context, nameOrID s func startAndAttach(ic *ContainerEngine, name string, detachKeys *string, input, output, errput *os.File) error { //nolint attachErr := make(chan error) attachReady := make(chan bool) + options := new(containers.AttachOptions).WithStream(true) + if dk := detachKeys; dk != nil { + options.WithDetachKeys(*dk) + } go func() { - err := containers.Attach(ic.ClientCxt, name, detachKeys, bindings.PFalse, bindings.PTrue, input, output, errput, attachReady) + err := containers.Attach(ic.ClientCxt, name, input, output, errput, attachReady, options) attachErr <- err }() // Wait for the attach to actually happen before starting // the container. select { case <-attachReady: - if err := containers.Start(ic.ClientCxt, name, detachKeys); err != nil { + startOptions := new(containers.StartOptions) + if dk := detachKeys; dk != nil { + startOptions.WithDetachKeys(*dk) + } + if err := containers.Start(ic.ClientCxt, name, startOptions); err != nil { return err } case err := <-attachErr: @@ -495,6 +502,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri if err != nil { return nil, err } + removeOptions := new(containers.RemoveOptions).WithVolumes(true).WithForce(false) // There can only be one container if attach was used for i, ctr := range ctrs { name := ctr.ID @@ -527,14 +535,14 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri // Defer the removal, so we can return early if needed and // de-spaghetti the code. defer func() { - shouldRestart, err := containers.ShouldRestart(ic.ClientCxt, ctr.ID) + shouldRestart, err := containers.ShouldRestart(ic.ClientCxt, ctr.ID, nil) if err != nil { logrus.Errorf("Failed to check if %s should restart: %v", ctr.ID, err) return } if !shouldRestart { - if err := containers.Remove(ic.ClientCxt, ctr.ID, bindings.PFalse, bindings.PTrue); err != nil { + if err := containers.Remove(ic.ClientCxt, ctr.ID, removeOptions); err != nil { if errorhandling.Contains(err, define.ErrNoSuchCtr) || errorhandling.Contains(err, define.ErrCtrRemoved) { logrus.Warnf("Container %s does not exist: %v", ctr.ID, err) @@ -564,10 +572,12 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri } // Start the container if it's not running already. if !ctrRunning { - err = containers.Start(ic.ClientCxt, name, &options.DetachKeys) + + err = containers.Start(ic.ClientCxt, name, new(containers.StartOptions).WithDetachKeys(options.DetachKeys)) if err != nil { if ctr.AutoRemove { - if err := containers.Remove(ic.ClientCxt, ctr.ID, bindings.PFalse, bindings.PTrue); err != nil { + rmOptions := new(containers.RemoveOptions).WithForce(false).WithVolumes(true) + if err := containers.Remove(ic.ClientCxt, ctr.ID, rmOptions); err != nil { if errorhandling.Contains(err, define.ErrNoSuchCtr) || errorhandling.Contains(err, define.ErrCtrRemoved) { logrus.Warnf("Container %s does not exist: %v", ctr.ID, err) @@ -588,12 +598,14 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri return reports, nil } -func (ic *ContainerEngine) ContainerList(ctx context.Context, options entities.ContainerListOptions) ([]entities.ListContainer, error) { - return containers.List(ic.ClientCxt, options.Filters, &options.All, &options.Last, &options.Namespace, &options.Size, &options.Sync) +func (ic *ContainerEngine) ContainerList(ctx context.Context, opts entities.ContainerListOptions) ([]entities.ListContainer, error) { + options := new(containers.ListOptions).WithFilters(opts.Filters).WithAll(opts.All).WithLast(opts.Last) + options.WithNamespace(opts.Namespace).WithSize(opts.Size).WithSync(opts.Sync) + return containers.List(ic.ClientCxt, options) } func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.ContainerRunOptions) (*entities.ContainerRunReport, error) { - con, err := containers.CreateWithSpec(ic.ClientCxt, opts.Spec) + con, err := containers.CreateWithSpec(ic.ClientCxt, opts.Spec, nil) if err != nil { return nil, err } @@ -625,7 +637,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta report.ExitCode = define.ExitCode(err) if opts.Rm { - if rmErr := containers.Remove(ic.ClientCxt, con.ID, bindings.PFalse, bindings.PTrue); rmErr != nil { + if rmErr := containers.Remove(ic.ClientCxt, con.ID, new(containers.RemoveOptions).WithForce(false).WithVolumes(true)); rmErr != nil { logrus.Debugf("unable to remove container %s after failing to start and attach to it", con.ID) } } @@ -636,14 +648,14 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta // Defer the removal, so we can return early if needed and // de-spaghetti the code. defer func() { - shouldRestart, err := containers.ShouldRestart(ic.ClientCxt, con.ID) + shouldRestart, err := containers.ShouldRestart(ic.ClientCxt, con.ID, nil) if err != nil { logrus.Errorf("Failed to check if %s should restart: %v", con.ID, err) return } if !shouldRestart { - if err := containers.Remove(ic.ClientCxt, con.ID, bindings.PFalse, bindings.PTrue); err != nil { + if err := containers.Remove(ic.ClientCxt, con.ID, new(containers.RemoveOptions).WithForce(false).WithVolumes(true)); err != nil { if errorhandling.Contains(err, define.ErrNoSuchCtr) || errorhandling.Contains(err, define.ErrCtrRemoved) { logrus.Warnf("Container %s does not exist: %v", con.ID, err) @@ -705,7 +717,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta } func (ic *ContainerEngine) ContainerDiff(ctx context.Context, nameOrID string, _ entities.DiffOptions) (*entities.DiffReport, error) { - changes, err := containers.Diff(ic.ClientCxt, nameOrID) + changes, err := containers.Diff(ic.ClientCxt, nameOrID, nil) return &entities.DiffReport{Changes: changes}, err } @@ -720,7 +732,7 @@ func (ic *ContainerEngine) ContainerInit(ctx context.Context, namesOrIds []strin } reports := make([]*entities.ContainerInitReport, 0, len(ctrs)) for _, ctr := range ctrs { - err := containers.ContainerInit(ic.ClientCxt, ctr.ID) + err := containers.ContainerInit(ic.ClientCxt, ctr.ID, nil) // When using all, it is NOT considered an error if a container // has already been init'd. if err != nil && options.All && strings.Contains(errors.Cause(err).Error(), define.ErrCtrStateInvalid.Error()) { @@ -772,9 +784,16 @@ func (ic *ContainerEngine) ContainerPort(ctx context.Context, nameOrID string, o return reports, nil } -func (ic *ContainerEngine) ContainerCp(ctx context.Context, source, dest string, options entities.ContainerCpOptions) error { - return nil - // return containers.Copy(ic.ClientCxt, source, dest, options) +func (ic *ContainerEngine) ContainerCopyFromArchive(ctx context.Context, nameOrID string, path string, reader io.Reader) (entities.ContainerCopyFunc, error) { + return containers.CopyFromArchive(ic.ClientCxt, nameOrID, path, reader) +} + +func (ic *ContainerEngine) ContainerCopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (entities.ContainerCopyFunc, error) { + return containers.CopyToArchive(ic.ClientCxt, nameOrID, path, writer) +} + +func (ic *ContainerEngine) ContainerStat(ctx context.Context, nameOrID string, path string) (*entities.ContainerStatReport, error) { + return containers.Stat(ic.ClientCxt, nameOrID, path) } // Shutdown Libpod engine @@ -785,10 +804,10 @@ func (ic *ContainerEngine) ContainerStats(ctx context.Context, namesOrIds []stri if options.Latest { return nil, errors.New("latest is not supported for the remote client") } - return containers.Stats(ic.ClientCxt, namesOrIds, &options.Stream) + return containers.Stats(ic.ClientCxt, namesOrIds, new(containers.StatsOptions).WithStream(options.Stream)) } // ShouldRestart reports back whether the containre will restart func (ic *ContainerEngine) ShouldRestart(_ context.Context, id string) (bool, error) { - return containers.ShouldRestart(ic.ClientCxt, id) + return containers.ShouldRestart(ic.ClientCxt, id, nil) } diff --git a/pkg/domain/infra/tunnel/healthcheck.go b/pkg/domain/infra/tunnel/healthcheck.go index ac28712ce..b3ff888d8 100644 --- a/pkg/domain/infra/tunnel/healthcheck.go +++ b/pkg/domain/infra/tunnel/healthcheck.go @@ -9,5 +9,5 @@ import ( ) func (ic *ContainerEngine) HealthCheckRun(ctx context.Context, nameOrID string, options entities.HealthCheckOptions) (*define.HealthCheckResults, error) { - return containers.RunHealthCheck(ic.ClientCxt, nameOrID) + return containers.RunHealthCheck(ic.ClientCxt, nameOrID, nil) } diff --git a/pkg/domain/infra/tunnel/helpers.go b/pkg/domain/infra/tunnel/helpers.go index 63f9546be..0a806d860 100644 --- a/pkg/domain/infra/tunnel/helpers.go +++ b/pkg/domain/infra/tunnel/helpers.go @@ -4,7 +4,6 @@ import ( "context" "github.com/containers/podman/v2/libpod/define" - "github.com/containers/podman/v2/pkg/bindings" "github.com/containers/podman/v2/pkg/bindings/containers" "github.com/containers/podman/v2/pkg/bindings/pods" "github.com/containers/podman/v2/pkg/domain/entities" @@ -18,8 +17,8 @@ func getContainersByContext(contextWithConnection context.Context, all, ignore b if all && len(namesOrIDs) > 0 { return nil, errors.New("cannot lookup containers and all") } - - allContainers, err := containers.List(contextWithConnection, nil, bindings.PTrue, nil, nil, nil, bindings.PTrue) + options := new(containers.ListOptions).WithAll(true).WithSync(true) + allContainers, err := containers.List(contextWithConnection, options) if err != nil { return nil, err } @@ -38,7 +37,7 @@ func getContainersByContext(contextWithConnection context.Context, all, ignore b // First determine if the container exists by doing an inspect. // Inspect takes supports names and IDs and let's us determine // a containers full ID. - inspectData, err := containers.Inspect(contextWithConnection, nameOrID, bindings.PFalse) + inspectData, err := containers.Inspect(contextWithConnection, nameOrID, new(containers.InspectOptions).WithSize(false)) if err != nil { if ignore && errorhandling.Contains(err, define.ErrNoSuchCtr) { continue @@ -90,7 +89,7 @@ func getPodsByContext(contextWithConnection context.Context, all bool, namesOrID // First determine if the pod exists by doing an inspect. // Inspect takes supports names and IDs and let's us determine // a containers full ID. - inspectData, err := pods.Inspect(contextWithConnection, nameOrID) + inspectData, err := pods.Inspect(contextWithConnection, nameOrID, nil) if err != nil { if errorhandling.Contains(err, define.ErrNoSuchPod) { return nil, errors.Wrapf(define.ErrNoSuchPod, "unable to find pod %q", nameOrID) diff --git a/pkg/domain/infra/tunnel/pods.go b/pkg/domain/infra/tunnel/pods.go index ee4978787..1ceff9ca7 100644 --- a/pkg/domain/infra/tunnel/pods.go +++ b/pkg/domain/infra/tunnel/pods.go @@ -16,19 +16,20 @@ func (ic *ContainerEngine) PodExists(ctx context.Context, nameOrID string) (*ent return &entities.BoolReport{Value: exists}, err } -func (ic *ContainerEngine) PodKill(ctx context.Context, namesOrIds []string, options entities.PodKillOptions) ([]*entities.PodKillReport, error) { - _, err := util.ParseSignal(options.Signal) +func (ic *ContainerEngine) PodKill(ctx context.Context, namesOrIds []string, opts entities.PodKillOptions) ([]*entities.PodKillReport, error) { + _, err := util.ParseSignal(opts.Signal) if err != nil { return nil, err } - foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) + foundPods, err := getPodsByContext(ic.ClientCxt, opts.All, namesOrIds) if err != nil { return nil, err } reports := make([]*entities.PodKillReport, 0, len(foundPods)) + options := new(pods.KillOptions).WithSignal(opts.Signal) for _, p := range foundPods { - response, err := pods.Kill(ic.ClientCxt, p.Id, &options.Signal) + response, err := pods.Kill(ic.ClientCxt, p.Id, options) if err != nil { report := entities.PodKillReport{ Errs: []error{err}, @@ -49,7 +50,7 @@ func (ic *ContainerEngine) PodPause(ctx context.Context, namesOrIds []string, op } reports := make([]*entities.PodPauseReport, 0, len(foundPods)) for _, p := range foundPods { - response, err := pods.Pause(ic.ClientCxt, p.Id) + response, err := pods.Pause(ic.ClientCxt, p.Id, nil) if err != nil { report := entities.PodPauseReport{ Errs: []error{err}, @@ -70,7 +71,7 @@ func (ic *ContainerEngine) PodUnpause(ctx context.Context, namesOrIds []string, } reports := make([]*entities.PodUnpauseReport, 0, len(foundPods)) for _, p := range foundPods { - response, err := pods.Unpause(ic.ClientCxt, p.Id) + response, err := pods.Unpause(ic.ClientCxt, p.Id, nil) if err != nil { report := entities.PodUnpauseReport{ Errs: []error{err}, @@ -84,18 +85,19 @@ func (ic *ContainerEngine) PodUnpause(ctx context.Context, namesOrIds []string, return reports, nil } -func (ic *ContainerEngine) PodStop(ctx context.Context, namesOrIds []string, options entities.PodStopOptions) ([]*entities.PodStopReport, error) { +func (ic *ContainerEngine) PodStop(ctx context.Context, namesOrIds []string, opts entities.PodStopOptions) ([]*entities.PodStopReport, error) { timeout := -1 - foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) - if err != nil && !(options.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { + foundPods, err := getPodsByContext(ic.ClientCxt, opts.All, namesOrIds) + if err != nil && !(opts.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { return nil, err } - if options.Timeout != -1 { - timeout = options.Timeout + if opts.Timeout != -1 { + timeout = opts.Timeout } reports := make([]*entities.PodStopReport, 0, len(foundPods)) + options := new(pods.StopOptions).WithTimeout(timeout) for _, p := range foundPods { - response, err := pods.Stop(ic.ClientCxt, p.Id, &timeout) + response, err := pods.Stop(ic.ClientCxt, p.Id, options) if err != nil { report := entities.PodStopReport{ Errs: []error{err}, @@ -116,7 +118,7 @@ func (ic *ContainerEngine) PodRestart(ctx context.Context, namesOrIds []string, } reports := make([]*entities.PodRestartReport, 0, len(foundPods)) for _, p := range foundPods { - response, err := pods.Restart(ic.ClientCxt, p.Id) + response, err := pods.Restart(ic.ClientCxt, p.Id, nil) if err != nil { report := entities.PodRestartReport{ Errs: []error{err}, @@ -137,7 +139,7 @@ func (ic *ContainerEngine) PodStart(ctx context.Context, namesOrIds []string, op } reports := make([]*entities.PodStartReport, 0, len(foundPods)) for _, p := range foundPods { - response, err := pods.Start(ic.ClientCxt, p.Id) + response, err := pods.Start(ic.ClientCxt, p.Id, nil) if err != nil { report := entities.PodStartReport{ Errs: []error{err}, @@ -151,14 +153,15 @@ func (ic *ContainerEngine) PodStart(ctx context.Context, namesOrIds []string, op return reports, nil } -func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, options entities.PodRmOptions) ([]*entities.PodRmReport, error) { - foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) - if err != nil && !(options.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { +func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, opts entities.PodRmOptions) ([]*entities.PodRmReport, error) { + foundPods, err := getPodsByContext(ic.ClientCxt, opts.All, namesOrIds) + if err != nil && !(opts.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { return nil, err } reports := make([]*entities.PodRmReport, 0, len(foundPods)) + options := new(pods.RemoveOptions).WithForce(opts.Force) for _, p := range foundPods { - response, err := pods.Remove(ic.ClientCxt, p.Id, &options.Force) + response, err := pods.Remove(ic.ClientCxt, p.Id, options) if err != nil { report := entities.PodRmReport{ Err: err, @@ -173,32 +176,33 @@ func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, optio } func (ic *ContainerEngine) PodPrune(ctx context.Context, opts entities.PodPruneOptions) ([]*entities.PodPruneReport, error) { - return pods.Prune(ic.ClientCxt) + return pods.Prune(ic.ClientCxt, nil) } func (ic *ContainerEngine) PodCreate(ctx context.Context, opts entities.PodCreateOptions) (*entities.PodCreateReport, error) { podSpec := specgen.NewPodSpecGenerator() opts.ToPodSpecGen(podSpec) - return pods.CreatePodFromSpec(ic.ClientCxt, podSpec) + return pods.CreatePodFromSpec(ic.ClientCxt, podSpec, nil) } -func (ic *ContainerEngine) PodTop(ctx context.Context, options entities.PodTopOptions) (*entities.StringSliceReport, error) { +func (ic *ContainerEngine) PodTop(ctx context.Context, opts entities.PodTopOptions) (*entities.StringSliceReport, error) { switch { - case options.Latest: + case opts.Latest: return nil, errors.New("latest is not supported") - case options.NameOrID == "": + case opts.NameOrID == "": return nil, errors.New("NameOrID must be specified") } - - topOutput, err := pods.Top(ic.ClientCxt, options.NameOrID, options.Descriptors) + options := new(pods.TopOptions).WithDescriptors(opts.Descriptors) + topOutput, err := pods.Top(ic.ClientCxt, opts.NameOrID, options) if err != nil { return nil, err } return &entities.StringSliceReport{Value: topOutput}, nil } -func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOptions) ([]*entities.ListPodsReport, error) { - return pods.List(ic.ClientCxt, options.Filters) +func (ic *ContainerEngine) PodPs(ctx context.Context, opts entities.PodPSOptions) ([]*entities.ListPodsReport, error) { + options := new(pods.ListOptions).WithFilters(opts.Filters) + return pods.List(ic.ClientCxt, options) } func (ic *ContainerEngine) PodInspect(ctx context.Context, options entities.PodInspectOptions) (*entities.PodInspectReport, error) { @@ -208,9 +212,10 @@ func (ic *ContainerEngine) PodInspect(ctx context.Context, options entities.PodI case options.NameOrID == "": return nil, errors.New("NameOrID must be specified") } - return pods.Inspect(ic.ClientCxt, options.NameOrID) + return pods.Inspect(ic.ClientCxt, options.NameOrID, nil) } -func (ic *ContainerEngine) PodStats(ctx context.Context, namesOrIds []string, options entities.PodStatsOptions) ([]*entities.PodStatsReport, error) { +func (ic *ContainerEngine) PodStats(ctx context.Context, namesOrIds []string, opts entities.PodStatsOptions) ([]*entities.PodStatsReport, error) { + options := new(pods.StatsOptions).WithAll(opts.All) return pods.Stats(ic.ClientCxt, namesOrIds, options) } diff --git a/pkg/domain/infra/tunnel/system.go b/pkg/domain/infra/tunnel/system.go index 1e4b2e0b4..9013d44c2 100644 --- a/pkg/domain/infra/tunnel/system.go +++ b/pkg/domain/infra/tunnel/system.go @@ -20,7 +20,7 @@ func (ic *ContainerEngine) SetupRootless(_ context.Context, cmd *cobra.Command) // SystemPrune prunes unused data from the system. func (ic *ContainerEngine) SystemPrune(ctx context.Context, opts entities.SystemPruneOptions) (*entities.SystemPruneReport, error) { - options := new(system.PruneOptions).WithAll(opts.All).WithVolumes(opts.Volume).WithFilters(opts.ContainerPruneOptions.Filters) + options := new(system.PruneOptions).WithAll(opts.All).WithVolumes(opts.Volume).WithFilters(opts.Filters) return system.Prune(ic.ClientCxt, options) } |