Skip to content

Commit

Permalink
Unify Worker Environment Variables systemd/k8s
Browse files Browse the repository at this point in the history
Handling of worker environment variables was spread across a number of
different locations and in a number of cases had to be duplicated for
systemd/kubernetes.

This adds a common `MiqWorker#environment_variables` method that applies
to all runtime platforms, as well as specific
systemd/container_environment_variables which are merged in depending on
the runtime environment.
  • Loading branch information
agrare committed Sep 25, 2024
1 parent 9637774 commit f2fdad8
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 30 deletions.
5 changes: 4 additions & 1 deletion app/models/automation_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ def self.kill_priority
MiqWorkerType::KILL_PRIORITY_GENERIC_WORKERS
end

def container_environment_variables
super.merge("AUTOMATION_JOB_SERVICE_ACCOUNT" => ENV.fetch("WORKER_SERVICE_ACCOUNT"))
end

def configure_worker_deployment(definition, replicas = 0)
super

definition[:spec][:template][:spec][:serviceAccountName] = "manageiq-automation"
definition[:spec][:template][:spec][:containers][0][:env] << {:name => "AUTOMATION_JOB_SERVICE_ACCOUNT", :value => ENV.fetch("WORKER_SERVICE_ACCOUNT")}
end
end
8 changes: 7 additions & 1 deletion app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,15 @@ def command_line
self.class.build_command_line(*worker_options.values_at(:guid, :ems_id))
end

def environment_variables
{
"BUNDLER_GROUPS" => self.class.bundler_groups.join(",")
}
end

def start_runner_via_spawn
pid = Kernel.spawn(
{"BUNDLER_GROUPS" => self.class.bundler_groups.join(",")},
environment_variables,
command_line,
[:out, :err] => [Rails.root.join("log/evm.log"), "a"]
)
Expand Down
20 changes: 11 additions & 9 deletions app/models/miq_worker/container_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ def configure_worker_deployment(definition, replicas = 0)
definition[:spec][:template][:spec][:nodeSelector] = zone_selector
end

env = container_environment_variables.merge(environment_variables).map { |name, value| {:name => name, :value => value} }

container = definition[:spec][:template][:spec][:containers].first

if container_image_tag.include?("latest")
container[:imagePullPolicy] = "Always"
else
container[:imagePullPolicy] = "IfNotPresent"
end
container[:imagePullPolicy] = container_image_tag.include?("latest") ? "Always" : "IfNotPresent"
container[:image] = container_image
container[:resources] = resource_constraints
container[:env].concat(env)
end

container[:image] = container_image
container[:env] << {:name => "WORKER_CLASS_NAME", :value => self.class.name}
container[:env] << {:name => "BUNDLER_GROUPS", :value => self.class.bundler_groups.join(",")}
container[:resources] = resource_constraints
def container_environment_variables
{
"WORKER_CLASS_NAME" => self.class.name
}
end

def scale_deployment
Expand Down
1 change: 0 additions & 1 deletion app/models/miq_worker/deployment_per_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module DeploymentPerWorker
def create_container_objects
ContainerOrchestrator.new.create_deployment(worker_deployment_name) do |definition|
configure_worker_deployment(definition, 1)
definition[:spec][:template][:spec][:containers].first[:env] << {:name => "EMS_ID", :value => self.class.ems_id_from_queue_name(queue_name).to_s}
end
end

Expand Down
9 changes: 7 additions & 2 deletions app/models/miq_worker/service_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ def configure_service_worker_deployment(definition)

container = definition[:spec][:template][:spec][:containers].first
container[:ports] = [{:containerPort => SERVICE_PORT}, {:containerPort => HEALTH_PORT}]
container[:env] << {:name => "PORT", :value => container_port.to_s}
container[:env] << {:name => "BINDING_ADDRESS", :value => "0.0.0.0"}
container[:volumeMounts] ||= []
definition[:spec][:template][:spec][:volumes] ||= []
end
Expand All @@ -68,5 +66,12 @@ def container_image_name
def container_image
ENV["WEBSERVER_WORKER_IMAGE"] || default_image
end

def container_environment_variables
super.merge(
"PORT" => container_port.to_s,
"BINDING_ADDRESS" => "0.0.0.0"
)
end
end
end
10 changes: 7 additions & 3 deletions app/models/miq_worker/systemd_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ def unit_config_file_path
end

def unit_config_file
# Merge systemd specific environment variables with the generic worker
# environment variables
environment = systemd_environment_variables.merge(environment_variables)

<<~UNIT_CONFIG_FILE
[Service]
#{unit_settings.compact.map { |key, value| "#{key}=#{value}" }.join("\n")}
#{unit_environment_variables.compact.map { |key, value| "Environment=#{key}=#{value}" }.join("\n")}
#{unit_settings.compact.map { |key, value| "#{key}=#{value}" }.join("\n")}
#{environment.compact.map { |key, value| "Environment=#{key}=#{value}" }.join("\n")}
UNIT_CONFIG_FILE
end

Expand All @@ -135,7 +139,7 @@ def unit_settings
end

# Override this in a child class to add environment variables
def unit_environment_variables
def systemd_environment_variables
{}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/mixins/per_ems_worker_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def worker_options
super.merge(:ems_id => ems_id)
end

def unit_environment_variables
super.merge("EMS_ID" => ems_id)
def environment_variables
super.merge("EMS_ID" => ems_id.to_s)
end
end
1 change: 0 additions & 1 deletion systemd/manageiq-automation@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-automation.target
WantedBy=manageiq-automation.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb AutomationWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-ems_metrics_processor@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-ems_metrics_processor.target
WantedBy=manageiq-ems_metrics_processor.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqEmsMetricsProcessorWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-event_handler@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-event_handler.target
WantedBy=manageiq-event_handler.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqEventHandler --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-generic@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-generic.target
WantedBy=manageiq-generic.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqGenericWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-priority@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-priority.target
WantedBy=manageiq-priority.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqPriorityWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-remote_console@.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Wants=httpd.service
WantedBy=manageiq-remote_console.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqRemoteConsoleWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-reporting@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-reporting.target
WantedBy=manageiq-reporting.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqReportingWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-schedule@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-schedule.target
WantedBy=manageiq-schedule.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqScheduleWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-smart_proxy@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PartOf=manageiq-smart_proxy.target
WantedBy=manageiq-smart_proxy.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqSmartProxyWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-ui@.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Wants=httpd.service
WantedBy=manageiq-ui.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqUiWorker --heartbeat --guid=%i
User=manageiq
Expand Down
1 change: 0 additions & 1 deletion systemd/manageiq-web_service@.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Wants=httpd.service
WantedBy=manageiq-web_service.target
[Service]
WorkingDirectory=/var/www/miq/vmdb
Environment=BUNDLER_GROUPS=manageiq_default,ui_dependencies
EnvironmentFile=/etc/default/manageiq*.properties
ExecStart=/usr/bin/ruby lib/workers/bin/run_single_worker.rb MiqWebServiceWorker --heartbeat --guid=%i
User=manageiq
Expand Down

0 comments on commit f2fdad8

Please sign in to comment.