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

Kubevirt metrics capture specs #270

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 @@ -70,10 +70,10 @@ def labels_to_s(labels)
end

def validate_target
raise TargetValidationError, "ems not defined" unless @ext_management_system
raise TargetValidationWarning, "no associated hardware" unless @vm_hardware
raise ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::TargetValidationError, "ems not defined" unless @ext_management_system
raise ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::TargetValidationWarning, "no associated hardware" unless @vm_hardware

raise TargetValidationError, "cores not defined" unless @vm_cores.to_i > 0
raise TargetValidationError, "memory not defined" unless @vm_memory.to_i > 0
Comment on lines -73 to -77
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed since the capture context is subclassed from k8s which has its own error handling classes

class ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::PrometheusCaptureContext < ManageIQ::Providers::Kubernetes::ContainerManager::MetricsCapture::PrometheusCaptureContext

This should not be needed if ManageIQ/manageiq-providers-kubernetes#543 is resolved but that may necessitate other changes to the specs

raise ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::TargetValidationError, "cores not defined" unless @vm_cores.to_i > 0
raise ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::TargetValidationError, "memory not defined" unless @vm_memory.to_i > 0
end
end
8 changes: 8 additions & 0 deletions config/secrets.defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
test:
kubevirt_defaults: &kubevirt_defaults
hostname: KUBEVIRT_HOSTNAME
token: KUBEVIRT_TOKEN
metrics_hostname: METRICS_HOSTNAME
kubevirt:
<<: *kubevirt_defaults
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember if we changed this yet or not. cc @jrafanie

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. I haven't had time to get back to it. The referenced changes are coming from this core change: ManageIQ/manageiq#23292 and how each plugin would use these changes: ManageIQ/manageiq-providers-vmware#928

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might just leave the warnings with rails 7.1 for now and people can fix them when their usage of rails secrets in 7.1 doesn't work or gets a warning. Note, I don't think we actually use rails to store secrets encrypted. That's the part that's completely gone in rails 7.1. Access is probably gone in 7.2.

3 changes: 3 additions & 0 deletions spec/factories/host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryBot.define do
factory(:host_kubevirt, :class => "ManageIQ::Providers::Kubevirt::InfraManager::Host", :traits => [:with_ref], :parent => :host) { vmm_vendor { "kubevirt" } }
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
describe ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::PrometheusCaptureContext do
let(:ems) do
hostname = Rails.application.secrets.kubevirt[:hostname]
metrics_hostname = Rails.application.secrets.kubevirt[:metrics_hostname]
token = Rails.application.secrets.kubevirt[:token]

FactoryBot.create(
:ems_kubernetes_with_zone,
:name => 'KubernetesProvider',
:connection_configurations => [{:endpoint => {:role => :default,
:hostname => hostname,
:port => "6443",
:verify_ssl => false},
:authentication => {:role => :bearer,
:auth_key => token,
:userid => "_"}},
{:endpoint => {:role => :prometheus,
:hostname => metrics_hostname,
:port => "443",
:verify_ssl => false},
:authentication => {:role => :prometheus,
:auth_key => token,
:userid => "_"}}]
)
end
let(:ems_kubevirt) { FactoryBot.create(:ems_kubevirt, :parent_manager => ems) }

before(:each) do
VCR.use_cassette("#{described_class.name.underscore}_refresh") do
EmsRefresh.refresh(ems_kubevirt)
ems_kubevirt.reload

@vm = ems_kubevirt.vms.last
@targets = [['VmOrTemplate', @vm]]
end
end

it "will read prometheus metrics" do
start_time = Time.parse("2024-12-18 21:28:00 UTC").utc
end_time = Time.parse("2024-12-18 21:38:00 UTC").utc
interval = 60

@targets.each do |target_name, target|
VCR.use_cassette("#{described_class.name.underscore}_#{target_name}_metrics") do
context = ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::PrometheusCaptureContext.new(
target, start_time, end_time, interval
)

data = context.collect_metrics

expect(data).to be_a_kind_of(Hash)
expect(data.keys).to include(start_time, end_time)
expect(data[start_time].keys).to include(
"cpu_usage_rate_average",
"mem_usage_absolute_average"
)
end
end
end

it "will read only specific timespan prometheus metrics" do
start_time = Time.parse("2024-12-18 21:28:00 UTC").utc
end_time = Time.parse("2024-12-18 21:38:00 UTC").utc
interval = 60

@targets.each do |target_name, target|
VCR.use_cassette("#{described_class.name.underscore}_#{target_name}_timespan") do
context = ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::PrometheusCaptureContext.new(
target, start_time, end_time, interval
)

data = context.collect_metrics

expect(data.count).to be > 8
expect(data.count).to be < 13
end
end
end

describe "#ts_values" do
let(:start_time) { Time.parse("2024-12-18 21:28:00 UTC").utc }
let(:end_time) { Time.parse("2024-12-18 21:38:00 UTC").utc }
let(:interval) { 60 }
let!(:context) { ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture::PrometheusCaptureContext.new(@vm, start_time, end_time, interval) }

context "with no missing metrics" do
before do
ts_values = context.instance_variable_get(:@ts_values)
ts_values[start_time] = {"cpu_usage_rate_average" => [1], "container_memory_usage_bytes" => [1], "net_usage_rate_average" => [1]}
ts_values[end_time] = {"cpu_usage_rate_average" => [1], "container_memory_usage_bytes" => [1], "net_usage_rate_average" => [1]}
end

it "returns all timestamps" do
expect(context.ts_values.keys).to include(start_time, end_time)
end
end

context "with some timestamps missing metrics" do
before do
ts_values = context.instance_variable_get(:@ts_values)
ts_values[start_time] = {"cpu_usage_rate_average" => [1], "container_memory_usage_bytes" => [1], "net_usage_rate_average" => [1]}
ts_values[end_time] = {"cpu_usage_rate_average" => [1], "net_usage_rate_average" => [1]}
end

it "only returns timestamps with all metrics" do
expect(context.ts_values.keys).not_to include(end_time)
end
end

context "with some missing metrics from all timestamps" do
before do
ts_values = context.instance_variable_get(:@ts_values)
ts_values[start_time] = {"cpu_usage_rate_average" => [1], "net_usage_rate_average" => [1]}
ts_values[end_time] = {"cpu_usage_rate_average" => [1], "net_usage_rate_average" => [1]}
end

it "returns all timestamps" do
expect(context.ts_values.keys).to include(start_time, end_time)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe ManageIQ::Providers::Kubevirt::InfraManager::MetricsCapture do
let(:ems) { FactoryBot.create(:ems_kubevirt, :with_metrics_endpoint) }
let(:hardware) { FactoryBot.create(:hardware, :cpu1x1, :ram1GB) }
let(:host) { FactoryBot.create(:host_kubevirt, :ext_management_system => ems, :name => "test-host") }
let(:vm) { FactoryBot.create(:vm_kubevirt, :ext_management_system => ems, :hardware => hardware, :host => host) }
let(:template) { FactoryBot.create(:template_kubevirt, :ext_management_system => ems) }

context "#perf_capture_object" do
it "returns the correct class" do
expect(ems.perf_capture_object.class).to eq(described_class)
end
end

context "#build_capture_context!" do
it "detect prometheus metrics provider" do
metric_capture = described_class.new(vm)
context = metric_capture.build_capture_context!(ems, vm, 5.minutes.ago, 0.minutes.ago)

expect(context).to be_a(described_class::PrometheusCaptureContext)
end

context "on an invalid target" do
it "raises an exception" do
metric_capture = described_class.new(template)

expect { metric_capture.build_capture_context!(ems, template, 5.minutes.ago, 0.minutes.ago) }
.to raise_error(described_class::TargetValidationWarning, "no associated hardware")
end
end
end

context "#perf_capture_all_queue" do
context "with a missing metrics endpoint" do
let(:ems) { FactoryBot.create(:ems_kubevirt) }

it "returns no objects" do
expect(ems.perf_capture_object.perf_capture_all_queue).to be_empty
end
end

context "with invalid authentication on the metrics endpoint" do
let(:ems) { FactoryBot.create(:ems_kubevirt, :with_metrics_endpoint, :with_invalid_auth) }

it "returns no objects" do
expect(ems.perf_capture_object.perf_capture_all_queue).to be_empty
end
end
end

context "#perf_collect_metrics" do
it "fails when no cpu cores are defined" do
vm.hardware.cpu_total_cores = nil
expect { vm.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError)
end

it "fails when memory is not defined" do
vm.hardware.memory_mb = nil
expect { vm.perf_collect_metrics('interval_name') }.to raise_error(described_class::TargetValidationError)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading