diff --git a/internal/app/machined/pkg/controllers/k8s/extra_manifest.go b/internal/app/machined/pkg/controllers/k8s/extra_manifest.go index 60e4f70c7..46ed39f6e 100644 --- a/internal/app/machined/pkg/controllers/k8s/extra_manifest.go +++ b/internal/app/machined/pkg/controllers/k8s/extra_manifest.go @@ -163,7 +163,7 @@ func (ctrl *ExtraManifestController) processURL(ctx context.Context, r controlle // I wish we never used go-getter package, as it doesn't allow downloading into memory. // But there's not much we can do about it right now, as it supports lots of magic - // users might rely upon now. + // users might rely upon. // Disable netrc since we don't have getent installed, and most likely // never will. diff --git a/internal/app/machined/pkg/controllers/network/etcfile.go b/internal/app/machined/pkg/controllers/network/etcfile.go index 17fa5e1ef..16480ba1e 100644 --- a/internal/app/machined/pkg/controllers/network/etcfile.go +++ b/internal/app/machined/pkg/controllers/network/etcfile.go @@ -8,8 +8,8 @@ import ( "bytes" "context" "fmt" - "html/template" "strings" + "text/tabwriter" "github.com/cosi-project/runtime/pkg/controller" "github.com/cosi-project/runtime/pkg/resource" @@ -176,42 +176,36 @@ func (ctrl *EtcFileController) renderResolvConf(resolverStatus *network.Resolver return buf.Bytes() } -var hostsTemplate = template.Must(template.New("hosts").Parse(strings.TrimSpace(` -127.0.0.1 localhost -{{ .IP }} {{ .Hostname }} {{ if ne .Hostname .Alias }}{{ .Alias }}{{ end }} -::1 localhost ip6-localhost ip6-loopback -ff02::1 ip6-allnodes -ff02::2 ip6-allrouters - -{{- with .ExtraHosts }} -{{ range . }} -{{ .IP }} {{ range .Aliases }}{{.}} {{ end -}} -{{ end -}} -{{ end -}} -`))) - func (ctrl *EtcFileController) renderHosts(hostnameStatus *network.HostnameStatusSpec, nodeAddressStatus *network.NodeAddressSpec, cfgProvider talosconfig.Provider) ([]byte, error) { var buf bytes.Buffer - extraHosts := []talosconfig.ExtraHost{} + tabW := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0) + + write := func(s string) { + tabW.Write([]byte(s)) //nolint:errcheck + } + + write("127.0.0.1\tlocalhost\n") + + write(fmt.Sprintf("%s\t%s", nodeAddressStatus.Addresses[0].IP(), hostnameStatus.FQDN())) + + if hostnameStatus.Hostname != hostnameStatus.FQDN() { + write(" " + hostnameStatus.Hostname) + } + + write("\n") + + write("::1\tlocalhost ip6-localhost ip6-loopback\n") + write("ff02::1\tip6-allnodes\n") + write("ff02::2\tip6-allrouters\n") if cfgProvider != nil { - extraHosts = cfgProvider.Machine().Network().ExtraHosts() + for _, extraHost := range cfgProvider.Machine().Network().ExtraHosts() { + write(fmt.Sprintf("%s\t%s\n", extraHost.IP(), strings.Join(extraHost.Aliases(), " "))) + } } - data := struct { - IP string - Hostname string - Alias string - ExtraHosts []talosconfig.ExtraHost - }{ - IP: nodeAddressStatus.Addresses[0].IP().String(), - Hostname: hostnameStatus.FQDN(), - Alias: hostnameStatus.Hostname, - ExtraHosts: extraHosts, - } - - if err := hostsTemplate.Execute(&buf, data); err != nil { + if err := tabW.Flush(); err != nil { return nil, err } diff --git a/internal/app/machined/pkg/controllers/network/etcfile_test.go b/internal/app/machined/pkg/controllers/network/etcfile_test.go index 51da1319b..8ba59deda 100644 --- a/internal/app/machined/pkg/controllers/network/etcfile_test.go +++ b/internal/app/machined/pkg/controllers/network/etcfile_test.go @@ -227,7 +227,7 @@ func (suite *EtcFileConfigSuite) TestComplete() { suite.testFiles( []resource.Resource{suite.cfg, suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, "nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n\nsearch example.com\n", - "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n\n10.0.0.1 a b \n10.0.0.2 c d ", //nolint:lll + "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n10.0.0.1 a b\n10.0.0.2 c d\n", //nolint:lll ) } @@ -235,7 +235,7 @@ func (suite *EtcFileConfigSuite) TestNoExtraHosts() { suite.testFiles( []resource.Resource{suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, "nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n\nsearch example.com\n", - "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters", + "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n", ) } @@ -253,7 +253,7 @@ func (suite *EtcFileConfigSuite) TestNoSearchDomain() { suite.testFiles( []resource.Resource{cfg, suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, "nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n", - "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters", //nolint:lll + "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n", //nolint:lll ) } @@ -263,7 +263,7 @@ func (suite *EtcFileConfigSuite) TestNoDomainname() { suite.testFiles( []resource.Resource{suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, "nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n", - "127.0.0.1 localhost\n33.11.22.44 foo \n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters", + "127.0.0.1 localhost\n33.11.22.44 foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n", ) } @@ -279,7 +279,7 @@ func (suite *EtcFileConfigSuite) TestOnlyHostname() { suite.testFiles( []resource.Resource{suite.defaultAddress, suite.hostnameStatus}, "", - "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters", + "127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters\n", ) }