Skip to content

Commit

Permalink
Merge pull request #5605 from nalind/pull-policy-parsing
Browse files Browse the repository at this point in the history
Rework parsing of --pull flags
  • Loading branch information
openshift-merge-bot[bot] committed Jun 24, 2024
2 parents 9086bc0 + 21fb5ea commit 7a950c5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
3 changes: 3 additions & 0 deletions define/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

// PullPolicy takes the value PullIfMissing, PullAlways, PullIfNewer, or PullNever.
// N.B.: the enumeration values for this type differ from those used by
// github.com/containers/common/pkg/config.PullPolicy (their zero values
// indicate different policies), so they are not interchangeable.
type PullPolicy int

const (
Expand Down
63 changes: 46 additions & 17 deletions pkg/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,42 @@ func SystemContextFromFlagSet(flags *pflag.FlagSet, findFlagFunc func(name strin
return ctx, nil
}

// pullPolicyWithFlags parses a string value of a pull policy, evaluating it in
// combination with "always" and "never" boolean flags.
// Allow for:
// * --pull
// * --pull=""
// * --pull=true
// * --pull=false
// * --pull=never
// * --pull=always
// * --pull=ifmissing
// * --pull=missing
// * --pull=notpresent
// * --pull=newer
// * --pull=ifnewer
// and --pull-always and --pull-never as boolean flags.
func pullPolicyWithFlags(policySpec string, always, never bool) (define.PullPolicy, error) {
if always {
return define.PullAlways, nil
}
if never {
return define.PullNever, nil
}
policy := strings.ToLower(policySpec)
switch policy {
case "true", "missing", "ifmissing", "notpresent":
return define.PullIfMissing, nil
case "always":
return define.PullAlways, nil
case "false", "never":
return define.PullNever, nil
case "ifnewer", "newer":
return define.PullIfNewer, nil
}
return 0, fmt.Errorf("unrecognized pull policy %q", policySpec)
}

// PullPolicyFromOptions returns a PullPolicy that reflects the combination of
// the specified "pull" and undocumented "pull-always" and "pull-never" flags.
func PullPolicyFromOptions(c *cobra.Command) (define.PullPolicy, error) {
Expand All @@ -474,30 +510,23 @@ func PullPolicyFromFlagSet(flags *pflag.FlagSet, findFlagFunc func(name string)
return 0, errors.New("can only set one of 'pull' or 'pull-always' or 'pull-never'")
}

// Allow for --pull, --pull=true, --pull=false, --pull=never, --pull=always
// --pull-always and --pull-never. The --pull-never and --pull-always options
// will not be documented.
pullPolicy := define.PullIfMissing
pullFlagValue := findFlagFunc("pull").Value.String()
if strings.EqualFold(pullFlagValue, "true") || strings.EqualFold(pullFlagValue, "ifnewer") {
pullPolicy = define.PullIfNewer
}
// The --pull-never and --pull-always options will not be documented.
pullAlwaysFlagValue, err := flags.GetBool("pull-always")
if err != nil {
return 0, err
}
if pullAlwaysFlagValue || strings.EqualFold(pullFlagValue, "always") {
pullPolicy = define.PullAlways
return 0, fmt.Errorf("checking the --pull-always flag value: %w", err)
}
pullNeverFlagValue, err := flags.GetBool("pull-never")
if err != nil {
return 0, err
return 0, fmt.Errorf("checking the --pull-never flag value: %w", err)
}
if pullNeverFlagValue ||
strings.EqualFold(pullFlagValue, "never") ||
strings.EqualFold(pullFlagValue, "false") {
pullPolicy = define.PullNever

// The --pull[=...] flag is the one we really care about.
pullFlagValue := findFlagFunc("pull").Value.String()
pullPolicy, err := pullPolicyWithFlags(pullFlagValue, pullAlwaysFlagValue, pullNeverFlagValue)
if err != nil {
return 0, err
}

logrus.Debugf("Pull Policy for pull [%v]", pullPolicy)

return pullPolicy, nil
Expand Down
27 changes: 27 additions & 0 deletions pkg/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCommonBuildOptionsFromFlagSet(t *testing.T) {
Expand Down Expand Up @@ -193,6 +194,32 @@ func TestParsePlatform(t *testing.T) {
assert.Error(t, err)
}

func TestParsePullPolicy(t *testing.T) {
testCases := map[string]bool{
"missing": true,
"ifmissing": true,
"notpresent": true,
"always": true,
"true": true,
"ifnewer": true,
"newer": true,
"false": true,
"never": true,
"trye": false,
"truth": false,
}
for value, result := range testCases {
t.Run(value, func(t *testing.T) {
policy, err := pullPolicyWithFlags(value, false, false)
if result {
require.NoErrorf(t, err, "expected value %q to be recognized", value)
} else {
require.Errorf(t, err, "did not expect value %q to be recognized as %q", value, policy.String())
}
})
}
}

func TestSplitStringWithColonEscape(t *testing.T) {
tests := []struct {
volume string
Expand Down

1 comment on commit 7a950c5

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.