Skip to content

Commit e8073ff

Browse files
committed
Add explicit 'name' field to network link types
The 'name' field is now only included in the rendered output when explicitly set. Example usage: ```yaml networkData: links: ethernets: - id: eth0 name: enp1s0 # cloud-init will rename interface to enp1s0 macAddress: fromHostInterface: eth0 ``` Signed-off-by: s3rj1k <[email protected]>
1 parent 7c7331e commit e8073ff

File tree

4 files changed

+92
-35
lines changed

4 files changed

+92
-35
lines changed

api/v1beta1/metal3datatemplate_types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ type NetworkDataLinkEthernet struct {
240240
// Id is the ID of the interface (used for naming)
241241
Id string `json:"id"` //nolint:stylecheck,revive
242242

243+
// Name is the interface name to be used by cloud-init. When combined with
244+
// MACAddress, cloud-init will rename the interface matching the MAC to this name.
245+
// When MACAddress is omitted, cloud-init will use this name directly.
246+
// +kubebuilder:validation:MaxLength=15
247+
// +kubebuilder:validation:Pattern=`^[a-zA-Z][a-zA-Z0-9._-]*$`
248+
// +optional
249+
Name string `json:"name,omitempty"`
250+
243251
// +kubebuilder:default=1500
244252
// +kubebuilder:validation:Maximum=9000
245253
// MTU is the MTU of the interface
@@ -266,6 +274,14 @@ type NetworkDataLinkBond struct {
266274
// Id is the ID of the interface (used for naming)
267275
Id string `json:"id"` //nolint:stylecheck,revive
268276

277+
// Name is the interface name to be used by cloud-init. When combined with
278+
// MACAddress, cloud-init will rename the interface matching the MAC to this name.
279+
// When MACAddress is omitted, cloud-init will use this name directly.
280+
// +kubebuilder:validation:MaxLength=15
281+
// +kubebuilder:validation:Pattern=`^[a-zA-Z][a-zA-Z0-9._-]*$`
282+
// +optional
283+
Name string `json:"name,omitempty"`
284+
269285
// +kubebuilder:default=1500
270286
// +kubebuilder:validation:Maximum=9000
271287
// MTU is the MTU of the interface
@@ -296,6 +312,14 @@ type NetworkDataLinkVlan struct {
296312
// Id is the ID of the interface (used for naming)
297313
Id string `json:"id"` //nolint:stylecheck,revive
298314

315+
// Name is the interface name to be used by cloud-init. When combined with
316+
// MACAddress, cloud-init will rename the interface matching the MAC to this name.
317+
// When MACAddress is omitted, cloud-init will use this name directly.
318+
// +kubebuilder:validation:MaxLength=15
319+
// +kubebuilder:validation:Pattern=`^[a-zA-Z][a-zA-Z0-9._-]*$`
320+
// +optional
321+
Name string `json:"name,omitempty"`
322+
299323
// +kubebuilder:default=1500
300324
// +kubebuilder:validation:Maximum=9000
301325
// MTU is the MTU of the interface

baremetal/metal3data_manager.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,15 @@ func renderNetworkLinks(networkLinks infrav1.NetworkDataLink,
10681068
entry := map[string]any{
10691069
"type": "bond",
10701070
"id": link.Id,
1071-
"name": link.Id,
10721071
"mtu": link.MTU,
10731072
"bond_mode": link.BondMode,
10741073
"bond_xmit_hash_policy": link.BondXmitHashPolicy,
10751074
"bond_links": link.BondLinks,
10761075
}
1076+
// Name is optional - if provided, cloud-init will use it for interface renaming
1077+
if link.Name != "" {
1078+
entry["name"] = link.Name
1079+
}
10771080
// MAC address is optional - if provided, include it for interface matching
10781081
if link.MACAddress != nil {
10791082
macAddress, err := getLinkMacAddress(link.MACAddress, m3m, machine, bmh)
@@ -1107,9 +1110,12 @@ func renderNetworkLinks(networkLinks infrav1.NetworkDataLink,
11071110
entry := map[string]any{
11081111
"type": link.Type,
11091112
"id": link.Id,
1110-
"name": link.Id,
11111113
"mtu": link.MTU,
11121114
}
1115+
// Name is optional - if provided, cloud-init will use it for interface renaming
1116+
if link.Name != "" {
1117+
entry["name"] = link.Name
1118+
}
11131119
// MAC address is optional - if provided, include it for interface matching/renaming
11141120
if link.MACAddress != nil {
11151121
macAddress, err := getLinkMacAddress(link.MACAddress, m3m, machine, bmh)
@@ -1126,11 +1132,14 @@ func renderNetworkLinks(networkLinks infrav1.NetworkDataLink,
11261132
entry := map[string]any{
11271133
"type": "vlan",
11281134
"id": link.Id,
1129-
"name": link.Id,
11301135
"mtu": link.MTU,
11311136
"vlan_id": link.VlanID,
11321137
"vlan_link": link.VlanLink,
11331138
}
1139+
// Name is optional - if provided, cloud-init will use it for interface renaming
1140+
if link.Name != "" {
1141+
entry["name"] = link.Name
1142+
}
11341143
// MAC address is optional for VLANs
11351144
if link.MACAddress != nil {
11361145
macAddress, err := getLinkMacAddress(link.MACAddress, m3m, machine, bmh)

baremetal/metal3data_manager_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ var _ = Describe("Metal3Data manager", func() {
453453
},
454454
expectReady: true,
455455
expectedMetadata: ptr.To(fmt.Sprintf("String-1: String-1\nproviderid: %s\n", providerid)),
456-
expectedNetworkData: ptr.To("links:\n- ethernet_mac_address: 12:34:56:78:9A:BC\n id: eth0\n mtu: 1500\n name: eth0\n type: phy\nnetworks: []\nservices: []\n"),
456+
expectedNetworkData: ptr.To("links:\n- ethernet_mac_address: 12:34:56:78:9A:BC\n id: eth0\n mtu: 1500\n type: phy\nnetworks: []\nservices: []\n"),
457457
}),
458458
Entry("No Machine OwnerRef on M3M", testCaseCreateSecrets{
459459
m3d: &infrav1.Metal3Data{
@@ -2633,7 +2633,6 @@ var _ = Describe("Metal3Data manager", func() {
26332633
map[any]any{
26342634
"type": "phy",
26352635
"id": "eth0",
2636-
"name": "eth0",
26372636
"mtu": 1500,
26382637
"ethernet_mac_address": "12:34:56:78:9A:BC",
26392638
},
@@ -2813,7 +2812,6 @@ var _ = Describe("Metal3Data manager", func() {
28132812
map[string]any{
28142813
"type": "phy",
28152814
"id": "eth0",
2816-
"name": "eth0",
28172815
"mtu": 1500,
28182816
"ethernet_mac_address": "12:34:56:78:9A:BC",
28192817
},
@@ -2857,7 +2855,6 @@ var _ = Describe("Metal3Data manager", func() {
28572855
map[string]any{
28582856
"type": "bond",
28592857
"id": "bond0",
2860-
"name": "bond0",
28612858
"mtu": 1500,
28622859
"ethernet_mac_address": "12:34:56:78:9A:BC",
28632860
"bond_mode": "802.3ad",
@@ -2907,7 +2904,6 @@ var _ = Describe("Metal3Data manager", func() {
29072904
"vlan_link": "eth0",
29082905
"type": "vlan",
29092906
"id": "bond0",
2910-
"name": "bond0",
29112907
"mtu": 1500,
29122908
},
29132909
},
@@ -2949,9 +2945,8 @@ var _ = Describe("Metal3Data manager", func() {
29492945
map[string]any{
29502946
"type": "phy",
29512947
"id": "eth0",
2952-
"name": "eth0",
29532948
"mtu": 1500,
2954-
// no ethernet_mac_address field
2949+
// no ethernet_mac_address field, no name field
29552950
},
29562951
},
29572952
}),
@@ -2971,12 +2966,11 @@ var _ = Describe("Metal3Data manager", func() {
29712966
map[string]any{
29722967
"type": "bond",
29732968
"id": "bond0",
2974-
"name": "bond0",
29752969
"mtu": 1500,
29762970
"bond_mode": "802.3ad",
29772971
"bond_xmit_hash_policy": "",
29782972
"bond_links": []string{"eth0"},
2979-
// no ethernet_mac_address field
2973+
// no ethernet_mac_address field, no name field
29802974
},
29812975
},
29822976
}),
@@ -2996,22 +2990,22 @@ var _ = Describe("Metal3Data manager", func() {
29962990
map[string]any{
29972991
"type": "vlan",
29982992
"id": "vlan100",
2999-
"name": "vlan100",
30002993
"mtu": 1500,
30012994
"vlan_id": 100,
30022995
"vlan_link": "eth0",
3003-
// no vlan_mac_address field
2996+
// no vlan_mac_address field, no name field
30042997
},
30052998
},
30062999
}),
3007-
// Tests for interface renaming - verifies 'name' field is set from 'id'
3000+
// Tests for interface renaming - verifies 'name' field is set when Name is explicitly specified
30083001
// This enables cloud-init to rename interfaces based on MAC address matching
3009-
Entry("Ethernet interface rename - name field set from id", testCaseRenderNetworkLinks{
3002+
Entry("Ethernet interface rename - name field explicitly set", testCaseRenderNetworkLinks{
30103003
links: infrav1.NetworkDataLink{
30113004
Ethernets: []infrav1.NetworkDataLinkEthernet{
30123005
{
30133006
Type: "phy",
3014-
Id: "enp1s0", // desired interface name
3007+
Id: "eth0",
3008+
Name: "enp1s0", // desired interface name for cloud-init rename
30153009
MTU: 1500,
30163010
MACAddress: &infrav1.NetworkLinkEthernetMac{
30173011
String: ptr.To("AA:BB:CC:DD:EE:FF"),
@@ -3022,20 +3016,21 @@ var _ = Describe("Metal3Data manager", func() {
30223016
expectedOutput: []any{
30233017
map[string]any{
30243018
"type": "phy",
3025-
"id": "enp1s0",
3026-
"name": "enp1s0", // name must equal id for cloud-init rename
3019+
"id": "eth0",
3020+
"name": "enp1s0", // name field set from explicit Name
30273021
"mtu": 1500,
30283022
"ethernet_mac_address": "AA:BB:CC:DD:EE:FF",
30293023
},
30303024
},
30313025
}),
3032-
Entry("Bond interface rename - name field set from id", testCaseRenderNetworkLinks{
3026+
Entry("Bond interface rename - name field explicitly set", testCaseRenderNetworkLinks{
30333027
links: infrav1.NetworkDataLink{
30343028
Bonds: []infrav1.NetworkDataLinkBond{
30353029
{
30363030
BondMode: "active-backup",
30373031
BondXmitHashPolicy: "layer2",
3038-
Id: "bond-mgmt", // custom bond name
3032+
Id: "bond0",
3033+
Name: "bond-mgmt", // custom bond name for cloud-init rename
30393034
MTU: 9000,
30403035
MACAddress: &infrav1.NetworkLinkEthernetMac{
30413036
String: ptr.To("11:22:33:44:55:66"),
@@ -3047,8 +3042,8 @@ var _ = Describe("Metal3Data manager", func() {
30473042
expectedOutput: []any{
30483043
map[string]any{
30493044
"type": "bond",
3050-
"id": "bond-mgmt",
3051-
"name": "bond-mgmt", // name must equal id for cloud-init rename
3045+
"id": "bond0",
3046+
"name": "bond-mgmt", // name field set from explicit Name
30523047
"mtu": 9000,
30533048
"ethernet_mac_address": "11:22:33:44:55:66",
30543049
"bond_mode": "active-backup",
@@ -3057,12 +3052,13 @@ var _ = Describe("Metal3Data manager", func() {
30573052
},
30583053
},
30593054
}),
3060-
Entry("Vlan interface rename - name field set from id", testCaseRenderNetworkLinks{
3055+
Entry("Vlan interface rename - name field explicitly set", testCaseRenderNetworkLinks{
30613056
links: infrav1.NetworkDataLink{
30623057
Vlans: []infrav1.NetworkDataLinkVlan{
30633058
{
30643059
VlanID: 100,
3065-
Id: "vlan-storage", // custom vlan name
3060+
Id: "vlan100",
3061+
Name: "vlan-storage", // custom vlan name for cloud-init rename
30663062
MTU: 9000,
30673063
VlanLink: "bond0",
30683064
MACAddress: &infrav1.NetworkLinkEthernetMac{
@@ -3074,8 +3070,8 @@ var _ = Describe("Metal3Data manager", func() {
30743070
expectedOutput: []any{
30753071
map[string]any{
30763072
"type": "vlan",
3077-
"id": "vlan-storage",
3078-
"name": "vlan-storage", // name must equal id for cloud-init rename
3073+
"id": "vlan100",
3074+
"name": "vlan-storage", // name field set from explicit Name
30793075
"mtu": 9000,
30803076
"vlan_mac_address": "AA:BB:CC:DD:EE:00",
30813077
"vlan_id": 100,
@@ -3088,15 +3084,17 @@ var _ = Describe("Metal3Data manager", func() {
30883084
Ethernets: []infrav1.NetworkDataLinkEthernet{
30893085
{
30903086
Type: "phy",
3091-
Id: "mgmt0",
3087+
Id: "eth0",
3088+
Name: "mgmt0", // custom name different from id
30923089
MTU: 1500,
30933090
MACAddress: &infrav1.NetworkLinkEthernetMac{
30943091
String: ptr.To("AA:BB:CC:DD:EE:01"),
30953092
},
30963093
},
30973094
{
30983095
Type: "phy",
3099-
Id: "storage0",
3096+
Id: "eth1",
3097+
Name: "storage0", // custom name different from id
31003098
MTU: 9000,
31013099
MACAddress: &infrav1.NetworkLinkEthernetMac{
31023100
String: ptr.To("AA:BB:CC:DD:EE:02"),
@@ -3106,7 +3104,8 @@ var _ = Describe("Metal3Data manager", func() {
31063104
Bonds: []infrav1.NetworkDataLinkBond{
31073105
{
31083106
BondMode: "802.3ad",
3109-
Id: "bond-data",
3107+
Id: "bond0",
3108+
Name: "bond-data", // custom name different from id
31103109
MTU: 9000,
31113110
MACAddress: &infrav1.NetworkLinkEthernetMac{
31123111
String: ptr.To("AA:BB:CC:DD:EE:03"),
@@ -3117,7 +3116,8 @@ var _ = Describe("Metal3Data manager", func() {
31173116
Vlans: []infrav1.NetworkDataLinkVlan{
31183117
{
31193118
VlanID: 200,
3120-
Id: "tenant-net",
3119+
Id: "vlan200",
3120+
Name: "tenant-net", // custom name different from id
31213121
MTU: 1500,
31223122
VlanLink: "bond-data",
31233123
MACAddress: &infrav1.NetworkLinkEthernetMac{
@@ -3129,7 +3129,7 @@ var _ = Describe("Metal3Data manager", func() {
31293129
expectedOutput: []any{
31303130
map[string]any{
31313131
"type": "bond",
3132-
"id": "bond-data",
3132+
"id": "bond0",
31333133
"name": "bond-data",
31343134
"mtu": 9000,
31353135
"ethernet_mac_address": "AA:BB:CC:DD:EE:03",
@@ -3139,21 +3139,21 @@ var _ = Describe("Metal3Data manager", func() {
31393139
},
31403140
map[string]any{
31413141
"type": "phy",
3142-
"id": "mgmt0",
3142+
"id": "eth0",
31433143
"name": "mgmt0",
31443144
"mtu": 1500,
31453145
"ethernet_mac_address": "AA:BB:CC:DD:EE:01",
31463146
},
31473147
map[string]any{
31483148
"type": "phy",
3149-
"id": "storage0",
3149+
"id": "eth1",
31503150
"name": "storage0",
31513151
"mtu": 9000,
31523152
"ethernet_mac_address": "AA:BB:CC:DD:EE:02",
31533153
},
31543154
map[string]any{
31553155
"type": "vlan",
3156-
"id": "tenant-net",
3156+
"id": "vlan200",
31573157
"name": "tenant-net",
31583158
"mtu": 1500,
31593159
"vlan_mac_address": "AA:BB:CC:DD:EE:04",

config/crd/bases/infrastructure.cluster.x-k8s.io_metal3datatemplates.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ spec:
429429
description: MTU is the MTU of the interface
430430
maximum: 9000
431431
type: integer
432+
name:
433+
description: |-
434+
Name is the interface name to be used by cloud-init. When combined with
435+
MACAddress, cloud-init will rename the interface matching the MAC to this name.
436+
When MACAddress is omitted, cloud-init will use this name directly.
437+
maxLength: 15
438+
pattern: ^[a-zA-Z][a-zA-Z0-9._-]*$
439+
type: string
432440
parameters:
433441
additionalProperties:
434442
x-kubernetes-preserve-unknown-fields: true
@@ -492,6 +500,14 @@ spec:
492500
description: MTU is the MTU of the interface
493501
maximum: 9000
494502
type: integer
503+
name:
504+
description: |-
505+
Name is the interface name to be used by cloud-init. When combined with
506+
MACAddress, cloud-init will rename the interface matching the MAC to this name.
507+
When MACAddress is omitted, cloud-init will use this name directly.
508+
maxLength: 15
509+
pattern: ^[a-zA-Z][a-zA-Z0-9._-]*$
510+
type: string
495511
type:
496512
description: |-
497513
Type is the type of the ethernet link. It can be one of:
@@ -564,6 +580,14 @@ spec:
564580
description: MTU is the MTU of the interface
565581
maximum: 9000
566582
type: integer
583+
name:
584+
description: |-
585+
Name is the interface name to be used by cloud-init. When combined with
586+
MACAddress, cloud-init will rename the interface matching the MAC to this name.
587+
When MACAddress is omitted, cloud-init will use this name directly.
588+
maxLength: 15
589+
pattern: ^[a-zA-Z][a-zA-Z0-9._-]*$
590+
type: string
567591
vlanID:
568592
description: VlanID is the Vlan ID
569593
maximum: 4096

0 commit comments

Comments
 (0)