From 7d831d555c8ec2e556df390adde8421ba449637e Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 22 May 2023 13:30:39 +0200 Subject: [PATCH] network create/update: allow dns servers comma separated The examples show that --dns-add 8.8.8.8,1.1.1.1 is valid but it fails, fix this by using StringSliceVar which splits at commas. Added tests to ensure it is working. Fixes #18632 Signed-off-by: Paul Holzinger --- cmd/podman/networks/create.go | 2 +- cmd/podman/networks/update.go | 4 +-- test/e2e/run_networking_test.go | 58 +++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/cmd/podman/networks/create.go b/cmd/podman/networks/create.go index 626c2d32e1..289d83dec2 100644 --- a/cmd/podman/networks/create.go +++ b/cmd/podman/networks/create.go @@ -85,7 +85,7 @@ func networkCreateFlags(cmd *cobra.Command) { flags.BoolVar(&networkCreateOptions.IgnoreIfExists, "ignore", false, "Don't fail if network already exists") dnsserverFlagName := "dns" - flags.StringArrayVar(&networkCreateOptions.NetworkDNSServers, dnsserverFlagName, nil, "DNS servers this network will use") + flags.StringSliceVar(&networkCreateOptions.NetworkDNSServers, dnsserverFlagName, nil, "DNS servers this network will use") _ = cmd.RegisterFlagCompletionFunc(dnsserverFlagName, completion.AutocompleteNone) } func init() { diff --git a/cmd/podman/networks/update.go b/cmd/podman/networks/update.go index 61d0384538..cd3b8e8422 100644 --- a/cmd/podman/networks/update.go +++ b/cmd/podman/networks/update.go @@ -31,9 +31,9 @@ func networkUpdateFlags(cmd *cobra.Command) { flags := cmd.Flags() addDNSServerFlagName := "dns-add" - flags.StringArrayVar(&networkUpdateOptions.AddDNSServers, addDNSServerFlagName, nil, "add network level nameservers") + flags.StringSliceVar(&networkUpdateOptions.AddDNSServers, addDNSServerFlagName, nil, "add network level nameservers") removeDNSServerFlagName := "dns-drop" - flags.StringArrayVar(&networkUpdateOptions.RemoveDNSServers, removeDNSServerFlagName, nil, "remove network level nameservers") + flags.StringSliceVar(&networkUpdateOptions.RemoveDNSServers, removeDNSServerFlagName, nil, "remove network level nameservers") _ = cmd.RegisterFlagCompletionFunc(addDNSServerFlagName, completion.AutocompleteNone) _ = cmd.RegisterFlagCompletionFunc(removeDNSServerFlagName, completion.AutocompleteNone) } diff --git a/test/e2e/run_networking_test.go b/test/e2e/run_networking_test.go index 8b4b587c4f..e4a0d6ea80 100644 --- a/test/e2e/run_networking_test.go +++ b/test/e2e/run_networking_test.go @@ -34,6 +34,7 @@ var _ = Describe("Podman run networking", func() { session = podmanTest.Podman([]string{"network", "inspect", net}) session.WaitWithDefaultTimeout() defer podmanTest.removeNetwork(net) + Expect(session).Should(Exit(0)) var results []types.Network err := json.Unmarshal([]byte(session.OutputToString()), &results) Expect(err).ToNot(HaveOccurred()) @@ -41,8 +42,7 @@ var _ = Describe("Podman run networking", func() { result := results[0] Expect(result.Subnets).To(HaveLen(1)) aardvarkDNSGateway := result.Subnets[0].Gateway.String() - Expect(session.OutputToString()).To(ContainSubstring("1.1.1.1")) - Expect(session).Should(Exit(0)) + Expect(result.NetworkDNSServers).To(Equal([]string{"1.1.1.1"})) session = podmanTest.Podman([]string{"run", "-d", "--name", "con1", "--network", net, "busybox", "top"}) session.WaitWithDefaultTimeout() @@ -54,7 +54,7 @@ var _ = Describe("Podman run networking", func() { Expect(session.OutputToString()).To(ContainSubstring("Non-authoritative answer: Name: google.com Address:")) // Update to a bad DNS Server - session = podmanTest.Podman([]string{"network", "update", net, "--dns-add", "7.7.7.7"}) + session = podmanTest.Podman([]string{"network", "update", net, "--dns-add", "127.0.0.255"}) session.WaitWithDefaultTimeout() Expect(session).Should(Exit(0)) @@ -69,6 +69,58 @@ var _ = Describe("Podman run networking", func() { Expect(session.OutputToString()).To(ContainSubstring(";; connection timed out; no servers could be reached")) }) + It("podman network dns multiple servers", func() { + // Following test is only functional with netavark and aardvark + SkipIfCNI(podmanTest) + net := createNetworkName("IntTest") + session := podmanTest.Podman([]string{"network", "create", net, "--dns", "1.1.1.1,8.8.8.8", "--dns", "8.4.4.8"}) + session.WaitWithDefaultTimeout() + defer podmanTest.removeNetwork(net) + Expect(session).Should(Exit(0)) + + session = podmanTest.Podman([]string{"network", "inspect", net}) + session.WaitWithDefaultTimeout() + defer podmanTest.removeNetwork(net) + Expect(session).Should(Exit(0)) + var results []types.Network + err := json.Unmarshal([]byte(session.OutputToString()), &results) + Expect(err).ToNot(HaveOccurred()) + Expect(results).To(HaveLen(1)) + result := results[0] + Expect(result.Subnets).To(HaveLen(1)) + aardvarkDNSGateway := result.Subnets[0].Gateway.String() + Expect(result.NetworkDNSServers).To(Equal([]string{"1.1.1.1", "8.8.8.8", "8.4.4.8"})) + + session = podmanTest.Podman([]string{"run", "-d", "--name", "con1", "--network", net, "busybox", "top"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + session = podmanTest.Podman([]string{"exec", "con1", "nslookup", "google.com", aardvarkDNSGateway}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + Expect(session.OutputToString()).To(ContainSubstring("Non-authoritative answer: Name: google.com Address:")) + + // Update DNS server + session = podmanTest.Podman([]string{"network", "update", net, "--dns-drop=1.1.1.1,8.8.8.8", + "--dns-drop", "8.4.4.8", "--dns-add", "127.0.0.253,127.0.0.254", "--dns-add", "127.0.0.255"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + session = podmanTest.Podman([]string{"network", "inspect", net}) + session.WaitWithDefaultTimeout() + defer podmanTest.removeNetwork(net) + Expect(session).Should(Exit(0)) + err = json.Unmarshal([]byte(session.OutputToString()), &results) + Expect(err).ToNot(HaveOccurred()) + Expect(results).To(HaveLen(1)) + Expect(results[0].NetworkDNSServers).To(Equal([]string{"127.0.0.253", "127.0.0.254", "127.0.0.255"})) + + session = podmanTest.Podman([]string{"exec", "con1", "nslookup", "google.com", aardvarkDNSGateway}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(1)) + Expect(session.OutputToString()).To(ContainSubstring(";; connection timed out; no servers could be reached")) + }) + It("podman run network connection with default bridge", func() { session := podmanTest.RunContainerWithNetworkTest("") session.WaitWithDefaultTimeout()