diff --git a/app/models/miq_worker/container_common.rb b/app/models/miq_worker/container_common.rb index 0c57b7f9ae6..27e8edc0c15 100644 --- a/app/models/miq_worker/container_common.rb +++ b/app/models/miq_worker/container_common.rb @@ -31,11 +31,15 @@ def container_image_tag "latest" end + def deployment_prefix + "#{MiqServer.my_server.compressed_id}-" + end + def worker_deployment_name @worker_deployment_name ||= begin deployment_name = abbreviated_class_name.dup.chomp("Worker").sub("Manager", "").sub(/^Miq/, "") deployment_name << "-#{Array(ems_id).map { |id| ApplicationRecord.split_id(id).last }.join("-")}" if respond_to?(:ems_id) - deployment_name.underscore.dasherize.tr("/", "-") + "#{deployment_prefix}#{deployment_name.underscore.dasherize.tr("/", "-")}" end end end diff --git a/app/models/miq_worker/service_worker.rb b/app/models/miq_worker/service_worker.rb index 18e37418869..6df5a172ebf 100644 --- a/app/models/miq_worker/service_worker.rb +++ b/app/models/miq_worker/service_worker.rb @@ -7,11 +7,13 @@ module ServiceWorker def create_container_objects orchestrator = ContainerOrchestrator.new - orchestrator.create_service(worker_deployment_name, SERVICE_PORT) + orchestrator.create_service(service_name, service_label, SERVICE_PORT) orchestrator.create_deployment(worker_deployment_name) do |definition| configure_worker_deployment(definition) - definition[:spec][:serviceName] = worker_deployment_name + definition[:spec][:serviceName] = service_name + definition[:spec][:template][:metadata][:labels].merge!(service_label) + container = definition[:spec][:template][:spec][:containers].first container[:ports] = [{:containerPort => SERVICE_PORT}] container[:env] << {:name => "PORT", :value => container_port.to_s} @@ -25,7 +27,7 @@ def create_container_objects def delete_container_objects orch = ContainerOrchestrator.new orch.delete_deployment(worker_deployment_name) - orch.delete_service(worker_deployment_name) + orch.delete_service(service_name) end def stop_container @@ -40,6 +42,14 @@ def add_readiness_probe(container_definition) } end + def service_label + {:service => service_name} + end + + def service_name + worker_deployment_name.delete_prefix(deployment_prefix) + end + # Can be overriden by including classes def container_port SERVICE_PORT diff --git a/lib/container_orchestrator.rb b/lib/container_orchestrator.rb index ba486b2e2b2..b180dbb2fcc 100644 --- a/lib/container_orchestrator.rb +++ b/lib/container_orchestrator.rb @@ -22,8 +22,8 @@ def create_deployment(name) raise unless e.message =~ /already exists/ end - def create_service(name, port) - definition = service_definition(name, port) + def create_service(name, selector, port) + definition = service_definition(name, selector, port) yield(definition) if block_given? kube_connection.create_service(definition) rescue KubeException => e diff --git a/lib/container_orchestrator/object_definition.rb b/lib/container_orchestrator/object_definition.rb index eecee2815c2..f1c515163ca 100644 --- a/lib/container_orchestrator/object_definition.rb +++ b/lib/container_orchestrator/object_definition.rb @@ -26,7 +26,7 @@ def deployment_definition(name) } end - def service_definition(name, port) + def service_definition(name, selector, port) { :metadata => { :name => name, @@ -34,7 +34,7 @@ def service_definition(name, port) :namespace => my_namespace }, :spec => { - :selector => {:name => name}, + :selector => selector, :ports => [{ :name => "#{name}-#{port}", :port => port, diff --git a/spec/lib/container_orchestrator_spec.rb b/spec/lib/container_orchestrator_spec.rb index b9d5efdb981..23d78eef6fb 100644 --- a/spec/lib/container_orchestrator_spec.rb +++ b/spec/lib/container_orchestrator_spec.rb @@ -106,9 +106,11 @@ ports = definition[:spec][:ports] expect(ports.first).to eq(:name => "http-80", :port => 80, :targetPort => 80) expect(ports.last).to eq(:name => "https", :port => 443, :targetPort => 5000) + + expect(definition[:spec][:selector][:service]).to eq("http") end - subject.create_service("http", 80) do |spec| + subject.create_service("http", {:service => "http"}, 80) do |spec| spec[:spec][:ports] << {:name => "https", :port => 443, :targetPort => 5000} end end @@ -117,7 +119,7 @@ error = KubeException.new(500, "service already exists", "") expect(kube_connection_stub).to receive(:create_service).and_raise(error) - expect { subject.create_service("http", 80) }.not_to raise_error + expect { subject.create_service("http", {:service => "http"}, 80) }.not_to raise_error end end diff --git a/spec/models/miq_worker/container_common_spec.rb b/spec/models/miq_worker/container_common_spec.rb index a9f1721fbd2..283d6ce76da 100644 --- a/spec/models/miq_worker/container_common_spec.rb +++ b/spec/models/miq_worker/container_common_spec.rb @@ -1,11 +1,18 @@ describe MiqWorker::ContainerCommon do + before { EvmSpecHelper.local_miq_server } + let(:compressed_server_id) { MiqServer.my_server.compressed_id } + + def deployment_name_for(name) + "#{compressed_server_id}-#{name}" + end + describe "#worker_deployment_name" do let(:test_cases) do [ - {:subject => MiqGenericWorker.new, :name => "generic"}, - {:subject => MiqUiWorker.new, :name => "ui"}, - {:subject => ManageIQ::Providers::Openshift::ContainerManager::EventCatcher.new(:queue_name => "ems_2"), :name => "openshift-container-event-catcher-2"}, - {:subject => ManageIQ::Providers::Redhat::NetworkManager::MetricsCollectorWorker.new, :name => "redhat-network-metrics-collector"} + {:subject => MiqGenericWorker.new, :name => deployment_name_for("generic")}, + {:subject => MiqUiWorker.new, :name => deployment_name_for("ui")}, + {:subject => ManageIQ::Providers::Openshift::ContainerManager::EventCatcher.new(:queue_name => "ems_2"), :name => deployment_name_for("openshift-container-event-catcher-2")}, + {:subject => ManageIQ::Providers::Redhat::NetworkManager::MetricsCollectorWorker.new, :name => deployment_name_for("redhat-network-metrics-collector")} ] end @@ -33,14 +40,14 @@ it "scales the deployment to the number of configured workers" do allow(MiqGenericWorker).to receive(:worker_settings).and_return(:count => 2) - expect(orchestrator).to receive(:scale).with("generic", 2) + expect(orchestrator).to receive(:scale).with(deployment_name_for("generic"), 2) MiqGenericWorker.new.scale_deployment end it "deletes the container objects if the worker count is zero" do allow(MiqGenericWorker).to receive(:worker_settings).and_return(:count => 0) - expect(orchestrator).to receive(:scale).with("generic", 0) + expect(orchestrator).to receive(:scale).with(deployment_name_for("generic"), 0) worker = MiqGenericWorker.new expect(worker).to receive(:delete_container_objects) worker.scale_deployment