Skip to content

Commit 8e65951

Browse files
Add multiple nics support to nodes
1 parent 312a3c9 commit 8e65951

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

api/v1beta3/cloudstackmachine_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ type CloudStackMachineSpec struct {
7171
// +optional
7272
Affinity string `json:"affinity,omitempty"`
7373

74+
// The primary network interface (overrides zone.network)
75+
// +optional
76+
Network *NetworkSpec `json:"network,omitempty"`
77+
78+
// Additional networks (attached as secondary NICs)
79+
// +optional
80+
ExtraNetworks []NetworkSpec `json:"extraNetworks,omitempty"`
81+
7482
// Mutually exclusive parameter with AffinityGroupIDs.
7583
// Is a reference to a CloudStack affinity group CRD.
7684
// +optional
@@ -91,6 +99,12 @@ type CloudStackMachineSpec struct {
9199
UncompressedUserData *bool `json:"uncompressedUserData,omitempty"`
92100
}
93101

102+
type NetworkSpec struct {
103+
Name string `json:"name"`
104+
// +optional
105+
IP string `json:"ip,omitempty"`
106+
}
107+
94108
func (c *CloudStackMachine) CompressUserdata() bool {
95109
return c.Spec.UncompressedUserData == nil || !*c.Spec.UncompressedUserData
96110
}

pkg/cloud/instance.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ func (c *client) CheckLimits(
302302
return nil
303303
}
304304

305+
func (c *client) resolveNetworkIDByName(name string) (string, error) {
306+
netDetails, count, err := c.cs.Network.GetNetworkByName(name, cloudstack.WithProject(c.user.Project.ID))
307+
if err != nil || count != 1 {
308+
return "", fmt.Errorf("unable to resolve network ID for %s: %v", name, err)
309+
}
310+
return netDetails.Id, nil
311+
}
312+
305313
// DeployVM will create a VM instance,
306314
// and sets the infrastructure machine spec and status accordingly.
307315
func (c *client) DeployVM(
@@ -322,7 +330,45 @@ func (c *client) DeployVM(
322330
}
323331

324332
p := c.cs.VirtualMachine.NewDeployVirtualMachineParams(offering.Id, templateID, fd.Spec.Zone.ID)
325-
p.SetNetworkids([]string{fd.Spec.Zone.Network.ID})
333+
if csMachine.Spec.Network == nil && len(csMachine.Spec.ExtraNetworks) == 0 && fd.Spec.Zone.Network.ID != "" {
334+
// No explicit NICs; fallback to single default network ID
335+
p.SetNetworkids([]string{fd.Spec.Zone.Network.ID})
336+
} else {
337+
// Build iptonetworklist for multiple NICs
338+
iptonetworklist := []map[string]string{}
339+
340+
// Primary NIC
341+
if csMachine.Spec.Network != nil {
342+
id, err := c.resolveNetworkIDByName(csMachine.Spec.Network.Name)
343+
if err != nil {
344+
return err
345+
}
346+
entry := map[string]string{"networkid": id}
347+
if csMachine.Spec.Network.IP != "" {
348+
entry["ip"] = csMachine.Spec.Network.IP
349+
}
350+
iptonetworklist = append(iptonetworklist, entry)
351+
} else if fd.Spec.Zone.Network.ID != "" {
352+
iptonetworklist = append(iptonetworklist, map[string]string{
353+
"networkid": fd.Spec.Zone.Network.ID,
354+
})
355+
}
356+
357+
// Extra NICs
358+
for _, extra := range csMachine.Spec.ExtraNetworks {
359+
id, err := c.resolveNetworkIDByName(extra.Name)
360+
if err != nil {
361+
return err
362+
}
363+
entry := map[string]string{"networkid": id}
364+
if extra.IP != "" {
365+
entry["ip"] = extra.IP
366+
}
367+
iptonetworklist = append(iptonetworklist, entry)
368+
}
369+
370+
p.SetIptonetworklist(iptonetworklist)
371+
}
326372
setIfNotEmpty(csMachine.Name, p.SetName)
327373
setIfNotEmpty(capiMachine.Name, p.SetDisplayname)
328374
setIfNotEmpty(diskOfferingID, p.SetDiskofferingid)

0 commit comments

Comments
 (0)