From 01011e796d53b626de8b3c61491424a075cfc927 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Wed, 3 Jun 2020 16:46:18 -0400 Subject: [PATCH] Gate enforcing resource constraints on a setting 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 --- app/models/miq_worker/container_common.rb | 2 +- config/settings.yml | 1 + .../miq_worker/container_common_spec.rb | 69 +++++++++++-------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/app/models/miq_worker/container_common.rb b/app/models/miq_worker/container_common.rb index e8021db2fd9..32256e57730 100644 --- a/app/models/miq_worker/container_common.rb +++ b/app/models/miq_worker/container_common.rb @@ -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 diff --git a/config/settings.yml b/config/settings.yml index a613ccc053b..110f9e3fe2b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -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 diff --git a/spec/models/miq_worker/container_common_spec.rb b/spec/models/miq_worker/container_common_spec.rb index b83a0ba6237..8421fa36369 100644 --- a/spec/models/miq_worker/container_common_spec.rb +++ b/spec/models/miq_worker/container_common_spec.rb @@ -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