Skip to content

Commit

Permalink
Set MiqWorker system_uid if missing on kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Aug 19, 2024
1 parent 4304ce4 commit 31b93f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/miq_server/worker_management/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
30 changes: 30 additions & 0 deletions spec/models/miq_server/worker_management/kubernetes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31b93f7

Please sign in to comment.