Skip to content

Commit

Permalink
fix(proxmox): adding lxc support
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed Mar 23, 2024
1 parent 64b63df commit 0bf97a4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ const (

DeviceRoleServer = "Server"
DeviceRoleServerColor = "00add8"

DeviceRoleContainer = "Container"
DeviceRoleContainerColor = "0db7ed"
)

// Constants used for variables in our contexts.
Expand Down
2 changes: 2 additions & 0 deletions internal/netbox/objects/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type VM struct {
Memory int `json:"memory,omitempty"`
// Disk is the amount of disk space allocated to the virtual machine in GB.
Disk int `json:"disk,omitempty"`
// Role of the virtual machine.
Role *DeviceRole `json:"role,omitempty"`

// Additional Comments
Comments string `json:"comments,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions internal/source/proxmox/proxmox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ProxmoxSource struct {
NodeNetworks map[string][]*proxmox.NodeNetwork // NodeName -> NodeNetworks (interfaces)
Vms map[string][]*proxmox.VirtualMachine // NodeName -> VirtualMachines
VMNetworks map[string][]*proxmox.AgentNetworkIface // VMName -> NetworkDevices
Containers map[string][]*proxmox.Container // NodeName -> Contatiners

// Netbox related data for easier access. Initialized in sync functions.
NetboxCluster *objects.Cluster
Expand Down Expand Up @@ -102,6 +103,7 @@ func (ps *ProxmoxSource) Sync(nbi *inventory.NetboxInventory) error {
ps.syncCluster,
ps.syncNodes,
ps.syncVMs,
ps.syncContainers,

Check warning on line 106 in internal/source/proxmox/proxmox.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox.go#L106

Added line #L106 was not covered by tests
}
for _, syncFunc := range syncFunctions {
startTime := time.Now()
Expand Down
23 changes: 23 additions & 0 deletions internal/source/proxmox/proxmox_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (ps *ProxmoxSource) initNodes(ctx context.Context, c *proxmox.Client) error
ps.Nodes = make([]*proxmox.Node, 0, len(nodes))
ps.NodeNetworks = make(map[string][]*proxmox.NodeNetwork, len(nodes))
ps.Vms = make(map[string][]*proxmox.VirtualMachine, len(nodes))
ps.Containers = make(map[string][]*proxmox.Container, len(nodes))

Check warning on line 29 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L29

Added line #L29 was not covered by tests
for _, node := range nodes {
node, err := c.Node(ctx, node.Node)
if err != nil {
Expand All @@ -42,6 +43,11 @@ func (ps *ProxmoxSource) initNodes(ctx context.Context, c *proxmox.Client) error
if err != nil {
return fmt.Errorf("init nodeVMs: %s", err)
}

err = ps.initContainers(ctx, node)
if err != nil {
return fmt.Errorf("init node containers: %s", err)

Check warning on line 49 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L47-L49

Added lines #L47 - L49 were not covered by tests
}
}
return nil
}
Expand Down Expand Up @@ -79,3 +85,20 @@ func (ps *ProxmoxSource) initNodeVMs(ctx context.Context, node *proxmox.Node) er
}
return nil
}

// Helper function for initNodes. It collects all containers for given node.
func (ps *ProxmoxSource) initContainers(ctx context.Context, node *proxmox.Node) error {
containers, err := node.Containers(ctx)
if err != nil {
return err

Check warning on line 93 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L90-L93

Added lines #L90 - L93 were not covered by tests
}
ps.Containers[node.Name] = make([]*proxmox.Container, 0, len(containers))

Check warning on line 95 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L95

Added line #L95 was not covered by tests
// ps.VMNetworks = make(map[string][]*proxmox.AgentNetworkIface, len(containers))
for _, container := range containers {
ps.Containers[node.Name] = append(ps.Containers[node.Name], container)

Check warning on line 98 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L97-L98

Added lines #L97 - L98 were not covered by tests
// ifaces, _ := container.AgentGetNetworkIFaces(ctx)
// ps.VMNetworks[container.Name] = make([]*proxmox.AgentNetworkIface, 0, len(ifaces))
// ps.VMNetworks[container.Name] = append(ps.VMNetworks[container.Name], ifaces...)
}
return nil

Check warning on line 103 in internal/source/proxmox/proxmox_init.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_init.go#L103

Added line #L103 was not covered by tests
}
59 changes: 59 additions & 0 deletions internal/source/proxmox/proxmox_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,62 @@ func (ps *ProxmoxSource) syncVMNetworks(nbi *inventory.NetboxInventory, nbVM *ob
}
return nil
}

// Function that synces proxmox containers to the netbox inventory.
func (ps *ProxmoxSource) syncContainers(nbi *inventory.NetboxInventory) error {
if len(ps.Containers) > 0 {

Check warning on line 295 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L294-L295

Added lines #L294 - L295 were not covered by tests
// Create container role
containerRole, err := nbi.AddDeviceRole(ps.Ctx, &objects.DeviceRole{
Name: constants.DeviceRoleContainer,
Slug: utils.Slugify(constants.DeviceRoleContainer),
Color: constants.DeviceRoleContainerColor,
VMRole: true,
})
if err != nil {
return fmt.Errorf("create container role: %s", err)

Check warning on line 304 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L297-L304

Added lines #L297 - L304 were not covered by tests
}
for nodeName, containers := range ps.Containers {
nbHost := ps.NetboxNodes[nodeName]
for _, container := range containers {

Check warning on line 308 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L306-L308

Added lines #L306 - L308 were not covered by tests
// Determine Container status
containerStatus := &objects.VMStatusActive
if container.Status == "stopped" {
containerStatus = &objects.VMStatusOffline

Check warning on line 312 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L310-L312

Added lines #L310 - L312 were not covered by tests
}
// Determine Container tenant
vmTenant, err := common.MatchVMToTenant(ps.Ctx, nbi, container.Name, ps.VMTenantRelations)
if err != nil {
return fmt.Errorf("match vm to tenant: %s", err)

Check warning on line 317 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L315-L317

Added lines #L315 - L317 were not covered by tests
}
_, err = nbi.AddVM(ps.Ctx, &objects.VM{
NetboxObject: objects.NetboxObject{
Tags: ps.SourceTags,
CustomFields: map[string]string{
constants.CustomFieldSourceName: ps.SourceConfig.Name,
constants.CustomFieldSourceIDName: fmt.Sprintf("%d", container.VMID),
},
},
Host: nbHost,
Role: containerRole,
Cluster: ps.NetboxCluster, // Default single proxmox cluster
Tenant: vmTenant,
VCPUs: float32(container.CPUs),
Memory: int(container.MaxMem / constants.MiB), // Memory is in MB
Disk: int(container.MaxDisk / constants.GiB), // Disk is in GB
Site: nbHost.Site,
Name: container.Name,
Status: containerStatus,
})
if err != nil {
return fmt.Errorf("new vm: %s", err)

Check warning on line 339 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L319-L339

Added lines #L319 - L339 were not covered by tests
}

// err = ps.syncContainerNetworks(nbi, nbContainer)
// if err != nil {
// return fmt.Errorf("sync container networks: %s", err)
// }
}
}
}
return nil

Check warning on line 349 in internal/source/proxmox/proxmox_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/source/proxmox/proxmox_sync.go#L349

Added line #L349 was not covered by tests
}

0 comments on commit 0bf97a4

Please sign in to comment.