Skip to content

Commit 0bf97a4

Browse files
committed
fix(proxmox): adding lxc support
1 parent 64b63df commit 0bf97a4

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

internal/constants/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ const (
127127

128128
DeviceRoleServer = "Server"
129129
DeviceRoleServerColor = "00add8"
130+
131+
DeviceRoleContainer = "Container"
132+
DeviceRoleContainerColor = "0db7ed"
130133
)
131134

132135
// Constants used for variables in our contexts.

internal/netbox/objects/virtualization.go

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type VM struct {
9696
Memory int `json:"memory,omitempty"`
9797
// Disk is the amount of disk space allocated to the virtual machine in GB.
9898
Disk int `json:"disk,omitempty"`
99+
// Role of the virtual machine.
100+
Role *DeviceRole `json:"role,omitempty"`
99101

100102
// Additional Comments
101103
Comments string `json:"comments,omitempty"`

internal/source/proxmox/proxmox.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ProxmoxSource struct {
2424
NodeNetworks map[string][]*proxmox.NodeNetwork // NodeName -> NodeNetworks (interfaces)
2525
Vms map[string][]*proxmox.VirtualMachine // NodeName -> VirtualMachines
2626
VMNetworks map[string][]*proxmox.AgentNetworkIface // VMName -> NetworkDevices
27+
Containers map[string][]*proxmox.Container // NodeName -> Contatiners
2728

2829
// Netbox related data for easier access. Initialized in sync functions.
2930
NetboxCluster *objects.Cluster
@@ -102,6 +103,7 @@ func (ps *ProxmoxSource) Sync(nbi *inventory.NetboxInventory) error {
102103
ps.syncCluster,
103104
ps.syncNodes,
104105
ps.syncVMs,
106+
ps.syncContainers,
105107
}
106108
for _, syncFunc := range syncFunctions {
107109
startTime := time.Now()

internal/source/proxmox/proxmox_init.go

+23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (ps *ProxmoxSource) initNodes(ctx context.Context, c *proxmox.Client) error
2626
ps.Nodes = make([]*proxmox.Node, 0, len(nodes))
2727
ps.NodeNetworks = make(map[string][]*proxmox.NodeNetwork, len(nodes))
2828
ps.Vms = make(map[string][]*proxmox.VirtualMachine, len(nodes))
29+
ps.Containers = make(map[string][]*proxmox.Container, len(nodes))
2930
for _, node := range nodes {
3031
node, err := c.Node(ctx, node.Node)
3132
if err != nil {
@@ -42,6 +43,11 @@ func (ps *ProxmoxSource) initNodes(ctx context.Context, c *proxmox.Client) error
4243
if err != nil {
4344
return fmt.Errorf("init nodeVMs: %s", err)
4445
}
46+
47+
err = ps.initContainers(ctx, node)
48+
if err != nil {
49+
return fmt.Errorf("init node containers: %s", err)
50+
}
4551
}
4652
return nil
4753
}
@@ -79,3 +85,20 @@ func (ps *ProxmoxSource) initNodeVMs(ctx context.Context, node *proxmox.Node) er
7985
}
8086
return nil
8187
}
88+
89+
// Helper function for initNodes. It collects all containers for given node.
90+
func (ps *ProxmoxSource) initContainers(ctx context.Context, node *proxmox.Node) error {
91+
containers, err := node.Containers(ctx)
92+
if err != nil {
93+
return err
94+
}
95+
ps.Containers[node.Name] = make([]*proxmox.Container, 0, len(containers))
96+
// ps.VMNetworks = make(map[string][]*proxmox.AgentNetworkIface, len(containers))
97+
for _, container := range containers {
98+
ps.Containers[node.Name] = append(ps.Containers[node.Name], container)
99+
// ifaces, _ := container.AgentGetNetworkIFaces(ctx)
100+
// ps.VMNetworks[container.Name] = make([]*proxmox.AgentNetworkIface, 0, len(ifaces))
101+
// ps.VMNetworks[container.Name] = append(ps.VMNetworks[container.Name], ifaces...)
102+
}
103+
return nil
104+
}

internal/source/proxmox/proxmox_sync.go

+59
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,62 @@ func (ps *ProxmoxSource) syncVMNetworks(nbi *inventory.NetboxInventory, nbVM *ob
289289
}
290290
return nil
291291
}
292+
293+
// Function that synces proxmox containers to the netbox inventory.
294+
func (ps *ProxmoxSource) syncContainers(nbi *inventory.NetboxInventory) error {
295+
if len(ps.Containers) > 0 {
296+
// Create container role
297+
containerRole, err := nbi.AddDeviceRole(ps.Ctx, &objects.DeviceRole{
298+
Name: constants.DeviceRoleContainer,
299+
Slug: utils.Slugify(constants.DeviceRoleContainer),
300+
Color: constants.DeviceRoleContainerColor,
301+
VMRole: true,
302+
})
303+
if err != nil {
304+
return fmt.Errorf("create container role: %s", err)
305+
}
306+
for nodeName, containers := range ps.Containers {
307+
nbHost := ps.NetboxNodes[nodeName]
308+
for _, container := range containers {
309+
// Determine Container status
310+
containerStatus := &objects.VMStatusActive
311+
if container.Status == "stopped" {
312+
containerStatus = &objects.VMStatusOffline
313+
}
314+
// Determine Container tenant
315+
vmTenant, err := common.MatchVMToTenant(ps.Ctx, nbi, container.Name, ps.VMTenantRelations)
316+
if err != nil {
317+
return fmt.Errorf("match vm to tenant: %s", err)
318+
}
319+
_, err = nbi.AddVM(ps.Ctx, &objects.VM{
320+
NetboxObject: objects.NetboxObject{
321+
Tags: ps.SourceTags,
322+
CustomFields: map[string]string{
323+
constants.CustomFieldSourceName: ps.SourceConfig.Name,
324+
constants.CustomFieldSourceIDName: fmt.Sprintf("%d", container.VMID),
325+
},
326+
},
327+
Host: nbHost,
328+
Role: containerRole,
329+
Cluster: ps.NetboxCluster, // Default single proxmox cluster
330+
Tenant: vmTenant,
331+
VCPUs: float32(container.CPUs),
332+
Memory: int(container.MaxMem / constants.MiB), // Memory is in MB
333+
Disk: int(container.MaxDisk / constants.GiB), // Disk is in GB
334+
Site: nbHost.Site,
335+
Name: container.Name,
336+
Status: containerStatus,
337+
})
338+
if err != nil {
339+
return fmt.Errorf("new vm: %s", err)
340+
}
341+
342+
// err = ps.syncContainerNetworks(nbi, nbContainer)
343+
// if err != nil {
344+
// return fmt.Errorf("sync container networks: %s", err)
345+
// }
346+
}
347+
}
348+
}
349+
return nil
350+
}

0 commit comments

Comments
 (0)