Skip to content

Commit 3c025df

Browse files
authored
Refactor kvm resource capacity kpi to use effective capacity (#585)
## Changes - Adjusted the kvm resource capacity kpi to use the new `EffectiveCapacity` from the hypervisor CRD, see: cobaltcore-dev/openstack-hypervisor-operator#257
1 parent c71d9d4 commit 3c025df

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

internal/knowledge/kpis/plugins/compute/resource_capacity_kvm.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,30 @@ func (k *KVMResourceCapacityKPI) Collect(ch chan<- prometheus.Metric) {
154154
}
155155

156156
for _, hypervisor := range hvs.Items {
157-
cpuTotal, hasCPUTotal := hypervisor.Status.Capacity["cpu"]
158-
ramTotal, hasRAMTotal := hypervisor.Status.Capacity["memory"]
157+
if hypervisor.Status.EffectiveCapacity == nil {
158+
slog.Warn("hypervisor with nil effective capacity, skipping", "host", hypervisor.Name)
159+
continue
160+
}
161+
162+
cpuTotal, hasCPUTotal := hypervisor.Status.EffectiveCapacity[hv1.ResourceCPU]
163+
ramTotal, hasRAMTotal := hypervisor.Status.EffectiveCapacity[hv1.ResourceMemory]
159164

160165
if !hasCPUTotal || !hasRAMTotal {
161166
slog.Error("hypervisor missing cpu or ram total capacity", "hypervisor", hypervisor.Name)
162167
continue
163168
}
164169

165-
cpuUsed, hasCPUUtilized := hypervisor.Status.Allocation["cpu"]
170+
if cpuTotal.IsZero() || ramTotal.IsZero() {
171+
slog.Warn("hypervisor with zero cpu or ram total capacity, skipping", "host", hypervisor.Name)
172+
continue
173+
}
174+
175+
cpuUsed, hasCPUUtilized := hypervisor.Status.Allocation[hv1.ResourceCPU]
166176
if !hasCPUUtilized {
167177
cpuUsed = resource.MustParse("0")
168178
}
169179

170-
ramUsed, hasRAMUtilized := hypervisor.Status.Allocation["memory"]
180+
ramUsed, hasRAMUtilized := hypervisor.Status.Allocation[hv1.ResourceMemory]
171181
if !hasRAMUtilized {
172182
ramUsed = resource.MustParse("0")
173183
}

internal/knowledge/kpis/plugins/compute/resource_capacity_kvm_test.go

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,55 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
4747
hypervisors []hv1.Hypervisor
4848
expectedMetrics map[string][]expectedMetric // metric_name -> []expectedMetric
4949
}{
50+
{
51+
name: "single hypervisor with nil effective capacity",
52+
hypervisors: []hv1.Hypervisor{
53+
{
54+
ObjectMeta: v1.ObjectMeta{
55+
Name: "node001-bb088",
56+
Labels: map[string]string{
57+
"topology.kubernetes.io/zone": "qa-1a",
58+
},
59+
},
60+
Status: hv1.HypervisorStatus{
61+
EffectiveCapacity: nil, // Simulate nil effective capacity
62+
Allocation: map[hv1.ResourceName]resource.Quantity{
63+
hv1.ResourceCPU: resource.MustParse("64"),
64+
hv1.ResourceMemory: resource.MustParse("256Gi"),
65+
},
66+
Traits: []string{},
67+
},
68+
},
69+
},
70+
// No metrics should be emitted for this hypervisor since effective capacity is nil
71+
expectedMetrics: map[string][]expectedMetric{},
72+
},
73+
{
74+
name: "single hypervisor with zero total capacity",
75+
hypervisors: []hv1.Hypervisor{
76+
{
77+
ObjectMeta: v1.ObjectMeta{
78+
Name: "node001-bb088",
79+
Labels: map[string]string{
80+
"topology.kubernetes.io/zone": "qa-1a",
81+
},
82+
},
83+
Status: hv1.HypervisorStatus{
84+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
85+
hv1.ResourceCPU: resource.MustParse("0"), // Simulate zero CPU capacity
86+
hv1.ResourceMemory: resource.MustParse("0"), // Simulate zero RAM capacity
87+
},
88+
Allocation: map[hv1.ResourceName]resource.Quantity{
89+
hv1.ResourceCPU: resource.MustParse("0"),
90+
hv1.ResourceMemory: resource.MustParse("0"),
91+
},
92+
Traits: []string{},
93+
},
94+
},
95+
},
96+
// No metrics should be emitted for this hypervisor since total capacity is zero
97+
expectedMetrics: map[string][]expectedMetric{},
98+
},
5099
{
51100
name: "single hypervisor with default traits",
52101
hypervisors: []hv1.Hypervisor{
@@ -58,7 +107,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
58107
},
59108
},
60109
Status: hv1.HypervisorStatus{
61-
Capacity: map[hv1.ResourceName]resource.Quantity{
110+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
62111
hv1.ResourceCPU: resource.MustParse("128"),
63112
hv1.ResourceMemory: resource.MustParse("512Gi"),
64113
},
@@ -148,7 +197,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
148197
},
149198
},
150199
Status: hv1.HypervisorStatus{
151-
Capacity: map[hv1.ResourceName]resource.Quantity{
200+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
152201
hv1.ResourceCPU: resource.MustParse("256"),
153202
hv1.ResourceMemory: resource.MustParse("1Ti"),
154203
},
@@ -209,7 +258,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
209258
},
210259
},
211260
Status: hv1.HypervisorStatus{
212-
Capacity: map[hv1.ResourceName]resource.Quantity{
261+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
213262
hv1.ResourceCPU: resource.MustParse("64"),
214263
hv1.ResourceMemory: resource.MustParse("256Gi"),
215264
},
@@ -255,7 +304,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
255304
},
256305
},
257306
Status: hv1.HypervisorStatus{
258-
Capacity: map[hv1.ResourceName]resource.Quantity{
307+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
259308
hv1.ResourceCPU: resource.MustParse("100"),
260309
hv1.ResourceMemory: resource.MustParse("200Gi"),
261310
},
@@ -274,7 +323,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
274323
},
275324
},
276325
Status: hv1.HypervisorStatus{
277-
Capacity: map[hv1.ResourceName]resource.Quantity{
326+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
278327
hv1.ResourceCPU: resource.MustParse("200"),
279328
hv1.ResourceMemory: resource.MustParse("400Gi"),
280329
},
@@ -332,7 +381,7 @@ func TestKVMResourceCapacityKPI_Collect(t *testing.T) {
332381
},
333382
},
334383
Status: hv1.HypervisorStatus{
335-
Capacity: map[hv1.ResourceName]resource.Quantity{
384+
EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{
336385
hv1.ResourceCPU: resource.MustParse("96"),
337386
hv1.ResourceMemory: resource.MustParse("384Gi"),
338387
},

0 commit comments

Comments
 (0)