Skip to content

Commit 221d29c

Browse files
authored
feat: relationships and fields needed by FinOps tree-policies (#62)
1 parent 251726e commit 221d29c

7 files changed

Lines changed: 100 additions & 3 deletions

File tree

pkg/tree/aws/ecs/task_definition.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ type EnvironmentVariable struct {
2929
Value value.String `tree:"value"`
3030
}
3131

32-
type TaskDefinitionRelationships struct {
33-
Services []*Service
34-
}
32+
type TaskDefinitionRelationships struct{}
3533

3634
type InferenceAccelerator struct {
3735
DeviceType value.Value[InferenceAcceleratorDeviceType] `tree:"device_type"`

pkg/tree/azure/compute/compute.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,82 @@ type Compute struct {
1313
BatchPools []BatchPool `tree:"batch_pools"`
1414
DiskAttachments []DiskAttachment `tree:"disk_attachments"`
1515
}
16+
17+
// PostProcess links each managed disk to the VM it is attached to via
18+
// DiskAttachment entries or via the VM's inline OS/data disk references.
19+
func (c *Compute) PostProcess() {
20+
diskByID := make(map[string]*ManagedDisk)
21+
for i := range c.ManagedDisks {
22+
if id := c.ManagedDisks[i].ID; id != "" {
23+
diskByID[id] = &c.ManagedDisks[i]
24+
}
25+
}
26+
27+
link := func(managedDiskID string, set func(*ManagedDisk)) {
28+
if managedDiskID == "" {
29+
return
30+
}
31+
if disk, ok := diskByID[managedDiskID]; ok {
32+
set(disk)
33+
}
34+
}
35+
36+
for _, att := range c.DiskAttachments {
37+
vmID := att.VirtualMachineID.Value()
38+
link(att.ManagedDiskID.Value(), func(disk *ManagedDisk) {
39+
for i := range c.VirtualMachines {
40+
if c.VirtualMachines[i].ID == vmID {
41+
disk.Relationships.VirtualMachine = &c.VirtualMachines[i]
42+
return
43+
}
44+
}
45+
for i := range c.LinuxVirtualMachines {
46+
if c.LinuxVirtualMachines[i].ID == vmID {
47+
disk.Relationships.LinuxVirtualMachine = &c.LinuxVirtualMachines[i]
48+
return
49+
}
50+
}
51+
for i := range c.WindowsVirtualMachines {
52+
if c.WindowsVirtualMachines[i].ID == vmID {
53+
disk.Relationships.WindowsVirtualMachine = &c.WindowsVirtualMachines[i]
54+
return
55+
}
56+
}
57+
})
58+
}
59+
60+
for i := range c.VirtualMachines {
61+
vm := &c.VirtualMachines[i]
62+
setVM := func(d *ManagedDisk) { d.Relationships.VirtualMachine = vm }
63+
if vm.OSDisk != nil {
64+
link(vm.OSDisk.ManagedDiskID.Value(), setVM)
65+
}
66+
if vm.StorageOSDisk != nil {
67+
link(vm.StorageOSDisk.ManagedDiskID.Value(), setVM)
68+
}
69+
for _, dd := range vm.StorageDataDisks {
70+
if dd == nil {
71+
continue
72+
}
73+
link(dd.ManagedDiskID.Value(), setVM)
74+
}
75+
}
76+
77+
for i := range c.LinuxVirtualMachines {
78+
vm := &c.LinuxVirtualMachines[i]
79+
if vm.OSDisk != nil {
80+
link(vm.OSDisk.ManagedDiskID.Value(), func(d *ManagedDisk) {
81+
d.Relationships.LinuxVirtualMachine = vm
82+
})
83+
}
84+
}
85+
86+
for i := range c.WindowsVirtualMachines {
87+
vm := &c.WindowsVirtualMachines[i]
88+
if vm.OSDisk != nil {
89+
link(vm.OSDisk.ManagedDiskID.Value(), func(d *ManagedDisk) {
90+
d.Relationships.WindowsVirtualMachine = vm
91+
})
92+
}
93+
}
94+
}

pkg/tree/azure/compute/linux_virtual_machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ type LinuxVirtualMachine struct {
1111
Zone value.String `tree:"zone"`
1212
EncryptionAtHost value.Bool `tree:"encryption_at_host"`
1313
UltraSSDEnabled value.Bool `tree:"ultra_ssd_enabled"`
14+
AvailabilitySetID value.String `tree:"availability_set_id"`
1415
OSDisk *DiskData `tree:"os_disk"`
1516
}

pkg/tree/azure/compute/managed_disk.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ type ManagedDisk struct {
1111
DiskSizeGB value.Int `tree:"disk_size_gb"`
1212
IOPS value.Int `tree:"iops"`
1313
MBpsReadWrite value.Int `tree:"mbps_read_write"`
14+
15+
Relationships ManagedDiskRelationships `tree:"-"`
16+
}
17+
18+
// ManagedDiskRelationships links a managed disk to whichever VM resource it
19+
// is attached to (only one of the three pointers should be non-nil).
20+
type ManagedDiskRelationships struct {
21+
VirtualMachine *VirtualMachine
22+
LinuxVirtualMachine *LinuxVirtualMachine
23+
WindowsVirtualMachine *WindowsVirtualMachine
1424
}

pkg/tree/azure/compute/virtual_machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type VirtualMachine struct {
1212
LicenseType value.Value[LicenseType] `tree:"license_type"`
1313
Zone value.String `tree:"zone"`
1414
EncryptionAtHost value.Bool `tree:"encryption_at_host"`
15+
AvailabilitySetID value.String `tree:"availability_set_id"`
1516
OSDisk *DiskData `tree:"os_disk"`
1617
StorageOSDisk *DiskData `tree:"storage_os_disk"`
1718
StorageDataDisks []*DiskData `tree:"storage_data_disks"`

pkg/tree/azure/compute/windows_virtual_machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ type WindowsVirtualMachine struct {
1212
Zone value.String `tree:"zone"`
1313
EncryptionAtHost value.Bool `tree:"encryption_at_host"`
1414
UltraSSDEnabled value.Bool `tree:"ultra_ssd_enabled"`
15+
AvailabilitySetID value.String `tree:"availability_set_id"`
1516
OSDisk *DiskData `tree:"os_disk"`
1617
}

pkg/tree/google/container/container.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func (c *Container) PostProcess() {
2424

2525
// Append to Cluster's NodePools
2626
cluster.Relationships.NodePools = append(cluster.Relationships.NodePools, &c.NodePools[i])
27+
28+
// A node pool synthesised from the cluster's inline node_config block
29+
// is recorded against the cluster (IsChild==true). Treat the first
30+
// such pool as the default.
31+
if np.IsChild && cluster.Relationships.DefaultNodePool == nil {
32+
cluster.Relationships.DefaultNodePool = &c.NodePools[i]
33+
}
2734
break
2835
}
2936
}

0 commit comments

Comments
 (0)