diff --git a/app/models/miq_server/worker_management/kubernetes.rb b/app/models/miq_server/worker_management/kubernetes.rb index bb9fb528cac..224ebd2501e 100644 --- a/app/models/miq_server/worker_management/kubernetes.rb +++ b/app/models/miq_server/worker_management/kubernetes.rb @@ -23,10 +23,23 @@ def sync_from_system def sync_starting_workers starting = MiqWorker.find_all_starting + # Get a list of pods that aren't currently assigned to MiqWorker records + pods_without_workers = current_pods.keys - MiqWorker.server_scope.pluck(:system_uid).compact + # Non-rails workers cannot set their own miq_worker record to started once they # have finished initializing. Check for any starting non-rails workers whose # pod is running and mark the miq_worker as started. starting.reject(&:rails_worker?).each do |worker| + # If the current worker doesn't have a system_uid assigned then find the first + # pod available for our worker type and link them up. + if worker.system_uid.nil? + system_uid = pods_without_workers.detect { |pod_name| pod_name.start_with?(worker.worker_deployment_name) } + next if system_uid.nil? + + pods_without_workers.delete(system_uid) + worker.update!(:system_uid => system_uid) + end + worker_pod = current_pods[worker.system_uid] next if worker_pod.nil? diff --git a/spec/models/miq_server/worker_management/kubernetes_spec.rb b/spec/models/miq_server/worker_management/kubernetes_spec.rb index 407dde8cf19..1ff0dd8434d 100644 --- a/spec/models/miq_server/worker_management/kubernetes_spec.rb +++ b/spec/models/miq_server/worker_management/kubernetes_spec.rb @@ -270,6 +270,36 @@ expect(server.worker_manager.sync_starting_workers).to include(worker) end end + + context "with a worker that doesn't have a system_uid yet" do + let(:system_uid) { nil } + + context "without a pod" do + let(:current_pods) { {} } + + it "returns the worker as still starting" do + expect(server.worker_manager.sync_starting_workers).to include(worker) + end + end + + context "with a pod that is running" do + let(:pod_running) { true } + + it "sets the worker's system_uid and marks the worker started" do + expect(server.worker_manager.sync_starting_workers).to be_empty + expect(worker.reload.system_uid).to eq(pod_name) + end + end + + context "with a pod that isn't running" do + let(:pod_running) { false } + + it "sets the worker's system_uid and but doesn't mark the worker started" do + expect(server.worker_manager.sync_starting_workers).to include(worker) + expect(worker.reload.system_uid).to eq(pod_name) + end + end + end end end end