Skip to content
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
51 changes: 48 additions & 3 deletions pkg/asset/agent/agentconfig/agenthosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type nmStateInterface struct {
// AgentHosts generates the hosts information from the AgentConfig and
// OptionalInstallConfig assets.
type AgentHosts struct {
Hosts []agent.Host
rendezvousIP string
Hosts []agent.Host
rendezvousIP string
hostsFromAgentConfig bool
}

// Name returns a human friendly name.
Expand Down Expand Up @@ -74,7 +75,7 @@ func (a *AgentHosts) Generate(_ context.Context, dependencies asset.Parents) err
a.rendezvousIP = agentConfig.Config.RendezvousIP
a.Hosts = append(a.Hosts, agentConfig.Config.Hosts...)
if len(a.Hosts) > 0 {
// Hosts defined in agent-config take precedence
a.hostsFromAgentConfig = true
logrus.Debugf("Using hosts from %s", agentConfigFilename)
}
}
Expand All @@ -92,6 +93,11 @@ func (a *AgentHosts) Generate(_ context.Context, dependencies asset.Parents) err

case workflow.AgentWorkflowTypeAddNodes:
a.Hosts = append(a.Hosts, addNodesConfig.Config.Hosts...)
if len(a.Hosts) > 0 {
// nodes-config.yaml hosts are user-authored like agent-config.yaml hosts,
// unlike install-config hosts which have code-generated interface names.
a.hostsFromAgentConfig = true
}

default:
return fmt.Errorf("AgentWorkflowType value not supported: %s", agentWorkflow.Workflow)
Expand Down Expand Up @@ -125,6 +131,8 @@ func (a *AgentHosts) validateAgentHosts() field.ErrorList {
allErrs = append(allErrs, err...)
}

a.warnInterfaceNamesNotInNetworkConfig(host)

if err := a.validateHostRootDeviceHints(hostPath, host); err != nil {
allErrs = append(allErrs, err...)
}
Expand Down Expand Up @@ -171,6 +179,43 @@ func (a *AgentHosts) validateHostInterfaces(hostPath *field.Path, host agent.Hos
return allErrs
}

func (a *AgentHosts) warnInterfaceNamesNotInNetworkConfig(host agent.Host) {
if !a.hostsFromAgentConfig {
return
}
if len(host.NetworkConfig.Raw) == 0 || len(host.Interfaces) == 0 {
return
}

var netInterfaces nmStateInterface
if err := yaml.Unmarshal(host.NetworkConfig.Raw, &netInterfaces); err != nil {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was part of our design principles that we treat nmstate as an opaque blob and not rely on knowing the internal structure of it, which may change over time.

return
}

ncNames := make(map[string]bool, len(netInterfaces.Interfaces))
var ncNameList []string
for _, iface := range netInterfaces.Interfaces {
if iface.Name != "" {
ncNames[iface.Name] = true
ncNameList = append(ncNameList, iface.Name)
}
}
if len(ncNames) == 0 {
return
}

for _, iface := range host.Interfaces {
if iface.Name == "" {
continue
}
if !ncNames[iface.Name] {
logrus.Warnf("agent-config: interface name %q not found in networkConfig interfaces %v; "+
"connectivity may fail if interface names do not match at boot time",
iface.Name, ncNameList)
}
}
}

func (a *AgentHosts) validateHostRootDeviceHints(hostPath *field.Path, host agent.Host) field.ErrorList {
rdhPath := hostPath.Child("rootDeviceHints")
allErrs := validation.ValidateHostRootDeviceHints(&host.RootDeviceHints, rdhPath)
Expand Down
237 changes: 230 additions & 7 deletions pkg/asset/agent/agentconfig/agenthosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ routes:
next-hop-address: 192.168.111.126
next-hop-interface: eth0
table-id: 254
`
agentNetworkConfigBond = `interfaces:
- name: eth0
type: ethernet
state: up
mac-address: 28:d2:44:d2:b2:1a
- name: eth1
type: ethernet
state: up
mac-address: 28:d2:44:d2:b2:1b
- name: bond0
type: bond
state: up
link-aggregation:
mode: active-backup
port:
- eth0
- eth1
ipv4:
enabled: true
dhcp: false
address:
- ip: 192.168.111.80
prefix-length: 24
`
)

Expand Down Expand Up @@ -174,8 +198,8 @@ func TestAgentHosts_Generate(t *testing.T) {
getAgentConfigMultiHost("worker"),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne),
agentHost().name("test-2").role("worker").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigTwo)),
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne),
agentHost().name("test-2").role("worker").interfaces(iface("eth0", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigTwo)),
},
{
name: "multi-host-from-install-config",
Expand Down Expand Up @@ -329,8 +353,8 @@ func TestAgentHosts_Generate(t *testing.T) {
getAgentConfigMultiHostEmbeddedRendezvousIP(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigEmbeddedRendezvousIPOne),
agentHost().name("test-2").role("worker").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigEmbeddedRendezvousIPTwo)),
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigEmbeddedRendezvousIPOne),
agentHost().name("test-2").role("worker").interfaces(iface("eth0", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigEmbeddedRendezvousIPTwo)),
},
{
name: "multi-host-from-agent-config-with-arbiter",
Expand All @@ -341,8 +365,143 @@ func TestAgentHosts_Generate(t *testing.T) {
getAgentConfigMultiHost("arbiter"),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne),
agentHost().name("test-2").role("arbiter").interfaces(iface("enp3s1", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigTwo)),
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne),
agentHost().name("test-2").role("arbiter").interfaces(iface("eth0", "28:d2:44:d2:b2:1b")).networkConfig(agentNetworkConfigTwo)),
},
{
name: "interface-name-mismatch-with-networkconfig-warns-only",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigMismatchedInterfaceName(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("enp3s0", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne)),
},
{
name: "interface-name-matches-networkconfig",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigMatchingInterfaceName(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne)),
},
{
name: "bond-networkconfig-with-matching-interfaces",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigBondMatching(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a"), iface("eth1", "28:d2:44:d2:b2:1b")).deviceHint().networkConfig(agentNetworkConfigBond)),
},
{
name: "bond-networkconfig-with-mismatched-interfaces-warns-only",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigBondMismatched(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("nic1", "28:d2:44:d2:b2:1a"), iface("nic2", "28:d2:44:d2:b2:1b")).deviceHint().networkConfig(agentNetworkConfigBond)),
},
{
name: "install-config-with-networkconfig-no-warning-inert-path",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigWithMismatchedNetworkConfig(),
getNoHostsAgentConfig(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:b0:bf:01")).deviceHint().networkConfig(installNetworkConfigOne)),
},
{
name: "add-nodes-interface-mismatch-warns-only",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes},
&joiner.AddNodesConfig{
Config: joiner.Config{
Hosts: []agent.Host{
{
Hostname: "extra-worker-0",
Role: "worker",
Interfaces: []*aiv1beta1.Interface{
{
Name: "nic0",
MacAddress: "28:d2:44:d2:b2:1a",
},
},
NetworkConfig: aiv1beta1.NetConfig{
Raw: []byte(agentNetworkConfigOne),
},
},
},
},
},
getNoHostsInstallConfig(),
getNoHostsAgentConfig(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("extra-worker-0").role("worker").interfaces(iface("nic0", "28:d2:44:d2:b2:1a")).networkConfig(agentNetworkConfigOne)),
},
{
name: "add-nodes-interface-matches-no-warning",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes},
&joiner.AddNodesConfig{
Config: joiner.Config{
Hosts: []agent.Host{
{
Hostname: "extra-worker-0",
Role: "worker",
Interfaces: []*aiv1beta1.Interface{
{
Name: "eth0",
MacAddress: "28:d2:44:d2:b2:1a",
},
},
NetworkConfig: aiv1beta1.NetConfig{
Raw: []byte(agentNetworkConfigOne),
},
},
},
},
},
getNoHostsInstallConfig(),
getNoHostsAgentConfig(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("extra-worker-0").role("worker").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).networkConfig(agentNetworkConfigOne)),
},
{
name: "agent-config-empty-interface-name-skipped",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigEmptyInterfaceName(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("", "28:d2:44:d2:b2:1a")).deviceHint().networkConfig(agentNetworkConfigOne)),
},
{
name: "agent-config-malformed-networkconfig-no-panic",
dependencies: []asset.Asset{
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
&joiner.AddNodesConfig{},
getInstallConfigSingleHost(),
getAgentConfigMalformedNetworkConfig(),
},
expectedConfig: agentHosts().hosts(
agentHost().name("test").role("master").interfaces(iface("eth0", "28:d2:44:d2:b2:1a")).deviceHint().rawNetworkConfig("not: valid: yaml: [[")),
},
}
for _, tc := range cases {
Expand Down Expand Up @@ -453,13 +612,14 @@ func getAgentConfigSingleHost() *AgentConfig {

func getAgentConfigMultiHost(role string) *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces[0].Name = "eth0"
a.Config.Hosts[0].NetworkConfig.Raw = []byte(agentNetworkConfigOne)
host := agent.Host{
Hostname: "test-2",
Role: role,
Interfaces: []*aiv1beta1.Interface{
{
Name: "enp3s1",
Name: "eth0",
MacAddress: "28:d2:44:d2:b2:1b",
},
},
Expand Down Expand Up @@ -528,6 +688,26 @@ func getAgentConfigInvalidInterfaces() *AgentConfig {
return a
}

func getAgentConfigBondMatching() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces = []*aiv1beta1.Interface{
{Name: "eth0", MacAddress: "28:d2:44:d2:b2:1a"},
{Name: "eth1", MacAddress: "28:d2:44:d2:b2:1b"},
}
a.Config.Hosts[0].NetworkConfig.Raw = unmarshalJSON([]byte(agentNetworkConfigBond))
return a
}

func getAgentConfigBondMismatched() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces = []*aiv1beta1.Interface{
{Name: "nic1", MacAddress: "28:d2:44:d2:b2:1a"},
{Name: "nic2", MacAddress: "28:d2:44:d2:b2:1b"},
}
a.Config.Hosts[0].NetworkConfig.Raw = unmarshalJSON([]byte(agentNetworkConfigBond))
return a
}

func getAgentConfigMissingInterfaces() *AgentConfig {
a := getNoHostsAgentConfig()
a.Config.Hosts = []agent.Host{
Expand All @@ -547,6 +727,20 @@ func getAgentConfigMissingInterfaces() *AgentConfig {
return a
}

func getAgentConfigMismatchedInterfaceName() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces[0].Name = "enp3s0"
a.Config.Hosts[0].NetworkConfig.Raw = []byte(agentNetworkConfigOne)
return a
}

func getAgentConfigMatchingInterfaceName() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces[0].Name = "eth0"
a.Config.Hosts[0].NetworkConfig.Raw = []byte(agentNetworkConfigOne)
return a
}

func getAgentConfigInvalidRendezvousIP() *AgentConfig {
a := getAgentConfigMultiHost("worker")
a.Config.RendezvousIP = "192.168.111.81"
Expand Down Expand Up @@ -728,6 +922,13 @@ func (hb *HostBuilder) networkConfig(raw string) *HostBuilder {
return hb
}

func (hb *HostBuilder) rawNetworkConfig(raw string) *HostBuilder {
hb.Host.NetworkConfig = aiv1beta1.NetConfig{
Raw: []byte(raw),
}
return hb
}

func (hb *HostBuilder) deviceHint() *HostBuilder {
hb.Host.RootDeviceHints = baremetal.RootDeviceHints{
DeviceName: "/dev/sda",
Expand All @@ -753,3 +954,25 @@ func iface(name string, mac string) *InterfacetBuilder {
func (ib *InterfacetBuilder) build() *aiv1beta1.Interface {
return &ib.Interface
}

func getInstallConfigWithMismatchedNetworkConfig() *agentAsset.OptionalInstallConfig {
a := getInstallConfigSingleHost()
a.Config.Platform.BareMetal.Hosts[0].NetworkConfig = &apiextv1.JSON{
Raw: []byte(installNetworkConfigOne),
}
return a
}

func getAgentConfigEmptyInterfaceName() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces[0].Name = ""
a.Config.Hosts[0].NetworkConfig.Raw = []byte(agentNetworkConfigOne)
return a
}

func getAgentConfigMalformedNetworkConfig() *AgentConfig {
a := getAgentConfigSingleHost()
a.Config.Hosts[0].Interfaces[0].Name = "eth0"
a.Config.Hosts[0].NetworkConfig.Raw = []byte("not: valid: yaml: [[")
return a
}