Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Machine allocation with dns and ntp #255

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 107 additions & 7 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"time"

config "github.com/flatcar/ignition/config/v2_4"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-hammer/pkg/api"
"github.com/metal-stack/metal-images/cmd/templates"
v1 "github.com/metal-stack/metal-images/cmd/v1"
"github.com/metal-stack/metal-networker/pkg/netconf"
"github.com/metal-stack/v"
Expand Down Expand Up @@ -69,6 +71,12 @@ func (i *installer) do() error {
return err
}

err = i.writeNTPConf()
if err != nil {
i.log.Warn("writing ntp configuration failed", "err", err)
return err
}

err = i.createMetalUser()
if err != nil {
return err
Expand Down Expand Up @@ -155,23 +163,115 @@ func (i *installer) fileExists(filename string) bool {
}

func (i *installer) writeResolvConf() error {
i.log.Info("write /etc/resolv.conf")
const f = "/etc/resolv.conf"
i.log.Info("write configuration", "file", f)
// Must be written here because during docker build this file is synthetic
// FIXME enable systemd-resolved based approach again once we figured out why it does not work on the firewall
// most probably because the resolved must be running in the internet facing vrf.
// ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
// in ignite this file is a symlink to /proc/net/pnp, to pass integration test, remove this first
err := i.fs.Remove("/etc/resolv.conf")
err := i.fs.Remove(f)
if err != nil {
i.log.Info("no /etc/resolv.conf present")
i.log.Info("config file not present", "file", f)
}

// FIXME migrate to dns0.eu resolvers
content := []byte(
`nameserver 8.8.8.8
nameserver 8.8.4.4
`)
return afero.WriteFile(i.fs, "/etc/resolv.conf", content, 0644)

if i.config.DNSServers != nil {
var s string
for _, dnsServer := range i.config.DNSServers {
s += "nameserver " + *dnsServer.IP + "\n"
}
content = []byte(s)

if i.oss != osAlmalinux {
err = i.writeDNSconf()
if err != nil {
i.log.Warn("writing dns configuration failed", "err", err)
return err
}
}

}
return afero.WriteFile(i.fs, f, content, 0644)
}

func (i *installer) writeDNSconf() error {
const f = "/etc/systemd/resolved.conf.d/dns.conf"
i.log.Info("write configuration", "file", f)

err := i.fs.Remove(f)
if err != nil {
i.log.Info("config file not present", "file", f)
}

var addresses []string
for _, dnsServer := range i.config.DNSServers {
if dnsServer.IP == nil {
continue
}
addresses = append(addresses, *dnsServer.IP)
}
s := fmt.Sprintf("[Resolve]\nDNS=%s\nLLMNR=no\n", strings.Join(addresses, " "))

content := []byte(s)
return afero.WriteFile(i.fs, f, content, 0644)
}

func (i *installer) writeNTPConf() error {
if len(i.config.NTPServers) == 0 {
return nil
}

var (
ntpConfigPath string
s string
err error
)

switch i.config.Role {
case models.V1MachineAllocationRoleFirewall:
ntpConfigPath = "/etc/chrony/chrony.conf"
s, err = templates.RenderChronyTemplate(templates.Chrony{NTPServers: i.config.NTPServers})
if err != nil {
return fmt.Errorf("error rendering chrony template %w", err)
}

case models.V1MachineAllocationRoleMachine:
if i.oss == osDebian || i.oss == osUbuntu {
ntpConfigPath = "/etc/systemd/timesyncd.conf"
var addresses []string
for _, ntp := range i.config.NTPServers {
if ntp.Address == nil {
continue
}
addresses = append(addresses, *ntp.Address)
}
s = fmt.Sprintf("[Time]\nNTP=%s\n", strings.Join(addresses, " "))
}

if i.oss == osAlmalinux {
ntpConfigPath = "/etc/chrony.conf"
s, err = templates.RenderChronyTemplate(templates.Chrony{NTPServers: i.config.NTPServers})
if err != nil {
return fmt.Errorf("error rendering chrony template %w", err)
}
}
default:
return fmt.Errorf("unknown role:%s", i.config.Role)
}

content := []byte(s)
i.log.Info("write configuration", "file", ntpConfigPath)
err = i.fs.Remove(ntpConfigPath)
if err != nil {
i.log.Info("config file not present", "file", ntpConfigPath)
}

return afero.WriteFile(i.fs, ntpConfigPath, content, 0644)
}

func (i *installer) buildCMDLine() string {
Expand Down Expand Up @@ -324,9 +424,9 @@ func (i *installer) configureNetwork() error {

var kind netconf.BareMetalType
switch i.config.Role {
case "firewall":
case models.V1MachineAllocationRoleFirewall:
kind = netconf.Firewall
case "machine":
case models.V1MachineAllocationRoleMachine:
kind = netconf.Machine
default:
return fmt.Errorf("unknown role:%s", i.config.Role)
Expand Down
Loading
Loading