forked from concourse/concourse
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The nameserver values could come from manually defined properties or the host itself Revert "k8s topgun: behaviour: skip containerd dns tests" This reverts commit 0b03e84. Signed-off-by: Bishoy Youssef <byoussef@pivotal.io> Co-authored-by: Vikram Yadav <vyadav@pivotal.io> Co-authored-by: Izabela Gomes <igomes@pivotal.io>
- Loading branch information
1 parent
d0b12f8
commit 8f98888
Showing
8 changed files
with
141 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"regexp" | ||
"strings" | ||
|
||
"code.cloudfoundry.org/localip" | ||
) | ||
|
||
// Parse resolve.conf file from the provided path. | ||
// implementation is based on guardian's implementation | ||
// here: https://github.com/cloudfoundry/guardian/blob/master/kawasaki/dns/resolv_compiler.go | ||
func ParseHostResolveConf(path string) ([]string, error) { | ||
|
||
resolvConf, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read host's resolv.conf: %w", err) | ||
} | ||
|
||
resolvContents := string(resolvConf) | ||
|
||
loopbackNameserver := regexp.MustCompile(`^\s*nameserver\s+127\.0\.0\.\d+\s*$`) | ||
if loopbackNameserver.MatchString(resolvContents) { | ||
ip, err := localip.LocalIP() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []string{"nameserver " + ip}, nil | ||
} | ||
|
||
var entries []string | ||
|
||
for _, resolvEntry := range strings.Split(strings.TrimSpace(resolvContents), "\n") { | ||
if resolvEntry == "" { | ||
continue | ||
} | ||
|
||
if !strings.HasPrefix(resolvEntry, "nameserver") { | ||
entries = append(entries, strings.TrimSpace(resolvEntry)) | ||
continue | ||
} | ||
|
||
pattern := regexp.MustCompile(`127\.\d{1,3}\.\d{1,3}\.\d{1,3}`) | ||
if !pattern.MatchString(strings.TrimSpace(resolvEntry)) { | ||
nameserverFields := strings.Fields(resolvEntry) | ||
if len(nameserverFields) != 2 { | ||
continue | ||
} | ||
entries = append(entries, strings.Join(nameserverFields, " ")) | ||
} | ||
} | ||
|
||
return entries, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package runtime_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"code.cloudfoundry.org/localip" | ||
"github.com/concourse/concourse/worker/runtime" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ResolveconfParserSuite struct { | ||
suite.Suite | ||
*require.Assertions | ||
} | ||
|
||
func (s *ResolveconfParserSuite) TestParseHostResolvConf() { | ||
file := ` | ||
nameserver 8.8.8.8 | ||
nameserver 127.0.0.16 | ||
nameserver something 9.9.9.9 | ||
search something | ||
` | ||
|
||
tmpDir, _ := ioutil.TempDir("", "test-resolv") | ||
defer os.RemoveAll(tmpDir) | ||
ioutil.WriteFile(path.Join(tmpDir, "resolv.conf"), []byte(file), 0644) | ||
|
||
entries, err := runtime.ParseHostResolveConf(path.Join(tmpDir, "resolv.conf")) | ||
s.NoError(err) | ||
|
||
s.Equal([]string{"nameserver 8.8.8.8", "search something"}, entries) | ||
} | ||
|
||
func (s *ResolveconfParserSuite) TestParseHostResolvConfWithLoopback() { | ||
file := `nameserver 127.0.0.1` | ||
|
||
tmpDir, _ := ioutil.TempDir("", "test-resolv-noloopback") | ||
defer os.RemoveAll(tmpDir) | ||
ioutil.WriteFile(path.Join(tmpDir, "resolv.conf"), []byte(file), 0644) | ||
|
||
entries, err := runtime.ParseHostResolveConf(path.Join(tmpDir, "resolv.conf")) | ||
s.NoError(err) | ||
|
||
ip, _ := localip.LocalIP() | ||
s.Equal([]string{"nameserver " + ip}, entries) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters