Skip to content

Commit

Permalink
Use the server's zone to determine a node selector when running in pods
Browse files Browse the repository at this point in the history
This will allow workers to be target to specific nodes.
  • Loading branch information
carbonin committed Jan 31, 2020
1 parent f36c8d8 commit 07c0356
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/miq_worker/container_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions spec/models/miq_worker/container_common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand Down

0 comments on commit 07c0356

Please sign in to comment.