Skip to content

Commit ec0f322

Browse files
committed
feature: support dns related flags when creating container through pouch cli
Signed-off-by: mathspanda <[email protected]>
1 parent 4f6e625 commit ec0f322

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

cli/common_flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
3232
// device related options
3333
flagSet.StringSliceVarP(&c.devices, "device", "", nil, "Add a host device to the container")
3434

35+
// dns
36+
flagSet.StringArrayVar(&c.dns, "dns", nil, "Set DNS servers")
37+
flagSet.StringSliceVar(&c.dnsOptions, "dns-option", nil, "Set DNS options")
38+
flagSet.StringArrayVar(&c.dnsSearch, "dns-search", nil, "Set DNS search domains")
39+
3540
flagSet.BoolVar(&c.enableLxcfs, "enableLxcfs", false, "Enable lxcfs for the container, only effective when enable-lxcfs switched on in Pouchd")
3641
flagSet.StringVar(&c.entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
3742
flagSet.StringArrayVarP(&c.env, "env", "e", nil, "Set environment variables for container")

cli/container.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/alibaba/pouch/apis/opts/config"
88
"github.com/alibaba/pouch/apis/types"
99

10-
strfmt "github.com/go-openapi/strfmt"
10+
"github.com/go-openapi/strfmt"
1111
)
1212

1313
type container struct {
@@ -49,6 +49,10 @@ type container struct {
4949
scheLatSwitch int64
5050
oomKillDisable bool
5151

52+
dns []string
53+
dnsOptions []string
54+
dnsSearch []string
55+
5256
devices []string
5357
enableLxcfs bool
5458
privileged bool
@@ -233,6 +237,9 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
233237
Ulimits: c.ulimit.Value(),
234238
PidsLimit: c.pidsLimit,
235239
},
240+
DNS: c.dns,
241+
DNSOptions: c.dnsOptions,
242+
DNSSearch: c.dnsSearch,
236243
EnableLxcfs: c.enableLxcfs,
237244
Privileged: c.privileged,
238245
RestartPolicy: restartPolicy,

test/cli_run_dns_test.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (suite *PouchRunDNSSuite) TearDownTest(c *check.C) {
3131
}
3232

3333
// TestRunWithUserDefinedNetwork tests enabling libnetwork resolver if user-defined network.
34-
func (suite *PouchRunSuite) TestRunWithUserDefinedNetwork(c *check.C) {
34+
func (suite *PouchRunDNSSuite) TestRunWithUserDefinedNetwork(c *check.C) {
3535
cname := "TestRunWithUserDefinedNetwork"
3636

3737
// Create a user-defined network
@@ -52,7 +52,7 @@ func (suite *PouchRunSuite) TestRunWithUserDefinedNetwork(c *check.C) {
5252
}
5353

5454
// TestRunWithBridgeNetwork tests disabling libnetwork resolver if not user-defined network.
55-
func (suite *PouchRunSuite) TestRunWithBridgeNetwork(c *check.C) {
55+
func (suite *PouchRunDNSSuite) TestRunWithBridgeNetwork(c *check.C) {
5656
cname := "TestRunWithBridgeNetwork"
5757

5858
// Use bridge network if not set --net.
@@ -66,3 +66,72 @@ func (suite *PouchRunSuite) TestRunWithBridgeNetwork(c *check.C) {
6666

6767
c.Assert(res.Stdout(), check.Equals, hostRes.Stdout())
6868
}
69+
70+
// TestRunWithDNSFlags tests DNS related flags.
71+
func (suite *PouchRunDNSSuite) TestRunWithDNSFlags(c *check.C) {
72+
cname := "TestRunWithDNSFlags"
73+
74+
res := command.PouchRun("run", "--name", cname,
75+
"--dns", "1.2.3.4",
76+
"--dns-option", "timeout:3",
77+
"--dns-search", "example.com",
78+
busyboxImage,
79+
"cat", "/etc/resolv.conf")
80+
defer DelContainerForceMultyTime(c, cname)
81+
res.Assert(c, icmd.Success)
82+
83+
// test if the value is correct in container
84+
out := strings.Trim(res.Stdout(), "\n")
85+
c.Assert(strings.Contains(out, "nameserver 1.2.3.4"), check.Equals, true)
86+
c.Assert(strings.Contains(out, "options timeout:3"), check.Equals, true)
87+
c.Assert(strings.Contains(out, "search example.com"), check.Equals, true)
88+
89+
// test if the value is in inspect result
90+
dns, err := inspectFilter(cname, ".HostConfig.DNS")
91+
c.Assert(err, check.IsNil)
92+
c.Assert(dns, check.Equals, "[1.2.3.4]")
93+
94+
dnsOptions, err := inspectFilter(cname, ".HostConfig.DNSOptions")
95+
c.Assert(err, check.IsNil)
96+
c.Assert(dnsOptions, check.Equals, "[timeout:3]")
97+
98+
dnsSearch, err := inspectFilter(cname, ".HostConfig.DNSSearch")
99+
c.Assert(err, check.IsNil)
100+
c.Assert(dnsSearch, check.Equals, "[example.com]")
101+
}
102+
103+
// TestRunWithDNSRepeatFlags tests repeated DNS related flags.
104+
func (suite *PouchRunDNSSuite) TestRunWithDNSRepeatFlags(c *check.C) {
105+
cname := "TestRunWithDNSRepeatFlags"
106+
107+
res := command.PouchRun("run", "--name", cname,
108+
"--dns", "1.2.3.4",
109+
"--dns", "2.3.4.5",
110+
"--dns-option", "timeout:3",
111+
"--dns-option", "ndots:9",
112+
"--dns-search", "mydomain",
113+
"--dns-search", "mydomain2",
114+
busyboxImage,
115+
"cat", "/etc/resolv.conf")
116+
defer DelContainerForceMultyTime(c, cname)
117+
res.Assert(c, icmd.Success)
118+
119+
// test if the value is correct in container
120+
out := strings.Trim(res.Stdout(), "\n")
121+
c.Assert(strings.Contains(out, "nameserver 1.2.3.4\nnameserver 2.3.4.5"), check.Equals, true)
122+
c.Assert(strings.Contains(out, "options timeout:3 ndots:9"), check.Equals, true)
123+
c.Assert(strings.Contains(out, "search mydomain mydomain2"), check.Equals, true)
124+
125+
// test if the value is in inspect result
126+
dns, err := inspectFilter(cname, ".HostConfig.DNS")
127+
c.Assert(err, check.IsNil)
128+
c.Assert(dns, check.Equals, "[1.2.3.4 2.3.4.5]")
129+
130+
dnsOptions, err := inspectFilter(cname, ".HostConfig.DNSOptions")
131+
c.Assert(err, check.IsNil)
132+
c.Assert(dnsOptions, check.Equals, "[timeout:3 ndots:9]")
133+
134+
dnsSearch, err := inspectFilter(cname, ".HostConfig.DNSSearch")
135+
c.Assert(err, check.IsNil)
136+
c.Assert(dnsSearch, check.Equals, "[mydomain mydomain2]")
137+
}

0 commit comments

Comments
 (0)