Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add restart-policy to container filters & --filter to podman start #9689

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cmd/podman/containers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"fmt"
"os"
"strings"

"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/registry"
Expand Down Expand Up @@ -42,7 +43,9 @@ var (
)

var (
startOptions entities.ContainerStartOptions
startOptions = entities.ContainerStartOptions{
Filters: make(map[string][]string),
}
)

func startFlags(cmd *cobra.Command) {
Expand All @@ -56,6 +59,8 @@ func startFlags(cmd *cobra.Command) {

flags.BoolVarP(&startOptions.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
flags.BoolVar(&startOptions.SigProxy, "sig-proxy", false, "Proxy received signals to the process (default true if attaching, false otherwise)")
flags.StringSliceVarP(&filters, "filter", "f", []string{}, "Filter output based on conditions given")
boaz0 marked this conversation as resolved.
Show resolved Hide resolved
_ = cmd.RegisterFlagCompletionFunc("filter", common.AutocompletePsFilters)

flags.BoolVar(&startOptions.All, "all", false, "Start all containers regardless of their state or configuration")

Expand Down Expand Up @@ -116,7 +121,18 @@ func start(cmd *cobra.Command, args []string) error {
startOptions.Stdout = os.Stdout
}

responses, err := registry.ContainerEngine().ContainerStart(registry.GetContext(), args, startOptions)
var containers []string = args
if len(filters) > 0 {
for _, f := range filters {
split := strings.SplitN(f, "=", 2)
if len(split) == 1 {
return errors.Errorf("invalid filter %q", f)
}
startOptions.Filters[split[0]] = append(startOptions.Filters[split[0]], split[1])
}
}

responses, err := registry.ContainerEngine().ContainerStart(registry.GetContext(), containers, startOptions)
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions docs/source/markdown/podman-start.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and

Start all the containers created by Podman, default is only running containers.

#### **\-\-filter**, **-f**

Filter what containers are going to be started from the given arguments.
Multiple filters can be given with multiple uses of the --filter flag.
Filters with the same key work inclusive with the only exception being
`label` which is exclusive. Filters with different keys always work exclusive.

Valid filters are listed below:

| **Filter** | **Description** |
| --------------- | -------------------------------------------------------------------------------- |
| id | [ID] Container's ID (accepts regex) |
| name | [Name] Container's name (accepts regex) |
| label | [Key] or [Key=Value] Label assigned to a container |
| exited | [Int] Container's exit code |
| status | [Status] Container's status: 'created', 'exited', 'paused', 'running', 'unknown' |
| ancestor | [ImageName] Image or descendant used to create container |
| before | [ID] or [Name] Containers created before this container |
| since | [ID] or [Name] Containers created since this container |
| volume | [VolumeName] or [MountpointDestination] Volume mounted in container |
| health | [Status] healthy or unhealthy |
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |


## EXAMPLE

podman start mywebserver
Expand Down
9 changes: 9 additions & 0 deletions libpod/define/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ const (
// handling of system restart, which Podman does not yet support.
RestartPolicyUnlessStopped = "unless-stopped"
)

// RestartPolicyMap maps between restart-policy valid values to restart policy types
var RestartPolicyMap = map[string]string{
"none": RestartPolicyNone,
RestartPolicyNo: RestartPolicyNo,
RestartPolicyAlways: RestartPolicyAlways,
RestartPolicyOnFailure: RestartPolicyOnFailure,
RestartPolicyUnlessStopped: RestartPolicyUnlessStopped,
}
1 change: 1 addition & 0 deletions pkg/domain/entities/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ type ContainerExistsOptions struct {
// ContainerStartOptions describes the val from the
// CLI needed to start a container
type ContainerStartOptions struct {
Filters map[string][]string
All bool
Attach bool
DetachKeys string
Expand Down
27 changes: 27 additions & 0 deletions pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filters

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -226,6 +227,32 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
}
return false
}, nil
case "restart-policy":
invalidPolicyNames := []string{}
for _, policy := range filterValues {
if _, ok := define.RestartPolicyMap[policy]; !ok {
invalidPolicyNames = append(invalidPolicyNames, policy)
}
}
var filterValueError error = nil
if len(invalidPolicyNames) > 0 {
errPrefix := "invalid restart policy"
if len(invalidPolicyNames) > 1 {
errPrefix = "invalid restart policies"
}
filterValueError = fmt.Errorf("%s %s", strings.Join(invalidPolicyNames, ", "), errPrefix)
}
return func(c *libpod.Container) bool {
for _, policy := range filterValues {
if policy == "none" && c.RestartPolicy() == define.RestartPolicyNone {
return true
}
if c.RestartPolicy() == policy {
return true
}
}
return false
}, filterValueError
}
return nil, errors.Errorf("%s is an invalid filter", filter)
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,33 @@ func (ic *ContainerEngine) ContainerExecDetached(ctx context.Context, nameOrID s
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
reports := []*entities.ContainerStartReport{}
var exitCode = define.ExecErrorCodeGeneric
ctrs, rawInputs, err := getContainersAndInputByContext(options.All, options.Latest, namesOrIds, ic.Libpod)
containersNamesOrIds := namesOrIds
if len(options.Filters) > 0 {
filterFuncs := make([]libpod.ContainerFilter, 0, len(options.Filters))
if len(options.Filters) > 0 {
for k, v := range options.Filters {
generatedFunc, err := dfilters.GenerateContainerFilterFuncs(k, v, ic.Libpod)
if err != nil {
return nil, err
}
filterFuncs = append(filterFuncs, generatedFunc)
}
}
candidates, err := ic.Libpod.GetContainers(filterFuncs...)
if err != nil {
return nil, err
}
containersNamesOrIds = []string{}
for _, candidate := range candidates {
for _, nameOrID := range namesOrIds {
if nameOrID == candidate.ID() || nameOrID == candidate.Name() {
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
}
}
}
}

ctrs, rawInputs, err := getContainersAndInputByContext(options.All, options.Latest, containersNamesOrIds, ic.Libpod)
if err != nil {
return nil, err
}
Expand Down
25 changes: 24 additions & 1 deletion pkg/domain/infra/tunnel/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,30 @@ func startAndAttach(ic *ContainerEngine, name string, detachKeys *string, input,
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
reports := []*entities.ContainerStartReport{}
var exitCode = define.ExecErrorCodeGeneric
ctrs, err := getContainersByContext(ic.ClientCtx, options.All, false, namesOrIds)
containersNamesOrIds := namesOrIds
if len(options.Filters) > 0 {
containersNamesOrIds = []string{}
opts := new(containers.ListOptions).WithFilters(options.Filters).WithAll(true)
candidates, listErr := containers.List(ic.ClientCtx, opts)
if listErr != nil {
return nil, listErr
}
for _, candidate := range candidates {
for _, nameOrID := range namesOrIds {
if nameOrID == candidate.ID {
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
continue
}
for _, containerName := range candidate.Names {
if containerName == nameOrID {
containersNamesOrIds = append(containersNamesOrIds, nameOrID)
continue
}
}
}
}
}
ctrs, err := getContainersByContext(ic.ClientCtx, options.All, false, containersNamesOrIds)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions test/system/045-start.bats
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,21 @@ load helpers
fi
}

@test "podman start --filter - start only containers that match the filter" {
run_podman run -d $IMAGE /bin/true
cid="$output"
run_podman start --filter restart-policy=always $cid "CID of restart-policy=always container"
is "$output" ""

run_podman start --filter restart-policy=none $cid "CID of restart-policy=none container"
is "$output" "$cid"
}

@test "podman start --filter invalid-restart-policy - return error" {
run_podman run -d $IMAGE /bin/true
cid="$output"
run_podman 125 start --filter restart-policy=fakepolicy $cid "CID of restart-policy=<not-exists> container"
is "$output" "Error: fakepolicy invalid restart policy"
}

# vim: filetype=sh