Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.2] Collect VMI OS info from the Guest agent #11720

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 49 additions & 17 deletions pkg/monitoring/metrics/virt-controller/vmistats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
Name: "kubevirt_vmi_phase_count",
Help: "Sum of VMIs per phase and node. `phase` can be one of the following: [`Pending`, `Scheduling`, `Scheduled`, `Running`, `Succeeded`, `Failed`, `Unknown`].",
},
[]string{"node", "phase", "os", "workload", "flavor", "instance_type", "preference"},
[]string{"node", "phase", "os", "workload", "flavor", "instance_type", "preference", "guest_os_kernel_release", "guest_os_machine", "guest_os_name", "guest_os_version_id"},
)

vmiEvictionBlocker = operatormetrics.NewGaugeVec(
Expand All @@ -74,13 +74,17 @@ var (
)

type vmiCountMetric struct {
Phase string
OS string
Workload string
Flavor string
InstanceType string
Preference string
NodeName string
Phase string
OS string
Workload string
Flavor string
InstanceType string
Preference string
NodeName string
GuestOSKernelRelease string
GuestOSMachine string
GuestOSName string
GuestOSVersionID string
}

func vmiStatsCollectorCallback() []operatormetrics.CollectorResult {
Expand Down Expand Up @@ -117,8 +121,9 @@ func getVmisPhase(vmis []*k6tv1.VirtualMachineInstance) []operatormetrics.Collec
for vmc, count := range countMap {
cr = append(cr, operatormetrics.CollectorResult{
Metric: vmiCount,
Labels: []string{vmc.NodeName, vmc.Phase, vmc.OS, vmc.Workload, vmc.Flavor, vmc.InstanceType, vmc.Preference},
Value: float64(count),
Labels: []string{vmc.NodeName, vmc.Phase, vmc.OS, vmc.Workload, vmc.Flavor, vmc.InstanceType, vmc.Preference,
vmc.GuestOSKernelRelease, vmc.GuestOSMachine, vmc.GuestOSName, vmc.GuestOSVersionID},
Value: float64(count),
})
}

Expand All @@ -137,16 +142,21 @@ func makeVMICountMetricMap(vmis []*k6tv1.VirtualMachineInstance) map[vmiCountMet

func newVMICountMetric(vmi *k6tv1.VirtualMachineInstance) vmiCountMetric {
vmc := vmiCountMetric{
Phase: strings.ToLower(string(vmi.Status.Phase)),
OS: none,
Workload: none,
Flavor: none,
InstanceType: none,
Preference: none,
NodeName: vmi.Status.NodeName,
Phase: strings.ToLower(string(vmi.Status.Phase)),
OS: none,
Workload: none,
Flavor: none,
InstanceType: none,
Preference: none,
GuestOSKernelRelease: none,
GuestOSMachine: none,
GuestOSName: none,
GuestOSVersionID: none,
NodeName: vmi.Status.NodeName,
}

updateFromAnnotations(&vmc, vmi.Annotations)
updateFromGuestOSInfo(&vmc, vmi.Status.GuestOSInfo)

return vmc
}
Expand Down Expand Up @@ -228,6 +238,28 @@ func setPreferenceFromAnnotations(vmc *vmiCountMetric, annotations map[string]st
}
}

func updateFromGuestOSInfo(vmc *vmiCountMetric, guestOSInfo k6tv1.VirtualMachineInstanceGuestOSInfo) {
if guestOSInfo == (k6tv1.VirtualMachineInstanceGuestOSInfo{}) {
return
}

if guestOSInfo.KernelRelease != "" {
vmc.GuestOSKernelRelease = guestOSInfo.KernelRelease
}

if guestOSInfo.Machine != "" {
vmc.GuestOSMachine = guestOSInfo.Machine
}

if guestOSInfo.Name != "" {
vmc.GuestOSName = guestOSInfo.Name
}

if guestOSInfo.VersionID != "" {
vmc.GuestOSVersionID = guestOSInfo.VersionID
}
}

func getEvictionBlocker(vmis []*k6tv1.VirtualMachineInstance) []operatormetrics.CollectorResult {
var cr []operatormetrics.CollectorResult

Expand Down
58 changes: 38 additions & 20 deletions pkg/monitoring/metrics/virt-controller/vmistats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ var _ = Describe("Utility functions", func() {
},
Status: k6tv1.VirtualMachineInstanceStatus{
Phase: "Pending",
GuestOSInfo: k6tv1.VirtualMachineInstanceGuestOSInfo{
KernelRelease: "6.5.6-300.fc39.x86_64",
Machine: "x86_64",
Name: "Fedora Linux",
VersionID: "39",
},
},
},
{
Expand Down Expand Up @@ -189,28 +195,40 @@ var _ = Describe("Utility functions", func() {
Expect(countMap).To(HaveLen(3))

running := vmiCountMetric{
Phase: "running",
OS: "centos8",
Workload: "server",
Flavor: "tiny",
InstanceType: "<none>",
Preference: "<none>",
Phase: "running",
OS: "centos8",
Workload: "server",
Flavor: "tiny",
GuestOSKernelRelease: "<none>",
GuestOSMachine: "<none>",
GuestOSName: "<none>",
GuestOSVersionID: "<none>",
InstanceType: "<none>",
Preference: "<none>",
}
pending := vmiCountMetric{
Phase: "pending",
OS: "fedora33",
Workload: "workstation",
Flavor: "large",
InstanceType: "<none>",
Preference: "<none>",
Phase: "pending",
OS: "fedora33",
Workload: "workstation",
Flavor: "large",
InstanceType: "<none>",
Preference: "<none>",
GuestOSKernelRelease: "6.5.6-300.fc39.x86_64",
GuestOSMachine: "x86_64",
GuestOSName: "Fedora Linux",
GuestOSVersionID: "39",
}
scheduling := vmiCountMetric{
Phase: "scheduling",
OS: "centos7",
Workload: "server",
Flavor: "medium",
InstanceType: "<none>",
Preference: "<none>",
Phase: "scheduling",
OS: "centos7",
Workload: "server",
Flavor: "medium",
GuestOSKernelRelease: "<none>",
GuestOSMachine: "<none>",
GuestOSName: "<none>",
GuestOSVersionID: "<none>",
InstanceType: "<none>",
Preference: "<none>",
}
bogus := vmiCountMetric{
Phase: "bogus",
Expand Down Expand Up @@ -243,7 +261,7 @@ var _ = Describe("Utility functions", func() {
Expect(phaseResultMetric).ToNot(BeNil())
Expect(phaseResultMetric.Metric.GetOpts().Name).To(ContainSubstring("kubevirt_vmi_phase_count"))
Expect(phaseResultMetric.Value).To(BeEquivalentTo(1))
Expect(phaseResultMetric.Labels).To(HaveLen(7))
Expect(phaseResultMetric.Labels).To(HaveLen(11))
Expect(phaseResultMetric.Labels[5]).To(Equal(expected))
},
Entry("with no instance type expect <none>", k6tv1.InstancetypeAnnotation, "", "<none>"),
Expand Down Expand Up @@ -276,7 +294,7 @@ var _ = Describe("Utility functions", func() {

Expect(phaseResultMetric.Metric.GetOpts().Name).To(ContainSubstring("kubevirt_vmi_phase_count"))
Expect(phaseResultMetric.Value).To(BeEquivalentTo(1))
Expect(phaseResultMetric.Labels).To(HaveLen(7))
Expect(phaseResultMetric.Labels).To(HaveLen(11))
Expect(phaseResultMetric.Labels[6]).To(Equal(expected))
},
Entry("with no preference expect <none>", k6tv1.PreferenceAnnotation, "", "<none>"),
Expand Down
1 change: 1 addition & 0 deletions tests/monitoring/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_library(
"//tests/util:go_default_library",
"//vendor/github.com/onsi/ginkgo/v2:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/github.com/onsi/gomega/gstruct:go_default_library",
"//vendor/github.com/onsi/gomega/types:go_default_library",
"//vendor/github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1:go_default_library",
"//vendor/github.com/prometheus/client_golang/api/prometheus/v1:go_default_library",
Expand Down
38 changes: 38 additions & 0 deletions tests/monitoring/vm_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"github.com/onsi/gomega/types"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -242,6 +243,25 @@ var _ = Describe("[Serial][sig-monitoring]VM Monitoring", Serial, decorators.Sig
})
})

Context("VM metrics that are based on the guest agent", func() {
It("should have kubevirt_vmi_phase_count correctly configured with guest OS labels", func() {
agentVMI := createAgentVMI()
Expect(agentVMI.Status.GuestOSInfo.KernelRelease).ToNot(BeEmpty())
Expect(agentVMI.Status.GuestOSInfo.Machine).ToNot(BeEmpty())
Expect(agentVMI.Status.GuestOSInfo.Name).ToNot(BeEmpty())
Expect(agentVMI.Status.GuestOSInfo.VersionID).ToNot(BeEmpty())

labels := map[string]string{
"guest_os_kernel_release": agentVMI.Status.GuestOSInfo.KernelRelease,
"guest_os_machine": agentVMI.Status.GuestOSInfo.Machine,
"guest_os_name": agentVMI.Status.GuestOSInfo.Name,
"guest_os_version_id": agentVMI.Status.GuestOSInfo.VersionID,
}

waitForMetricValueWithLabels(virtClient, "kubevirt_vmi_phase_count", 1, labels, 1)
})
})

Context("VM alerts", func() {
var scales *Scaling

Expand Down Expand Up @@ -300,3 +320,21 @@ var _ = Describe("[Serial][sig-monitoring]VM Monitoring", Serial, decorators.Sig
})
})
})

func createAgentVMI() *v1.VirtualMachineInstance {
virtClient := kubevirt.Client()
vmiAgentConnectedConditionMatcher := MatchFields(IgnoreExtras, Fields{"Type": Equal(v1.VirtualMachineInstanceAgentConnected)})
vmi := tests.RunVMIAndExpectLaunch(libvmi.NewFedora(libvmi.WithMasqueradeNetworking()...), 180)

var err error
var agentVMI *v1.VirtualMachineInstance

By("VMI has the guest agent connected condition")
Eventually(func() []v1.VirtualMachineInstanceCondition {
agentVMI, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Get(context.Background(), vmi.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return agentVMI.Status.Conditions
}, 240*time.Second, 1*time.Second).Should(ContainElement(vmiAgentConnectedConditionMatcher), "Should have agent connected condition")

return agentVMI
}