Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type dataMetric struct {
Labels map[string]string
Annotations map[string]string
firmwareUpToDate bool
// AppliedVirtualMachineClassName is the class name that is actually applied to the running VM.
// It may differ from spec.virtualMachineClassName if the spec was changed but the VM wasn't restarted.
AppliedVirtualMachineClassName string
}

// DO NOT mutate VirtualMachine!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
MetricVirtualMachineLabels = "virtualmachine_labels"
MetricVirtualMachineAnnotations = "virtualmachine_annotations"
MetricVirtualMachineFirmwareUpToDate = "virtualmachine_firmware_up_to_date"
MetricVirtualMachineInfo = "virtualmachine_info"
)

var baseLabels = []string{"name", "namespace", "uid", "node"}
Expand Down Expand Up @@ -172,4 +173,11 @@ var virtualMachineMetrics = map[string]metrics.MetricInfo{
WithBaseLabels(),
nil,
),

MetricVirtualMachineInfo: metrics.NewMetricInfo(MetricVirtualMachineInfo,
"Information about the virtualmachine.",
prometheus.GaugeValue,
WithBaseLabels("virtualmachineclass"),
nil,
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *scraper) Report(m *dataMetric) {
s.updateMetricVirtualMachineAnnotations(m)
s.updateMetricVirtualMachineAgentReady(m)
s.updateMetricVirtualMachineFirmwareUpToDate(m)
s.updateMetricVirtualMachineInfo(m)
}

func (s *scraper) updateMetricVirtualMachineStatusPhase(m *dataMetric) {
Expand Down Expand Up @@ -164,6 +165,10 @@ func (s *scraper) updateMetricVirtualMachineFirmwareUpToDate(m *dataMetric) {
s.defaultUpdate(MetricVirtualMachineFirmwareUpToDate, common.BoolFloat64(m.firmwareUpToDate), m)
}

func (s *scraper) updateMetricVirtualMachineInfo(m *dataMetric) {
s.defaultUpdate(MetricVirtualMachineInfo, 1, m, m.AppliedVirtualMachineClassName)
}

func (s *scraper) defaultUpdate(name string, value float64, m *dataMetric, labelValues ...string) {
info := virtualMachineMetrics[name]
metric, err := prometheus.NewConstMetric(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package virtualmachine

import (
"context"
"encoding/json"

"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -41,8 +42,10 @@ func (l *iterator) Iter(ctx context.Context, h handler) error {
if err := l.reader.List(ctx, &vms, client.UnsafeDisableDeepCopy); err != nil {
return err
}

for _, vm := range vms.Items {
m := newDataMetric(&vm)
m.AppliedVirtualMachineClassName = appliedClassName(&vm)
if stop := h(m); stop {
return nil
}
Expand All @@ -55,3 +58,30 @@ func (l *iterator) Iter(ctx context.Context, h handler) error {
}
return nil
}

// appliedClassName returns the VirtualMachineClass name that is actually running on the VM.
// If there are no pending restart changes, spec value is already applied.
// Otherwise, it looks for a virtualMachineClassName change in restartAwaitingChanges
// and returns its currentValue (the one still running).
func appliedClassName(vm *v1alpha2.VirtualMachine) string {
if len(vm.Status.RestartAwaitingChanges) == 0 {
return vm.Spec.VirtualMachineClassName
}

// TODO: consider using gjson for faster JSON field extraction when tuning performance.
for _, raw := range vm.Status.RestartAwaitingChanges {
var change struct {
Path string `json:"path"`
CurrentValue string `json:"currentValue"`
}
if err := json.Unmarshal(raw.Raw, &change); err != nil {
continue
}
if change.Path == "virtualMachineClassName" {
return change.CurrentValue
}
}

// No virtualMachineClassName change among pending changes — spec value is applied.
return vm.Spec.VirtualMachineClassName
}
Loading
Loading