fix: table align hosts file

Easy to read/parse the hosts file

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
This commit is contained in:
Serge Logvinov 2022-05-28 14:49:27 +00:00 committed by Andrey Smirnov
parent be644c96e4
commit 0a6fc906fc
No known key found for this signature in database
GPG Key ID: 7B26396447AB6DFD
3 changed files with 30 additions and 36 deletions

View File

@ -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. // 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 // 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 // Disable netrc since we don't have getent installed, and most likely
// never will. // never will.

View File

@ -8,8 +8,8 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"html/template"
"strings" "strings"
"text/tabwriter"
"github.com/cosi-project/runtime/pkg/controller" "github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource" "github.com/cosi-project/runtime/pkg/resource"
@ -176,42 +176,36 @@ func (ctrl *EtcFileController) renderResolvConf(resolverStatus *network.Resolver
return buf.Bytes() 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) { func (ctrl *EtcFileController) renderHosts(hostnameStatus *network.HostnameStatusSpec, nodeAddressStatus *network.NodeAddressSpec, cfgProvider talosconfig.Provider) ([]byte, error) {
var buf bytes.Buffer 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 { 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 { if err := tabW.Flush(); err != nil {
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 {
return nil, err return nil, err
} }

View File

@ -227,7 +227,7 @@ func (suite *EtcFileConfigSuite) TestComplete() {
suite.testFiles( suite.testFiles(
[]resource.Resource{suite.cfg, suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, []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", "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( suite.testFiles(
[]resource.Resource{suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, []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", "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( suite.testFiles(
[]resource.Resource{cfg, suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, []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", "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( suite.testFiles(
[]resource.Resource{suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus}, []resource.Resource{suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus},
"nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n", "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( suite.testFiles(
[]resource.Resource{suite.defaultAddress, suite.hostnameStatus}, []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",
) )
} }