diff --git a/app/models/miq_worker/container_common.rb b/app/models/miq_worker/container_common.rb index 27e8edc0c158..b85cdffe82e7 100644 --- a/app/models/miq_worker/container_common.rb +++ b/app/models/miq_worker/container_common.rb @@ -8,6 +8,10 @@ def configure_worker_deployment(definition, replicas = 0) definition[:spec][:replicas] = replicas definition[:spec][:template][:spec][:terminationGracePeriodSeconds] = self.class.worker_settings[:stopping_timeout].seconds + if MiqServer.my_zone != "default" + definition[:spec][:template][:spec][:nodeSelector] = zone_selector + end + container = definition[:spec][:template][:spec][:containers].first container[:image] = "#{container_image_namespace}/#{container_image_name}:#{container_image_tag}" container[:env] << {:name => "WORKER_CLASS_NAME", :value => self.class.name} @@ -19,6 +23,10 @@ def scale_deployment delete_container_objects if self.class.workers.zero? end + def zone_selector + {"#{Vmdb::Appliance.PRODUCT_NAME.downcase}/zone" => MiqServer.my_zone} + end + def container_image_namespace ENV["CONTAINER_IMAGE_NAMESPACE"] end diff --git a/spec/models/miq_worker/container_common_spec.rb b/spec/models/miq_worker/container_common_spec.rb index 04b3b8b7df0e..19c02cf37387 100644 --- a/spec/models/miq_worker/container_common_spec.rb +++ b/spec/models/miq_worker/container_common_spec.rb @@ -6,6 +6,46 @@ def deployment_name_for(name) "#{compressed_server_id}-#{name}" end + describe "#configure_worker_deplyoment" do + let(:test_deployment) do + { + :metadata => { + :name => "test", + :labels => {:app => "manageiq"}, + :namespace => "manageiq", + }, + :spec => { + :selector => {:matchLabels => {:name => "test"}}, + :template => { + :metadata => {:name => "test", :labels => {:name => "test", :app => "manageiq"}}, + :spec => { + :serviceAccountName => "miq-anyuid", + :containers => [{ + :name => "test", + :env => [] + }] + } + } + } + } + end + + it "adds a node selector based on the zone name" do + worker = FactoryBot.create(:miq_generic_worker) + worker.configure_worker_deployment(test_deployment) + + expect(test_deployment.dig(:spec, :template, :spec, :nodeSelector)).to eq("manageiq/zone" => MiqServer.my_zone) + end + + it "doesn't add a node selector for the default zone" do + MiqServer.my_server.zone.update(:name => "default") + worker = FactoryBot.create(:miq_generic_worker) + worker.configure_worker_deployment(test_deployment) + + expect(test_deployment.dig(:spec, :template, :spec).keys).not_to include(:nodeSelector) + end + end + describe "#worker_deployment_name" do let(:test_cases) do [