Skip to content

Commit

Permalink
fix: table align hosts file
Browse files Browse the repository at this point in the history
Easy to read/parse the hosts file

Signed-off-by: Serge Logvinov <[email protected]>
Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
sergelogvinov authored and smira committed May 31, 2022
1 parent be644c9 commit 0a6fc90
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 23 additions & 29 deletions internal/app/machined/pkg/controllers/network/etcfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

if cfgProvider != nil {
extraHosts = cfgProvider.Machine().Network().ExtraHosts()
write := func(s string) {
tabW.Write([]byte(s)) //nolint:errcheck
}

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,
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 {
for _, extraHost := range cfgProvider.Machine().Network().ExtraHosts() {
write(fmt.Sprintf("%s\t%s\n", extraHost.IP(), strings.Join(extraHost.Aliases(), " ")))
}
}

if err := hostsTemplate.Execute(&buf, data); err != nil {
if err := tabW.Flush(); err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions internal/app/machined/pkg/controllers/network/etcfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ 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
)
}

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",
)
}

Expand All @@ -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
)
}

Expand All @@ -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",
)
}

Expand All @@ -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",
)
}

Expand Down

0 comments on commit 0a6fc90

Please sign in to comment.