From e6f5647c1d5eda41db245e841fd61d4fd4df409b Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 18 Jul 2023 19:51:26 +0200 Subject: [PATCH] feat: Add work-around for Docker Desktop breaking /etc/hosts --- pkg/chezmoi/chezmoi.go | 5 +++++ pkg/chezmoi/chezmoi_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/chezmoi/chezmoi.go b/pkg/chezmoi/chezmoi.go index fb02c1b4d16..f7972da52ba 100644 --- a/pkg/chezmoi/chezmoi.go +++ b/pkg/chezmoi/chezmoi.go @@ -313,6 +313,11 @@ func etcHostsFQDNHostname(fileSystem vfs.FS) (string, error) { if domainname == "localdomain" { continue } + // Docker Desktop breaks /etc/hosts. Filter out all docker.internal + // domain names. See https://github.com/twpayne/chezmoi/issues/3095. + if domainname == "docker.internal" { + continue + } if runtime.GOOS == "darwin" && domainname == "local" { continue } diff --git a/pkg/chezmoi/chezmoi_test.go b/pkg/chezmoi/chezmoi_test.go index 5879f059e9a..88b90caa19c 100644 --- a/pkg/chezmoi/chezmoi_test.go +++ b/pkg/chezmoi/chezmoi_test.go @@ -109,6 +109,30 @@ func TestEtcHostsFQDNHostname(t *testing.T) { f: etcHostsFQDNHostname, expected: "host.example.com", }, + { + name: "etc_hosts_kubernetes_docker_internal", + root: map[string]any{ + "/etc/hosts": chezmoitest.JoinLines( + `##`, + `# Host Database`, + `#`, + `# localhost is used to configure the loopback interface`, + `# when the system is booting. Do not change this entry.`, + `##`, + `127.0.0.1 localhost`, + `255.255.255.255 broadcasthost`, + `::1 localhost`, + `# Added by Docker Desktop`, + `# To allow the same kube context to work on the host and the container:`, + `127.0.0.1 kubernetes.docker.internal`, + `# End of section`, + `127.0.1.1`, + `127.0.1.1 host.example.com`, + ), + }, + f: etcHostsFQDNHostname, + expected: "host.example.com", + }, { name: "etc_hostname", root: map[string]any{