From 926d265df378f3defe127c2d50f541b9ff7d447d Mon Sep 17 00:00:00 2001 From: Xiangcheng Kuo Date: Fri, 17 May 2024 15:21:56 +0800 Subject: [PATCH] enhancement(link_local_dns.go): avoid occur runtime panic when the nameservers are empty. --- helper/link_local_dns.go | 6 ++++++ helper/link_local_dns_test.go | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/helper/link_local_dns.go b/helper/link_local_dns.go index ad59063..9c42fb9 100644 --- a/helper/link_local_dns.go +++ b/helper/link_local_dns.go @@ -32,6 +32,12 @@ type LinkLocalDNS struct { } func (l LinkLocalDNS) Execute() (map[string]string, error) { + // In some cases, the nameservers are empty. + // For example, when using the host network, and host '/etc/resolv.conf' file content is empty. + if len(l.Config.Servers) == 0 { + return nil, nil + } + if !net.ParseIP(l.Config.Servers[0]).IsLinkLocalUnicast() { return nil, nil } diff --git a/helper/link_local_dns_test.go b/helper/link_local_dns_test.go index 0acf26c..dc6f135 100644 --- a/helper/link_local_dns_test.go +++ b/helper/link_local_dns_test.go @@ -59,6 +59,15 @@ func testLinkLocalDNS(t *testing.T, context spec.G, it spec.S) { Expect(ioutil.ReadFile(path)).To(Equal([]byte("test"))) }) + it("do nothing if no nameservers", func() { + config := &ddns.ClientConfig{Servers: []string{}} + + l := helper.LinkLocalDNS{Config: config} + + Expect(l.Execute()).To(BeNil()) + Expect(ioutil.ReadFile(path)).To(Equal([]byte(`test`))) + }) + it("returns an error if $JAVA_SECURITY_PROPERTIES is not set", func() { config := &ddns.ClientConfig{Servers: []string{"169.254.0.1"}} l := helper.LinkLocalDNS{Config: config}