Skip to content

Commit

Permalink
Gate enforcing resource constraints on a setting
Browse files Browse the repository at this point in the history
This setting will be set by the orchestrator startup process
which is in turn driven by a parameter in the CR.

The default for this is false in the operator so will also be false
in the settings.

Fixes #20163
  • Loading branch information
carbonin committed Jun 3, 2020
1 parent ff24b2c commit 01011e7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/models/miq_worker/container_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def resource_constraints
mem_threshold = self.class.worker_settings[:memory_threshold]
cpu_threshold = self.class.worker_settings[:cpu_threshold]

return {} if mem_threshold.nil? && cpu_threshold.nil?
return {} if !Settings.server.worker_monitor.enforce_resource_constraints || (mem_threshold.nil? && cpu_threshold.nil?)

{:limits => {}}.tap do |h|
h[:limits][:memory] = "#{mem_threshold / 1.megabyte}Mi" if mem_threshold
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@
:worker_messaging_frequency: 5.seconds
:worker_monitor_frequency: 15.seconds
:worker_monitor:
:enforce_resource_constraints: false
:kill_algorithm:
:name: :used_swap_percent_gt_value
:value: 80
Expand Down
69 changes: 41 additions & 28 deletions spec/models/miq_worker/container_common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,53 @@ def deployment_name_for(name)
end

describe "#resource_constraints" do
it "returns an empty hash when no thresholds are set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return({})
expect(MiqGenericWorker.new.resource_constraints).to eq({})
end
context "when allowing resource constraints" do
before { stub_settings(:server => {:worker_monitor => {:enforce_resource_constraints => true}}) }

it "returns an empty hash when no thresholds are set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return({})
expect(MiqGenericWorker.new.resource_constraints).to eq({})
end

it "returns the correct hash when both values are set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 500)
constraints = {
:limits => {
:memory => "500Mi",
:cpu => "500m"
it "returns the correct hash when both values are set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 500)
constraints = {
:limits => {
:memory => "500Mi",
:cpu => "500m"
}
}
}
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
end
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
end

it "returns only memory when memory is set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes)
constraints = {
:limits => {
:memory => "500Mi",
it "returns only memory when memory is set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes)
constraints = {
:limits => {
:memory => "500Mi",
}
}
}
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
end
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
end

it "returns only cpu when cpu is set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:cpu_threshold => 500)
constraints = {
:limits => {
:cpu => "500m"
it "returns only cpu when cpu is set" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:cpu_threshold => 500)
constraints = {
:limits => {
:cpu => "500m"
}
}
}
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
expect(MiqGenericWorker.new.resource_constraints).to eq(constraints)
end
end

context "when not allowing resource constraints" do
before { stub_settings(:server => {:worker_monitor => {:enforce_resource_constraints => false}}) }

it "always returns an empty hash" do
allow(MiqGenericWorker).to receive(:worker_settings).and_return(:memory_threshold => 500.megabytes, :cpu_threshold => 500)
expect(MiqGenericWorker.new.resource_constraints).to eq({})
end
end
end
end

0 comments on commit 01011e7

Please sign in to comment.