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

Collect allocated storage for container projects #15973

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions app/models/metric/container_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Metric::ContainerStorage
def self.fill_allocated_container_storage(obj)
return {} unless obj.kind_of?(ContainerProject)

sum_storage = obj.persistent_volume_claims.inject(0) do |sum, volume|
sum + volume.capacity[:storage] if volume.capacity.present?
end

{
:derived_vm_allocated_disk_storage => sum_storage
}
end
end
1 change: 1 addition & 0 deletions app/models/metric/rollup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def self.rollup_hourly(obj, hour, _interval_name, _time_profile, new_perf, orig_
new_perf.reverse_merge!(orig_perf)
new_perf.merge!(Metric::Processing.process_derived_columns(obj, new_perf, hour)) unless DERIVED_COLS_EXCLUDED_CLASSES.include?(obj.class.base_class.name)
new_perf.merge!(Metric::Statistic.calculate_stat_columns(obj, hour))
new_perf.merge!(Metric::ContainerStorage.fill_allocated_container_storage(obj))

new_perf
end
Expand Down
28 changes: 28 additions & 0 deletions spec/models/metric/container_storage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe Metric::ContainerStorage do
context ".fill_allocated_container_storage" do
let(:project1) { FactoryGirl.create(:container_project, :name => 'project1') }
let(:project2) { FactoryGirl.create(:container_project, :name => 'project2') }
let(:project3) { FactoryGirl.create(:container_project, :name => 'project3') }

let(:pvc1) { FactoryGirl.create(:persistent_volume_claim, :capacity => {:storage => 10.gigabytes}) }
let(:pvc2) { FactoryGirl.create(:persistent_volume_claim, :capacity => {:storage => 1.gigabytes}) }
let(:pvc3) { FactoryGirl.create(:persistent_volume_claim, :capacity => {:storage => 3.gigabytes}) }

it "calculates container storage out of persistent volume claims" do
# single pvc
project1.persistent_volume_claims << pvc1
derived_columns = described_class.fill_allocated_container_storage(project1)
expect(derived_columns[:derived_vm_allocated_disk_storage]).to eq(pvc1.capacity[:storage])

# multiple pvc's
project2.persistent_volume_claims << [pvc2, pvc3]
derived_columns = described_class.fill_allocated_container_storage(project2)
expect(derived_columns[:derived_vm_allocated_disk_storage]).to eq(pvc2.capacity[:storage] + pvc3.capacity[:storage])
end

it "calculates container storage when having zero persistent volume claims" do
derived_columns = described_class.fill_allocated_container_storage(project3)
expect(derived_columns[:derived_vm_allocated_disk_storage]).to eq(0)
Copy link
Author

Choose a reason for hiding this comment

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

note: projects with no pvc's or unbounded pvcs will have zero instead of nil.

end
end
end