Skip to content

Commit

Permalink
Merge pull request #3704 from abergmeier/nso
Browse files Browse the repository at this point in the history
Allow processing of network options from FlagSet
  • Loading branch information
openshift-merge-robot committed Jan 12, 2022
2 parents 0969cff + ba3bd6c commit 031e72c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/parse/parse.go
Expand Up @@ -803,11 +803,16 @@ func parseIDMap(spec []string) (m [][3]uint32, err error) {

// NamespaceOptions parses the build options for all namespaces except for user namespace.
func NamespaceOptions(c *cobra.Command) (namespaceOptions define.NamespaceOptions, networkPolicy define.NetworkConfigurationPolicy, err error) {
return NamespaceOptionsFromFlagSet(c.Flags(), c.Flag)
}

// NamespaceOptionsFromFlagSet parses the build options for all namespaces except for user namespace.
func NamespaceOptionsFromFlagSet(flags *pflag.FlagSet, findFlagFunc func(name string) *pflag.Flag) (namespaceOptions define.NamespaceOptions, networkPolicy define.NetworkConfigurationPolicy, err error) {
options := make(define.NamespaceOptions, 0, 7)
policy := define.NetworkDefault
for _, what := range []string{"cgroupns", string(specs.IPCNamespace), "network", string(specs.PIDNamespace), string(specs.UTSNamespace)} {
if c.Flags().Lookup(what) != nil && c.Flag(what).Changed {
how := c.Flag(what).Value.String()
if flags.Lookup(what) != nil && findFlagFunc(what).Changed {
how := findFlagFunc(what).Value.String()
switch what {
case "network":
what = string(specs.NetworkNamespace)
Expand Down
16 changes: 16 additions & 0 deletions pkg/parse/parse_test.go
Expand Up @@ -5,6 +5,8 @@ import (
"runtime"
"testing"

"github.com/containers/buildah/define"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -138,6 +140,20 @@ func TestIsolation(t *testing.T) {
}
}

func TestNamespaceOptions(t *testing.T) {
fs := pflag.NewFlagSet("testme", pflag.PanicOnError)
fs.String("cgroupns", "", "")
err := fs.Parse([]string{"--cgroupns", "private"})
assert.NoError(t, err)
nsos, np, err := NamespaceOptionsFromFlagSet(fs, fs.Lookup)
assert.NoError(t, err)
assert.Equal(t, np, define.NetworkEnabled)
nso := nsos.Find(string(specs.CgroupNamespace))
assert.Equal(t, *nso, define.NamespaceOption{
Name: string(specs.CgroupNamespace),
})
}

func TestParsePlatform(t *testing.T) {
os, arch, variant, err := Platform("a/b/c")
assert.NoError(t, err)
Expand Down

0 comments on commit 031e72c

Please sign in to comment.