mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-18 21:21:10 +02:00
fix(networkd): Set hostname properly for dhcp when no hostname option is returned
This fixes a condition where a dhcp response does not provide a hostname. Previously this would cause the default hostname ( talos-127-0-1-1 ) to be used. This catches the condition and changes it to compute the hostname via talos-ip. Signed-off-by: Brad Beam <brad.beam@talos-systems.com>
This commit is contained in:
parent
d933ca9a88
commit
3dff2b234d
@ -135,6 +135,10 @@ func (d *DHCP) Hostname() string {
|
|||||||
return fmt.Sprintf("%s.%s", shortHostname, d.Ack.DomainName())
|
return fmt.Sprintf("%s.%s", shortHostname, d.Ack.DomainName())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.Ack.HostName() == "" {
|
||||||
|
return fmt.Sprintf("%s-%s", "talos", strings.ReplaceAll(d.Address().IP.String(), ".", "-"))
|
||||||
|
}
|
||||||
|
|
||||||
return d.Ack.HostName()
|
return d.Ack.HostName()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,10 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/talos-systems/talos/internal/app/networkd/pkg/address"
|
||||||
"github.com/talos-systems/talos/internal/pkg/runtime"
|
"github.com/talos-systems/talos/internal/pkg/runtime"
|
||||||
"github.com/talos-systems/talos/pkg/config/machine"
|
"github.com/talos-systems/talos/pkg/config/machine"
|
||||||
"github.com/talos-systems/talos/pkg/config/types/v1alpha1"
|
"github.com/talos-systems/talos/pkg/config/types/v1alpha1"
|
||||||
@ -39,7 +41,7 @@ func (suite *NetworkdSuite) TestNetworkd() {
|
|||||||
|
|
||||||
func (suite *NetworkdSuite) TestHostname() {
|
func (suite *NetworkdSuite) TestHostname() {
|
||||||
var (
|
var (
|
||||||
address net.IP
|
addr net.IP
|
||||||
domainname string
|
domainname string
|
||||||
err error
|
err error
|
||||||
hostname string
|
hostname string
|
||||||
@ -50,31 +52,37 @@ func (suite *NetworkdSuite) TestHostname() {
|
|||||||
nwd, err = New(nil)
|
nwd, err = New(nil)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
hostname, _, address, err = nwd.decideHostname()
|
// Default test
|
||||||
|
hostname, _, addr, err = nwd.decideHostname()
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
suite.Assert().Equal("talos-127-0-1-1", hostname)
|
suite.Assert().Equal("talos-127-0-1-1", hostname)
|
||||||
suite.Assert().Equal(address, net.ParseIP("127.0.1.1"))
|
suite.Assert().Equal(addr, net.ParseIP("127.0.1.1"))
|
||||||
|
|
||||||
|
// Static addressing tests
|
||||||
|
|
||||||
|
// Static with hostname
|
||||||
sampleConfig = sampleConfigFile()
|
sampleConfig = sampleConfigFile()
|
||||||
|
|
||||||
nwd, err = New(sampleConfig)
|
nwd, err = New(sampleConfig)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
hostname, _, address, err = nwd.decideHostname()
|
hostname, _, addr, err = nwd.decideHostname()
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
suite.Assert().Equal("myhostname", hostname)
|
suite.Assert().Equal("myhostname", hostname)
|
||||||
suite.Assert().Equal(address, net.ParseIP("192.168.0.10"))
|
suite.Assert().Equal(addr, net.ParseIP("192.168.0.10"))
|
||||||
|
|
||||||
|
// Static for computed hostname ( talos-ip )
|
||||||
sampleConfig.Machine().Network().SetHostname("")
|
sampleConfig.Machine().Network().SetHostname("")
|
||||||
|
|
||||||
nwd, err = New(sampleConfig)
|
nwd, err = New(sampleConfig)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
hostname, _, address, err = nwd.decideHostname()
|
hostname, _, addr, err = nwd.decideHostname()
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
suite.Assert().Equal("talos-192-168-0-10", hostname)
|
suite.Assert().Equal("talos-192-168-0-10", hostname)
|
||||||
suite.Assert().Equal(address, net.ParseIP("192.168.0.10"))
|
suite.Assert().Equal(addr, net.ParseIP("192.168.0.10"))
|
||||||
|
|
||||||
|
// Static for hostname too long
|
||||||
sampleConfig.Machine().Network().SetHostname("somereallyreallyreallylongstringthathasmorethan63charactersbecauseweneedtotestit")
|
sampleConfig.Machine().Network().SetHostname("somereallyreallyreallylongstringthathasmorethan63charactersbecauseweneedtotestit")
|
||||||
|
|
||||||
nwd, err = New(sampleConfig)
|
nwd, err = New(sampleConfig)
|
||||||
@ -84,6 +92,7 @@ func (suite *NetworkdSuite) TestHostname() {
|
|||||||
_, _, _, err = nwd.decideHostname()
|
_, _, _, err = nwd.decideHostname()
|
||||||
suite.Require().Error(err)
|
suite.Require().Error(err)
|
||||||
|
|
||||||
|
// Static for hostname vs domain name
|
||||||
sampleConfig.Machine().Network().SetHostname("dadjokes.biz.dev.com.org.io")
|
sampleConfig.Machine().Network().SetHostname("dadjokes.biz.dev.com.org.io")
|
||||||
|
|
||||||
nwd, err = New(sampleConfig)
|
nwd, err = New(sampleConfig)
|
||||||
@ -93,6 +102,49 @@ func (suite *NetworkdSuite) TestHostname() {
|
|||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
suite.Assert().Equal("dadjokes", hostname)
|
suite.Assert().Equal("dadjokes", hostname)
|
||||||
suite.Assert().Equal("biz.dev.com.org.io", domainname)
|
suite.Assert().Equal("biz.dev.com.org.io", domainname)
|
||||||
|
|
||||||
|
// DHCP addressing tests
|
||||||
|
|
||||||
|
// DHCP with OptionHostName
|
||||||
|
nwd, err = New(dhcpConfigFile())
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
nwd.Interfaces["eth0"].AddressMethod = []address.Addressing{
|
||||||
|
&address.DHCP{
|
||||||
|
Ack: &dhcpv4.DHCPv4{
|
||||||
|
YourIPAddr: net.ParseIP("192.168.0.11"),
|
||||||
|
Options: dhcpv4.Options{
|
||||||
|
uint8(dhcpv4.OptionHostName): []byte("evenbetterdadjokes"),
|
||||||
|
uint8(dhcpv4.OptionSubnetMask): []byte{255, 255, 255, 0},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname, _, addr, err = nwd.decideHostname()
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().Equal("evenbetterdadjokes", hostname)
|
||||||
|
suite.Assert().Equal(addr, net.ParseIP("192.168.0.11"))
|
||||||
|
|
||||||
|
// DHCP without OptionHostNAme
|
||||||
|
nwd, err = New(dhcpConfigFile())
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
nwd.Interfaces["eth0"].AddressMethod = []address.Addressing{
|
||||||
|
&address.DHCP{
|
||||||
|
Ack: &dhcpv4.DHCPv4{
|
||||||
|
YourIPAddr: net.ParseIP("192.168.0.11"),
|
||||||
|
Options: dhcpv4.Options{
|
||||||
|
uint8(dhcpv4.OptionSubnetMask): []byte{255, 255, 255, 0},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname, _, addr, err = nwd.decideHostname()
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
suite.Assert().Equal("talos-192-168-0-11", hostname)
|
||||||
|
suite.Assert().Equal(addr, net.ParseIP("192.168.0.11"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sampleConfigFile() runtime.Configurator {
|
func sampleConfigFile() runtime.Configurator {
|
||||||
@ -120,3 +172,17 @@ func sampleConfigFile() runtime.Configurator {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dhcpConfigFile() runtime.Configurator {
|
||||||
|
return &v1alpha1.Config{
|
||||||
|
MachineConfig: &v1alpha1.MachineConfig{
|
||||||
|
MachineNetwork: &v1alpha1.NetworkConfig{
|
||||||
|
NetworkInterfaces: []machine.Device{
|
||||||
|
{
|
||||||
|
Interface: "eth0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user